我这个程序功能很简单:通过tx-1c实验班的第一个矩阵键盘按键开始计时
1、烧写在板子上按键下去可以计时,但是数码管只有当个位数变动时,十位数的显示就闪一下,
就是说个位数的数字显示正常,但是十位数的数字一秒闪一次,想请教一下是什么问题?
2、我想让按键能实现开始和暂停的功能,请问我应该怎么改?
3、还有想问一个小知识点,keil中的bit关键字定义的变量如果没有初始化,那么这个变量初始值是1还是0呢?
刚开始学单片机,很多东西不是很懂,希望各位大佬能帮我解答一下,如果我的代码写得不好也希望能给我一些小建议吧,谢谢了。
单片机源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code led[]={0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f}; //数码管段选信息
- uchar single, tenth;
- uchar flag;
- sbit dula=P2^6;
- sbit wela=P2^7;
- uint num=1;
- void delay(uchar ms)
- {
- uchar i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--) ;
- }
- void display(uchar tenth, uchar single) //数码管显示函数
- {
- dula=1;
- P0=led[tenth];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfe;
- wela=0;
- delay(5);
- dula=1;
- P0=led[single];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfd;
- wela=0;
- delay(5);
- }
- void keyscan() //键盘检测
- {
- uchar temp;
- P3=0xfe;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xee: flag=1;
- break;
-
- }
-
- while(temp!=0xf0)
- {
- temp=P3;
- temp=0xf0;
- }
- }
- }
- }
- void main()
- {
- single=0;
- tenth=0;
- display(tenth,single);
- TMOD=0x02; //利用定时器0方式2计数
- TL0=6; //方式2为初值自动重装的8位定时器
- TH0=6;
- keyscan(); //检测按键是否被按下
- EA=1;
- ET0=1;
- TR0=1;
- while(flag) //当检测按键被按下开始计时
- {
- if(num==3686) //中断3686次相当于1s
- {
- num=1;
- single++;
- if(single==10)
- {
- single=0;
- tenth++;
- }
- display(tenth,single);
- }
-
- }
- }
- void T0_time() interrupt 1 //中断函数
- {
- num++;
- }
复制代码
|