电路原理图如下:
单片机源程序如下:
- #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)
|