这个代码有问题,按下按键不是按顺序从0-9的显示,是跳着显示,帮看看哪里错了。
单片机源程序:
- #include "reg52.h"
- #define uint unsigned int
- #define uchar unsigned char
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阳数码管码表
- sbit key_s1 = P3^2; //独立按键S1
- uchar num;
- void delay(uint z);
- void main()
- {
- uchar i;
- P2 = 0xff;
- while(1)
- {
- for(i=0;i<16;i++)
-
- if(key_s1 == 0)
- {
- delay(1000);
- P0=~table[i];
-
- delay(20); //按键消抖
- if(key_s1 == 0)
- num++;
- if(num == 10)
- num = 0;
- while(!key_s1); //松手检测
- }
- }
- }
- void delay(uint z)
- {
- uint x,y; for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
复制代码
|