变量名不要用a,b,c,d!
变量名不要用a,b,c,d!
变量名不要用a,b,c,d!
在群里问了几次都不贴全部代码
现在终于表弄明白那个for是怎么运作的
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit L1=P1^0; //定义键盘的4列线
- sbit L2=P1^1;
- sbit L3=P1^2;
- sbit L4=P1^3;
- uchar dis[16]= { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar dispbuf[2] = {0,0};
- void delay_ms(unsigned int n);
- uint n;
- uchar KeyVal;
- void main() //主程序
- {
- uchar temp;
- uchar i;
- while(1)
- {
- //******display******************
- P2=0xFF;
- P3=0x04;
- P2=dis[dispbuf[1]];
- delay_ms(1000);
- P2=0xFF;
- P3=0x08;
- P2=dis[dispbuf[0]];
- delay_ms(1000);
- //******display******************
- //******Keypad scan******************
- P1=0xef; //行扫描初值,P1.4=0,P1.5~P1.7=1
- for(i=0; i<=3; i++) //按行扫描,一共4行
- {
- if(L1==0)
- {
- KeyVal=i*4+0;
- } //判断第一列是否有键按下
- if(L2==0)
- {
- KeyVal=i*4+1;
- }
- if(L3==0)
- {
- KeyVal=i*4+2;
- }
- if(L4==0)
- {
- KeyVal=i*4+3;
- }
- dispbuf[1]=KeyVal/10%10;
- dispbuf[0]=KeyVal%10;
- delay_ms(50); //延时
- temp=P1; //读入P1口状态
- temp=temp|0x0f; //使P1.3~P1.0为输入
- temp=temp<<1; //P1.7~P1.4左移1位,准备下一行扫描
- temp=temp|0x0f; //移位后,置P1.3~P1.0为1,保证其仍为输入
- P1=temp; //行扫描值送P1口,为下一行扫描做准备
- }
- //******Keypad scan******************
- }
- }
- void delay_ms(unsigned int n)
- {
- {
- while(n--);
- }
- }
复制代码
|