功能:系统上电或按键复位后能自动提示界面,进入准备工作状态。 系统根据环境明暗自动亮灭路灯,当路灯出现故障,能发出声光报警,能根据交通环境自动亮灭路灯,能设定路灯亮灭的时间,可以独立控制每个路灯。
(proteus原理图,程序在附件文件夹中)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- /****************************************************************
- 课题名称:模拟路灯控制系统
- 功能 :系统上电或按键复位后能自动提示界面,进入准备工作状态。
- 系统根据环境明暗自动亮灭路灯,当路灯出现故障,能发出声光报警,能根据交通环境自动亮灭路灯,能设定路灯亮灭的时间,可以独立控制每个路灯。
- 硬件说明:单片机AT89S52,晶振12MHZ,输入设备-接在P1口的8位独立式键盘,输出设备-LCD12864液晶屏,P2口接LCD12864 D0至D7,RS、RW、EN分别接到单片机的P3.0、P3.1、P3.2。时钟模块-DS1302模块的CLK、IO、RST分别接到单片机的P3.4、P3.3、P3.5。
- ****************************************************************/
- #include<reg52.h>
- #include"intrins.h"
- #define LCD_Data P2
- sbit P00=P0^0;
- sbit P01=P0^1;
- sbit P02=P0^2;
- sbit P03=P0^3;
- sbit P04=P0^4;
- sbit P05=P0^5;
- sbit P07=P0^7;
- sbit P36=P3^6;
- sbit P37=P3^7;
- sbit DS1302_CLK=P3^4;
- sbit DS1302_IO=P3^3;
- sbit DS1302_RST=P3^5;
- sbit LCD_RS=P3^0;
- sbit LCD_RW=P3^1;
- sbit LCD_E=P3^2;
- unsigned char sec,min,hour,day,month,year;
- unsigned char temp0;
- unsigned char count,count1,Hour1,Minite1,v,SB,SC,m,n;
- char Led1onH,Led1onM,Led2onH,Led2onM,Led1offH,Led1offM,Led2offH,Led2offM;
- /**************延时*********************/
- void delay(unsigned int a)
- //延时1MS/次
- {
- unsigned char i;
- while(--a)
- {
- for(i=0;i<125;i++) ;
- }
- }
- /*************************************/
- void delays()
- { ; ;}
- /***************1302时钟程序****************/
- void WriteDs1302Byte(unsigned char temp)
- {
- unsigned char i;
- for (i=8;i>0;i--)
- {
- DS1302_CLK=0;
- DS1302_IO=temp&0x01;
- delays();
- DS1302_CLK=1;
- temp>>=1;
- delays();
- }
- }
- /***********************************/
- void WriteDs1302( unsigned char address,unsigned char dat )
- {
- DS1302_RST=0;
- DS1302_CLK=0;
- DS1302_RST=1;
- delay(1);
- WriteDs1302Byte(address);
- WriteDs1302Byte(dat);
- DS1302_CLK=1;
- DS1302_RST=0;
- }
- /***********************************/
- unsigned char read_byte()
- {
- unsigned char i;
- for(i=8;i>0;i--)
- {
- if(DS1302_IO)
- temp0=temp0|0x80;
- DS1302_CLK=1;
- delays();
- DS1302_CLK=0;
- temp0=temp0>>1;
- delays();
- }
- return temp0;
- }
- /**************************************************/
- unsigned char ReadDs1302(unsigned char address)
- {
- unsigned char temp;
- DS1302_RST=0;
- DS1302_CLK=0;
- DS1302_RST=1;
- WriteDs1302Byte(address);
- temp=read_byte();
- DS1302_RST=0;
- DS1302_CLK=1;
- return temp;
- }
- /*********************1302初始化***********************/
- void InitDS1302()
- {
- {
- WriteDs1302(0x8e,0x00);
- delay(5);
- WriteDs1302(0x8c,0x16);
- //写入年份16 年
- delay(5);
- WriteDs1302(0x8a,0x04);
- //写入星期4
- delay(5);
- WriteDs1302(0x88,0x05);
- //定入月分5 月
- delay(5);
- WriteDs1302(0x86,0x12);
- //写入日期12 日
- delay(5);
- WriteDs1302(0x84,0x02);
- //写入小时2 点
- delay(5);
- WriteDs1302(0x82,0x00);
- //写入秒00 分
- delay(5);
- WriteDs1302(0x80,0x00);
- delay(5);
- WriteDs1302(0x8e,0x80);
- //控制命令,WP 为1,禁止写操作
- }
- }
- /***************检查忙否*****************/
- void Checkstates()
- {
- unsigned char dat;
- LCD_RS=0;
- LCD_RW=1;
- do
- {
- LCD_E=1;//下降沿
- _nop_();//保持一定间隔
- _nop_();
- dat=LCD_Data;
- _nop_();
- _nop_();
- LCD_E=0;
- }while((dat&0x80)==1);
- }
- /**********LCD写数据*******************************/
- void WriteDataLCD(unsigned char dat)
- {
- Checkstates();
- LCD_RS=1;
- LCD_RW=0;
- LCD_Data=dat;
- LCD_E=1;
- delay(2);
- LCD_E=0;
- }
- /**********LCD写地址*******************************/
- void WriteCommandLCD(unsigned char udat)
- {
- Checkstates();
- LCD_RS=0;
- LCD_RW=0;
- LCD_Data=udat;
- LCD_E=1;
- delay(2);
- LCD_E=0;
- }
- /*************LCD初始化******************************/
- void LCDInit(void)
- {
- WriteCommandLCD(0x38);
- delay(2);
- WriteCommandLCD(0x38);
- delay(2);
- WriteCommandLCD(0x01);
- delay(2);
- WriteCommandLCD(0x06);
- delay(2);
- WriteCommandLCD(0x0C);
- delay(2);
- }
- /*************LCD清屏指令**********************/
- void LCDClear(void)
- {
- WriteCommandLCD(0x01);
- delay(2);
- }
- /**********发送内容*********************************/
- void LCDSendWord(unsigned char *p)
- {
- while(*p>0)
- {
- WriteDataLCD(*p);
- p++;
- }
- }
- /*********发送地址*********************************/
- void LCDTestWord(bit i,unsigned char word)
- {
- if(i==0)
- {
- WriteCommandLCD(word);
- }
- else
- {
- WriteDataLCD(word);
- }
- }
- /***************LCD显示年************/
- void DisplayYear(void)
- {
- year=ReadDs1302(0x8d);
- LCDTestWord(0,0x80);
- LCDSendWord("20");
- LCDTestWord(1,(year/16)+0x30);
- LCDTestWord(1,year%16+0x30);
- LCDTestWord(1,'/');
- }
- /***************LCD显示月*************************/
- void DisplayMonth(void)
- {
- month=ReadDs1302(0x89);
- LCDTestWord(1,(month/16)+0x30);
- LCDTestWord(1,month%16+0x30);
- LCDTestWord(1,'/');
- }
- /****************LCD显示日**************************/
- void DisplayDay(void)
- {
- day=ReadDs1302(0x87);
- LCDTestWord(1,(day/16)+0x30);
- LCDTestWord(1,day%16+0x30);
- }
- /**********LCD显示时*********************************/
- void DisplayHour(void)
- {
- hour=ReadDs1302(0x85);
- LCDTestWord(0,0xc0);
- LCDTestWord(1,(hour/16)+0x30);
- LCDTestWord(1,hour%16+0x30);
- LCDTestWord(1,':');
- }
- /**********LCD显示分**********************************/
- void DisplayMin(void)
- {
- min=ReadDs1302(0x83);
- LCDTestWord(1,(min/16)+0x30);
- LCDTestWord(1,min%16+0x30);
- LCDTestWord(1,':');
- }
- /**********LCD显示秒**********************************/
- void DisplaySec(void)
- {
- sec=ReadDs1302(0x81);
- LCDTestWord(1,(sec/16)+0x30);
- LCDTestWord(1,sec%16+0x30);
- }
- /************************************************************/
- void UpDate1(void)
- {
- DisplayHour();
- DisplayMin();
- DisplaySec();
- }
- /************************************************************/
- void UpDate2(void)
- {
- DisplayYear();
- DisplayMonth();
- DisplayDay();
- }
- /*************键扫描程序**************/
- unsigned char keys(void)
- {
- unsigned char tmp;
- if((P1&0xff)!=0xff)
- {
- delay(10);
- if((P1&0xff)!=0xff)
- {
- tmp=(P1&0xff);
- do{}while((P1&0xff)!=0xff);
- tmp=~tmp;
- return (tmp);
- }
- }
- return (0);
- }
- void baojing(void)
- {
- if(P01==0)
- {
- LCDTestWord(0,0x80);
- LCDSendWord("LED1 Broken");
- P07=0;
- }
- else
- {
- LCDTestWord(0,0x80);
- LCDSendWord("LED1 OK ");
- }
- if(P02==0)
- {
- LCDTestWord(0,0xc0);
- LCDSendWord("LED2 Broken");
- P07=0;
- }
- else
- {
- LCDTestWord(0,0xc0);
- LCDSendWord("LED2 OK ");
- }
- if(P01==1&&P02==1)
- P07=1;
- }
-
- void huanjing(void)
- {
- while(v!=4)
- {
- if(P00==1)
- {
- P36=1;
- P37=1;
- }
- if(P00==0)
- {
- P36=0;
- P37=0;
- }
- baojing();
- v=keys();
- }
- }
- void jiaotong(void)
- {
- while(v!=4)
- {
- SC=0;
- SB=0;
- if(P03==0)
- {
- P36=0;
- P37=1;
- while(1)
- {
- if(P04==0)
- {
- P36=1;
- P37=0;
- while(1)
- {
- if(P05==0)
- {
- P36=1;
- P37=1;
- SC=1;
- SB=1;
- while(P05==0);
- }
- if(SC==1)break;
- }
- }
- if(SB==1)break;
- }
- }
- if(P05==0)
- {
- P36=1;
- P37=0;
- while(1)
- {
- if(P04==0)
- {
- P36=0;
- P37=1;
- while(1)
- {
- if(P03==0)
- {
- P36=1;
- P37=1;
- SC=1;
- SB=1;
- while(P03==0);
- }
- if(SC==1)break;
- }
- }
- if(SB==1)break;
- }
- }
- v=keys();
- }
- }
- void Displayrealtime(void)
- {
- while(v!=4)
- {
- UpDate2();
- UpDate1();
- v=keys();
- }
- }
- void Display1(void)
- {
- LCDTestWord(0,0x80);
- LCDSendWord("LED1 ON ");
- LCDTestWord(1,(Led1onH/10)+0x30);
- LCDTestWord(1,(Led1onH%10)+0x30);
- LCDTestWord(1,':');
- LCDTestWord(1,(Led1onM/10)+0x30);
- LCDTestWord(1,(Led1onM%10)+0x30);
- LCDTestWord(0,0xc0);
- LCDSendWord("LED1 OFF ");
- LCDTestWord(1,(Led1offH/10)+0x30);
- LCDTestWord(1,(Led1offH%10)+0x30);
- LCDTestWord(1,':');
- LCDTestWord(1,(Led1offM/10)+0x30);
- LCDTestWord(1,(Led1offM%10)+0x30);
- }
- void Display2(void)
- {
- LCDTestWord(0,0x80);
- LCDSendWord("LED2 ON ");
- LCDTestWord(1,(Led2onH/10)+0x30);
- LCDTestWord(1,(Led2onH%10)+0x30);
- LCDTestWord(1,':');
- LCDTestWord(1,(Led2onM/10)+0x30);
- LCDTestWord(1,(Led2onM%10)+0x30);
- LCDTestWord(0,0xc0);
- LCDSendWord("LED2 OFF ");
- LCDTestWord(1,(Led2offH/10)+0x30);
- LCDTestWord(1,(Led2offH%10)+0x30);
- LCDTestWord(1,':');
- LCDTestWord(1,(Led2offM/10)+0x30);
- LCDTestWord(1,(Led2offM%10)+0x30);
- }
- void settime(void)
- {
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Please Set Time");
- delay(2000);
- LCDClear();
- v=keys();
- while(v!=4)
- {
- switch(v)
- {
- case 0: UpDate2();
- UpDate1();
- v=keys();
- break;
- case 2: LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Set Succes");
- delay(1000);
- LCDClear();
- while(v==0|v==2)
- {
- UpDate2();
- UpDate1();
- Hour1=hour/16*10+hour%16;
- Minite1=min/16*10+min%16;
- if(Hour1==Led1onH&&Minite1==Led1onM)
- P36=0;
- if(Hour1==Led2onH&&Minite1==Led2onM)
- P37=0;
- if(Hour1==Led1offH&&Minite1==Led1offM)
- P36=1;
- if(Hour1==Led2offH&&Minite1==Led2offM)
- P37=1;
- v=keys();
- }
- break;
- case 8: LCDClear();
- while(v==0|v==8|v==32|v==64|v==128)
- {
- Display1();
- v=keys();
- switch(v)
- {
- case 32: count1++;
- if(count1==5)
- count1=1;
- break;
- case 64: if(count1>0&&count1<5)
- {
- if(count1==1)
- {
- Led1onH++;
- if(Led1onH==24)
- Led1onH=0;
- }
- if(count1==2)
- {
- Led1onM++;
- if(Led1onM==60)
- Led1onM=0;
- }
- if(count1==3)
- {
- Led1offH++;
- if(Led1offH==24)
- Led1offH=0;
- }
- if(count1==4)
- {
- Led1offM++;
- if(Led1offM==60)
- Led1offM=0;
- }
- }
- break;
- case 128: if(count1>0&&count1<5)
- {
- if(count1==1)
- {
- Led1onH--;
- if(Led1onH<0)
- Led1onH=23;
- }
- if(count1==2)
- {
- Led1onM--;
- if(Led1onM<0)
- Led1onM=59;
- }
- if(count1==3)
- {
- Led1offH--;
- if(Led1offH<0)
- Led1offH=23;
- }
- if(count1==4)
- {
- Led1offM--;
- if(Led1offM<0)
- Led1offM=59;
- }
- }
- break;
- }
- }
- break;
- case 16: LCDClear();
- while(v==0|v==16|v==32|v==64|v==128)
- {
- Display2();
- v=keys();
- switch(v)
- {
- case 32: count1++;
- if(count1==5)
- count1=1;
- break;
- case 64: if(count1>0&&count1<5)
- {
- if(count1==1)
- {
- Led2onH++;
- if(Led2onH==24)
- Led2onH=0;
- }
- if(count1==2)
- {
- Led2onM++;
- if(Led2onM==60)
- Led2onM=0;
- }
- if(count1==3)
- {
- Led2offH++;
- if(Led2offH==24)
- Led2offH=0;
- }
- if(count1==4)
- {
- Led2offM++;
- if(Led2offM==60)
- Led2offM=0;
- }
- }
- break;
- case 128: if(count1>0&&count1<5)
- {
- if(count1==1)
- {
- Led2onH--;
- if(Led2onH<0)
- Led2onH=23;
- }
- if(count1==2)
- {
- Led2onM--;
- if(Led2onM<0)
- Led2onM=59;
- }
- if(count1==3)
- {
- Led2offH--;
- if(Led2offH<0)
- Led2offH=23;
- }
- if(count1==4)
- {
- Led2offM--;
- if(Led2offM<0)
- Led2offM=59;
- }
- }
- break;
- }
- }
- break;
- }
- }
- }
- /*****************主函数程序*************************/
- void main(void)
- {
- LCDInit();
- LCDClear();
- InitDS1302();
- LCDTestWord(0,0x80);
- LCDSendWord("Entering system");
- LCDTestWord(0,0xc0);
- LCDSendWord("Please Waiting");
- delay(2000);
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Select Menu");
- while(1)
- {
- v=keys();
- switch(v)
- {
- case 1: count++;
- if(count==5)
- count=1;
- LCDTestWord(0,0x80);
- if(count==1)
- {
- LCDClear();
- LCDSendWord("Realtime");
- }
- if(count==2)
- {
- LCDClear();
- LCDSendWord("Environment");
- }
- if(count==3)
- {
- LCDClear();
- LCDSendWord("Traffic");
- }
- if(count==4)
- {
- LCDClear();
- LCDSendWord("Set Time");
- }
- break;
- case 2: if(0<count&&count<5)
- {
- if(count==1)
- {
- P36=1;
- P37=1;
- Displayrealtime();
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Select Menu");
- break;
- }
- if(count==2)
- {
- P36=1;
- P37=1;
- huanjing();
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Select Menu");
- break;
- }
- if(count==3)
- {
- P36=1;
- P37=1;
- jiaotong();
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Select Menu");
- break;
- }
- if(count==4)
- {
- P36=1;
- P37=1;
- settime();
- LCDClear();
- LCDTestWord(0,0x80);
- LCDSendWord("Select Menu");
- break;
- }
- }
- break;
- case 64: m++;
- if(m%2==0)
- P36=1;
- else
- P36=0;
- break;
- case 128: n++;
- if(n%2==0)
- P37=1;
- else
- P37=0;
- break;
- }
- }
- }
-
复制代码
所有资料51hei提供下载:
模拟路灯系统.zip
(48.87 KB, 下载次数: 79)
|