【第十三届蓝桥杯省单片机软件部分2】
一、题目要求
二、软件实现
#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};
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--);
}
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)
{
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)
{
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)
{
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)
{
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;
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;
}
}
}
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;
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;
}
}
}
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;
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)
{
led_status = 0xFD;
P0 = led_status;
Init_74HC138(4);
if(Real_T/10 > Set_T)
{
P0 = 0x10;
relay_flag = 1;
}
else
{
P0 = 0x00;
relay_flag = 0;
}
Init_74HC138(5);
}
else if(work_mode == 2)
{
P0 = 0xFF;
Init_74HC138(4);
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;
Init_74HC138(4);
P0 = 0x10;
relay_flag = 1;
Init_74HC138(5);
}
else
{
P0 = 0XFF;
Init_74HC138(4);
P0 = 0x00;
relay_flag = 0;
Init_74HC138(5);
}
}
}
if(relay_flag == 1)
{
if(L3_flag == 1)
{
L3_flag = 0;
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;
TL0 = (65535-10000)%256;
if(work_mode == 2 && clock_flag == 1)
{
t++;
if(t >500)
{
t = 0;
time_5s_flag = 1;
}
}
s++;
if(s == 10)
{
s = 0;
L3_flag = 1;
}
}