蓝桥杯单片机第13届省赛程序题参考答案
生活随笔
收集整理的這篇文章主要介紹了
蓝桥杯单片机第13届省赛程序题参考答案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????????答案僅供參考,有錯誤歡迎指正,非常感謝!!!
????????文件較多,代碼較長,感謝查看!!!
目錄
main.h
main.c
Init.h
Init.c
SMG.h
SMG.c
DSQ.h
DSQ.c
YanShi.h
YanShi.c
JZKey.h
JZKey.c
ds1302.h
ds1302.c
onewire.h
onewire.c
main.h
#ifndef __MAIN_H_ #define __MAIN_H_#define uchar unsigned char #define uint unsigned int#include <STC15F2K60S2.H>#include "Init.h" #include "SMG.h" #include "DSQ.h" #include "YanShi.h" #include "JZKey.h" #include "ds1302.h" #include "onewire.h"unsigned char JZKey_GetKeynum(); void JieMian_Show(); void LED_Show();#endifmain.c
#include "main.h"uchar flag0=0; uchar jie_mian=0; uchar Key_num=0; uchar mo_shi=0; uchar shi_jian_show_flag=0; uchar can_shu=23; float wen_du; uint L1_time=0; uchar L1_time_flag=0; uchar JDQ_flag=0; uint L3_time=0; uchar flag=2; uint wen_du_temp=0; uchar flag1=1;unsigned char JZKey_GetKeynum(){unsigned char key_num=0;if(P30==0){P30=0;P34=1;P35=1;P42=1;P44=1;if(P34==0){Delay10ms();if(P34==0){key_num=19;}while(P34==0);}else if(P35==0){Delay10ms();if(P35==0){key_num=15;}while(P35==0);}else if(P42==0){Delay10ms();if(P42==0){key_num=11;}while(P42==0);}else if(P44==0){Delay10ms();if(P44==0){key_num=7;}while(P44==0);}}else if(P31==0){P31=0;P34=1;P35=1;P42=1;P44=1;if(P34==0){Delay10ms();if(P34==0){key_num=18;}while(P34==0);}else if(P35==0){Delay10ms();if(P35==0){key_num=14;}while(P35==0);}else if(P42==0){Delay10ms();if(P42==0){key_num=10;}while(P42==0);}else if(P44==0){Delay10ms();if(P44==0){key_num=6;}while(P44==0);}}else if(P32==0){P32=0;P34=1;P35=1;P42=1;P44=1;if(P34==0){ Delay10ms();if(P34==0){key_num=17;}if(jie_mian==2||jie_mian==0){while(P34==0);}}else if(P35==0){Delay10ms();if(P35==0){key_num=13;} while(P35==0);} else if(P42==0){Delay10ms();if(P42==0){key_num=9;}while(P42==0); }else if(P44==0){Delay10ms();if(P44==0){key_num=5;}while(P44==0);} }else if(P33==0){P33=0;P34=1;P35=1;P42=1;P44=1;if(P34==0){Delay10ms();if(P34==0){key_num=16;}while(P34==0);}else if(P35==0){Delay10ms();if(P35==0){key_num=12;}while(P35==0);}else if(P42==0){Delay10ms();if(P42==0){key_num=8;}while(P42==0);}else if(P44==0){Delay10ms();if(P44==0){key_num=4;}while(P44==0);}}JZKey_Init();return key_num; }void main(){Init_BZ();SMG_Init();DSQ_0_Init();JZKey_Init();DS1302_XieDate(23,59,30);while(1){EA=0;DS1302_GetDate();EA=1;Key_num=JZKey_GetKeynum();if(Key_num==12){jie_mian++;jie_mian=jie_mian%3;shi_jian_show_flag=0;}else if(Key_num==13){mo_shi++;mo_shi=mo_shi%2;}else if(Key_num==16&&jie_mian==2){can_shu++;if(can_shu>=99){can_shu=99;}}else if(Key_num==17&&jie_mian==2){//參數設置界面can_shu--;if(can_shu<=10){can_shu=10;}}else if(Key_num==17&&jie_mian==1){//時間顯示界面shi_jian_show_flag=1;}else if(Key_num==0){shi_jian_show_flag=0;}if(flag==2){wen_du=DS18B20_GetWenDu();wen_du_temp=wen_du*10;flag=0;}} }void DSQ_0_ZD() interrupt 1{flag0++;flag++;//L1時間技計數if(fen==0&&miao==0){L1_time_flag=1;if(mo_shi==1){//繼電器受時間控制JDQ_flag=1;P2=(P2&0x1f)|0x00;P0=0x10;P2=(P2&0x1f)|0xa0;P2=(P2&0x1f)|0x00;}}else if(L1_time==0){JDQ_flag=0;P2=(P2&0x1f)|0x00;P0=0x00;P2=(P2&0x1f)|0xa0;P2=(P2&0x1f)|0x00;}if(L1_time_flag){L1_time++;if(L1_time>=5000){L1_time=0;L1_time_flag=0;if(mo_shi==1){//繼電器受時間控制JDQ_flag=0;P2=(P2&0x1f)|0x00;P0=0x00;P2=(P2&0x1f)|0xa0;P2=(P2&0x1f)|0x00;}}}//繼電器標志位if(mo_shi==0){//溫度控制模式if(wen_du>can_shu){//繼電器開JDQ_flag=1;P2=(P2&0x1f)|0x00;P0=0x10;P2=(P2&0x1f)|0xa0;P2=(P2&0x1f)|0x00;}else{//繼電器關JDQ_flag=0;P2=(P2&0x1f)|0x00;P0=0x00;P2=(P2&0x1f)|0xa0;P2=(P2&0x1f)|0x00;}}//L3時間計數if(JDQ_flag){//繼電器開啟,L3開始閃爍L3_time++;L3_time=L3_time%200;}else{L3_time=0;}if(flag0>=1){flag0=0;P2=(P2&0x1f)|0x00;JieMian_Show();}LED_Show(); }void JieMian_Show(){switch(jie_mian){case 0:{//溫度顯示界面if(wen_du_temp>=100){SMG_Show(25,1,16,16,16,wen_du_temp/100,((wen_du_temp/10)%10)+32,wen_du_temp%10);}else if(wen_du_temp>=0){SMG_Show(25,1,16,16,16,16,(wen_du_temp/10)+32,wen_du_temp%10);}break;}case 1:{//時間顯示界面if(shi_jian_show_flag){//顯示分秒SMG_Show(25,2,16,fen/10,fen%10,17,miao/10,miao%10);}else{//顯示時分SMG_Show(25,2,16,shi/10,shi%10,17,fen/10,fen%10);}break;}case 2:{//參數設置界面SMG_Show(25,3,16,16,16,16,can_shu/10,can_shu%10);break;}} }void LED_Show(){uchar L1=0xff;uchar L2=0xff;uchar L3=0xff;if(L1_time>0){//L1時間大于0,時間計數啟動,L1點亮L1=0xfe;}else{L1=0xff;}if(mo_shi==0){//溫度控制模式,L2點亮L2=0xfd;}else{L2=0xff;}if((L3_time>=0&&L3_time<100)&&JDQ_flag==1){//繼電器開啟,L3閃爍L3=0xfb;}else{L3=0xff;}P0=L1&L2&L3;P2=(P2&0x1f)|0x80;P2=(P2&0x1f)|0x00; }Init.h
#ifndef __INIT_H_ #define __INIT_H_#include <STC15F2K60S2.H>void Init_BZ();#endifInit.c
#include "Init.h"void Init_BZ(){P2=(P2&0x1f)|0xa0;P0=0x00;P2=(P2&0x1f)|0x80;P0=0xff; }SMG.h
#ifndef __SMG_H_ #define __SMG_H_#include <STC15F2K60S2.H>void SMG_Init(); void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);#endifSMG.c
#include "SMG.h"unsigned char code SMG_ZK[]={ //?????? // 0 1 2 3 4 5 6 7 8 9 A B C D E F0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71, //black - H J K L N o P U t G Q r M y0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1void SMG_Init(){P2=(P2&0x1f)|0xc0;P0=0xff;P2=(P2&0x1f)|0xe0;P0=0xff; }void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){static unsigned char i=0;i++;i=i%8;P2=(P2&0x1f)|0xc0;switch(i){case 1:{P0=0x01;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n1];break;}case 2:{P0=0x02;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n2];break;}case 3:{P0=0x04;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n3];break;}case 4:{P0=0x08;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n4];break;}case 5:{P0=0x10;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n5];break;}case 6:{P0=0x20;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n6];break;}case 7:{P0=0x40;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n7];break;}case 0:{P0=0x80;P2=(P2&0x1f)|0xe0;P0=~SMG_ZK[n8];break;}}P2=(P2&0x1f)|0x00; }DSQ.h
#ifndef __DSQ_H_ #define __DSQ_H_#include <STC15F2K60S2.H>void DSQ_0_Init();#endifDSQ.c
#include "DSQ.h"void DSQ_0_Init(){AUXR |= 0x80; //????????T???TMOD &= 0xF0; //????????a??TL0 = 0x20; //????????€?TH0 = 0xD1; //????????€?TF0 = 0; //???TF0???TR0 = 1; //??????€?????EA=1;ET0=1; }YanShi.h
#ifndef __YANSHI_H_ #define __YANSHI_H_#include <STC15F2K60S2.h> #include "intrins.h"void Delay10ms();#endifYanShi.c
#include "YanShi.h"void Delay10ms() //@12.000MHz {unsigned char i, j;i = 117;j = 184;do{while (--j);} while (--i); }JZKey.h
#ifndef __JZKEY_H_ #define __JZKEY_H_#include "YanShi.h"void JZKey_Init();#endifJZKey.c
#include "JZKey.h"void JZKey_Init(){P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0; }ds1302.h
#ifndef __DS1302_H #define __DS1302_H#include <STC15F2K60S2.H> #include <intrins.h>extern unsigned char shi,fen,miao;sbit SCK = P1^7; sbit SDA = P2^3; sbit RST = P1^3; void Write_Ds1302(unsigned char temp); void Write_Ds1302_Byte( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302_Byte( unsigned char address );void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s); void DS1302_GetDate();#endifds1302.c
#include "ds1302.h" unsigned char shi,fen,miao;//寫字節 void Write_Ds1302(unsigned char temp) {unsigned char i;for (i=0;i<8;i++) { SCK = 0;SDA = temp&0x01;temp>>=1; SCK=1;} } //向DS1302寄存器寫入數據 void Write_Ds1302_Byte( unsigned char address,unsigned char dat ) {RST=0; _nop_();SCK=0; _nop_();RST=1; _nop_(); Write_Ds1302(address); Write_Ds1302(dat); RST=0; }//從DS1302寄存器讀出數據 unsigned char Read_Ds1302_Byte ( unsigned char address ) {unsigned char i,temp=0x00;RST=0; _nop_();SCK=0; _nop_();RST=1; _nop_();Write_Ds1302(address);for (i=0;i<8;i++) { SCK=0;temp>>=1; if(SDA)temp|=0x80; SCK=1;} RST=0; _nop_();SCK=0; _nop_();SCK=1; _nop_();SDA=0; _nop_();SDA=1; _nop_();return (temp); }void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s){h=(h/10)*16+(h%10);m=(m/10)*16+(m%10);s=(s/10)*16+(s%10);Write_Ds1302_Byte(0x8e,0x00);Write_Ds1302_Byte(0x84,h);Write_Ds1302_Byte(0x82,m);Write_Ds1302_Byte(0x80,s);Write_Ds1302_Byte(0x8e,0x80); }void DS1302_GetDate(){shi=Read_Ds1302_Byte(0x85);fen=Read_Ds1302_Byte(0x83);miao=Read_Ds1302_Byte(0x81);shi=(shi/16)*10+(shi%16);fen=(fen/16)*10+(fen%16);miao=(miao/16)*10+(miao%16); }onewire.h
#ifndef __ONEWIRE_H #define __ONEWIRE_H#include <STC15F2K60S2.H>sbit DQ = P1^4; unsigned char rd_temperature(void); float DS18B20_GetWenDu();#endifonewire.c
#include "onewire.h"//單總線內部延時函數 void Delay_OneWire(unsigned int t) {unsigned char i;while(t--){for(i=0;i<12;i++);} }//單總線寫操作 void Write_DS18B20(unsigned char dat) {unsigned char i;for(i=0;i<8;i++){DQ = 0;DQ = dat&0x01;Delay_OneWire(5);DQ = 1;dat >>= 1;}Delay_OneWire(5); }//單總線讀操作 unsigned char Read_DS18B20(void) {unsigned char i;unsigned char dat;for(i=0;i<8;i++){DQ = 0;dat >>= 1;DQ = 1;if(DQ){dat |= 0x80;} Delay_OneWire(5);}return dat; }//DS18B20初始化 bit init_ds18b20(void) {bit initflag = 0;DQ = 1;Delay_OneWire(12);DQ = 0;Delay_OneWire(80);DQ = 1;Delay_OneWire(10); initflag = DQ; Delay_OneWire(5);return initflag; }float DS18B20_GetWenDu(){float wen_du,xiao_shu,zheng_shu;unsigned char di,gao;init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0x44);Delay_OneWire(200);init_ds18b20();EA=0;Write_DS18B20(0xcc);Write_DS18B20(0xbe);EA=1;EA=0;di=Read_DS18B20();gao=Read_DS18B20();EA=1;if(gao>=248){di=~di;gao=~gao;if(di==0xff){di=0;gao++;}else{di++;}zheng_shu=((gao<<4)|(di>>4))*1.0;xiao_shu=(di&0x0f)*0.0625;wen_du=-(zheng_shu+xiao_shu);}else{zheng_shu=((gao<<4)|(di>>4))*1.0;xiao_shu=(di&0x0f)*0.0625;wen_du=(zheng_shu+xiao_shu);}return wen_du; }總結
以上是生活随笔為你收集整理的蓝桥杯单片机第13届省赛程序题参考答案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜狗浏览器屏蔽广告插件_“云法庭”里“云
- 下一篇: Java安全之BCEL ClassLoa