51 development board independent button to adjust the application experiment of the clock, you can use the independent button to adjust the time (the time can be re-defined)

Experimental principle:

Referring to the circuit schematic diagram of the digital tube in Experiment 2, using the display principle of the digital tube and the working principle of the timer, combined with the application of the button, the digital tube displays the clock and has the function of clock adjustment.

Experiment content:

Utilize the acquired microcontroller timer interrupt knowledge and circuit knowledge, control the digital tube to display the clock through the microcontroller programming, and have the function of adjusting the clock. Using the knowledge of timer interruption, control the digital tube to display the clock through programming. The display format is XX—XX—XX, which are hours, minutes, and seconds, and set the first three keys of the second row of the matrix keyboard as independent keys. The first button controls the hour, minute, and second of the clock, the second button controls the addition of the hour, minute, and second, and the third button controls the adjustment of the hour, minute, and second.

Experiment code:

#include<REGX52.h>
typedef unsigned char uchar;
//共阴极段码
uchar code leddata[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
 
char s=56,m=59,h=23;
int n;
//延时函数
void Delay(unsigned int xms){
	unsigned char i, j;
	while(xms--){
		i = 2;
		j = 239;
		do{
			while (--j);
		} while (--i);
	}
}
void init(){
	TMOD=0x01;//定时器 0 的方式 1,定时 4ms 0000 0001
	TH0=(65536-4000)/256;
	TL0=(65536-4000)%256;
	ET0=1;      //小开关
	EA=1;        //总开关
	TR0=1;  //启动定时器
}
 
void display(uchar hour,uchar minute,uchar second){
    int i;
    for(i=1;i<=8;i++){
        switch(i){
           case 1: P2_4=0;P2_3=0;P2_2=0;P0=leddata[second%10];break;
           case 2: P2_4=0;P2_3=0;P2_2=1;P0=leddata[second/10];break;
           case 3: P2_4=0;P2_3=1;P2_2=0;P0=0x40;break;//显示  —
           case 4: P2_4=0;P2_3=1;P2_2=1;P0=leddata[minute%10];break;
           case 5: P2_4=1;P2_3=0;P2_2=0;P0=leddata[minute/10];break;
           case 6: P2_4=1;P2_3=0;P2_2=1;P0=0x40;break;//显示  —
           case 7: P2_4=1;P2_3=1;P2_2=0;P0=leddata[hour%10];break;
           case 8: P2_4=1;P2_3=1;P2_2=1;P0=leddata[hour/10];break;
        }
        Delay(1);
        P0=0;        //消影
    }
}
 
void key_scan(){
 
    static uchar flag=0;
    if(P3_1==0){
        Delay(20);
        while(P3_1==0)
        /*display(h,m,s)*/;
        Delay(20);
        TR0=0;//关闭中断请求
        flag++;
        if(flag==4){
           flag=0;
            TR0=1;
            }
    }
        
    if(flag!=0){
 
        switch(flag){
            case 1:if(P3_0==0){ Delay(20);while(P3_0==0)display(h,m,s);Delay(20);h++; if(h>23) h=0;}break;
            case 2:if(P3_0==0){ Delay(20);while(P3_0==0)display(h,m,s);Delay(20);m++; if(m>59) m=0;}break;
            case 3:if(P3_0==0){ Delay(20);while(P3_0==0)display(h,m,s);Delay(20);s++; if(s>59) s=0;}break;
            default:break;
        }
        switch(flag){
            case 1:if(P3_2==0){ Delay(20);while(P3_2==0)display(h,m,s);Delay(20);h--; if(h<0) h=23;}break;
            case 2:if(P3_2==0){ Delay(20);while(P3_2==0)display(h,m,s);Delay(20);m--; if(m<0) m=59;}break;
            case 3:if(P3_2==0){ Delay(20);while(P3_2==0)display(h,m,s);Delay(20);s--; if(s<0) s=59;}break;
            default:break;
        }
    }
}
void main(){
 
    init();
    while(1){
        display(h,m,s);
        key_scan();       
    } 
}
//4毫秒的定时器中断
void timer0() interrupt 1
{
    TH0=(65536-4000)/256;
    TL0=(65536-4000)%256;
    n++;
    if(n==250)
    {
        n=0;
        s++;
        if(s>=60)
        {
            s=0;
            m++;
           if(m>=60)
            {
                m=0;
                h++;
                if(h>=24)
                h=0;
            } 
        } 
    } 
}
  

Main flow chart:

529e0083615b4ab495efebd5dabf0df0.png

Simulation circuit diagram:

24eb46ecd191414caa2f494b97ca42c3.png

Development board circuit:

d1ea79b9b5314e568744e71b620c6867.png

Experimental renderings:

64acbe09721b454d8eac2900ac14e865.gif

Button K1 effect (there are four gears, press the first time to pause, and select "hour", press the second time to select "minute", press the third time to select "minute"

, press the fourth time to end the pause and continue timing)

6fb8b30a2e9c4a5485c85fb4cd73370c.gif

 The effect of buttons K2 and K3: (the selected "hour, minute and second" is added by one when K2 is pressed; the selected "hour, minute and second" is subtracted by one when K3 is pressed)

ec8cff73553e44c8bd12f49b71959eac.gif

Guess you like

Origin blog.csdn.net/yvge669/article/details/124924965