#include <c8051f020.h> // SFR declarations #include <stdio.h> #include <intrins.h>
char data keyflag; //定时器2和键盘中断标志 (由int改为char,看好不好使)
//初始化配置 void config(){ WDTCN=0x7; //禁止看门狗 WDTCN = 0xde; WDTCN = 0xad; //交叉开关配置 XBR0=0x04; //UART0的RX0,TX0连接到2个端口引脚 XBR1=0x04; //INT0联到P0.2 XBR2=0x40; //交叉开关允许 IE |= 0x01; //INT0中断允许
P7=0Xf0 OSCXCN=0x00; //155页,内部振荡器,2M OSCICN=0x04;
_nop_(); _nop_(); _nop_(); return; }
//延时 void delay(unsigned int x1) { while(--x1) { } return; }
void ADC0_Init() { REF0CN=0x02; //143,外部基准 VREF0 AMX0SL = 0x00; ADC0CF = 0x01; //119 AIN0,AIN1为+,-差分输入 AMX0CF = 0xfc; //?转换时钟周期为最大值32,内部放大器增益为1 ADC0CN = 0x8c; //123,定时器2益出启动ADC转换,数据右对齐 //设置定时器2 //RCAP2=0x //196 转换率为??? T2CON=0x04; CKCON |= 0x20; //T2M=1 定时器2使用系统时钟 EIE2 |= 0x02; //15,允许ADC0转换结束的中断请求 //EIP2 |= 0x02; //17,ADC0转换结束中断为高优先级。 } void keytab() { unsigned char idata scancode; unsigned char idata keycode; //P7口高4位对应接上拉电阻的4行,低4位对应无上拉电阻的4列。
delay(200); //有键按下,去抖动 if((P7 & 0xf0)==0xf0) return; //如果检测不到,推出键盘处理程序
scancode=0xfe; while(scancode !=0xff) //循环扫描是否有键按下 { P7=scancode; keycode=P7; if((keycode & 0xf0)!=0xf0) break ; //说明有键按下 scancode=((keycode<<1)|0xf0); } _nop_(); if(keycode==0x77)/////key1 { ACC=12; //1 } if(keycode==0x87)////key2 { ACC=34; //2 } _nop_();
P7= 0xf0; // IE |= 0x01; //键盘中断允许 // EA=1; return; } void Key_Int0(void) interrupt 0 using 3 { IE &= 0xfe; //delay(100); if (keyflag==0) keyflag=1; //相当于键盘处理程序执行完后才能再次接收按键,否则不进行键盘处理 IE |= 0x01;
}
|