#define MAIN_Fosc 22118400L //定义主时钟
#include "STC15Fxxxx.H"
#define Timer0_Reload (65536UL -(MAIN_Fosc / 2000)) //Timer 0 中断频率, 1000次/秒
u8 code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
sbit P_HC595_SER = P4^0; //pin 14 SER data input
sbit P_HC595_RCLK = P5^4; //pin 12 RCLk store (latch) clock
sbit P_HC595_SRCLK = P4^3; //pin 11 SRCLK Shift data clock
u8 LED8[8]; //显示缓冲
u8 display_index; //显示位索引
bit B_1ms; //1ms标志
u8 ADC_KeyState,ADC_KeyState1,ADC_KeyState2,ADC_KeyState3; //键状态
u8 ADC_KeyHoldCnt; //键按下计时
u8 KeyCode; //给用户使用的键码, 1~16有效
u8 cnt10ms;
void CalculateAdcKey(u16 adc);
u16 Get_ADC10bitResult(u8 channel); //channel = 0~7
void DisplayScan();
void main(void)
{
u16 j;
u16 i;
P0M1 = 0; P0M0 = 0; //设置为准双向口
P1M1 = 0; P1M0 = 0; //设置为准双向口
P2M1 = 0; P2M0 = 0; //设置为准双向口
P3M1 = 0; P3M0 = 0; //设置为准双向口
P4M1 = 0; P4M0 = 0; //设置为准双向口
P5M1 = 0; P5M0 = 0; //设置为准双向口
P6M1 = 0; P6M0 = 0; //设置为准双向口
P7M1 = 0; P7M0 = 0; //设置为准双向口
display_index = 0;
P1ASF = 0x10; //P1.4做ADC
ADC_CONTR = 0xE0; //90T,90个周期转换一次, ADC power on,ADC电源打开
AUXR = 0x80; //Timer0 set as 1T, 16 bits timer auto-reload,
ET0 = 1; //Timer0 interrupt enable
TR0 = 1; //Tiner0 run
EA = 1; //打开总中断
ADC_KeyState = 0;
ADC_KeyState1 = 0;
ADC_KeyState2 = 0;
ADC_KeyState3 = 0; //键状态
ADC_KeyHoldCnt = 0;
KeyCode = 0; //键码 1~16有效
cnt10ms = 0;
for(i=0;i<8;i++)
{
LED8[i] = 0x10;
}
while(1)
{
DisplayScan();
if(B_1ms) //1ms到
{
B_1ms = 0;
j=Get_ADC10bitResult(4);
CalculateAdcKey(j);
if(KeyCode > 0) //有键按下
{
LED8[6] = KeyCode / 10; //显示键码
LED8[7] = KeyCode % 10; //显示键码
KeyCode = 0;
}
}
}
}
u16 Get_ADC10bitResult(u8 channel) //channel = 0~7
{
ADC_RES = 0; //ADC结果高位寄存器
ADC_RESL = 0; //ADC结果低位寄存器
ADC_CONTR = (ADC_CONTR & 0xe0) | 0x08 | channel; //start the ADC,将P1.4引脚作为内部ADC模块采样输入
NOP(4);
while((ADC_CONTR & 0x10) == 0) ; //wait for ADC finish,如果adc转换你未完成,就空操作,转换完成就执行下一句
ADC_CONTR &= ~0x10; //清除ADC结束标志,就是让adc转换不结束,一直处于运行状态
return (((u16)ADC_RES << 2) | (ADC_RESL & 3)); //10位ADC结果,取ADC_RES的八位和ADC_RESL的低两位
}
#define ADC_OFFSET 16
void CalculateAdcKey(u16 adc)
{
u8 i;
u16 j;
if(adc < (64-ADC_OFFSET)) //判断无键按下,然后让键状态归0
{
ADC_KeyState = 0; //键状态归0
ADC_KeyHoldCnt = 0;
}
j = 64; //键值1所对应的ADC值
for(i=1; i<=16; i++) //让i值轮询去判断键值i
{
if((adc >= (j - ADC_OFFSET)) && (adc <= (j + ADC_OFFSET))) break; //判断是否在偏差范围内
j += 64;
}
ADC_KeyState3 = ADC_KeyState2;
ADC_KeyState2 = ADC_KeyState1;
if(i > 16) ADC_KeyState1 = 0; //键无效
else //键有效
{
ADC_KeyState1 = i;
if((ADC_KeyState3 == ADC_KeyState2) && (ADC_KeyState2 == ADC_KeyState1) &&
(ADC_KeyState3 > 0) && (ADC_KeyState2 > 0) && (ADC_KeyState1 > 0))
{
if(ADC_KeyState == 0) //第一次检测到
{
KeyCode = i; //保存键码
ADC_KeyState = i; //保存键状态
ADC_KeyHoldCnt = 0;
}
if(ADC_KeyState == i) //连续检测到同一键按着
{
if(++ADC_KeyHoldCnt >= 100) //按下1秒后,以10次每秒为周期的速度Repeat Key
{
ADC_KeyHoldCnt = 90;
KeyCode = i; //保存键码
}
}
else ADC_KeyHoldCnt = 0; //按下时间计数归0
}
}
}
/**************** 向HC595发送一个字节函数 ******************/
void Send_595(u8 dat)
{
u8 i;
for(i=0; i<8; i++)
{
dat <<= 1;
P_HC595_SER = CY;
P_HC595_SRCLK = 1;
P_HC595_SRCLK = 0;
}
}
/********************** 显示扫描函数 ************************/
void DisplayScan(void)
{
Send_595(~T_COM[display_index]); //输出位码
Send_595(t_display[LED8[display_index]]); //输出段码
P_HC595_RCLK = 1;
P_HC595_RCLK = 0; //锁存输出数据
if(++display_index >= 8) display_index = 0; //8位结束回0
}
/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt TIMER0_VECTOR
{
B_1ms = 1; //1ms标志
}