根据作息时间控制教师、宿舍、路灯、打铃
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include <stdio.h>
- #include "ds1302.h"
- #include "delay.h"
- #include "1602.h"
- uchar ReadTimeFlag;
- bit pace;
- uchar flag;
- sbit key1=P3^4;
- sbit key2=P3^5;
- sbit key3=P3^6;
- sbit key4=P3^7;
- sbit led1=P2^5;
- sbit led2=P2^6;
- sbit led3=P2^7;
- sbit bell=P2^4; //0为响
- uchar code time[]=
- {
- 6,30,
- 6,40,
- 7,10,
- 8,10,
- 9, 0,
- 9,10,
- 10,0,
- 10,15,
- 11,5,
- 11,10,
- 12,0,
- 14,40,
- 15,10,
- 15,40,
- 16,30,
- 19,0,
- 21,0,
- 23,0,
- };
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关打开
- }
- void led()
- { uchar i;
- for(i=0;i<18;i++)
- {
- if(time_data[4]==time[2*i]&&time_data[5]==time[1+(2*i)]&&time_data[6]<=10)
- {
- bell=0;break;
-
- }
- else
- {
- bell=1;
- }
- }
- if(time_data[4]>=19&&time_data[4]<21&&time_data[5])
- {led1=0;led2=1;led3=0;return;}
- if(time_data[4]>=21&&time_data[4]<23)
- {led1=1;led2=0;led3=0;return;}
- if(time_data[4]>=23&&time_data[4]<24)
- {led1=1;led2=1;led3=1;return;}
- if(time_data[4]==6&&time_data[5]>=30&&time_data[5]<40)
- {led1=0;led2=0;led3=0;return;}
- if(time_data[4]==6&&time_data[5]>=40)
- {led1=0;led2=1;led3=0;return;}
- if(time_data[4]>=7&&time_data[5]>=10&&time_data[4]<19)
- {led1=1;led2=1;led3=1;return;}
- }
- void main (void)
- {
- LCD_Init();
- // set_rtc();
- Init_Timer0();
- while (1) //主循环
- {
- if(ReadTimeFlag)
- {
- ReadTimeFlag=0;
- if(flag==0)
- {
- read_rtc();
- pace=0;
- }
- led();
- LCD_Write_String(0,0,"DATA:");
- LCD_Write_String(0,1,"TIME:");
- LCD_Write_String(5,0,"20");
- LCD_Write_String(9,0,"-");
- LCD_Write_String(12,0,"-");
- LCD_Write_String(8,1,"-");
- LCD_Write_String(11,1,"-");
- LCD_Write_Time(7,0,time_data[0]); //年
- LCD_Write_Time(10,0,time_data[2]); //月
- LCD_Write_Time(13,0,time_data[3]); //日
- LCD_Write_Time(6,1,time_data[4]); //时
- LCD_Write_Time(9,1,time_data[5]); //分
- LCD_Write_Time(12,1,time_data[6]); //秒
- if(pace)
- {
- switch(flag)
- {
- case 1:LCD_Write_String(7,0," ");break;
- case 2:LCD_Write_String(10,0," ");break;
- case 3:LCD_Write_String(13,0," ");break;
- case 4:LCD_Write_String(6,1," ");break;
- case 5:LCD_Write_String(9,1," ");break;
- case 6:LCD_Write_String(12,1," ");break;
- case 7:set_rtc();flag=0;break;//模式7的时候为保存模式,将设置的时钟数据保存后直接退出设置模式
- default:break;
- }
- }
- }
- }
- }
- void Timer0_isr(void) interrupt 1
- {
- static unsigned int num;
- static unsigned char key1num;//按键一按下标志
- static unsigned char key2num;//按键二按下标志
- static unsigned char key3num;//按键三按下标志
- static unsigned char key4num;//按键三按下标志
- TH0=(65536-2000)/256; //重新赋值 2ms
- TL0=(65536-2000)%256;
-
- num++;
- if(num==250) //大致100ms
- {
- num=0;
- if(flag)pace=!pace;
- ReadTimeFlag=1; //读标志位置1
- }
-
-
- if(key1==0)key1num=1;
- else if(key1num==1)
- {
- key1num=0;
- flag++;
- }
- if(key2==0)key2num=1;
- else if(key2num==1)
- {
- key2num=0;
- switch(flag)
- {
- case 1:if(time_data[0]<99)time_data[0]++;break;
- case 2:if(time_data[2]<11)time_data[2]++;break;
- case 3:if(time_data[3]<31)time_data[3]++;break;
- case 4:if(time_data[4]<23)time_data[4]++;break;
- case 5:if(time_data[5]<59)time_data[5]++;break;
- case 6:if(time_data[6]<59)time_data[6]++;break;
- default:break;
- }
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
校园作息时间.zip
(92.99 KB, 下载次数: 125)
|