|
功能:系統(tǒng)上電或按鍵復(fù)位后能自動(dòng)提示界面,進(jìn)入準(zhǔn)備工作狀態(tài)。 系統(tǒng)根據(jù)環(huán)境明暗自動(dòng)亮滅路燈,當(dāng)路燈出現(xiàn)故障,能發(fā)出聲光報(bào)警,能根據(jù)交通環(huán)境自動(dòng)亮滅路燈,能設(shè)定路燈亮滅的時(shí)間,可以獨(dú)立控制每個(gè)路燈。
(proteus原理圖,程序在附件文件夾中)
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (8.9 KB, 下載次數(shù): 33)
下載附件
2019-5-30 05:48 上傳
單片機(jī)源程序如下:
- /****************************************************************
- 課題名稱(chēng):模擬路燈控制系統(tǒng)
- 功能 :系統(tǒng)上電或按鍵復(fù)位后能自動(dòng)提示界面,進(jìn)入準(zhǔn)備工作狀態(tài)。
- 系統(tǒng)根據(jù)環(huán)境明暗自動(dòng)亮滅路燈,當(dāng)路燈出現(xiàn)故障,能發(fā)出聲光報(bào)警,能根據(jù)交通環(huán)境自動(dòng)亮滅路燈,能設(shè)定路燈亮滅的時(shí)間,可以獨(dú)立控制每個(gè)路燈。
- 硬件說(shuō)明:?jiǎn)纹瑱C(jī)AT89S52,晶振12MHZ,輸入設(shè)備-接在P1口的8位獨(dú)立式鍵盤(pán),輸出設(shè)備-LCD12864液晶屏,P2口接LCD12864 D0至D7,RS、RW、EN分別接到單片機(jī)的P3.0、P3.1、P3.2。時(shí)鐘模塊-DS1302模塊的CLK、IO、RST分別接到單片機(jī)的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;
- /**************延時(shí)*********************/
- void delay(unsigned int a)
- //延時(shí)1MS/次
- {
- unsigned char i;
- while(--a)
- {
- for(i=0;i<125;i++) ;
- }
- }
- /*************************************/
- void delays()
- { ; ;}
- /***************1302時(shí)鐘程序****************/
- 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);
- //寫(xiě)入年份16 年
- delay(5);
- WriteDs1302(0x8a,0x04);
- //寫(xiě)入星期4
- delay(5);
- WriteDs1302(0x88,0x05);
- //定入月分5 月
- delay(5);
- WriteDs1302(0x86,0x12);
- //寫(xiě)入日期12 日
- delay(5);
- WriteDs1302(0x84,0x02);
- //寫(xiě)入小時(shí)2 點(diǎn)
- delay(5);
- WriteDs1302(0x82,0x00);
- //寫(xiě)入秒00 分
- delay(5);
- WriteDs1302(0x80,0x00);
- delay(5);
- WriteDs1302(0x8e,0x80);
- //控制命令,WP 為1,禁止寫(xiě)操作
- }
- }
- /***************檢查忙否*****************/
- 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寫(xiě)數(shù)據(jù)*******************************/
- void WriteDataLCD(unsigned char dat)
- {
- Checkstates();
- LCD_RS=1;
- LCD_RW=0;
- LCD_Data=dat;
- LCD_E=1;
- delay(2);
- LCD_E=0;
- }
- /**********LCD寫(xiě)地址*******************************/
- 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);
- }
- /**********發(fā)送內(nèi)容*********************************/
- void LCDSendWord(unsigned char *p)
- {
- while(*p>0)
- {
- WriteDataLCD(*p);
- p++;
- }
- }
- /*********發(fā)送地址*********************************/
- 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顯示時(shí)*********************************/
- 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;
- }
- }
- }
- /*****************主函數(shù)程序*************************/
- 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;
- }
- }
- }
-
復(fù)制代碼
所有資料51hei提供下載:
模擬路燈系統(tǒng).zip
(48.87 KB, 下載次數(shù): 79)
2019-5-29 15:01 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|