weixin_55319901
2022-06-20 23:51
采纳率: 57.1%
浏览 39

DS1302时钟模块不能调时间

main为什么按键之后无法通过flag改变选中的值


void selected(uchar flag,uchar x)
{
        switch(flag)
        {
            case 0:{
                lcd_write_str (6,1,"--");
                delay(500);
                DisplayOneData (6,1,x);//秒
                delay(500);
                break;
            }
            case 1:{
                lcd_write_str (3,1,"--");
                delay(500);
                DisplayOneData (3,1,x);//分
                delay(500);
                break;
            }
            case 2:{
                lcd_write_str (0,1,"--");
                delay(500);
                DisplayOneData (0,1,x);//时
                delay(500);
                break;
            }
            case 3:{
                lcd_write_str (8,0,"--");
                delay(500);
                DisplayOneData (8,0,x);//秒
                delay(500);
                break;
            }
            case 4:{
                lcd_write_str (5,0,"--");
                delay(500);
                DisplayOneData (5,0,x);//秒
                delay(500);
                break;
            }
            case 5:{
                lcd_write_str (11,0,"--");
                delay(500);
                DisplayOneData (11,0,x);//秒
                delay(500);
                break;
            }
            case 6:{
                lcd_write_str (2,0,"--"); //年
                delay(500);
                DisplayOneData (2,0,x); //年
                delay(500);
                break;
            }
        }
}
void display(uint temp)
{    
    
    uchar i,dat[]={0,0,0,0};
    lcd_write_cmd(0xc9);
    dat[0]=temp/1000;
    dat[1]=temp/100%10;
    dat[2]=temp/10%10;
    dat[3]=temp%10;
    if(dat[0])
        lcd_write_data(dat[0]+'0');
    for(i=1;i<4;i++)
    {
        lcd_write_data(dat[i]+'0');
        if(i==2)
        {
            lcd_write_data('.');
        }

    }
    lcd_write_data(0xdf);
    lcd_write_data('C');
}
void main()
{
    uchar key=10,i;
    lcd_init();
    I2C_init();
    Set_DS1302_Time(DS1302_W_ADDR);    //先写入时钟日历寄存器起始地址再设置时钟日历初值
    while(1)
    {
        uchar flag=0;
        key=key_scan();
        if(key==0)
        {
            TR0=0;
            for(i=0;i<7;i++)
                TimeData[i]=TimeData[i]/16*10+TimeData[i]%16;
            while(1) 
            {
                uchar flag=0;
                selected(flag,TimeData[flag]);
                key=key_scan();
                if(key==1){
                    flag=flag++;
                    flag%=6;
                    key=10;
                }
                if(key==3){
                    if(flag<2)
                        TimeData[flag]=(TimeData[flag]+1)%60;
                    else
                        TimeData[flag]=(TimeData[flag]+1)%24;
                    key=10;
                }
                if(key==0){
                    Set_DS1302_Time(DS1302_W_ADDR);
                    key=10;
                    break;
                }
            }
        }
        Read_DS1302_Time(DS1302_R_ADDR); //先写入时钟日历寄存器起始地址再读出时钟日历写入到TimeData数组中
        lcd_write_str(0,0,"20");
        DisplayOneStr (2,0,TimeData[6]); //年
        DisplayOneChar (4,0,'_');         //-
        DisplayOneStr (5,0,TimeData[4]); //月
        DisplayOneChar (7,0,'_');         //-
        DisplayOneStr (8,0,TimeData[3]);//日
        DisplayOneChar (10,0,'_');         //-
        DisplayOneStr (11,0,TimeData[5]);//星期

        DisplayOneStr (0,1,TimeData[2]); //时
        DisplayOneChar (2,1,':');         //:
        DisplayOneStr (3,1,TimeData[1]); //分
        DisplayOneChar (5,1,':');         //:
        DisplayOneStr (6,1,TimeData[0]);//秒
        display(Get_temp());
    }
}

4条回答 默认 最新

相关推荐 更多相似问题