一、題目要求
二、軟件實(shí)現(xiàn)
#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;
//簡單延時(shí)
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;
}
//數(shù)碼管按位操作
void SMG_DisplayByBit(unsigned char pos,unsigned char dat)
{P0 = (0x01<< pos);
Init_74HC138(6);
P0 = dat;
Init_74HC138(7);
}
//控制所有數(shù)碼管
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;
}
//數(shù)碼管數(shù)據(jù)顯示溫度
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);
}
//數(shù)碼管數(shù)據(jù)顯示設(shè)置溫度
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);
}
//數(shù)碼管數(shù)據(jù)時(shí)間
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]); //時(shí)
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);
}
//寫入時(shí)間
void Write_DS1302_Data(void)
{unsigned char i = 0;
//關(guān)閉寫保護(hù)
Write_Ds1302_Byte(0x8e,0x00);
for(i =0;i<7;i++)
{Write_Ds1302_Byte(DS1302_Write_Addr[i],Init_DS1302_Clock[i]);
}
//打開寫保護(hù)
Write_Ds1302_Byte(0x8e,0x80);
}
//讀取時(shí)間
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++;
}
}
}
}
//定時(shí)器初始化
void Init_Timer(void)
{TMOD = 0x01;
TH0 = (65535-10000)/256;//10ms
TL0 = (65535-10000)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
//系統(tǒng)初始化
void Init_System(void)
{Init_74HC138(0);
Controal_All_SMG(0xff);
P0 = 0xff;
Init_74HC138(4);
P0 = 0x00;
Init_74HC138(5);
}
//主函數(shù)
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);
}
//時(shí)間控制模式
else if(work_mode == 2)
{ P0 = 0xFF;
Init_74HC138(4);
//整點(diǎn)繼電器吸合 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; //時(shí)間整點(diǎn)標(biāo)志
}
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;
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
標(biāo)題名稱:【第十三屆藍(lán)橋杯省單片機(jī)軟件部分2】-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article44/dhocee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、企業(yè)建站、建站公司、手機(jī)網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容