关于一个数码管前三位显示一个跑,范围000~999,相邻两数间隔0.01s,当按下按键时跑表停止,松手后跑表继续。
单片机源程序如下:- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- void display();
- uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 欢迎各位大神指正!!!!
- char hour=0,min=0,sec=0;
- sbit addr_A=P3^0;
- sbit addr_B=P3^1;
- sbit addr_C=P3^2;
- sbit P10=P1^0;
- void delay(uint i)
- {
- while(i--);
- }
- void initT0() {
- TMOD=0x01;
- TH0 = (65536-10000)/256;
- TL0 = (65536-10000)%256;
- EA =1;
- ET0 =1;
- TR0 =1;
- }
- void keyScan(){
- static bit oldState = 1;
- if(!P10){
- delay(1000);
- if(!P10){
- if(oldState) {
- oldState = 0;
- TR0=0;
- while(!P10){
- display();
- }
- TR0=1;
- }
- }
- }
- else if(!oldState) oldState = 1;
- }
- void main() {
- initT0();
- while(1){
- keyScan();
- display();
- }
- }
- void INT_T0() interrupt 1
- {
- static uchar counter = 0;
- TR0 = 0;
- TH0 = (65536-10000)/256;
- TL0 = (65536-10000)%256;
- TR0 = 1;
- counter++;
- if(counter==1) {
- counter=0;
- }
- sec++;
- if(sec>=10) {
- sec=0;
- min++;
- }
- if(min>=10) {
- min=0;
- hour++;
- }
- if(hour>=10) {
- hour=0;
- }
- }
- void display() {
- uchar LED[3];
- LED[0]=hour;
- LED[1]=min;
- LED[2]=sec;
-
- addr_A=0;
- P0=table[LED[0]];
- delay(200);
- addr_A=1;
-
-
- addr_B=0;
- P0=table[LED[1]];
- delay(200);
- addr_B=1;
-
- addr_C=0;
- P0=table[LED[2]];
- delay(2000);
-
- }
复制代码
|