第8届蓝桥答案解析

 #include "reg52.h"
 #include "onewire.h"
 #include "absacc.h"




sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit Led1 = P0^0;

unsigned char T_Hour = 23,T_Min= 59,T_Sec = 50;
unsigned char C_Hour = 0,C_Min = 0,C_Sec = 0;
unsigned char Temperature = 0;//温度存储
unsigned char time_set = 0,clock_set = 0,mode_set = 0;//时间设置和闹钟时钟标志
unsigned char t_count0 = 0,t_flag = 0,t_count1,c_count = 0;//计时和闪烁标志
unsigned char t_led = 0,led_flag = 0;//分别标记0.2s 和5s
unsigned char code smg_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
//------------------------------简单延时----------------------------
void Delay(unsigned int t)
{
     while(t--);
}
//--------------------------74CHC138初始化-------------------------
void  Init_74HC138(unsigned char n)
{
     switch(n)
    {
         case 0:P2 = (P2 & 0x1f) | 0x00;break;
        case 4:P2 = (P2 & 0x1f) | 0x80;break;
        case 5:P2 = (P2 & 0x1f) | 0xa0;break;
        case 6:P2 = (P2 & 0x1f) | 0xc0;break;
        case 7:P2 = (P2 & 0x1f) | 0xe0;break;
    }
}
//-------------------------系统初始化-----------------------------
void InitSyetem(void)
{
     Init_74HC138(4);
    P0 = 0xff; //关闭LED
    Init_74HC138(5);
    P0 = 0x00;//关闭继电器和蜂鸣器
}
//-------------------------数码管安位显示-------------------------
void SMG_DisplayBit(unsigned char pos,unsigned char dat)
{
     Init_74HC138(6);
    P0 = (0x01 << pos);
    Init_74HC138(7);
    P0 = dat;
}

void SMG_Close(void)
{
     Init_74HC138(6);
    P0 = 0xff;
    Init_74HC138(7);
    P0 = 0xff;
}
//-----------------------数码管显示------------------------------
void SMG_Display(unsigned char hour,unsigned char min,unsigned char sec)
{
    SMG_DisplayBit(0,smg_duan[hour/10]);
  Delay(200);
    SMG_DisplayBit(1,smg_duan[hour%10]);
    Delay(200);
    SMG_DisplayBit(2,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(3,smg_duan[min/10]);
    Delay(200);
    SMG_DisplayBit(4,smg_duan[min%10]);
    Delay(200);
    SMG_DisplayBit(5,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(6,smg_duan[sec/10]);
    Delay(200);
    SMG_DisplayBit(7,smg_duan[sec%10]);
    Delay(200);    
    SMG_Close();
}

void SMG_Hour_Flash(unsigned char hour,unsigned char min,unsigned char sec)
{
    SMG_DisplayBit(2,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(3,smg_duan[min/10]);
    Delay(200);
    SMG_DisplayBit(4,smg_duan[min%10]);
    Delay(200);
    SMG_DisplayBit(5,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(6,smg_duan[sec/10]);
    Delay(200);
    SMG_DisplayBit(7,smg_duan[sec%10]);
    Delay(200);
    if(t_flag == 0)
    {
              SMG_DisplayBit(0,smg_duan[hour/10]);
            Delay(200);
            SMG_DisplayBit(1,smg_duan[hour%10]);
            Delay(200);
    }
    else
    {
             SMG_DisplayBit(0,smg_duan[11]);
            Delay(200);
            SMG_DisplayBit(1,smg_duan[11]);
            Delay(200);
    }
    SMG_Close();
}

void SMG_Min_Flash(unsigned char hour,unsigned char min,unsigned char sec)
{
    SMG_DisplayBit(0,smg_duan[hour/10]);
    Delay(200);
    SMG_DisplayBit(1,smg_duan[hour%10]);
    Delay(200);
    SMG_DisplayBit(2,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(5,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(6,smg_duan[sec/10]);
    Delay(200);
    SMG_DisplayBit(7,smg_duan[sec%10]);
    Delay(200);
    if(t_flag == 0)
    {
             SMG_DisplayBit(3,smg_duan[min/10]);
            Delay(200);
            SMG_DisplayBit(4,smg_duan[min%10]);
            Delay(200);     
    }
    else
    {
             SMG_DisplayBit(3,smg_duan[11]);
            Delay(200);
            SMG_DisplayBit(4,smg_duan[11]);
            Delay(200);
    }
    SMG_Close();
}

void SMG_Sec_Flash(unsigned char hour,unsigned char min,unsigned char sec)
{
    SMG_DisplayBit(0,smg_duan[hour/10]);
    Delay(200);
    SMG_DisplayBit(1,smg_duan[hour%10]);
    Delay(200);
    SMG_DisplayBit(2,smg_duan[10]);
    Delay(200);
    SMG_DisplayBit(3,smg_duan[min/10]);
    Delay(200);
    SMG_DisplayBit(4,smg_duan[min%10]);
    Delay(200); 
    SMG_DisplayBit(5,smg_duan[10]);
    Delay(200);
    
    if(t_flag == 0)
    {
          SMG_DisplayBit(6,smg_duan[sec/10]);
            Delay(200);
            SMG_DisplayBit(7,smg_duan[sec%10]);
            Delay(200);    
    }
    else
    {
             SMG_DisplayBit(6,smg_duan[11]);
            Delay(200);
            SMG_DisplayBit(7,smg_duan[11]);
            Delay(200);
    }
    SMG_Close();
}
//---------------------------------温度读取------------------------
void Read_Temperature(void)
{
     unsigned char LSB = 0,MSB = 0;
    init_ds18b20();
    Write_DS18B20(0xcc);//跳过Rom
    Write_DS18B20(0x44);//开始转换

    Delay(500);
    init_ds18b20();
    Write_DS18B20(0xcc);//跳过Rom
    Write_DS18B20(0xbe);//读取温度到暂存区

    LSB = Read_DS18B20();//读取第0字节
    MSB = Read_DS18B20();//读取第1字节

    Temperature = (MSB << 8)| LSB;
    Temperature = Temperature >> 4;//保留整数
    
    /*
        小数处理
        if((Temperature & 0x1f)    != 0x0000) //高五位为符号 全零为+
        {
          Temperature = Temperature >> 4;
            Temperature = Temperature * 10;
            Temperature = Temperature + (LSB & 0x0f) *0.625;
        }    
    */ 
}
//-------------------------------温度显示---------------------------
void Temperature_Display(void)
{
     SMG_DisplayBit(0,smg_duan[11]);
  Delay(200);
    SMG_DisplayBit(1,smg_duan[11]);
    Delay(200);
    SMG_DisplayBit(2,smg_duan[11]);
    Delay(200);
    SMG_DisplayBit(3,smg_duan[11]);
    Delay(200);
    SMG_DisplayBit(4,smg_duan[11]);
    Delay(200);
    SMG_DisplayBit(5,smg_duan[Temperature/10]);
    Delay(200);
    SMG_DisplayBit(6,smg_duan[Temperature%10]);
    Delay(200);
    SMG_DisplayBit(7,0xC6);     // C
    Delay(200);    
    SMG_Close();
}

//----------------------------------按键处理------------------------
void KeyScans(void)
{
     if(S7 == 0)
    {
         Delay(20);
        if(S7 == 0)
        {    
            mode_set = 1;        
             while(S7 == 0);
            time_set++;
            
            ET0 = 0,TR0 = 0;
            if(time_set == 1)
            {
                    //时设置
                    SMG_Hour_Flash(T_Hour,T_Min,T_Sec);
            }
            else if(time_set == 2)
            {
                 //分设置
                 SMG_Min_Flash(T_Hour,T_Min,T_Sec);
                
            }
            else if(time_set > 3)
            {
                //秒
                SMG_Sec_Flash(T_Hour,T_Min,T_Sec);
                 time_set = 0;    
                ET0 = 1,TR0 = 1;            
            }
        }
}    
    // s6 闹钟时分秒选择
    if(S6 == 0)
    {
         Delay(20);
        if(S6 == 0)
        {
             clock_set++;
            mode_set = 2;
            ET0 = 0,TR0 = 0;
            while(S6 == 0); //等待按键松开
            if(clock_set == 1) //选择时
            {
                  SMG_Hour_Flash(C_Hour,C_Min,C_Sec);
            }
            else if(clock_set == 2)    //选择分
            {
                  SMG_Min_Flash(C_Hour,C_Min,C_Sec);    
            }
            else if(clock_set == 3)
            {
                
                 SMG_Sec_Flash(C_Hour,C_Min,C_Sec);
            }
            else if(clock_set == 4)
            {
                    SMG_Display(T_Hour,T_Min,T_Sec); //时间实时显示    
                    clock_set = 0;
                    ET0 = 1,TR0 = 1;    
            }
        }
    }
    if(S5 == 0)
    {
         Delay(20);
        if(S5 == 0)
        {
            while(S5 == 0);
            if(time_set == 1) //小时进行加
            {
                 T_Hour++;
                if(T_Hour >= 24) //边界处理
                {
                     T_Hour = 0;
                }
            }
            else if(time_set == 2) //分钟进行加
            {
                 T_Min++;
                 if(T_Min >= 60) //边界处理
                 {
                        T_Min = 0;
                 }
            }
            else if(time_set == 3)//秒进行加
            {
                     T_Sec++;
                    if(T_Sec >= 60) //边界处理
                    {
                         T_Sec = 0;
                    }
            }
            else if(clock_set == 1)
            {
                 C_Hour++;
                if(C_Hour == 24) //边界处理
                {
                     C_Hour = 0;
                }
            }
            else if(clock_set == 2)
            {
                 C_Min++;
                if(C_Min == 60)
                {
                     C_Min = 0;
                }
            }
            else if(clock_set == 3)
            {
                 C_Sec++;
                if(C_Sec == 60)
                {
                     C_Sec = 0;
                }
            }
        }
}
        if(S4 == 0)
        {
             Delay(20);
            if(S4 == 0)
            {
                while(S4 == 0);
                if(time_set == 1)//时进行减
                {
                    T_Hour--;
                    if(T_Hour == 0)//边界处理
                    {
                         T_Hour = 24;
                    }
                }
                else if(time_set == 2) //分钟减
                {
                     T_Min--;
                    if(T_Min == 0) //边界处理
                    {
                         T_Min = 60;
                    }
                }
                else if(time_set == 3) //秒进行减
                {
                    T_Sec--;
                    if(T_Sec == 0)//边界处理
                    {
                         T_Sec = 60;
                    }
                }

                else if(clock_set == 1)
            {
                 C_Hour--;
                if(C_Hour == 0) //边界处理
                {
                     C_Hour = 24;
                }
            }
            else if(clock_set == 2)
            {
                 C_Min--;
                if(C_Min == 0)
                {
                     C_Min = 60;
                }
            }
            else if(clock_set == 3)
            {
                 C_Sec--;
                if(C_Sec == 0)
                {
                     C_Sec = 60;
                }
            }
            }
        }
}
//-------------------------------------定时器初始化-------------------
void Init_Timer0(void)
{
     TMOD = 0x11; //定时器0 定时器1     方式一16 
    TH0 = (65535-50000)/256;//50ms
    TL0 = (65535-50000)%256;

    TH1 = (65535-50000)/256;//50ms
    TL1 = (65535-50000)%256;
    ET1 = 1;
    TR1 = 1;
    ET0 = 1;
    TR0 = 1;
    EA  = 1;
}
//-----------------------------主函数-------------------------------
void main(void)
{
    InitSyetem();
    Init_Timer0();
    while(1)
    {
        //读取温度
         Read_Temperature();
        if((time_set == 0) && (clock_set == 0))
        {
            if(S4 == 0)
            {
                Delay(20);
                if(S4 == 0)
                {
                     while(S4 == 0)
                    {
                         Delay(200);
                        //显示温度
                        Temperature_Display();
                    }
                }
                 
            }        
            SMG_Display(T_Hour,T_Min,T_Sec); //时间实时显示    
        //    SMG_Display(C_Hour,C_Min,C_Sec); //闹钟实时显示    
        }                         
         KeyScans();    
            if(mode_set == 1)
            {
                switch(time_set)
                {
                     case 1:SMG_Hour_Flash(T_Hour,T_Min,T_Sec);break;
                    case 2:SMG_Min_Flash(T_Hour,T_Min,T_Sec);break;
                    case 3:SMG_Sec_Flash(T_Hour,T_Min,T_Sec);break;
                }
            }
            else if(mode_set == 2)
            {
                 switch(clock_set)
                {
                     case 1:SMG_Hour_Flash(C_Hour,C_Min,C_Sec);break;
                    case 2:SMG_Min_Flash(C_Hour,C_Min,C_Sec);break;
                    case 3:SMG_Sec_Flash(C_Hour,C_Min,C_Sec);break;
                }
            }    
            
            //闹钟到时响应 led1 0.2s闪烁 5s
            else if(((T_Hour == 0) && (T_Min == 0) && (T_Sec == 0))|| ((T_Hour == C_Hour) && (T_Min == C_Min) && (T_Sec == C_Sec)))
             {
                     Init_74HC138(4);
                     if(led_flag == 0)
                    {                          
                         P0 = 0xfe;
                    }
                    else if(led_flag == 1)
                    {
                        P0 = 0xff;
                    }
                     Delay(50000);
                    Delay(50000);
                    P0 = 0xff;
            }
    }     
}
//-----------------------------定时器中断服务-------------------------
void Server_Timero() interrupt 1
{
     TH0 = (65535-50000)/256;
    TL0 = (65535-50000)%256;
    t_count0++;
    c_count++;
    if(t_count0 == 4) // 0.2s
    {
         led_flag = ~led_flag;
    }
    else if(t_count0 == 20) // 1s 时间计数标志
    {
         t_count0 = 0;
        if(T_Sec++ == 60) 
        {
             T_Sec = 0;
            T_Min++;
            if(T_Min == 60)
            {
                 T_Min = 0;
                T_Hour++;
                if(T_Hour == 24)
                {
                     T_Hour = 0;
                }
            }               
        }
    }
    if(c_count == 20) //闹钟计时1s
    {                                                                                                                                                                                 
         c_count = 0;
        C_Sec++;
        if(C_Sec == 60)
        {
             C_Sec = 0;
            C_Min++;
            if(C_Min == 60)
            {
                 C_Min = 0;
                C_Hour++;
                if(C_Hour == 24)
                {
                     C_Hour = 0;
                }
            }
        }

    }
}

void Server_Timer1() interrupt 3
{
     TH1 = (65535-50000)/256;//50ms
    TL1 = (65535-50000)%256;
    t_count1++;
    if(t_count1 == 20) //1s
    {
         t_count1 = 0;
        t_flag = ~t_flag;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值