變量名不要用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--);
- }
- }
復制代碼
|