赞
踩
声明:1、我发布程序旨在希望大家蓝桥杯单片机比赛可以取得好成绩
2、在看我程序之前希望大家先去B站观看完小蜜蜂老师的蓝桥杯单片机视频讲解
小蜜蜂视频链接:【小蜜蜂笔记】蓝桥杯大赛-单片机设计与开发基础技能与进阶强化教程_哔哩哔哩_bilibili
本程序资源包采用第十三届蓝桥杯资源包
本博客包含第八届到第十三届蓝桥杯单片机真题所有程序
main.c
- #include <reg52.h>
- #include <ds1302.h>
- #include <onewire.h>
- sfr P4 = 0xC0;
-
- sbit L1 = P0^0;
- sbit L2 = P0^1;
- sbit L3 = P0^2;
-
- sbit R1 = P3^0;
- sbit R2 = P3^1;
- sbit R3 = P3^2;
- sbit R4 = P3^3;
-
- sbit C1 = P3^4;
- sbit C2 = P3^5;
- sbit C3 = P4^2;
- sbit C4 = P4^4;
-
- sbit Relay = P0^4;
- sbit Buzzer = P0^6;
-
- //变量区
- unsigned int count;//继电器5s
- unsigned char count1;//0.1s
- unsigned int count2;//LED5s
- unsigned char state_5 = 0;//0.1s标志位
- unsigned char state_4 = 0;//LED5s标志位
- unsigned char state_3 = 0;//继电器标志位
- unsigned char state_2 = 1;//时间控制
- unsigned char mode = 1;//模式控制
- unsigned char state_1 = 1;//界面控制
- unsigned char number = 23;//参数
- unsigned int temp;//温度
- unsigned char Write_DS1302_Addr[3] = {0x80,0x82,0x84};
- unsigned char Read_DS1302_Addr[3] = {0x81,0x83,0x85};
- unsigned char Timer[3] = {0x50,0x59,0x22};//22:59:50
- unsigned char SMG_Val[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- //锁存器的选择
- void SelectHC573(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;
- }
- }
- //数码管的选择(无点)
- void Select_SMG(unsigned char pos,val)
- {
- SelectHC573(7);
- P0 = 0xff;
- SelectHC573(6);
- P0 = 0x01 << pos;
- SelectHC573(7);
- P0 = val;
- }
- //数码管的选择(有点)
- void Select_DotSMG(unsigned char pos,val)
- {
- SelectHC573(7);
- P0 = 0xff;
- SelectHC573(6);
- P0 = 0x01 << pos;
- SelectHC573(7);
- P0 = val & 0x7f;
- }
- //关闭所有
- void CloseAll()
- {
- SelectHC573(6);
- P0 = 0xff;
- SelectHC573(7);
- P0 = 0xff;
- }
- //DS1302初始化
- void Init_DS1302()
- {
- unsigned char i;
- Write_Ds1302_Byte(0x8e,0x00);
- for(i = 0;i < 3;i ++)
- {
- Write_Ds1302_Byte(Write_DS1302_Addr[i],Timer[i]);
- }
- Write_Ds1302_Byte(0x8e,0x80);
- }
- //读取DS1302时间
- void Read_DS1302()
- {
- unsigned char i;
- for(i = 0;i < 3;i ++)
- {
- Timer[i] = Read_Ds1302_Byte(Read_DS1302_Addr[i]);
- }
- }
- //定时器0初始化
- void Init_Timer0()
- {
- TMOD = 0x01;
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
-
- EA = 1;
- ET0 = 1;
- // TR0 = 1;
- }
- //定时器0服务函数
- void Service_T0() interrupt 1
- {
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
-
- count ++;
- count2 ++;
- if(count == 100)
- {
- state_3 = 0;
- count = 0;
- }
- if(count2 == 100)
- {
- state_4 = 0;
- count2 = 0;
- }
- }
- //定时器1初始化
- void Init_Timer1()
- {
- TMOD = 0x01;
- TH1 = (65536 - 50000) / 256;
- TL1 = (65536 - 50000) % 256;
-
- EA = 1;
- ET1 = 1;
- // TR1 = 1;
- }
- //定时器1服务函数
- void Service_T1() interrupt 3
- {
- TH1 = (65536 - 50000) / 256;
- TL1 = (65536 - 50000) % 256;
-
- count1 ++;
- if(count1 == 2)
- {
- state_5 = !state_5;
- count1 = 0;
- }
- }
- //延迟函数
- void Delay(unsigned int t)
- {
- while(t --);
- }
- //DS1302界面
- void Display_DS1302()
- {
- Select_SMG(0,0xc1);
- Delay(100);
- Select_SMG(1,SMG_Val[2]);
- Delay(100);
-
- if(state_2 == 1)
- {
- Select_SMG(3,SMG_Val[Timer[2] / 16]);
- Delay(100);
- Select_SMG(4,SMG_Val[Timer[2] % 16]);
- Delay(100);
- Select_SMG(5,0xbf);
- Delay(100);
- Select_SMG(6,SMG_Val[Timer[1] / 16]);
- Delay(100);
- Select_SMG(7,SMG_Val[Timer[1] % 16]);
- Delay(100);
- }
- else if(state_2 == 2)
- {
- Select_SMG(3,SMG_Val[Timer[1] / 16]);
- Delay(100);
- Select_SMG(4,SMG_Val[Timer[1] % 16]);
- Delay(100);
- Select_SMG(5,0xbf);
- Delay(100);
- Select_SMG(6,SMG_Val[Timer[0] / 16]);
- Delay(100);
- Select_SMG(7,SMG_Val[Timer[0] % 16]);
- Delay(100);
- }
- CloseAll();
- }
- //DS18B20界面//123
- void Display_DS18B20()
- {
- temp = DS18B20_Temp();
- Select_SMG(7,SMG_Val[temp % 10]);
- Delay(500);
- Select_DotSMG(6,SMG_Val[temp % 100 / 10]);
- Delay(500);
- Select_SMG(5,SMG_Val[temp / 100]);
- Delay(500);
-
- Select_SMG(1,SMG_Val[1]);
- Delay(500);
- Select_SMG(0,0xc1);
- Delay(500);
- CloseAll();
- }
- //参数界面
- void Display_CS()
- {
- Select_SMG(7,SMG_Val[number % 10]);
- Delay(100);
- Select_SMG(6,SMG_Val[number / 10]);
- Delay(100);
-
- Select_SMG(1,SMG_Val[3]);
- Delay(100);
- Select_SMG(0,0xc1);
- Delay(100);
- CloseAll();
- }
- //所有界面
- void Display()
- {
- switch(state_1)
- {
- case 1 : Display_DS18B20(); break;
- case 2 : Display_DS1302(); break;
- case 3 : Display_CS(); break;
- }
- }
- void Control_mode();
- void LED_Control();
- //按键控制
- void KEY_KBO()
- {
- R4 = 0;R1 = R2 = R3 = 1; //S12
- C1 = C2 = C3 = C4 = 1;
- if(C2 == 0)
- {
- Delay(200);
- if(C2 == 0)
- {
- if(state_1 == 1)
- {
- state_1 = 2;
- }
- else if(state_1 == 2)
- {
- state_1 = 3;
- }
- else if(state_1 == 3)
- {
- state_1 = 1;
- }
- }
- while(C2 == 0)
- {
- Display();
- LED_Control();
- }
- }
- R3 = 0;R1 = R2 = R4 = 1; //S13
- C1 = C2 = C3 = C4 = 1;
- if(C2 == 0)
- {
- Delay(200);
- if(C2 == 0)
- {
- if(mode == 1)
- {
- mode = 2;
- }
- else if(mode ==2)
- {
- mode = 1;
- }
- }
- while(C2 == 0)
- {
- Display();
- LED_Control();
- }
- }
- R4 = 0;R1 = R2 = R3 = 1; //S16
- C1 = C2 = C3 = C4 = 1;
- if(C1 == 0)
- {
- Delay(200);
- if(C1 == 0)
- {
- if(state_1 == 3)
- {
- number ++;
- if(number > 99)
- {
- number = 10;
- }
- }
- }
- while(C1 == 0)
- {
- Display();
- LED_Control();
- }
- }
- R3 = 0;R1 = R2 = R4 = 1; //S17
- C1 = C2 = C3 = C4 = 1;
- if(C1 == 0)
- {
- Delay(200);
- if(C1 == 0)
- {
- if(state_1 == 2)
- {
- while(C1 == 0)
- {
- state_2 = 2;
- Display();
- // Control_mode();
- LED_Control();
- Read_DS1302();
- }
- state_2 = 1;
- }
- else if(state_1 == 3)
- {
- number --;
- if(number < 10)
- {
- number = 99;
- }
- }
- while(C1 == 0)
- {
- Display();
- LED_Control();
- }
- }
- }
- }
- //控制模块
- void Control_mode()
- {
- if(mode == 1) //温度
- {
- if(temp > number * 10)
- {
- SelectHC573(5);
- Relay = 1;
- Buzzer = 0;
- state_3 = 1;
- }
- else
- {
- SelectHC573(5);
- Relay = 0;
- Buzzer = 0;
- state_3 = 0;
- }
- }
- if(mode == 2) //时间
- {
- if(Timer[1] == 0x00 && Timer[0] == 0x00)
- {
- SelectHC573(5);
- Relay = 1;
- Buzzer = 0;
- state_3 = 1;
- TR0 = 1;
- }
- else if(state_3 == 0)
- {
- SelectHC573(5);
- Relay = 0;
- Buzzer = 0;
- TR0 = 0;
- }
- }
- }
- //LED
- void LED_Control()
- {
- SelectHC573(4);
- if(Timer[1] == 0x00 && Timer[0] == 0x00)
- {
- state_4 = 1;
- TR0 = 1;
- }
- else if(state_4 == 0)
- {
- TR0 = 0;
- }
- if(state_4 == 1)
- {
- L1 = 0;
- }
- else if(state_4 == 0)
- {
- L1 = 1;
- }
- if(mode == 1)
- {
- L2 = 0;
- }
- else if(mode == 2)
- {
- L2 = 1;
- }
- if(state_3 == 1)
- {
- TR1 = 1;
- if(state_5 == 1)
- {
- L3 = 0;
- }
- else if(state_5 == 0)
- {
- L3 = 1;
- }
- }
- else if(state_3 == 0)
- {
- TR1 = 0;
- }
- }
- //系统初始化
- void InitSystem()
- {
- SelectHC573(5);
- P0 = 0x00;
- SelectHC573(4);
- P0 = 0xff;
- }
- //主函数
- void main()
- {
- Init_Timer1();
- Init_Timer0();
- Init_DS1302();
- InitSystem();
- while(1)
- {
- LED_Control();
- Control_mode();
- Read_DS1302();
- Display();
- KEY_KBO();
- }
- }
DS1302.c
- #include "ds1302.h"
-
- //写字节
- 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);
- }
OneWire.c
- #include "onewire.h"
-
- //单总线内部延时函数
- void Delay_OneWire(unsigned int t)
- {
- while(t--);
- }
-
- //单总线写操作
- void Write_DS18B20(unsigned char dat)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- DQ = 0;
- DQ = dat&0x01;
- Delay_OneWire(50);
- DQ = 1;
- dat >>= 1;
- }
- Delay_OneWire(50);
- }
-
- //单总线读操作
- 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(50);
- }
- return dat;
- }
-
- //DS18B20初始化
- bit init_ds18b20(void)
- {
- bit initflag = 0;
-
- DQ = 1;
- Delay_OneWire(120);
- DQ = 0;
- Delay_OneWire(800);
- DQ = 1;
- Delay_OneWire(100);
- initflag = DQ;
- Delay_OneWire(50);
-
- return initflag;
- }
-
- unsigned int DS18B20_Temp()
- {
- unsigned char LSB,MSB;
- unsigned int temp;
-
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0x44);
-
- Delay_OneWire(50);
-
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0xbe);
- LSB = Read_DS18B20();
- MSB = Read_DS18B20();
-
- temp = MSB << 8;
- temp = temp | LSB;
- temp = temp * 0.625;
- return temp;
- }
DS1302.h
- #ifndef __DS1302_H
- #define __DS1302_H
-
- #include <reg52.h>
- #include <intrins.h>
-
- 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 );
-
- #endif
OneWire.h
- #ifndef __ONEWIRE_H
- #define __ONEWIRE_H
-
- #include "reg52.h"
-
- sbit DQ = P1^4;
-
- unsigned int DS18B20_Temp();
-
- #endif
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。