電路原理圖如下:
c5b9f659a0e703aea3663be5efa05f8.png (163.85 KB, 下載次數: 57)
下載附件
下位機原理圖
2021-4-1 15:42 上傳
單片機源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <stdio.h>
- #include "delay.h"
- #include "ds1302.h"
- #include "1602.h"
- sbit relay = P1^3;//接口定義
- unsigned char dis0[16];//定義顯示區域臨時存儲數組
- unsigned char dis1[16];
- unsigned char dis2[16];
- unsigned char ReadTimeFlag= 0;//讀取時間標志
- unsigned char SetFlag =0; //時間設置標志
- unsigned char i;
- unsigned char relayFlag=0; //繼電器狀態標志
- unsigned long times_20ms=0xaaaaaaaa; //定時器計數 初始值可以任意值 這里隨便賦值為0xaaaaaaaa
- unsigned char uart_i =0; //串口接收計數
- unsigned char time_buf2[17]={20,18,4,1,12,55,00,6};//空年月日時分秒周; //更新時間數組
- bit ReadTempFlag;//定義讀時間標志
- unsigned char repotimes ;
- unsigned char disFlag ; //更新
- xdata unsigned char dsHour_start_01 = 8; //定時時分
- xdata unsigned char dsMin_start_01 = 0;
- xdata unsigned char dsHour_end_01 = 18;
- xdata unsigned char dsMin_end_01 = 30;
- //xdata unsigned char dsHour_start_01 = 8; //A組定時時分
- //xdata unsigned char dsMin_start_01 = 0;
- //xdata unsigned char dsH02_A = 12;
- //xdata unsigned char dsM02_A = 30;
- unsigned char timeOverFlag =0;
- xdata unsigned char firstin =0; //首次接收到標志
- xdata unsigned char tab[20]; //串口數據暫存
- xdata unsigned char Count=0; //串口接收計數
- xdata unsigned char uartbusy =0; //串口判忙
- void Init_Timer0(void); //函數聲明
- void uartSendStr(unsigned char *s,unsigned char length);
- void UART_Init(void);
- void uartSendByte(unsigned char dat);
- void main(void)
- {
- Init_Timer0(); //定時器0初始化
- times_20ms = 0; //初始化對應值
- UART_Init();
-
- relay = 0;//打開
- DelayMs(200); //延時有助于穩定
- relay = 1;//關閉
-
- Ds1302_Init();
- // Ds1302_Write_Time();
- Ds1302_Read_Time();//讀取時間參數
- uartSendStr("ready ok !",10);
- LCD_Init(); //初始化液晶
- DelayMs(20); //延時有助于穩定
- LCD_Clear();
- LCD_Write_String(0,0,"my desiger! ");
-
- while(1)
- {
-
- if(SetFlag==1) //如果接收到串口信息則更新時鐘
- {
- for(i=0;i<8;i++)
- {
- time_buf1[i]=time_buf2[2*i]*10+time_buf2[2*i+1];//數據整合,如2個數 1和5整合成15
- }
- Ds1302_Write_Time();//接收更新的時間然后寫入ds1302
- SetFlag=0; //時鐘信息更新后標志位清零
- }
- if(disFlag == 1) //定時讀取ds1302 定時時間到 則標志位置1,處理過時間參數標志位清零
- {
- disFlag=0; //標志位清零
- Ds1302_Read_Time();//讀取時間參數
- if((time_buf1[4]>=dsHour_start_01)&&(time_buf1[4]<=dsHour_end_01)) //對比時間段1
- {
- if((dsHour_start_01 != dsHour_end_01)) //設置起止時不再同一小時
- {
- if((time_buf1[4]>dsHour_start_01)&&(time_buf1[4]<dsHour_end_01)) //兩個時 范圍內
- {
- timeOverFlag = 1;//打開
- }
- else if((time_buf1[4] == dsHour_start_01) && (time_buf1[5]>=dsMin_start_01)) //啟停不在一個小時內 當前時間在啟動時內
- {
- timeOverFlag = 1;//打開
- }
- else if((time_buf1[4] == dsHour_end_01) && (time_buf1[5]<dsMin_end_01)) //啟停不在一個小時內 當前時間在停止時內
- {
- timeOverFlag = 1;//打開
- }
- else
- {timeOverFlag = 0;}
- }
- else if((dsHour_end_01==dsHour_start_01)) //同一個小時內
- {
- if((time_buf1[5]>=dsMin_start_01) && (time_buf1[5]<dsMin_end_01)) //統一小時分鐘范圍內
- {timeOverFlag = 1;}//打開
- else
- {timeOverFlag = 0;} //關閉
- }
- }
- else
- {
- timeOverFlag = 0; //關閉
- }
- if(timeOverFlag == 1){relay = 0;}//打開 繼電器控制
- else {relay =1 ;}//關閉
-
- sprintf(dis0,"%02d-%02d-%02d-%d",(int)time_buf1[1],(int)time_buf1[2],(int)time_buf1[3],(int)time_buf1[7]);//年月日周 步數
- LCD_Write_String(0,0,dis0); //顯示數據
- sprintf(dis0,"s%02d:%02d",(int)dsHour_start_01,(int)dsMin_start_01);//起始時間
- LCD_Write_String(10,0,dis0); //顯示數據
-
- sprintf(dis1,"%02d:%02d:%02d ",(int)time_buf1[4],(int)time_buf1[5],(int)time_buf1[6]);//時分秒
- LCD_Write_String(0,1,dis1); //顯示數據
-
- sprintf(dis1," e%02d:%02d",(int)dsHour_end_01,(int)dsMin_end_01);//結束時間
- LCD_Write_String(9,1,dis1); //顯示數據
- repotimes++;
- if(repotimes >= 8) //定時上報
- {
- repotimes = 0;//上報時間
-
- sprintf(dis0,"*D20%02d%02d%02d#",(int)time_buf1[1],(int)time_buf1[2],(int)time_buf1[3],(int)time_buf1[7]);//年月日周
- uartSendStr(dis0,11); //發送數據
- uartSendStr("\r\n",2);
-
- sprintf(dis1,"*T%02d%02d%02d#",(int)time_buf1[4],(int)time_buf1[5],(int)time_buf1[6]);//時分秒
- uartSendStr(dis1,9); //發送數據
- uartSendStr("\r\n",2);
-
- sprintf(dis2,"*S%02d:%02d~%02d:%02d#",(int)dsHour_start_01,(int)dsMin_start_01,(int)dsHour_end_01,(int)dsMin_end_01);//打印
- uartSendStr(dis2,14); //發送數據
- uartSendStr("\r\n",2);
- }
- }
- }
- }
- void Init_Timer0(void)
- {
- //**All notes can be deleted and modified**//
- TMOD |= 0x10; //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響
- TH0=(65536-20000)/256; //重新賦值 20ms
- TL0=(65536-20000)%256;
- EA=1; //總中斷打開
- ET0=1; //定時器中斷打開
- TR0=1; //定時器開關打開
- }
- void Timer0_isr(void) interrupt 1
- {
- TH0=(65536-20000)/256; //重新賦值 20ms
- TL0=(65536-20000)%256;
- times_20ms++; //計時++
- if(times_20ms%5==0)
- {
- disFlag=1; //定時更新顯示 100ms
- }
- if(uartbusy>0) //串口數據采集處理
- {uartbusy--;} //串口一定時間內接收到數據
- else
- {
- firstin =0; //重新賦值接收數據
- Count=0;//接收計數
- }
- }
- void UART_Init(void)
- {
- SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
- TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重裝
- TH1 = 0xFD; // TH1: 重裝值 9600 波特率 晶振 11.0592MHz
- TL1 = TH1;
- TR1 = 1; // TR1: timer 1 打開
- EA = 1; //打開總中斷
- ES = 1; //打開串口中斷
- }
- void uartSendByte(unsigned char dat)
- {
- unsigned char time_out;
- time_out=0x00;
- SBUF = dat; //將數據放入SBUF中
- while((!TI)&&(time_out<100)) //檢測是否發送出去
- {time_out++;DelayUs2x(10);} //未發送出去 進行短暫延時
- TI = 0; //清除ti標志
- }
- void uartSendStr(unsigned char *s,unsigned char length)
- {
- unsigned char NUM;
- NUM=0x00;
- while(NUM<length) //發送長度對比
- {
- uartSendByte(*s); //放松單字節數據
- s++; //指針++
- NUM++; //下一個++
- }
- }
- void UART_SER (void) interrupt 4 //串行中斷服務程序
- {
- unsigned char r_buf;
- if(RI) //判斷是接收中斷產生
- {
- RI=0; //標志位清零
- r_buf = SBUF;
- uartbusy = 20;
- if(r_buf=='*') //接收到起始標志
- {
- firstin = 1; //接收標志成功
- Count = 0;
- tab[Count++]=r_buf;
- }
- else if(firstin == 1) //接收到其實標志成功
- {
- tab[Count++]=r_buf;
- if((Count>=18)&&(tab[17] == '#')) //接收到設置時間命令
- {
- for(i=0;i<16;i++)
- {
- time_buf2[i]=tab[1+i]&0x0F; //提取設置值
- }
- SetFlag=1 ; //設置時間標志置位
- firstin =0;
- Count=0;
- }
- else if((Count>=14)&&(tab[13] == '#')) //接收到設置時間段//**All notes can be deleted and modified**//
- {
- if(tab[2] == 'T')
- {
- dsHour_start_01 =( tab[3] - '0' )*10 + (tab[4]-'0'); //起始時間提取
- dsMin_start_01 =( tab[5] - '0' )*10 + (tab[6]-'0');
- dsHour_end_01 =( tab[8] - '0' )*10 + (tab[9]-'0'); //結束時間提取
- dsMin_end_01 =( tab[10] - '0' )*10 + (tab[11]-'0');
- }
- firstin =0;
- Count=0;
- }
- else if(Count>=20) //接收數據很長 但是不是所需要的
- {
- firstin =0;
- Count=0;
- }
- }
- }
- if(TI) //如果是發送標志位,清零
- TI=0;
- }
復制代碼
代碼:
51-162、程序-單片機源碼.zip
(90.66 KB, 下載次數: 48)
2021-4-1 15:40 上傳
點擊文件名下載附件
下位機程序 下載積分: 黑幣 -5
|