c51单片机利用串口和定时器做日历

完成日历NOW(DAT和TIME结构体)的计算,TIME包含毫秒成员,例如在PC端输入:2022-
08-15-18-35-59-33, 计时到发送的时间,即是18时35分59秒33毫秒,蜂鸣器叫,数码管显示当前年月日,即是20220815

那就是说,DAT是原来写年月日,TIME是原来记录时分秒毫秒的

没有格式化日期,所以没有显示-


#include <REG52.H>
#include <INTRINS.H>
#include <STRING.H>
#include <STDIO.H>
sbit BEEP=P2^5;
sbit L1=P2^4;
sbit L2=P2^3;
sbit L3=P2^2;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x7d,0x07,0x7f,0x6f,0x77};
unsigned char s[22]={0};
unsigned char c[10]={0};

void delay(int z){
    int x,y;
    for(x=z;x>0;x++)
        for(y=114;y>0;y--);

}

struct DAT{
    
    unsigned char year;
    unsigned char month;
    unsigned char day;

}str1;

struct TIME{

    unsigned char hour;
    unsigned char min;
    unsigned char sec;
    unsigned char ms;
    
    unsigned int n;

}str2;

void uart_init()
{
    PCON|=0x80;
    SCON=0x40;
    TMOD&=0x0F;
    TMOD|=0x20;
    TH1=0xFA;
    TL1=0xFA;
    TH0=0x4c;
    TL0=0x4c;
    TR1=1;
    TR0=1;
    SM0=0;
    REN=1;
    ES=1;
    EA=1;
}

void time0() interrupt 1{        //计时时分秒毫秒

    TH0=0x4c;
    TH0=0x00;
    str2.n=0;
    (str2.n)++;
    if(str2.n==20){
        str2.n=0;
        str2.ms=str2.ms+1;
    }
    if(str2.ms==50){
        str2.ms=0;
        str2.sec=str2.sec+1;
    }
    if(str2.sec==60){
        str2.min=str2.min+1;
        str2.sec=0;
    }
    if(str2.min==60){
        str2.min=str2.min+1;
        str2.min=0;
    }
    if(str2.hour==24){
            str2.hour=0;
    }
    
    
}


void time1() interrupt 3{

    TH1=0x4c;
    TL1=0x00;

    switch(str1.month){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(str1.day==32){
                str1.month=str1.month+1;
                str1.day=1;
            }
            break;

        case 4:
        case 6:
        case 9:
        case 11:
            if(str1.day==31){
                str1.month=str1.month+1;
                str1.day=1;
            }
            break;
            
        case 2:
            if(str1.year%400==0||str1.year%4==0&&str1.year%100!=0){
                
                if(str1.day==30){
                    str1.month=str1.month+1;
                    str1.day=1;
                }
            }
            else if(str1.day==29){
                str1.month=str1.month+1;
                str1.day=1;
            }
            break;
    }
}

void display(){    //展示年月日
    
    char i;
    for(i=1;i<=8;i++){
    switch(i){
        case 1:L1=0;L2=0;L3=0;
        P0=table[a%10];delay(1);break;    //展示字符串个位
        case 2:L1=0;L2=0;L3=1;
        P0=table[a/10%10];;delay(1);break;    //展示字符串十位
        case 3:L1=0;L2=1;L3=0;
        P0=table[a/100%10];delay(1);break;    //展示字符串百位
        case 4:L1=0;L2=1;L3=1;
        P0=table[a/1000%10];delay(1);break;    //展示字符串千位
        case 5:L1=1;L2=0;L3=0;
        P0=table[a/10000%10];delay(1);break;    //展示字符串万位
        case 6:L1=1;L2=0;L3=1;
        P0=table[a/100000%10];delay(1);break;    //展示字符串十万位
        case 7:L1=1;L2=1;L3=0;
        P0=table[a/1000000%10];delay(1);break;    //展示字符串百万位
        case 8:L1=1;L2=1;L3=1;
        P0=table[a/10000000];delay(1);break;    //展示字符串千万位
    }
}
    P0=0xff;
    delay(1);
}

void main(){

    uart_init();
    while(1){
        display();
    }

}

void set() interrupt 4{    //串口中断

    if(RI==1){
        s[22]=SBUF;    //接收数据
        dx(s[22],10);
        RI=0;
    
    }
    
}

char dx(unsigned char s[],unsigned int num){
    unsigned int i,j,k;
    unsigned char a[10]={0};
    unsigned char b='-';

    for(k=0;k<10;k++){
    
        a[k]=s[k];
    }
    for(i=0;i<10;i++){                //去除‘-’字符
        if(a[i]==b){
            for(j=i;a[j]!='\0';j++){
                a[j]=a[j+1];
                i--;
            }
        }
    
    }
    
    return a;    //返回字符串
}

这个代码哪里错了