【第十三届蓝桥杯省单片机软件部分2】-创新互联
【第十三届蓝桥杯省单片机软件部分2】
当前名称:【第十三届蓝桥杯省单片机软件部分2】-创新互联
文章转载:http://ybzwz.com/article/dhocee.html
一、题目要求
二、软件实现
#include "reg52.h"
#include "ds1302.h"
#include "onewire.h"
sfr P4 = 0xC0;
sbit H1 = P3^0;
sbit H2 = P3^1;
sbit H3 = P3^2;
sbit H4 = P3^3;
sbit L1 = P3^4;
sbit L2 = P3^5;
sbit L3 = P4^2;
sbit L4 = P4^4;
unsigned char DS1302_Write_Addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char DS1302_Read_Addr[]= {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//2022-12-23 23-25-30 周五
unsigned char Init_DS1302_Clock[] = {0x40,0x54,0x30,0x23,0x12,0x05,0x22};
float T_temp = 0.0;
unsigned int Real_T = 0,Set_T = 23;
unsigned char code smg_data[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90};
unsigned char code smg_dot[] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char channel = 1;
unsigned char interface = 1,work_mode = 1,led_status = 0xff;//界面
unsigned char s17_flag = 1,time_5s_flag = 0,L3_flag = 0,relay_flag = 0,clock_flag = 0;
//简单延时
void delay(unsigned int t)
{while(t--);
}
//74HC138初始化
void Init_74HC138(unsigned char dat)
{switch(dat)
{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;
}
P2 = (P2 & 0x1f)|0x00;
}
//数码管按位操作
void SMG_DisplayByBit(unsigned char pos,unsigned char dat)
{P0 = (0x01<< pos);
Init_74HC138(6);
P0 = dat;
Init_74HC138(7);
}
//控制所有数码管
void Controal_All_SMG(unsigned char dat)
{P0 = 0x0ff;
Init_74HC138(6);
P0 = dat;
Init_74HC138(7);
}
//温度获取
void Read_Temperature(void)
{unsigned char LSB = 0,MSB = 0;
unsigned int temp = 0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(800);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp = (MSB<< 8)| LSB;
if((temp & 0xf800) == 0x0000)
{T_temp = temp * 0.0625;
}
Real_T = T_temp * 10;
}
//数码管数据显示温度
void SMG_Display_Temperature(void)
{//U 0111 1100
SMG_DisplayByBit(0,~0x3e);
delay(200);
SMG_DisplayByBit(1,smg_data[channel]);
delay(200);
SMG_DisplayByBit(2,0xff);
delay(200);
SMG_DisplayByBit(3,0xff);
delay(200);
SMG_DisplayByBit(4,0xff);
delay(200);
SMG_DisplayByBit(5,smg_data[Real_T/100]);
delay(200);
SMG_DisplayByBit(6,smg_dot[Real_T/10%10]);
delay(200);
SMG_DisplayByBit(7,smg_data[Real_T%10]);
delay(200);
Controal_All_SMG(0xff);
delay(200);
}
//数码管数据显示设置温度
void SMG_Display_Set_Temperature(void)
{//U 0111 1100
SMG_DisplayByBit(0,~0x3e);
delay(200);
SMG_DisplayByBit(1,smg_data[channel]);
delay(200);
SMG_DisplayByBit(2,0xff);
delay(200);
SMG_DisplayByBit(3,0xff);
delay(200);
SMG_DisplayByBit(4,0xff);
delay(200);
SMG_DisplayByBit(5,0xff);
delay(200);
SMG_DisplayByBit(6,smg_data[Set_T/10]);
delay(200);
SMG_DisplayByBit(7,smg_data[Set_T%10]);
delay(200);
Controal_All_SMG(0xff);
delay(200);
}
//数码管数据时间
void SMG_Display_Time(void)
{//U 0111 1100
SMG_DisplayByBit(0,~0x3e);
delay(200);
SMG_DisplayByBit(1,smg_data[channel]);
delay(200);
SMG_DisplayByBit(2,0xff);
delay(200);
SMG_DisplayByBit(3,smg_data[Init_DS1302_Clock[1]/16]); //分
delay(200);
SMG_DisplayByBit(4,smg_data[Init_DS1302_Clock[1]%16]);
delay(200);
SMG_DisplayByBit(5,0xbf);
delay(200);
SMG_DisplayByBit(6,smg_data[Init_DS1302_Clock[0]/16]);//秒
delay(200);
SMG_DisplayByBit(7,smg_data[Init_DS1302_Clock[0]%16]);
delay(200);
Controal_All_SMG(0xff);
delay(200);
}
void SMG_Display_Time2(void)
{//U 0111 1100
SMG_DisplayByBit(0,~0x3e);
delay(200);
SMG_DisplayByBit(1,smg_data[channel]);
delay(200);
SMG_DisplayByBit(2,0xff);
delay(200);
SMG_DisplayByBit(3,smg_data[Init_DS1302_Clock[2]/16]); //时
delay(200);
SMG_DisplayByBit(4,smg_data[Init_DS1302_Clock[2]%16]);
delay(200);
SMG_DisplayByBit(5,0xbf);
delay(200);
SMG_DisplayByBit(6,smg_data[Init_DS1302_Clock[1]/16]);//分
delay(200);
SMG_DisplayByBit(7,smg_data[Init_DS1302_Clock[1]%16]);
delay(200);
Controal_All_SMG(0xff);
delay(200);
}
//写入时间
void Write_DS1302_Data(void)
{unsigned char i = 0;
//关闭写保护
Write_Ds1302_Byte(0x8e,0x00);
for(i =0;i<7;i++)
{Write_Ds1302_Byte(DS1302_Write_Addr[i],Init_DS1302_Clock[i]);
}
//打开写保护
Write_Ds1302_Byte(0x8e,0x80);
}
//读取时间
void Read_DS1302_Data(void)
{unsigned char i = 0;
for(i = 0;i<7;i++)
{Init_DS1302_Clock[i] = Read_Ds1302_Byte(DS1302_Read_Addr[i]);
}
}
//按键操作
void Matrix_KEY_Scan(void)
{ H3 = 0;
H1 = H2 = H4 = 1;
//13
if(L2 == 0)
{delay(20);
if(L2 == 0)
{ while(L2 == 0)
{ if(interface == 1)
{SMG_Display_Temperature();
}
else if(interface == 2)
{SMG_Display_Time2();
}
else if(interface == 3)
{SMG_Display_Set_Temperature();
}
}
if(work_mode == 1)
{ work_mode = 2;
}
else if(work_mode == 2)
{ work_mode = 1;
}
}
}
//S17
if(interface == 3)
{if(L1 == 0)
{ delay(20);
if(L1 == 0)
{ while(L1 == 0)
{SMG_Display_Set_Temperature();
}
Set_T--;
}
}
}
else if(interface == 2)
{if(L1 == 0)
{ delay(20);
if(L1 == 0)
{ while(L1 == 0)
{SMG_Display_Time();
s17_flag = 1;
}
s17_flag = 0;
SMG_Display_Time2();
}
}
}
H4 = 0;
H1 = H3 = H2 = 1;
//S12
if(L2 == 0)
{delay(20);
if(L2 == 0)
{ while(L2 == 0)
{ if(interface == 1)
{SMG_Display_Temperature();
}
else if(interface == 2)
{SMG_Display_Time2();
}
else if(interface == 3)
{SMG_Display_Set_Temperature();
}
}
if(interface == 1)
{ interface = 2;
channel = 2;
}
else if(interface == 2)
{ interface = 3;
channel = 3;
}
else if(interface == 3)
{ interface = 1;
channel = 1;
}
}
}
//S16
if(L1 == 0)
{delay(20);
if(L1 == 0)
{ while(L1 == 0)
{ if(interface == 1)
{SMG_Display_Temperature();
}
else if(interface == 2)
{SMG_Display_Time2();
}
else if(interface == 3)
{SMG_Display_Set_Temperature();
}
}
if(interface == 3)
{ Set_T++;
}
}
}
}
//定时器初始化
void Init_Timer(void)
{TMOD = 0x01;
TH0 = (65535-10000)/256;//10ms
TL0 = (65535-10000)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
//系统初始化
void Init_System(void)
{Init_74HC138(0);
Controal_All_SMG(0xff);
P0 = 0xff;
Init_74HC138(4);
P0 = 0x00;
Init_74HC138(5);
}
//主函数
void main(void)
{Init_System();
Init_Timer();
Write_DS1302_Data();
while(1)
{Read_Temperature();
delay(200);
Read_DS1302_Data();
Matrix_KEY_Scan();
switch(interface)
{ case 1:SMG_Display_Temperature();break;
case 2:SMG_Display_Time2();break;
case 3:SMG_Display_Set_Temperature();break;
default:break;
}
//温度控制模式
if(work_mode == 1)
{ //L2亮温度模式指示灯 1111 1101
led_status = 0xFD;
P0 = led_status;
Init_74HC138(4);
if(Real_T/10 >Set_T)
{ //继电器吸合
P0 = 0x10; //0001 0000
relay_flag = 1;
}
else
{ //继电器断开
P0 = 0x00;
relay_flag = 0;
}
Init_74HC138(5);
}
//时间控制模式
else if(work_mode == 2)
{ P0 = 0xFF;
Init_74HC138(4);
//整点继电器吸合 5s后断开
if((Init_DS1302_Clock[0]/16 + Init_DS1302_Clock[0]%16) == 0 && (Init_DS1302_Clock[1]/16 + Init_DS1302_Clock[1]%16) == 0 && Init_DS1302_Clock[2] != 0)
{ clock_flag = 1; //时间整点标志
}
if(clock_flag == 1)
{ if(time_5s_flag == 0)
{ P0 = 0xfe;// 1111 1110
Init_74HC138(4);
//继电器吸合
P0 = 0x10; //0001 0000
relay_flag = 1;
Init_74HC138(5);
}
else
{P0 = 0XFF;// 1111 1110
Init_74HC138(4);
P0 = 0x00;// 1111 1110
relay_flag = 0;
Init_74HC138(5);
}
}
}
if(relay_flag == 1)
{ if(L3_flag == 1)
{ L3_flag = 0;
//L3 1111 1011
P0 = 0xfb;
Init_74HC138(4);
}
else
{ P0 = 0xFF;
Init_74HC138(4);
}
}
}
}
void Timer0_Server() interrupt 1
{static unsigned int t = 0,s = 0;
TH0 = (65535-10000)/256;//10ms
TL0 = (65535-10000)%256;
if(work_mode == 2 && clock_flag == 1)
{t++;
if(t >500) // >5s
{ t = 0;
time_5s_flag = 1;
}
}
s++;
if(s == 10)
{s = 0;
L3_flag = 1;
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前名称:【第十三届蓝桥杯省单片机软件部分2】-创新互联
文章转载:http://ybzwz.com/article/dhocee.html