一、题目
历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这里就不再赘述了。
二、hex文件
读者下载这个文件然后用烧录软件直接烧入单片机就可以用了!
链接:https://pan.baidu.com/s/1-4Ywh6EojT-65uO8GX9sIQ
提取码:tono
三、主函数实现
提示:比赛过程中,仅仅主函数修改可能不够,有的时候需要注意,比赛官方给的各个驱动的代码是否写完整了,比如有时候,它的.h文件中就没有把这些写全,故意注释掉,你需要去对应的.c文件里找都需要一些什么函数,一个个都补全了才行。
另外,我的代码都是完全在一个文件中写完的,所以各位读者大大用起来就比较方便,可以直接拷贝我的.c文件也可以把内容复制粘贴走,放到你想要的地方去。
上代码:
# include "reg52.h"
# include "onewire.h"
# include "ds1302.h"
typedef unsigned char uchar;
typedef unsigned int uint;
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
sbit L1 = P0^0;
uchar duanma[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
uchar interval = 1; //温度采样间隔时间
uchar index = 0; //采样的温度的索引值
uchar temps[10] = {0}; //存放采样到的所有温度
uchar Write_Time[3] = {0x80,0x82,0x84}; //时钟写入的地址
uchar Read_Time[3] = {0x81,0x83,0x85}; //时钟读取的地址
uchar Timer[3] = {0x50,0x59,0x23}; //用来存储的初始化的时间
uchar smg_stat = 0; //标志此时数码管当前的显示状态,0采集前的设置,1采集中时间显示,2采集后的数据显示
uchar count = 0; //定时器的计数变量
bit smg_f = 1; //控制数码管提示符闪烁的标志
bit temp_f = 1; //控制温度读取的开关变量
uint temp_c = 0; //定时器中控制温度取样间隔的计数变量
uchar temp_num; //温度采样个数
uchar led = 0; //控制led显示的开关变量
uchar k6 = 0; //控制读取后将每一个温度依次显示
//===================锁存器选择函数======================
void SelectHC573 (uchar 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 Delay (uint t)
{
while (t--);
}
//=======================================================
//====================初始化函数=========================
void InitSystem ()
{
SelectHC573 (4);
P0 = 0xff;
SelectHC573 (5);
P0 = 0x00;
SelectHC573 (0);
}
//=======================================================
//=================DS18B20温度读取函数=================== //有返回值
uchar Read_Temp ()
{
uchar LSB;
uchar MSB;
uint temp;
uchar recv;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay (1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = (MSB << 8) | LSB;
if ((temp & 0xf8000) == 0x0000)
{
recv = temp >> 4;
}
return recv;
}
//=======================================================
//================十组温度数据的采集=====================
void TempCollection ()
{
if (smg_stat == 1)
{
if (temp_num <= 9)
{
if (temp_f == 1)
{
temps[index] = Read_Temp ();
temp_f = 0;
index++;
temp_num++;
}
}
else if (temp_num >= 10)
{
led = 1;
}
}
}
//=======================================================
//=================DS1302时钟控制函数====================
void Write_1302 ()
{
uchar i;
Write_Ds1302_Byte(0x8e,0x00);
for (i = 0;i <= 2; i++)
{
Write_Ds1302_Byte(Write_Time[i],Timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void Read_1302 ()
{
uchar i;
for (i = 0;i <= 2; i++)
{
Timer[i] = Read_Ds1302_Byte(Read_Time[i]);
}
}
//=======================================================
//======================定时器 10ms======================
void InitTimer0 ()
{
TMOD = 0x01;
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void ServiceTimer0 () interrupt 1
{
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
count++;
if (count >= 100)
{
count = 0;
smg_f = ~smg_f;
}
if (temp_f == 0)
{
temp_c++;
}
if (temp_c >= interval * 100)
{
temp_c = 0;
temp_f = 1;
}
}
//=======================================================
//===================数码管相关函数======================
void Delay_SMG (uint t)
{
while (t--);
}
void ShowSMG_Bit (uchar pos,uchar dat)
{
SelectHC573 (7);
P0 = 0xff;
SelectHC573 (6);
P0 = 0x01 << pos - 1;
SelectHC573 (7);
P0 = dat;
SelectHC573 (0);
}
void AllSMG (uchar dat)
{
SelectHC573 (6);
P0 = 0xff;
SelectHC573 (7);
P0 = dat;
SelectHC573 (0);
}
void ShowSMG ()
{
if (smg_stat == 0)
{
ShowSMG_Bit(6,duanma[16]);
Delay_SMG(500);
ShowSMG_Bit(7,duanma[interval / 10]);
Delay_SMG(500);
ShowSMG_Bit(8,duanma[interval % 10]);
Delay_SMG(500);
}
else if (smg_stat == 1)
{
ShowSMG_Bit(1,duanma[Timer[2] / 16]);
Delay_SMG(500);
ShowSMG_Bit(2,duanma[Timer[2] % 16]);
Delay_SMG(500);
if (smg_f == 1)
{
ShowSMG_Bit(3,duanma[16]);
Delay_SMG(500);
}
ShowSMG_Bit(4,duanma[Timer[1] / 16]);
Delay_SMG(500);
ShowSMG_Bit(5,duanma[Timer[1] % 16]);
Delay_SMG(500);
if (smg_f == 1)
{
ShowSMG_Bit(6,duanma[16]);
Delay_SMG(500);
}
ShowSMG_Bit(7,duanma[Timer[0] / 16]);
Delay_SMG(500);
ShowSMG_Bit(8,duanma[Timer[0] % 16]);
Delay_SMG(500);
}
else if (smg_stat == 2)
{
ShowSMG_Bit(1,duanma[16]);
Delay_SMG(500);
ShowSMG_Bit(2,duanma[index / 10]);
Delay_SMG(500);
ShowSMG_Bit(3,duanma[index % 10]);
Delay_SMG(500);
ShowSMG_Bit(6,duanma[16]);
Delay_SMG(500);
ShowSMG_Bit(7,duanma[temps[index] / 10]);
Delay_SMG(500);
ShowSMG_Bit(8,duanma[temps[index] % 10]);
Delay_SMG(500);
}
AllSMG(0xff);
}
//=======================================================
//======================浏览按键=========================
void Delay_Key (uchar t)
{
while (t--);
}
void ScanKey ()
{
uchar i;
if (S7 == 0)
{
Delay_Key(100);
if (S7 == 0)
{
while (S7 == 0)
{
ShowSMG ();
}
if (smg_stat == 2)
{
smg_stat = 0;
}
temp_num = 0;
index = 0;
temp_f = 1;
k6 = 0;
for (i = 0;i<=9;i++)
{
temps[i] = 0;
}
}
}
if (S6 == 0)
{
Delay_Key(100);
if (S6 == 0)
{
while (S6 == 0)
{
ShowSMG ();
}
if (smg_stat == 1)
{
smg_stat = 2;
}
if (led == 1)
{
led = 0;
}
if (smg_stat == 2)
{
k6++;
if (k6 > 1);
{
index++;
}
if (index >= 10)
{
index = 0;
k6 = 1;
}
}
}
}
if (S5 == 0)
{
Delay_Key(100);
if (S5 == 0)
{
while (S5 == 0)
{
ShowSMG ();
}
if (smg_stat == 0)
{
smg_stat = 1;
} //从1到2是由S6决定的。从2回0是由S7决定
}
}
if (S4 == 0)
{
Delay_Key(100);
if (S4 == 0)
{
while (S4 == 0)
{
ShowSMG ();
}
if (smg_stat == 0)
{
if (interval == 1)
{
interval = 5;
}
else if (interval == 5)
{
interval = 30;
}
else if (interval == 30)
{
interval = 60;
}
else if (interval == 60)
{
interval = 1;
}
}
}
}
}
//=======================================================
//=====================LED显示函数=======================
void LEDRunning ()
{
SelectHC573(4);
if (led == 1)
{
if (smg_f == 1)
{
L1 = 0;
}
else if (smg_f == 0)
{
L1 = 1;
}
}
SelectHC573(0);
}
//=======================================================
//=======================主函数==========================
void main ()
{
InitSystem ();
Write_1302 ();
Read_Temp ();
InitTimer0 ();
while (1)
{
Read_1302 ();
TempCollection ();
ScanKey ();
ShowSMG ();
LEDRunning ();
}
}
//=======================================================
四、整个工程文件
链接:https://pan.baidu.com/s/1gqOXbTnccPZ7Tc5Dgx3OsA
提取码:6ue0
直接打开这项目如果失败的话,可能是因为keil使用版本问题,我用的是keil3,出现问题的话,可以直接拷贝.c文件的内容,前面我也说了,我的实现过程我在一个.c文件中实现的,方便读者大大取用!
最后
有需要的小伙伴可以随时评论或者私信我,讨论学习过程中的问题,我会尽我所能提供一些帮助的
温馨提示: 关注我不容易让文章走丢哦!
蓝桥杯比赛 单片机组 历届省赛题目解答(代码加注释)剩余参见——https://blog.csdn.net/weixin_45386875/article/details/114136549