我们公司自己做了一个攻牙机,我想着只是简单的攻牙也不行,得知道一天生产了多少数量,所以就想要设计一个
单片机程序来实现按键开关启动一次,数码管计数一次。但是程序还不能实现不良的不计数,“空”转的也不计数,这些实现起来相对而言比较复杂了(软件还好,硬件可能要求比较高)。以下程序足以满足我们小公司了,程序详见下(希望大家一起探讨)。最开始写的代码,我把按键扫描和显示功能都放到了main函数里面,但是按键一次数码管会闪一次,觉得不好,后面网上查了下资料,就把显示功能放在了定时器0里面,每隔10ms扫描一次就对了。扫描时间不能太短也不能太长,效果是实现了,但是始终还是不明白为什么。代码详见下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,
- 0x77,0x1F,0x4E,0x2D,
- 0x4F,0x47 };
- sbit button = P3^0;
- sbit dula = P2^6;
- sbit wela = P2^7;
- sbit lcden = P3^4;
- uchar count = 0;
- void buttonScan();
- void display(uchar count);
- void delayms(uint ms);
- void main()
- {
- lcden = 0;
- TMOD = 0x01;
- TH0 = (65536 - 9174)/256;
- TL0 = (65536 - 9174)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- while(1)
- {
- buttonScan();
- //display(count);
- }
- }
- void buttonScan()
- {
- if(button == 0)
- {
- delayms(20);
- if(button == 0)
- {
- while(!button);
- count++;
- }
- }
- }
- void display(uchar count)
- {
- uchar w = count/10000;
- uchar q = count%10000/1000;
- uchar b = count%10000%1000/100;
- uchar s = count%10000%1000%100/10;
- uchar g = count%10;
- dula = 1;
- P0 = table[w];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xfe;
- wela = 0;
- delayms(1);
- dula = 1;
- P0 = table[q];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xfd;
- wela = 0;
- delayms(1);
- dula = 1;
- P0 = table[b];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xfb;
- wela = 0;
- delayms(1);
- dula = 1;
- P0 = table[s];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xf7;
- wela = 0;
- delayms(1);
- dula = 1;
- P0 = table[g];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xef;
- wela = 0;
- delayms(1);
-
- }
- void Interrupt_T0()interrupt 1
- {
- TH0 = (65536 - 9174)/256;
- TL0 = (65536 - 9174)%256;
- display(count);
- }
- void delayms(uint ms)
- {
- uint i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--);
- }
复制代码
10Ms能不能就实际检验一下好了,要显示的位数越多,这个中断时间就要设置的越短一些,满足人眼的视觉暂留最低显示频率。display函数里面为了实现每次进去显示不同的位,所以肯定需要一个只是标志位,static也可以,全局变量也可以,然后每次进去执行一次++,然后判别,去显示对应的位就可以了。
一周热门 更多>