其他相关文章:
【提分必看!】蓝桥杯单片机提分技巧(国一经验分享)
【国一超全代码分享!】蓝桥杯单片机各模块代码整合
【蓝桥杯单片机客观题知识点汇总】
本代码经过编译后生成的hex文件经过了4T测试平台的满分测试。读者对代码有疑问的可以在评论区里提出。
主函数
#include "stdio.h"
#include "intrins.h"#include "bsp_init.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_seg.h"
#include "bsp_sonic.h"
#include "bsp_555.h"
#include "bsp_iic.h"#include "Timer.h"//
void Key_Proc(void);
void Seg_Proc(void);//
unsigned char Key_Slow=0;
unsigned char Seg_Slow=0;unsigned char onems_tick=0;
unsigned char freq_tick=0;
unsigned char pwm_tick=0;
unsigned int clear_tick=0;// 按键
unsigned char Key_Value;
unsigned char Key_Down;
unsigned char Key_Old;// 数码管
unsigned char seg_string[11];
unsigned char seg_buf[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char pos=0;// LED
unsigned char ucLed=0;// EEPROM
unsigned char eeprom_string[2];// 超声波和频率测量
unsigned char dist=0;
unsigned int freq=0;// 逻辑标志位
unsigned char ui_state=0;
unsigned char par_mode=0;
bit cm_mode=0;
bit hz_mode=0;
bit pwm_flag=0;
bit dist_flag=0;
bit count_flag=0;
bit clear_flag=0;bit pp1_flag=0;
bit pp2_flag=0;// 湿度
unsigned char shidu=0;// 参数
float freq_par=9.0;
unsigned char shidu_par=40;
float dist_par=0.6;unsigned char count=0;void main(void)
{Cls_Waishe();Sonic_Timer1Init();Freq_Timer0Init();Timer2Init();EA=1;while(1){Key_Proc();Seg_Proc();}
}//
void Key_Proc(void)
{if(Key_Slow) return;Key_Slow=1;Key_Value=Read_BTN();Key_Down=Key_Value&(Key_Old^Key_Value);Key_Old=Key_Value;if(Key_Down==4){ui_state++;if(ui_state>3) ui_state=0;}if(ui_state==3 && Key_Down==5){par_mode++;if(par_mode>2) par_mode=0;}if(ui_state==0 && Key_Down==7) hz_mode^=1;if(ui_state==2 && Key_Down==6) cm_mode^=1;if(ui_state==3){if(par_mode==0){if(Key_Down==6){freq_par+=0.5;if(freq_par>12.0) freq_par=1.0;}else if(Key_Down==7){freq_par-=0.5;if(freq_par<1.0) freq_par=12.0;}}else if(par_mode==1){if(Key_Down==6){shidu_par+=10;if(shidu_par>60) shidu_par=10;}else if(Key_Down==7){shidu_par-=10;if(shidu_par<10) shidu_par=60;}}else if(par_mode==2){if(Key_Down==6){dist_par+=0.1;if(dist_par>1.2) dist_par=0.1;}else if(Key_Down==7){dist_par-=0.1;if(dist_par<0.1) dist_par=1.2;}}}if(ui_state==1){if(Key_Value==7){if(clear_tick++>100){clear_flag=1;}}else{if(clear_flag==1){clear_tick=0;clear_flag=0;count=0;}else{clear_tick=0;}}}
}
//
void Seg_Proc(void)
{if(Seg_Slow) return;Seg_Slow=1;shidu=(Pcf_ADC(0x43)/51.0)*20.0;dist=Wave_Rec();if((dist/100.0)>dist_par) {if(count_flag==0){count_flag=1;count++;}dist_flag=1;ucLed |= 0x20;}else {if(count_flag==1){count_flag=0;count++;}dist_flag=0;ucLed &= 0xDF;}eeprom_string[0]=count;EEPROM_Write(eeprom_string,0,1);if(shidu<=shidu_par){Pcf_DAC(51);}else if(shidu>=80){Pcf_DAC(255);}else{Pcf_DAC(((4.0/(80.0-1.0*shidu_par))*(float)shidu+((80.0-5.0*shidu_par)/(80.0-1.0*shidu_par)))*51.0);}if(shidu>shidu_par) ucLed |= 0x10;else ucLed &= 0xEF;if(freq>(freq_par*1000)) {pwm_flag=1;ucLed |= 0x08;}else {pwm_flag=0;ucLed &= 0xF7;}switch(ui_state){case 0:{ucLed = ucLed&0xF9|0x01;par_mode=0;if(hz_mode==0){sprintf(seg_string,"F %6d",freq);}else if(hz_mode==1){sprintf(seg_string,"F %7.1f",freq/1000.0);}break;}case 1:{ucLed = ucLed&0xFA|0x02;sprintf(seg_string,"H %2d",(unsigned int)shidu);break;}case 2:{ucLed = ucLed&0xFC|0x04;if(cm_mode==0) {sprintf(seg_string,"A %3d",(unsigned int)dist);}else if(cm_mode==1){sprintf(seg_string,"A %4.2f",dist/100.0);}break;}case 3:{if(par_mode==0){ucLed = ucLed&0xF9^0x01;sprintf(seg_string,"P1 %4.1f",freq_par);}else if(par_mode==1){ucLed = ucLed&0xFA^0x02;sprintf(seg_string,"P2 %2d",(unsigned int)shidu_par);}else if(par_mode==2){ucLed = ucLed&0xFC^0x04;sprintf(seg_string,"P3 %3.1f",dist_par);}break;}default:break;}Seg_Tran(seg_string,seg_buf);
}//-----------------------------------------------
//中断服务程序
void t2int() interrupt 12 //中断入口
{if(++onems_tick==10){onems_tick=0;if(Key_Slow++==10) Key_Slow=0;if(Seg_Slow++==100) Seg_Slow=0;if(++freq_tick==100){freq_tick=0;TR0 = 0;freq=((TH0<<8)|TL0);freq=freq*10;TL0 = 0; TH0 = 0; TR0 = 1;}Seg_Disp(seg_buf,pos);if(++pos==8) pos=0;Led_Disp(ucLed);}if(dist_flag==0) P0=0x00;else if(dist_flag==1) P0=0x10;
// if(pwm_flag==1){pp2_flag=0;if(pwm_tick<8){if(pp1_flag==0){pp1_flag=1;P0|=0x20;P2=P2&0x1F|0xA0;P2=P2&0x1F;}}else{if(pp1_flag==1){pp1_flag=0;P0|=0x00;P2=P2&0x1F|0xA0;P2=P2&0x1F;}}if(++pwm_tick==10) pwm_tick=0;}else if(pwm_flag==0){pp1_flag=0;if(pwm_tick<2){if(pp2_flag==0){pp2_flag=1;P0|=0x20;P2=P2&0x1F|0xA0;P2=P2&0x1F;}}else{if(pp2_flag==1){pp2_flag=0;P0|=0x00;P2=P2&0x1F|0xA0;P2=P2&0x1F;}}if(++pwm_tick==10) pwm_tick=0;}
}
其他模块
其他模块代码直接参考:【国一超全代码分享!】蓝桥杯单片机各模块代码整合