|
#include<reg51.h> unsigned long i;//êyÂë1üÏÔê¾êy×Ö unsigned char s;//¼ÆËãÖüÆú unsigned char number;//é¨Ãè°Ë¸öêyÂë1ü #define led P0 #define LED P2 sbit led1=P2^0;//74HC138A sbit led2=P2^1;//74HC138B sbit led3=P2^2;//74HC138C unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//êyÂëμÆ′ó0-9 unsigned char LedBuff[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//½«°Ë¸öêyÂëμÆ¶¼3õê¼Öμ void time(unsigned char qq)//Ñó3ùoˉêy { unsigned char j; for(;qq>0;qq--) { for(j=110;j>0;j++); } } void main() { P1=0x00;//3õê¼ÖμP1 P2=0xff;//3õê¼ÖμO2 i=0;//3õê¼ÖμLEDμÆÏÔê¾Öμ s=0;//3õê¼ÖüÆú number=0;//3õê¼é¨Ãè TMOD=0x01;//¼Æê±Æ÷Ä£ê½1 TH0=0xff;//éèÖÃ0.001oáÃëò»ÖüÆú TL0=0xff; TR0=1;//¿aÆô¼ÆêyÆ÷ while(1) { if(TF0==1) { TF0=0; TH0=0xff; TL0=0xff; s++;//ò»¸öÖüÆúàۼƼóò» } if(s>=1)//×Ô¼o¸ù¾Y1oáÃëò»ÖüÆúμÄê±¼äè¥è¡×Ô¼oÏëòaμÄê±¼ä { s=0; i++; } if(i<10)//èç1ûÖ»Åüμ½¸öλêy£¬Ö»èøöλêyáá { LedBuff[0]=LedChar[i%10]; }else if(i>=10&&i<100) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; }else if(i>=100&&i<1000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; }else if(i>=1000&&i<10000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; }else if(i>=10000&&i<100000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; }else if(i>=100000&&i<1000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; }else if(i>=1000000&&i<10000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; LedBuff[6]=LedChar[i/1000000%10]; }else if(i>=10000000&&i<100000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; LedBuff[6]=LedChar[i/1000000%10]; LedBuff[7]=LedChar[i/10000000%10]; }else //èç1ûêy×ÖÅüÂú8¸öLEDéèÖÃéÏÏT { LedBuff[0]=LedChar[9]; LedBuff[1]=LedChar[9]; LedBuff[2]=LedChar[9]; LedBuff[3]=LedChar[9]; LedBuff[4]=LedChar[9]; LedBuff[5]=LedChar[9]; LedBuff[6]=LedChar[9]; LedBuff[7]=LedChar[9]; } if(number==0)//é¨Ãè1-8¸öledμÆ { led3=1; led2=1; led1=1; led=LedBuff[0]; number++; time(2);//èÃÿ¸öLED′óÔ¼áá2oáÃë P0=0x00;//ÿ¸öledáá2oáÃëÖ®oó3õê¼ÖμLED } if(number==1) { led3=1; led2=1; led1=0; led=LedBuff[1]; number++; time(2); P0=0x00; } if(number==2) { led3=1; led2=0; led1=1; led=LedBuff[2]; number++; time(2); P0=0x00; } if(number==3) { led3=1; led2=0; led1=0; led=LedBuff[3]; number++; time(2); P0=0x00; } if(number==4) { led3=0; led2=1; led1=1; led=LedBuff[4]; number++; time(2); P0=0x00; } if(number==5) { led3=0; led2=1; led1=0; led=LedBuff[5]; number++; time(2); P0=0x00; } if(number==6) { led3=0; led2=0; led1=1; led=LedBuff[6]; number++; time(2); P0=0x00; } if(number==7) { led3=0; led2=0; led1=0; led=LedBuff[7]; number=0; time(2); P0=0x00; } } } 这是我修改后的,谢谢大家点评。原因就是我没有消隐。谢谢 |
wulin 发表于 2018-5-3 15:13 是的,我的比较啰嗦点。不过现在可以了,谢谢指点。 |
|
给你一个8位数码管时钟示例程序,根据实际电路更改端口定义。 //8位数码管时钟+闹钟 //K1键调整选择,K2键+,长按连+,K3键-,长按连-,K4键闹钟设置 //定时器初始化程序根据晶振频率选择 //用计数法代替软件延时,消除按键干扰其它程序 //数码管采用分时动态显示,2ms显示1位 #include <reg51.h> #define uint unsigned int #define uchar unsigned char #define key_S 300 //宏定义短按(约20ms) #define key_L 6000 //宏定义长按(约2/3s) #define key_M 3000 //宏定义长按(约1/3s) sbit K1 = P3^0; //时钟调整选择/退出 sbit K2 = P3^1; //++,长按连+ sbit K3 = P3^2; //--,长按连- sbit K4 = P3^3; //闹钟调整选择/退出 sbit Buzzer=P2^5; //蜂鸣器 sbit led138c=P2^4; //38译码器位选 sbit led138b=P2^3; //38译码器位选 sbit led138a=P2^2; //38译码器位选 uchar code table[]={ //0~F数组 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; uint Cnt2ms; //定义2ms秒变量 uchar hour=0,min=0,sec=0; //定义时、分、秒变量 uchar hour_1=0,min_1=0; //定义闹钟时、分变量 uchar Choice=0,Choice_1=0; //调整走时的选择变量、设置闹钟的选择变量 bit Buzzer_sign; //蜂鸣器闹钟标志 bit Twinkle,second=1; //闪烁标志、秒标志 bit flag=0; //2ms数码管动态显示标志 /*************************************** 定时器T0/T1初始化程序 @12MHz ***************************************/ /* void Timer_Init() { TMOD = 0x21; //设置T0模式1,T1模式2 TL0 = 0x30; //设置定时初值2ms TH0 = 0xF8; //设置定时初值2ms TL1 = 0x06; //设置定时初值250微秒 TH1 = 0x06; //设置定时重载值250微秒 TR0 = 1; //定时器0开始计时 // TR1 = 1; //定时器1开始计时 EA=1; //开总中断 ET0=1; //开定时器1中断 ET1=1; //开定时器1中断 }*/ /*************************************** 定时器T0/T1初始化程序 @11.0592MHz ***************************************/ void Timer_Init() { TMOD = 0x21; //设置T0模式1,T1模式2 TL0 = 0xCD; //设置定时初值2ms TH0 = 0xF8; //设置定时初值2ms TL1 = 0x1A; //设置定时初值250微秒 TH1 = 0x1A; //设置定时重载值250微秒 TR0 = 1; //定时器0开始计时 // TR1 = 1; //定时器1开始计时 EA=1; //开总中断 ET0=1; //开定时器1中断 ET1=1; //开定时器1中断 } /************************* 按键扫描程序 *************************/ void key_scan() { static bit key1_sign,key4_sign; //按键自锁标志变量 static uint count1,count2,count3=0,count4=0;//消抖计数变量 if(!K1) //检测按键1按下 { count1++; //消抖计数1自+1 if((count1>=key_S)&&(key1_sign==0))//检测消抖计数与按键1自锁标志 { key1_sign=1; //按键1自锁标志置1 Choice++; //调整选择变量自+1 if(Choice>=4) //调整时间选择0正常走时,1调时,2调分,3调秒 { Choice=0; //调整时间选择清0 Cnt2ms=0; //时间变量Cnt2ms清0 } } } else { key1_sign=0; //按键1自锁标志清0 count1=0; //消抖计数count1清0 } if(!K2) { count2++; if(count2>=key_L) //长按快调 { if(Choice==1) //选择变量1调时 { hour++; if(hour>=24) hour=0; } if(Choice==2) //选择变量2调分 { min++; if(min>=60) min=0; } if(Choice==3) //选择变量3调秒 { sec++; if(sec>=60) sec=0; } if(Choice_1==1) //选择变量1调时 { hour_1++; if(hour_1>=24) hour_1=0; } if(Choice_1==2) //选择变量2调分 { min_1++; if(min_1>=60) min_1=0; } count2=key_M; } } else //按键抬起 { if(count2>key_S && count2<key_L)//短按 { if(Choice==1) //选择变量1调时 { hour++; if(hour>=24) hour=0; } if(Choice==2) //选择变量2调分 { min++; if(min>=60) min=0; } if(Choice==3) //选择变量3调秒 { sec++; if(sec>=60) sec=0; } if(Choice_1==1) //选择变量1调时 { hour_1++; if(hour_1>=24) hour_1=0; } if(Choice_1==2) //选择变量2调分 { min_1++; if(min_1>=60) min_1=0; } } count2=0; //count2清0 } if(!K3) { count3++; if(count3>=key_L) //长按 { if(Choice==1) //选择变量 { hour--; if(hour>=24) hour=23; } if(Choice==2) //选择变量 { min--; if(min>=60) min=59; } if(Choice==3) //选择变量 { sec--; if(sec>=60) sec=59; } if(Choice_1==1) //选择变量 { hour_1--; if(hour_1>=24) hour_1=23; } if(Choice_1==2) //选择变量 { min_1--; if(min_1>=60) min_1=59; } count3=key_M; } } else //按键抬起 { if(count3>key_S && count3<key_L)//短按 { if(Choice==1) //选择变量 { hour--; if(hour>=24) hour=23; } if(Choice==2) //选择变量 { min--; if(min>=60) min=59; } if(Choice==3) //选择变量 { sec--; if(sec>=60) sec=59; } if(Choice_1==1) //选择变量 { hour_1--; if(hour_1>=24) hour_1=23; } if(Choice_1==2) //选择变量 { min_1--; if(min_1>=60) min_1=59; } } count3=0; //count3清0 } if(!K4) //检测按键1按下 { count4++; //消抖计数1自+1 if((count4>=key_S)&&(key4_sign==0))//检测消抖计数与按键1自锁标志 { key4_sign=1; //按键1自锁标志置1 Choice_1++; //调整选择变量自+1 if(Choice_1>=4) //调整闹钟时间选择0闹钟关闭,1调时,2调分,3开启闹钟 { Choice_1=0; //调整时间选择清0 } } } else { key4_sign=0; //按键1自锁标志清0 count4=0; //消抖计数count1清0 } } /******************************** 数码管显示程序 ********************************/ void display() { static uchar num=0; //分时显示变量 if(flag==1) { flag=0; if((Choice_1>0)&&(Choice_1<3))//闹钟设置显示部分 { switch(num) { case 0: P0=0x00; //消隐 led138a=0;led138b=0;led138c=0;//时十位位码 P0=table[hour_1/10]; //时十位段码 num++; break; case 1: P0=0x00; //消隐 led138a=1;led138b=0;led138c=0;//时个位位码 if((Twinkle==1)&&(Choice_1==1))//时点闪烁 P0=table[hour_1%10]|0x80; else P0=table[hour_1%10];//时个位段码+点 num++; break; case 2: P0=0x00; //消隐 led138a=0;led138b=1;led138c=0;//- P0=0x40; //- num++; break; case 3: P0=0x00; //消隐 led138a=1;led138b=1;led138c=0;//分十位位码 P0=table[min_1/10]; //分十位段码 num++; break; case 4: P0=0x00; //消隐 led138a=0;led138b=0;led138c=1;//分个位位码 if((Twinkle==1)&&(Choice_1==2))//分点闪烁 P0=table[min_1%10]|0x80; else P0=table[min_1%10];//分个位段码+点 num++; break; case 5: P0=0x00; //消隐 led138a=1;led138b=0;led138c=1;//- P0=0x40; //- num++; break; case 6: P0=0x00; //消隐 led138a=0;led138b=1;led138c=1;//秒十位位码 P0=0x40; //秒十位段码 num++; break; case 7: P0=0x00; //消隐 led138a=1;led138b=1;led138c=1;//秒个位位码 P0=0x40;//秒个位段码 num=0; break; } } else //正常走时显示部分 { switch(num) { case 0: P0=0x00; //消隐 led138a=0;led138b=0;led138c=0;//时十位位码 P0=table[hour/10]; //时十位段码 num++; break; case 1: P0=0x00; //消隐 led138a=1;led138b=0;led138c=0;//时个位位码 if((Twinkle==1)&&(Choice==1))//时点闪烁 P0=table[hour%10]|0x80; else P0=table[hour%10];//时个位段码+点 num++; break; case 2: P0=0x00; //消隐 led138a=0;led138b=1;led138c=0;//- P0=0x40; //- num++; break; case 3: P0=0x00; //消隐 led138a=1;led138b=1;led138c=0;//分十位位码 P0=table[min/10]; //分十位段码 num++; break; case 4: P0=0x00; //消隐 led138a=0;led138b=0;led138c=1;//分个位位码 if((Twinkle==1)&&(Choice==2))//分点闪烁 P0=table[min%10]|0x80; else P0=table[min%10];//分个位段码+点 num++; break; case 5: P0=0x00; //消隐 led138a=1;led138b=0;led138c=1;//- P0=0x40; //- num++; break; case 6: P0=0x00; //消隐 led138a=0;led138b=1;led138c=1;//秒十位位码 P0=table[sec/10]; //秒十位段码 num++; break; case 7: P0=0x00; //消隐 led138a=1;led138b=1;led138c=1;//秒个位位码 if(Choice_1==3) P0=table[sec%10]|0x80;//闹钟启动后秒个位+点 else if((Twinkle==1)&&(Choice==3))//秒点闪烁+点 P0=table[sec%10]|0x80; else P0=table[sec%10];//秒个位段码 num=0; break; } } } } /******************************** 闹钟程序 ********************************/ void Buzzer_nz() { if((Choice_1==3)&&(hour_1==hour)&&(min_1==min)&&(sec<30)) Buzzer_sign=1; else Buzzer_sign=0; if((second==1)&&(Buzzer_sign==1)) //计时周期1s { TR1 = 1; //定时器1开,蜂鸣器响一下 second=0; //定时周期1s变量清0 } } /******************************** 主程序 ********************************/ void main(void) { Timer_Init(); //初始化定时器 while(1) { key_scan(); //按键扫描 display(); //显示 Buzzer_nz(); //闹钟 } } /*----------------------------- 定时器0中断服务程序 200微秒 ------------------------------*/ void timer0() interrupt 1 { TL0 = 0xCD; //设置定时初值2ms TH0 = 0xF8; //设置定时初值2ms Cnt2ms++; //时间变量Cnt2ms自+1 flag=1; if((Cnt2ms>125 && Cnt2ms<250)||(Cnt2ms>375 && Cnt2ms<500))//闪烁频率2Hz Twinkle=1; //闪烁标志 else Twinkle=0; if(Cnt2ms>=500) //在此可以调整精度 { Cnt2ms=0; //变量Cnt2ms清0 second=1; if(Choice!=3) //调整选择变量为3停止走秒 sec++; //秒自+1 if(sec>=60) //如果秒>=60 { sec=0; //秒清0 min++; //分自+1 if(min>=60) //分>=60 { min=0; //分清0 hour++; //小时自+1 if(hour>=24) //小时>=24 hour=0; //小时清0 } } } } /*----------------------------- 定时器1中断服务程序 250微秒 (无源蜂鸣器驱动程序) ------------------------------*/ void timer1() interrupt 3 { static uint count3=0; //中断计数变量 count3++; //中断计数变量count3自+1 Buzzer=~Buzzer; //蜂鸣器端口取反 if(count3>=500) //0.1秒时间到500 { count3=0; //计数清0 Buzzer=1; //蜂鸣器端口清0 TR1 = 0; //定时器1关闭 } } |
zlzzlf 发表于 2018-5-2 17:32 if(s>=50) { s=0; i++; LedBuff[0]=0x00; LedBuff[1]=0x00; LedBuff[2]=0x00; LedBuff[3]=0x00; LedBuff[4]=0x00; LedBuff[5]=0x00; LedBuff[6]=0x00; LedBuff[7]=0x00; } if(i<10) { LedBuff[1]=0x00; LedBuff[2]=0x00; LedBuff[3]=0x00; LedBuff[4]=0x00; LedBuff[5]=0x00; LedBuff[6]=0x00; LedBuff[7]=0x00; LedBuff[0]=LedChar[i%10]; 我清除了,还是那样子。 |
你的程序写的太啰嗦,发生问题不易查找,简化一下即正常。
#include<reg51.h> #define led P0 #define LED P2 sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; unsigned int i=0,j=0; unsigned char k=0,s=0; unsigned char code LedChar[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code LedArr[]={0,1,2,3,4,5,6,7}; unsigned char LedBuff[8]; void main() { TMOD=0x01; TH0=0xb1; TL0=0xe0; TR0=1; while(1) { if(TF0==1) { TF0=0; TH0=0xb1; TL0=0xe0; s++; } if(s>=50) { s=0; i++; if(i>9999) { i=0; j++; if(j>9999) j=0; } } /************数据分解到缓存************/ LedBuff[0]=LedChar[j/1000]; LedBuff[1]=LedChar[j%1000/100]; LedBuff[2]=LedChar[j%100/10]; LedBuff[3]=LedChar[j%10]; LedBuff[4]=LedChar[i/1000]; LedBuff[5]=LedChar[i%1000/100]; LedBuff[6]=LedChar[i%100/10]; LedBuff[7]=LedChar[i%10]; /************8位数码管显示************/ led=0x00; //消隐 LED&=0xF8; //清除P2低3位,保留高5位状态不变 LED|=LedArr[k]; //送位码 led=LedBuff[k]; //送段码 k++; if(k>=8) k=0; } } |