蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】

第十三届蓝桥杯单片机省赛冲刺-巩固练习3

明天就正式比赛啦,希望大家都可以取得一个不错的成绩。
最后一天在把各个模块的使用敲一遍哦,一定要熟悉各个模块的代码
蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】
文章图片

下面的内容是小蜜蜂老师在微信公众号发布的,大家可以在练习一下,都是常考内容的综合使用
蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】
文章图片

#include "reg52.h" #include "iic.h"sbit S6 = P3^1; sbit S5 = P3^2; sbit S4 = P3^3; unsigned int adc_volt = 0,volt = 0; //采集电压 和数码管显示变量 unsigned int param_volt = 300,alarm_count = 0; //电压参数 、计数参数 unsigned char alarm_flag = 0,flash_flag = 0; //报警标志 、闪烁标志 unsigned char led_stat = 0xff; //led状态 unsigned int t_count = 0; //计时 unsigned char interface = 0; //界面切换 unsigned char code smg_data[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80}; unsigned char code smg_data_dot[]={0xc0-0x80,0xf9-0x80,0xa4-0x80,0xb0-0x80,0x99-0x80,0x92-0x80,0x82-0x80,0xf8-0x80,0x80-0x80}; void delay(unsigned int t) { while(t--); }void Init_74HC138(unsigned char channel) { 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 SMG_All(unsigned char dat) { P0 = 0xff; Init_74HC138(6); P0 = dat; Init_74HC138(7); }void SMG_DisplayBit(unsigned char pos,unsigned char dat) { P0 = (0x01 << pos); Init_74HC138(6); P0 = dat; Init_74HC138(7); }void Read_ADC(void) { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x03); IIC_WaitAck(); IIC_Stop(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); adc_volt = IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); //数据处理 volt = adc_volt * (5.0/255)*100; }void SMG_Display_Data(void) { SMG_DisplayBit(1,0xff); delay(200); SMG_DisplayBit(2,0xff); delay(200); SMG_DisplayBit(3,0xff); delay(200); SMG_DisplayBit(4,0xff); delay(200); if(interface == 0) { //U 1000 0011 0xc1 SMG_DisplayBit(0,0xc1); delay(200); SMG_DisplayBit(5,smg_data_dot[volt/100]); delay(200); SMG_DisplayBit(6,smg_data[volt/10%10]); delay(200); SMG_DisplayBit(7,smg_data[volt%10]); delay(200); } else if(interface == 1) { //P 0011 0001 0x8c SMG_DisplayBit(0,0x8c); delay(200); SMG_DisplayBit(5,smg_data_dot[param_volt/100]); delay(200); SMG_DisplayBit(6,smg_data[param_volt/10%10]); delay(200); SMG_DisplayBit(7,smg_data[param_volt%10]); delay(200); } else if(interface == 2) { //S SMG_DisplayBit(0,0x92); delay(200); SMG_DisplayBit(5,0xff); delay(200); if(alarm_count > 9) { SMG_DisplayBit(6,smg_data[alarm_count/10]); delay(200); } SMG_DisplayBit(7,smg_data[alarm_count%10]); delay(200); } SMG_All(0xff); delay(200); }void Key_Scans(void) { //S4界面切换 if(S4 == 0) { delay(20); if(S4 == 0) { while(S4 == 0) { SMG_Display_Data(); } if(interface == 0) { interface = 1; } else if(interface == 1) { interface = 2; } else if(interface == 2) { interface = 0; } } } //电压参数减0.5v每次 if(S5 == 0) { delay(20); if(S5 == 0) { while(S5 == 0) { SMG_Display_Data(); } if(interface == 1) { param_volt -= 50; //扩大100倍 分别显示处理 if(param_volt == 0)//边界处理 { //减到最小值后 变成最大 param_volt = 500; } } } }//电压参数加0.5v每次 if(S6 == 0) { delay(20); if(S6 == 0) { while(S6 == 0) { SMG_Display_Data(); } if(interface == 1) { param_volt += 50; //扩大100倍 分别显示处理 if(param_volt == 500)//边界处理 { //加到最大值后变成最小值 param_volt = 0; } } } } }void Led_Tackle(void) { if(volt < param_volt) { if(alarm_flag == 0) { alarm_flag = 1; //报警标志 } } else { alarm_flag = 0; flash_flag = 0; t_count = 0; alarm_count = 0; led_stat |= 0xff; P0 = led_stat; Init_74HC138(4); } } void Init_Timer(void) { TMOD = 0x01; TH0= (65535-50000)/256; TL0= (65535-50000)%256; ET0= 1; TR0= 1; EA= 1; } void Init_System(void) { P0 = 0xff; Init_74HC138(4); P0 = 0x00; Init_74HC138(5); SMG_All(0xff); }void main(void) { Init_System(); Init_Timer(); while(1) { Read_ADC(); Key_Scans(); SMG_Display_Data(); Led_Tackle(); } }void Server_Timer0() interrupt 1 { TH0= (65535-50000)/256; TL0= (65535-50000)%256; //启动报警 if(alarm_flag == 1) { t_count++; if(t_count % 20 == 0) //1s { //还未到达最值计数值 if(alarm_count != 99) { alarm_count++; } //秒闪标志 if(flash_flag == 1) { if(led_stat & 0x80 == 0x80) { led_stat &= (~0x80); //点亮L8 } else { led_stat |= 0x80; //熄灭L8 } } }if(t_count > 120) //超过6s { led_stat &= (~0x07); //点亮L1 L2 L 3 flash_flag = 1; } else if(t_count > 80) //超过4s { led_stat &= (~0x03); } else if(t_count > 40) //超过2s { led_stat &= (~0x01); //L1亮 } else { led_stat = 0xff; //关闭LED } P0 = led_stat; Init_74HC138(4); } }

【蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】】蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】
文章图片

#include "reg52.h" #include "iic.h"sbit S5 = P3^2; sbit S4 = P3^3; unsigned int temp = 0; unsigned char led_stat = 0xff; //led 状态 unsigned char adc_volt= 0,old_volt = 0; //电压采集 unsigned int volt = 0; //数码管显示电压 unsigned char old_grade = 0,grade = 0; //灯光等级 //0-9 段码 unsigned char code smg_data[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80}; //0.-9.段码 unsigned char code smg_data_dot[]={0xc0-0x80,0xf9-0x80,0xa4-0x80,0xb0-0x80,0x99-0x80,0x92-0x80,0x82-0x80,0xf8-0x80,0x80-0x80}; //简单延时 void delay(unsigned int t) { while(t--); }//锁存器初始化 void Init_74HC138(unsigned char channel) { 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 SMG_All(unsigned char dat) { P0 = 0xff; Init_74HC138(6); P0 = dat; Init_74HC138(7); } //数码管安位操作 void SMG_DisplayBit(unsigned char pos,unsigned char dat) { P0 = (0x01 << pos); Init_74HC138(6); P0 = dat; Init_74HC138(7); }//数码管数据显示 void SMG_Display_V(void) { SMG_DisplayBit(0,0xbf); delay(200); SMG_DisplayBit(1,smg_data[grade]); delay(200); SMG_DisplayBit(2,0xbf); delay(200); SMG_DisplayBit(3,0xff); delay(200); SMG_DisplayBit(4,0xff); delay(200); SMG_DisplayBit(5,smg_data_dot[volt/100]); delay(200); SMG_DisplayBit(6,smg_data[volt/10%10]); delay(200); SMG_DisplayBit(7,smg_data[volt%10]); delay(200); SMG_All(0xff); delay(200); }//读取光敏通道电压 void Read_ADC(void) { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x01); IIC_WaitAck(); IIC_Stop(); SMG_Display_V(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); adc_volt = IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); //数据处理 volt = adc_volt * (5.0/255)*100; SMG_Display_V(); }//灯光控制 void Led_Working(void) { Read_ADC(); //等级0 全部灯熄灭 if(volt <= 500 && volt > 450) { grade = 0; led_stat = 0xff; } //等级1 L1 点亮 else if(volt <= 450 && volt > 400) { grade = 1; led_stat = 0xfe; } //等级2L1-L2 亮 else if(volt <= 400 && volt > 350) { grade = 2; led_stat = 0xfc; } //等级3 L1-L3 亮 else if(volt <= 350 && volt > 300) { grade = 3; led_stat = 0xf8; }//等级4 L1-L4 点亮 else if(volt <= 300 && volt > 250) { grade = 4; led_stat = 0xf0; } //等级5L1- L5 亮 else if(volt <= 250 && volt > 200) { grade = 5; led_stat = 0xe0; } //等级6 L1-L6 亮 else if(volt <= 200 && volt > 150) { grade = 6; led_stat = 0xc0; } //等级7 L1-L7 亮 else if(volt <= 150 && volt > 100) { grade = 7; led_stat = 0x80; } //等级8 L1-L8 亮 else if(volt <= 50 && volt > 0) { grade = 8; led_stat = 0x00; ; } P0 = led_stat; Init_74HC138(4); }//AT24C02处理 void Write_AT24C02(unsigned char addr,unsigned char dat) { IIC_Start(); IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); }//读AT24C02 unsigned int Read_AT24C02(unsigned char addr) { IIC_Start(); IIC_SendByte(0xa0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck(); IIC_Start(); IIC_SendByte(0xa1); IIC_WaitAck(); temp = IIC_RecByte(); IIC_SendAck(1); IIC_Stop(); return temp; }//保存当前数据 void Save_To_24C02(void) { Write_AT24C02(0x01,grade); SMG_Display_V(); Write_AT24C02(0x02,volt); SMG_Display_V(); }//读取历史数据 void Read_From_24c02(void) { old_grade = Read_AT24C02(0x01); delay(500); old_volt = Read_AT24C02(0x02); delay(500); grade = old_grade; volt= old_volt; } //按键处理 void Key_Scans(void) { if(S4 == 0) { delay(20); if(S4 == 0) { Save_To_24C02(); while(S4 == 0) { Led_Working(); }} } if(S5 == 0) { delay(20); if(S5 == 0) { //关闭所有灯光 led_stat = 0xff; P0 = led_stat; Init_74HC138(4); //读取数据 Read_From_24c02(); while(S5 == 0) { SMG_Display_V(); }} } } //系统初始化 void Init_System(void) { P0 = 0xff; Init_74HC138(4); P0 = 0x00; Init_74HC138(5); SMG_All(0xff); }//主函数 void main(void) { Init_System(); while(1) { Led_Working(); Key_Scans(); }}

蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】
文章图片

兄 弟 们 冲 鸭

    推荐阅读