我们公司自己做了一个攻牙机,我想着只是简单的攻牙也不行,得知道一天生产了多少数量,所以就想要设计一个
单片机程序来实现按键开关启动一次,数码管计数一次。但是程序还不能实现不良的不计数,“空”转的也不计数,这些实现起来相对而言比较复杂了(软件还好,硬件可能要求比较高)。以下程序足以满足我们小公司了,程序详见下(希望大家一起探讨)。最开始写的代码,我把按键扫描和显示功能都放到了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函数里面应该还有可以控制数码管显示位的才行,或者说每隔10ms,数码管显示控制位循环左移一位。。。。。。
一周热门 更多>