蓝桥杯单片机第十三届省赛代码(有注释)
下面是本次试题的主函数,可以看出我是将键盘模块、数码管模块、继电器模块和led灯模块是完全分开的。这主要源于我的六个标志位来实现的,通过标志位来实现各个模块的分开编程,可以让编程思路变得简单,同时也便于增加新的功能。
对于官方提供的底层驱动代码的调用函数都是写在对应的.c文件的最后,中断服务函数在main.c中最后面
源代码
main.c
#include<reg52.h>
#include<onewire.h>
#include<ds1302.h>
#include<smg.h>
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit C3 = P3^2;
sbit C4 = P3^3;
sbit R3 = P3^5;
sbit R4 = P3^4;
unsigned int Temperature = 0; //储存温度
unsigned char time[3] = {0x22,0x59,0x50}; //储存时间
unsigned char Parameter = 30; //温度参数
unsigned char x = 0; //界面切换标志,0,1,2分别为温度、时间和参数界面
bit y; //模式切换标志,0为温控,1为时控
bit time_flag; //时控模式下继电器吸合标志,1为进行5s计时,0为计时完毕
bit L1_flag; //L1状态标志,0为点亮,1为熄灭
bit relay_flag; //继电器吸合标志,1为吸合
bit L3_flag; //L3状态标志,0为点亮
//750ms延时用于防止上电85
void Delay_ms(unsigned int t)
{
unsigned char data i, j;
while(t--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
//初始化继电器、蜂鸣器和计时器0
void InitSystem()
{
SelectHC573(5);
P0 = 0x00;
SelectHC573(4);
P0 = 0xff;
SelectHC573(0);
TMOD = 0x01;
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
//键盘延时函数,用于去抖动
void DelayKey(unsigned char t)
{
while(t--);
}
//数码管显示函数
void DisplaySMG()
{
if(x == 0)
Display_Temperature(Temperature);
else if(x == 1)
Display_Time0(time);
else
Display_parameter(Parameter);
}
//键盘扫描函数
void KeyScan()
{
C3 = 0; C4 = 1;
if(R3 == 0) //S13
{
DelayKey(20);
if(R3 == 0)
{
y = !y;
while(R3 == 0)DisplaySMG();
}
}
if(R4 == 0) //S17
{
DelayKey(20);
if(R4 == 0)
{
if(x == 2 && Parameter < 99)Parameter++;
while(R4 == 0 && x == 1)//时间界面下显示分秒
{
ReadTime(time);
Display_Time1(time);
}
while(R4 == 0)DisplaySMG();
}
}
C3 = 1; C4 = 0;
if(R3 == 0) //S12
{
DelayKey(20);
if(R3 == 0)
{
if(++x == 3)x = 0;
while(R3 == 0)DisplaySMG();
}
}
if(R4 == 0) //S16
{
DelayKey(20);
if(R4 == 0)
{
if(x == 2 && Parameter > 10)Parameter--;
while(R4 == 0)DisplaySMG();
}
}
}
//继电器状态更改函数
void Relay()
{
if(!y) //温控
{
if(Temperature > Parameter * 10)
{
SelectHC573(5);
P0 = 0x10;
SelectHC573(0);
relay_flag = 1;
}
else
{
SelectHC573(5);
P0 = 0x00;
SelectHC573(0);
relay_flag = 0;
}
}
else //时控
{
if(time[1] == 0x00 && time[2] == 0x00)
{
SelectHC573(5);
P0 = 0x10;
SelectHC573(0);
time_flag = 1;
relay_flag = 1;
}
if(!time_flag)
{
SelectHC573(5);
P0 = 0x00;
SelectHC573(0);
relay_flag = 0;
}
}
}
//单独对某个led灯状态改变
void SelectLx(unsigned char Lx,x)
{
if(x)
{
SelectHC573(4);
P0 = P0 | 0x01 << (Lx - 1);
SelectHC573(0);
}
else
{
SelectHC573(4);
P0 = P0 & ~(0x01 << (Lx - 1));
SelectHC573(0);
}
}
//led灯状态改变函数
void led()
{
SelectHC573(4);
P0 = (P0 | 0xf8); //关闭高五位led灯
SelectHC573(0);
if(time[1] == 0x00 && time[2] == 0x00) //L1
{
SelectLx(1,0);
L1_flag = 1;
}
if (!L1_flag)SelectLx(1,1);
if(!y)SelectLx(2,0); //L2
else SelectLx(2,1);
if(relay_flag) //L3
{
if(L3_flag)
SelectLx(3,0);
else
SelectLx(3,1);
}
else SelectLx(3,1);
}
void main()
{
InitSystem();
Read_temperature();
Delay_ms(750);
SetTime(time);
while(1)
{
Temperature = (unsigned int)(Read_temperature() * 10);//温度读取
ReadTime(time); //时间读取
KeyScan(); //键盘扫描
DisplaySMG(); //数码管显示
Relay(); //继电器状态更改
led(); //led灯状态更改
}
}
void Service_Timer0() interrupt 1
{
unsigned int i1,i2,i3;
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
if(time_flag)//时控下五秒计时
{
if(++i1 == 5000)
{
i1 = 0;
time_flag = 0;
}
}
if(L1_flag)//L1的五秒计时
{
if(++i2 == 5000)
{
i2 = 0;
L1_flag = 0;
}
}
if(++i3 == 100)//L3每隔0.1s状态切换
{
i3 = 0;
L3_flag = !L3_flag;
}
}
ds1302.c
#include<reg52.h>
#include<intrins.h>
sbit RST = P1^3;
sbit SCK = P1^7;
sbit SDA = P2^3;
//
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//
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;
}
//
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 SetTime(unsigned char *timer)
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x80);
for(i = 0;i < 3; i++)
Write_Ds1302_Byte(0x84 - 2 * i,timer[i]);
Write_Ds1302_Byte(0x8e,0x81);
}
void ReadTime(unsigned char *timer)
{
unsigned char i;
for(i = 0; i < 3; i++)
timer[i] = Read_Ds1302_Byte(0x85 - 2 * i);
}
ds1302.h
#ifndef _ds1302_h_
#define _ds1302_h_
void SetTime(unsigned char *timer);
void ReadTime(unsigned char *timer);
#endif
onewire.c
#include<reg52.h>
sbit DQ = P1^4;
//
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;
}
//
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 Read_temperature()
{
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low = Read_DS18B20();
high = Read_DS18B20();
return ((high << 8) | low) / 16.0;
}
onewrie.h
#ifndef _onewire_h_
#define _onewire_h_
float Read_temperature();
#endif
smg.c
#include<reg52.h>
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e //F
};
//带有小数点的段码表
code unsigned char Seg_point[] =
{
0x40, //0.
0x79, //1.
0x24, //2.
0x30, //3.
0x19, //4.
0x12, //5.
0x02, //6.
0x78, //7.
0x00, //8.
0x10, //9.
};
//HC573芯片选择
void SelectHC573(unsigned char n)
{
switch(n)
{
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;
}
}
//数码管动态显示必要延时
void Delay0(unsigned int t)
{
while(t--);
}
//关闭所有数码管,避免某个数码管过亮
void ShutDownSMG()
{
SelectHC573(7);
P0 = 0xff;
SelectHC573(6);
P0 = 0xff;
}
//数码管位选和段选
void ShowSMG_byte(unsigned char pos,value)
{
SelectHC573(7);//消隐
P0 = 0xff;
SelectHC573(6);
P0 = 0x01 << pos;
SelectHC573(7);
P0 = value;
Delay0(500);//动态显示必要延时
}
void Display_Temperature(unsigned int temperature) //温度显示
{
ShowSMG_byte(0,0xc1);
ShowSMG_byte(1,Seg_Table[1]);
ShowSMG_byte(2,0xff);
ShowSMG_byte(3,0xff);
ShowSMG_byte(4,0xff);
ShowSMG_byte(5,Seg_Table[temperature / 100]);
ShowSMG_byte(6,Seg_point[temperature / 10 %10]);
ShowSMG_byte(7,Seg_Table[temperature % 10]);
ShutDownSMG();
}
void Display_Time0(unsigned char* timer) //时间的时分显示
{
ShowSMG_byte(0,0xc1);
ShowSMG_byte(1,Seg_Table[2]);
ShowSMG_byte(2,0xff);
ShowSMG_byte(3,Seg_Table[timer[0] / 16]);
ShowSMG_byte(4,Seg_Table[timer[0] % 16]);
ShowSMG_byte(5,0xbf);
ShowSMG_byte(6,Seg_Table[timer[1] / 16]);
ShowSMG_byte(7,Seg_Table[timer[1] % 16]);
ShutDownSMG();
}
void Display_Time1(unsigned char* timer) //时间的分秒显示
{
ShowSMG_byte(0,0xc1);
ShowSMG_byte(1,Seg_Table[2]);
ShowSMG_byte(2,0xff);
ShowSMG_byte(3,Seg_Table[timer[1] / 16]);
ShowSMG_byte(4,Seg_Table[timer[1] % 16]);
ShowSMG_byte(5,0xbf);
ShowSMG_byte(6,Seg_Table[timer[2] / 16]);
ShowSMG_byte(7,Seg_Table[timer[2] % 16]);
ShutDownSMG();
}
void Display_parameter(unsigned char parameter) //温度参数显示
{
ShowSMG_byte(0,0xc1);
ShowSMG_byte(1,Seg_Table[3]);
ShowSMG_byte(2,0xff);
ShowSMG_byte(3,0xff);
ShowSMG_byte(4,0xff);
ShowSMG_byte(5,0xff);
ShowSMG_byte(6,Seg_Table[parameter / 10]);
ShowSMG_byte(7,Seg_Table[parameter % 10]);
ShutDownSMG();
}
smg.h
#ifndef _smg_h_
#define _smg_h_
void SelectHC573(unsigned char n);
void Display_Temperature(unsigned int temperature); //温度显示
void Display_Time0(unsigned char* timer); //时间的时分显示
void Display_Time1(unsigned char* timer); //时间的分秒显示
void Display_parameter(unsigned char parameter); //温度参数显示
#endif
以上就是完整的源代码了,数码管相关的底层代码都在smg.c中。
第一个标志x
界面切换标志:0,1,2分别为温度、时间和参数界面。在键盘扫描函数中进行修改,在数码管显示函数中发挥作用。
第二个标志y
模式切换标志,0为温控,1为时控。同样在键盘扫描函数中更改。对于本套试题,除了参数修改和时间界面下的长按S17,键盘扫描函数就可以说已经基本完成。完全可以凭借标志x和y将继电器模块和led灯模块独立出去。
在温控模式下:通过标志位y为0且满足温度大于温度参数对继电器进行吸合
第三个标志time_flag
时控模式下继电器吸合标志,1为进行5s计时,0为计时完毕。
在时控模式下满足整点条件就进行继电器吸合同时对time_flag进行置1
标志time_flag在定时器0的中断服务函数中生效,进行5秒计时,计时完成time_flag置0,回到继电器函数中对继电器继续断开。
第四个标志L1_flag
L1状态标志,0为点亮,1为熄灭
原理基本与第三个标志相同
第五个标志relay_flag
继电器吸合标志,1为吸合;用于L3的闪烁
在继电器函数中进行更改,作用于led函数中。
第六个标志L3_flag
L3状态标志,0为点亮;在定时器0的中断服务函数中更改,在led函数中生效