第13届蓝桥杯答案解析

第一批客观题答案

第13届蓝桥杯答案解析_第1张图片

第二批客观题答案

第13届蓝桥杯答案解析_第2张图片

第一批试题:

第13届蓝桥杯答案解析_第3张图片

第13届蓝桥杯答案解析_第4张图片
#include "reg52.h"
#include "ds1302.h"
#include "onewire.h"
 
sfr P4 = 0xC0;
 
sbit H1 = P3^0;
sbit H2 = P3^1;
sbit H3 = P3^2;
sbit H4 = P3^3;
 
sbit L1 = P3^4;
sbit L2 = P3^5;
sbit L3 = P4^2;
sbit L4 = P4^4;
 
unsigned char DS1302_Write_Addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char DS1302_Read_Addr[]= {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//2022-12-23 23-25-30 周五
unsigned char Init_DS1302_Clock[] =  {0x40,0x54,0x30,0x23,0x12,0x05,0x22};
float T_temp = 0.0;
unsigned int  Real_T = 0,Set_T = 23;
unsigned char code smg_data[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90};
unsigned char code smg_dot[] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char channel = 1;  
unsigned char interface = 1,work_mode = 1,led_status = 0xff;//界面
unsigned char  s17_flag = 1,time_5s_flag = 0,L3_flag = 0,relay_flag = 0,clock_flag = 0;
 
//简单延时
void delay(unsigned int t)
{
    while(t--);
}
//74HC138初始化
void Init_74HC138(unsigned char dat)
{
    switch(dat)
    {
        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;
        case 0:P2 = (P2 & 0x1f)|0x00;break;
    }
    P2 = (P2 & 0x1f)|0x00;
}
 
 
//数码管按位操作
void SMG_DisplayByBit(unsigned char pos,unsigned char dat)
{
    P0 = (0x01 << pos);
    Init_74HC138(6);
    P0 = dat;
    Init_74HC138(7);
}
 
//控制所有数码管
void Controal_All_SMG(unsigned char dat)
{
    P0 = 0x0ff;
    Init_74HC138(6);
    P0 = dat;
    Init_74HC138(7);
}
 
//温度获取
void Read_Temperature(void)
{
    unsigned char LSB = 0,MSB = 0;
    unsigned int temp = 0;
    init_ds18b20();
    Write_DS18B20(0xcc);
    Write_DS18B20(0x44);
    
    delay(800);
    init_ds18b20();
    Write_DS18B20(0xcc);
    Write_DS18B20(0xbe);
    LSB = Read_DS18B20();
    MSB = Read_DS18B20();
    init_ds18b20();
    temp = (MSB << 8)| LSB;
    if((temp & 0xf800) == 0x0000)
    {
        T_temp = temp * 0.0625;
    }
    Real_T = T_temp * 10;    
}
 
 
//数码管数据显示温度
void SMG_Display_Temperature(void)
{
    //U 0111 1100
    SMG_DisplayByBit(0,~0x3e);
    delay(200);
    SMG_DisplayByBit(1,smg_data[channel]);
    delay(200);
    SMG_DisplayByBit(2,0xff);
    delay(200);
    SMG_DisplayByBit(3,0xff);
    delay(200);
    SMG_DisplayByBit(4,0xff);
    delay(200);
    SMG_DisplayByBit(5,smg_data[Real_T/100]);
    delay(200);
    SMG_DisplayByBit(6,smg_dot[Real_T/10%10]);
    delay(200);
    SMG_DisplayByBit(7,smg_data[Real_T%10]);
    delay(200);
    Controal_All_SMG(0xff);
    delay(200);
}
 
 
//数码管数据显示设置温度
void SMG_Display_Set_Temperature(void)
{
    //U 0111 1100
    SMG_DisplayByBit(0,~0x3e);
    delay(200);
    SMG_DisplayByBit(1,smg_data[channel]);
    delay(200);
    SMG_DisplayByBit(2,0xff);
    delay(200);
    SMG_DisplayByBit(3,0xff);
    delay(200);
    SMG_DisplayByBit(4,0xff);
    delay(200);
    SMG_DisplayByBit(5,0xff);
    delay(200);
    SMG_DisplayByBit(6,smg_data[Set_T/10]);
    delay(200);
    SMG_DisplayByBit(7,smg_data[Set_T%10]);
    delay(200);
    Controal_All_SMG(0xff);
    delay(200);
}
 
//数码管数据时间
void SMG_Display_Time(void)
{
    //U 0111 1100
    SMG_DisplayByBit(0,~0x3e);
    delay(200);
    SMG_DisplayByBit(1,smg_data[channel]);
    delay(200);
    SMG_DisplayByBit(2,0xff);
    delay(200);
    SMG_DisplayByBit(3,smg_data[Init_DS1302_Clock[1]/16]); //分
    delay(200);
    SMG_DisplayByBit(4,smg_data[Init_DS1302_Clock[1]%16]);
    delay(200);
    SMG_DisplayByBit(5,0xbf);
    delay(200);
    SMG_DisplayByBit(6,smg_data[Init_DS1302_Clock[0]/16]);//秒
    delay(200);
    SMG_DisplayByBit(7,smg_data[Init_DS1302_Clock[0]%16]);
    delay(200);
    Controal_All_SMG(0xff);
    delay(200);
}
 
void SMG_Display_Time2(void)
{
    //U 0111 1100
    SMG_DisplayByBit(0,~0x3e);
    delay(200);
    SMG_DisplayByBit(1,smg_data[channel]);
    delay(200);
    SMG_DisplayByBit(2,0xff);
    delay(200);
    SMG_DisplayByBit(3,smg_data[Init_DS1302_Clock[2]/16]); //时
    delay(200);
    SMG_DisplayByBit(4,smg_data[Init_DS1302_Clock[2]%16]);
    delay(200);
    SMG_DisplayByBit(5,0xbf);
    delay(200);
    SMG_DisplayByBit(6,smg_data[Init_DS1302_Clock[1]/16]);//分
    delay(200);
    SMG_DisplayByBit(7,smg_data[Init_DS1302_Clock[1]%16]);
    delay(200);
    Controal_All_SMG(0xff);
    delay(200);
}
 
 
 
//写入时间
void Write_DS1302_Data(void)
{
    unsigned char i = 0;
    //关闭写保护
    Write_Ds1302(0x8e,0x00);
    for(i =0;i<7;i++)
    {
        Write_Ds1302(DS1302_Write_Addr[i],Init_DS1302_Clock[i]);
    }
    //打开写保护
    Write_Ds1302(0x8e,0x80);
}
 
 
 
//读取时间
void Read_DS1302_Data(void)
{
    unsigned char  i = 0;
    for(i = 0;i<7;i++)
    {
        Init_DS1302_Clock[i] = Read_Ds1302(DS1302_Read_Addr[i]);
    }
}
 
 
//按键操作
void Matrix_KEY_Scan(void)
{    
    H3 = 0;
    H1 = H2 = H4 = 1;
    //13
    if(L2 == 0)
    {
        delay(20);
        if(L2 == 0)
        {
            while(L2 == 0)
            {
                if(interface == 1)
                {
                    SMG_Display_Temperature();
                }
                else if(interface == 2)
                {
                    SMG_Display_Time2();
                }
                else if(interface == 3)
                {
                    SMG_Display_Set_Temperature();
                }
            }
            if(work_mode == 1)
            {
                work_mode = 2;
            }
            else if(work_mode == 2)
            {
                work_mode = 1;
            }
        }
    }
    
    //S17
    if(interface == 3)
    {
        if(L1 == 0)
        {
            delay(20);
            if(L1 == 0)
            {
                while(L1 == 0)
                {
                    SMG_Display_Set_Temperature();
                }
                Set_T--;
            }
        }
    }
    else if(interface == 2)
    {
        if(L1 == 0)
        {
            delay(20);
            if(L1 == 0)
            {
                while(L1 == 0)
                {
                    SMG_Display_Time();
                    s17_flag = 1;
                }
                s17_flag = 0;
                SMG_Display_Time2();
            }
        }
    }
    
    
    
    H4 = 0;
    H1 = H3 = H2 = 1;
    //S12
    if(L2 == 0)
    {
        delay(20);
        if(L2 == 0)
        {
            while(L2 == 0)
            {
                if(interface == 1)
                {
                    SMG_Display_Temperature();
                }
                else if(interface == 2)
                {
                    SMG_Display_Time2();
                }
                else if(interface == 3)
                {
                    SMG_Display_Set_Temperature();
                }
            }
            if(interface == 1)
            {
                interface = 2;
                channel = 2;
            }
            else if(interface == 2)
            {
                interface = 3;
                channel = 3;
            }
            else if(interface == 3)
            {
                interface = 1;
                channel = 1;
            }
        }
    }
    //S16
    
    if(L1 == 0)
    {
        delay(20);
        if(L1 == 0)
        {
            while(L1 == 0)
            {
                if(interface == 1)
                {
                    SMG_Display_Temperature();
                }
                else if(interface == 2)
                {
                    SMG_Display_Time2();
                }
                else if(interface == 3)
                {
                    SMG_Display_Set_Temperature();
                }
            }
            if(interface == 3)
            {
                Set_T++;
            }
            
        }
    }
}
 
 
//定时器初始化
void Init_Timer(void)
{
    TMOD = 0x01;
    TH0 = (65535-10000)/256;//10ms
    TL0 = (65535-10000)%256;
    ET0 = 1;
    TR0 = 1;
    EA  = 1;
}
 
 
//系统初始化
void Init_System(void)
{
    Init_74HC138(0);
    Controal_All_SMG(0xff);
    P0 = 0xff;
    Init_74HC138(4);
    P0 = 0x00;
    Init_74HC138(5);
}
 
 
//主函数
void main(void)
{
    Init_System();
    Init_Timer();
    Write_DS1302_Data();
    while(1)
    {
        Read_Temperature();
        delay(200);
        Read_DS1302_Data();
        Matrix_KEY_Scan();
        switch(interface)
        {
            case 1:SMG_Display_Temperature();break;
            case 2:SMG_Display_Time2();break;
            case 3:SMG_Display_Set_Temperature();break;
            default:break;
        }    
 
        
        //温度控制模式
        if(work_mode == 1)
        {
            //L2亮温度模式指示灯 1111 1101
            led_status =  0xFD;
            P0 = led_status;
            Init_74HC138(4);
            if(Real_T/10 > Set_T)
            {
                //继电器吸合
                P0 = 0x10;  //0001 0000
                relay_flag = 1; 
            }
            else
            {
                //继电器断开
                P0 = 0x00;
                relay_flag = 0; 
            }
            Init_74HC138(5);
            
        }
        //时间控制模式
        else if(work_mode == 2)
        {
            P0 =  0xFF;
            Init_74HC138(4);
            //整点继电器吸合  5s后断开
            if((Init_DS1302_Clock[0]/16 + Init_DS1302_Clock[0]%16) == 0 && (Init_DS1302_Clock[1]/16 + Init_DS1302_Clock[1]%16)  == 0 && Init_DS1302_Clock[2] != 0)
            {
                clock_flag = 1; //时间整点标志
            }
            if(clock_flag == 1)
            {
                if(time_5s_flag == 0)
                {
    
                    P0 = 0xfe;// 1111 1110
                    Init_74HC138(4);
                                    //继电器吸合
                    P0 = 0x10;  //0001 0000
                    relay_flag = 1; 
                    Init_74HC138(5);
                }
                else
                {
                    P0 = 0XFF;// 1111 1110
                    Init_74HC138(4);
                    P0 = 0x00;// 1111 1110
                    relay_flag = 0; 
                    Init_74HC138(5);
                }
                
            }
        }
        
        if(relay_flag == 1)
        {
            if(L3_flag == 1)
            {
                L3_flag = 0;
                //L3 1111 1011
                
                P0 = 0xfb;
                Init_74HC138(4);
            }
            else
            {
                P0 = 0xFF;
                Init_74HC138(4);
            }
        }
    }
}
 
void Timer0_Server() interrupt 1
{
    static unsigned int t = 0,s = 0;
    TH0 = (65535-10000)/256;//10ms
    TL0 = (65535-10000)%256;
    if(work_mode == 2 && clock_flag == 1)
    {
        t++;
        if(t >500)  // >5s
        {
            t = 0;
            time_5s_flag = 1;
        }
    }
    s++;
    if(s == 10)
    {
        s = 0;
        L3_flag = 1;
    }
}

第二场试题

第13届蓝桥杯答案解析_第5张图片
#include "reg52.h"
#include "iic.h"
#include "intrins.h"
 
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
sbit TX = P1^0;  //发射引脚
sbit RX = P1^1;  //接收引脚
 
typedef unsigned char uc;
typedef unsigned int ui;
 
#define SMGTIME 500
 
unsigned char SMGNODot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char SMGDot_CA[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
 
uc adc_value = 0;  //ad采样电压
float adc_volt = 0; //ad实际电压
ui adc_smg = 0;        //
 
uc F_smg = 1; // 1电压 2测距 3参数
 
uc F_csyx = 2; //1参数设置无效 2 参数设置有效 开始未按下S5时是有效的
uc F_param = 1; // 1上限 2下限
uc param_sx_volt = 45; //上限参数
uc param_xx_volt = 5;    //下限参数
float f_param_sx_volt = 0;
float f_param_xx_volt = 0;
 
ui csb_l = 0;    //超声波距离
uc F_csb = 1; // 1未启动测量 2 启动连续测量
bit csb_flag; //超声波标志位
ui csb_t = 0; //超声波时间
 
uc count_t = 0; //LED定时计时值
uc count_tc = 0;//超声波定时计时值
 
uc led_stat = 0xff; //LED状态
 
uc da_volt = 0; //da输出电压
 
void Set_HC573(uc channel, uc dat)
{
    P0 = dat;
    switch(channel)
    {
        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;
        case 0: P2 = (P2 & 0x1f) | 0x00; break;
    }
    P2 = (P2 & 0x1f) | 0x00;
}
 
void Delay1us(ui t)        //@12.000MHz
{
    while(t--)
    {
        _nop_();
        _nop_();
        _nop_();
        _nop_();
    }
}
 
void Delay(ui t)
{
    while(t--);
}
 
void DisplaySMG_Bit(uc pose, uc value)
{
    Set_HC573(6,0x01< adc_volt) &&  (adc_volt > f_param_xx_volt) )
        {
            F_csb = 2;//启动连续测量功能
        }
        else
        {
            F_csb = 1;//关闭测量
        }
    }
    else ;
}
 
 
void Deal_DA_Ceju()
{
    if(F_csb == 2) //启动测量的情况下
    {
        if(csb_l <=20)
        {
            da_volt = 51;
            Set_DAC(da_volt);//输出1V
        }
        else if(csb_l>20 && csb_l<80)
        {
            float f_da_volt = 0;
            f_da_volt = 1.0/15 * csb_l + 1.0/3;
            da_volt = f_da_volt/5.0*255;
            Set_DAC(da_volt);
        }
        else
        {
            da_volt = 255;
            Set_DAC(da_volt);//输出5V
        }
    }
    else
    {
        Set_DAC(0);//DA输出0V
    }
}
 
void Scan_Keys()
{
    if(S4 == 0) //切换界面
    {
        Delay(200);
        if(S4 == 0)
        {
            if(F_smg == 1) //测距下
            {
                led_stat |= 0x01; //L1熄灭
                Set_HC573(4,led_stat);
                
                F_smg = 2;
                
                led_stat &= ~0x02; //L2点亮
                Set_HC573(4,led_stat);
            }
            
            else if(F_smg ==2) //参数下
            {
                led_stat |= 0x02; //L2熄灭
                Set_HC573(4,led_stat);
                
                F_param = 1; //默认设置上限
                F_smg = 3;
                
                led_stat &= ~0x04; //L3点亮
                Set_HC573(4,led_stat);
                
            }
            else if(F_smg == 3) //电压下
            {
                led_stat |= 0x04; //L3x熄灭
                Set_HC573(4,led_stat);
                
                F_csyx = 2; //参数设置有效
                F_smg = 1 ;
                
                led_stat &= ~0x01; //L1点亮
                Set_HC573(4,led_stat);
                
            }
            while(S4 == 0)
            {
                DisplaySMG_info();
            }
            
        }
    }
    
    if(S5 == 0) //参数选择
    {
        Delay(200);
        if(S5 == 0)
        {
            F_csyx = 1;//参数设置无效
            if(F_smg == 3)
            {
                if(F_param == 1) F_param = 2;
                else if(F_param == 2) F_param = 1;
            }            
            while(S5 == 0)
            {
                DisplaySMG_info();
            }
            
        }
    }
    
    if(S6 == 0)
    {
        Delay(200);
        if(S6 == 0)
        {
            F_csyx = 1;//参数设置无效
            if(F_smg == 3) //在参数界面下
            {
                if(F_param == 1) //上限设置加
                {
                    if(param_sx_volt != 50)
                    {
                        param_sx_volt+=5;
                    }
                    else
                    {
                        param_sx_volt = 5;
                    }
                }
                else if(F_param == 2) //下限设置加
                {
                    if(param_xx_volt != 50)
                    {
                        param_xx_volt+=5;
                    }
                    else
                    {
                        param_xx_volt = 5;
                    }
                }
        
            }
            while(S6 == 0)
            {
                DisplaySMG_info();
            }
            
        }
    }
    
    if(S7 == 0)
    {
        Delay(200);
        if(S7 == 0)
        {
            F_csyx = 1;//参数设置无效
            if(F_smg == 3) //在参数界面下
            {
                    if(F_param == 1) //上限设置减
                    {
                        if(param_sx_volt != 5)
                        {
                            param_sx_volt-=5;
                        }
                        else
                        {
                            param_sx_volt = 50;
                        }
                    }
                    else if(F_param == 2) //下限设置减
                    {
                        if(param_xx_volt != 5)
                        {
                            param_xx_volt-=5;
                        }
                        else
                        {
                            param_xx_volt = 50;
                        }
                    }
                }
                
            while(S7 == 0)
            {
                DisplaySMG_info();
            }
        }
    }
}
 
void Init_Timer0()
{
    TMOD = 0x11; // 开启两个定时器 
    TH0 = (65535-50000)/256; //计时50ms
    TL0 = (65535-50000)%256;
    TH1 = 0;
    TL1 = 0;
    
    TR0 = 1;
    EA = 1;
    ET0 = 1;
}
 
void Service_Time0() interrupt 1
{
    TH0 = (65535-50000)/256;
    TL0 = (65535-50000)%256; 
    //50ms * 2 = 100ms = 0.1s
    if(F_csb == 2 ) //启动连续测量 
    {
        count_t++;
        if(count_t % 2 == 0) //达到0.1s 
        {
            led_stat &= ~0x80; //L8以0.1s 闪烁
            Set_HC573(4,led_stat);
        }
        else
        {
            led_stat |= 0x80; //熄灭L8
            Set_HC573(4,led_stat);
        }
    }
    else
    {
        count_t = 0;
        led_stat |= 0x80; //L8熄灭
        Set_HC573(4,led_stat);
    }
    
    //DisplaySMG_info();
    count_tc++;
    if(count_tc == 10)   //500ms执行一次
    {
        csb_flag = 1;
        count_tc = 0;
  }
}
 
void Init_sys()
{
    Set_HC573(0,0x00);
    Set_HC573(4,0xff);
    Set_HC573(5,0x00);
    DisplaySMG_All(0xff);
    led_stat &= ~0x01; //L1点亮,L2 L3熄灭
    Set_HC573(4,led_stat);
    Init_Timer0();
}
 
void main()
{
    Init_sys();
    while(1)
    {
        Read_ADC();
        Scan_Keys();
        DisplaySMG_info();
        Csb_func();
        Csb_ceju();
        Deal_DA_Ceju();
    }
}

资源引用全网,爱你小蜜蜂老师

你可能感兴趣的:(蓝桥真题解析,单片机)