找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3154|回复: 1
收起左侧

51单片机模拟路灯系统Proteus仿真设计+C语言源程序

[复制链接]
ID:550179 发表于 2019-5-29 15:01 | 显示全部楼层 |阅读模式
功能:系统上电或按键复位后能自动提示界面,进入准备工作状态。
系统根据环境明暗自动亮灭路灯,当路灯出现故障,能发出声光报警,能根据交通环境自动亮灭路灯,能设定路灯亮灭的时间,可以独立控制每个路灯。
(proteus原理图,程序在附件文件夹中)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png

单片机源程序如下:
  1. /****************************************************************
  2. 课题名称:模拟路灯控制系统
  3. 功能 :系统上电或按键复位后能自动提示界面,进入准备工作状态。
  4. 系统根据环境明暗自动亮灭路灯,当路灯出现故障,能发出声光报警,能根据交通环境自动亮灭路灯,能设定路灯亮灭的时间,可以独立控制每个路灯。
  5. 硬件说明:单片机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。
  6. ****************************************************************/
  7. #include<reg52.h>
  8. #include"intrins.h"
  9. #define LCD_Data P2
  10. sbit P00=P0^0;
  11. sbit P01=P0^1;
  12. sbit P02=P0^2;
  13. sbit P03=P0^3;
  14. sbit P04=P0^4;
  15. sbit P05=P0^5;
  16. sbit P07=P0^7;
  17. sbit P36=P3^6;
  18. sbit P37=P3^7;
  19. sbit DS1302_CLK=P3^4;
  20. sbit DS1302_IO=P3^3;
  21. sbit DS1302_RST=P3^5;
  22. sbit LCD_RS=P3^0;
  23. sbit LCD_RW=P3^1;
  24. sbit LCD_E=P3^2;
  25. unsigned char sec,min,hour,day,month,year;
  26. unsigned char temp0;
  27. unsigned char count,count1,Hour1,Minite1,v,SB,SC,m,n;
  28. char Led1onH,Led1onM,Led2onH,Led2onM,Led1offH,Led1offM,Led2offH,Led2offM;
  29. /**************延时*********************/
  30. void delay(unsigned int a)
  31. //延时1MS/次
  32. {
  33. unsigned char i;
  34. while(--a)
  35. {
  36. for(i=0;i<125;i++) ;
  37. }
  38. }

  39. /*************************************/
  40. void delays()
  41. { ; ;}

  42. /***************1302时钟程序****************/
  43. void WriteDs1302Byte(unsigned char temp)
  44. {
  45. unsigned char i;
  46. for (i=8;i>0;i--)         
  47. {
  48. DS1302_CLK=0;
  49. DS1302_IO=temp&0x01;
  50. delays();
  51. DS1302_CLK=1;
  52.   temp>>=1;
  53.   delays();
  54. }
  55. }
  56. /***********************************/
  57. void WriteDs1302( unsigned char address,unsigned char dat )
  58. {
  59. DS1302_RST=0;
  60. DS1302_CLK=0;
  61. DS1302_RST=1;
  62. delay(1);
  63. WriteDs1302Byte(address);
  64. WriteDs1302Byte(dat);
  65. DS1302_CLK=1;
  66. DS1302_RST=0;
  67. }
  68. /***********************************/
  69. unsigned char read_byte()
  70. {
  71. unsigned char i;
  72. for(i=8;i>0;i--)
  73. {
  74.   if(DS1302_IO)
  75.   temp0=temp0|0x80;
  76.   DS1302_CLK=1;
  77.   delays();
  78.   DS1302_CLK=0;
  79.   temp0=temp0>>1;
  80. delays();
  81. }
  82. return temp0;
  83. }
  84. /**************************************************/
  85. unsigned char ReadDs1302(unsigned char address)
  86. {
  87. unsigned char temp;
  88. DS1302_RST=0;
  89. DS1302_CLK=0;
  90. DS1302_RST=1;
  91. WriteDs1302Byte(address);
  92. temp=read_byte();
  93. DS1302_RST=0;
  94. DS1302_CLK=1;
  95. return temp;
  96. }
  97. /*********************1302初始化***********************/
  98. void InitDS1302()
  99. {
  100. {
  101. WriteDs1302(0x8e,0x00);
  102. delay(5);
  103. WriteDs1302(0x8c,0x16);
  104. //写入年份16 年
  105. delay(5);
  106. WriteDs1302(0x8a,0x04);
  107. //写入星期4
  108. delay(5);
  109. WriteDs1302(0x88,0x05);
  110. //定入月分5 月
  111. delay(5);
  112. WriteDs1302(0x86,0x12);
  113. //写入日期12 日
  114. delay(5);
  115. WriteDs1302(0x84,0x02);
  116. //写入小时2 点
  117. delay(5);
  118. WriteDs1302(0x82,0x00);
  119. //写入秒00 分
  120. delay(5);
  121. WriteDs1302(0x80,0x00);
  122. delay(5);
  123. WriteDs1302(0x8e,0x80);
  124. //控制命令,WP 为1,禁止写操作
  125. }
  126. }

  127. /***************检查忙否*****************/
  128. void Checkstates()
  129. {
  130. unsigned char dat;
  131. LCD_RS=0;
  132. LCD_RW=1;
  133. do
  134. {
  135. LCD_E=1;//下降沿
  136. _nop_();//保持一定间隔
  137. _nop_();
  138. dat=LCD_Data;
  139. _nop_();
  140. _nop_();
  141. LCD_E=0;
  142. }while((dat&0x80)==1);
  143. }

  144. /**********LCD写数据*******************************/
  145. void WriteDataLCD(unsigned char dat)
  146. {
  147. Checkstates();
  148. LCD_RS=1;
  149. LCD_RW=0;
  150. LCD_Data=dat;
  151. LCD_E=1;
  152. delay(2);
  153. LCD_E=0;

  154. }
  155. /**********LCD写地址*******************************/
  156. void WriteCommandLCD(unsigned char udat)
  157. {
  158. Checkstates();
  159. LCD_RS=0;
  160. LCD_RW=0;
  161. LCD_Data=udat;
  162. LCD_E=1;
  163. delay(2);
  164. LCD_E=0;

  165. }
  166. /*************LCD初始化******************************/
  167. void LCDInit(void)
  168. {
  169. WriteCommandLCD(0x38);
  170. delay(2);
  171. WriteCommandLCD(0x38);
  172. delay(2);
  173. WriteCommandLCD(0x01);
  174. delay(2);       
  175. WriteCommandLCD(0x06);
  176. delay(2);       
  177. WriteCommandLCD(0x0C);
  178. delay(2);
  179. }
  180. /*************LCD清屏指令**********************/
  181. void LCDClear(void)
  182. {
  183. WriteCommandLCD(0x01);
  184. delay(2);
  185. }

  186. /**********发送内容*********************************/
  187. void LCDSendWord(unsigned char *p)
  188. {
  189. while(*p>0)
  190. {
  191. WriteDataLCD(*p);
  192. p++;
  193. }
  194. }
  195. /*********发送地址*********************************/
  196. void LCDTestWord(bit i,unsigned char word)
  197. {
  198. if(i==0)
  199. {
  200. WriteCommandLCD(word);
  201. }
  202. else
  203. {
  204.   WriteDataLCD(word);
  205.   }
  206. }
  207. /***************LCD显示年************/
  208. void DisplayYear(void)
  209. {
  210. year=ReadDs1302(0x8d);
  211. LCDTestWord(0,0x80);
  212. LCDSendWord("20");
  213. LCDTestWord(1,(year/16)+0x30);
  214. LCDTestWord(1,year%16+0x30);
  215. LCDTestWord(1,'/');
  216. }
  217. /***************LCD显示月*************************/
  218. void DisplayMonth(void)
  219. {
  220. month=ReadDs1302(0x89);
  221. LCDTestWord(1,(month/16)+0x30);
  222. LCDTestWord(1,month%16+0x30);
  223. LCDTestWord(1,'/');
  224. }
  225. /****************LCD显示日**************************/
  226. void DisplayDay(void)
  227. {
  228. day=ReadDs1302(0x87);
  229. LCDTestWord(1,(day/16)+0x30);
  230. LCDTestWord(1,day%16+0x30);
  231. }
  232. /**********LCD显示时*********************************/
  233. void DisplayHour(void)
  234. {
  235. hour=ReadDs1302(0x85);
  236. LCDTestWord(0,0xc0);
  237. LCDTestWord(1,(hour/16)+0x30);
  238. LCDTestWord(1,hour%16+0x30);
  239. LCDTestWord(1,':');
  240. }
  241. /**********LCD显示分**********************************/
  242. void DisplayMin(void)
  243. {
  244. min=ReadDs1302(0x83);
  245. LCDTestWord(1,(min/16)+0x30);
  246. LCDTestWord(1,min%16+0x30);
  247. LCDTestWord(1,':');
  248. }
  249. /**********LCD显示秒**********************************/
  250. void DisplaySec(void)
  251. {
  252. sec=ReadDs1302(0x81);
  253. LCDTestWord(1,(sec/16)+0x30);
  254. LCDTestWord(1,sec%16+0x30);
  255. }
  256. /************************************************************/
  257. void UpDate1(void)
  258. {
  259. DisplayHour();
  260. DisplayMin();
  261. DisplaySec();
  262. }
  263. /************************************************************/
  264. void UpDate2(void)
  265. {
  266. DisplayYear();
  267. DisplayMonth();
  268. DisplayDay();
  269. }
  270. /*************键扫描程序**************/
  271. unsigned char keys(void)
  272. {
  273. unsigned char tmp;                       
  274. if((P1&0xff)!=0xff)   
  275. {
  276. delay(10);         
  277. if((P1&0xff)!=0xff)         
  278.    {
  279.         tmp=(P1&0xff);
  280.     do{}while((P1&0xff)!=0xff);
  281.         tmp=~tmp;                  
  282.     return (tmp);                         
  283.         }                        
  284. }                                                                                                                       
  285. return (0);                                
  286. }
  287. void baojing(void)
  288. {
  289. if(P01==0)
  290. {                        
  291. LCDTestWord(0,0x80);
  292. LCDSendWord("LED1 Broken");
  293. P07=0;
  294. }
  295. else
  296. {
  297. LCDTestWord(0,0x80);
  298. LCDSendWord("LED1 OK    ");
  299. }
  300. if(P02==0)
  301. {
  302. LCDTestWord(0,0xc0);
  303. LCDSendWord("LED2 Broken");
  304. P07=0;
  305. }
  306. else
  307. {
  308.   LCDTestWord(0,0xc0);
  309.   LCDSendWord("LED2 OK    ");               
  310.   }
  311. if(P01==1&&P02==1)
  312.   P07=1;
  313. }
  314.                                                   
  315. void huanjing(void)
  316. {
  317. while(v!=4)
  318. {
  319.   if(P00==1)
  320.    {
  321.            P36=1;
  322.         P37=1;
  323.     }
  324.    if(P00==0)
  325.     {
  326.             P36=0;
  327.          P37=0;
  328.      }
  329.      baojing();
  330.      v=keys();
  331. }
  332. }         

  333. void jiaotong(void)
  334. {  
  335. while(v!=4)
  336. {
  337. SC=0;
  338. SB=0;       
  339. if(P03==0)
  340. {
  341. P36=0;
  342. P37=1;
  343. while(1)
  344.   {
  345.    if(P04==0)
  346.     {
  347.      P36=1;
  348.      P37=0;
  349.      while(1)
  350.        {
  351.              if(P05==0)
  352.              {                  
  353.                P36=1;
  354.                P37=1;
  355.                SC=1;
  356.                SB=1;
  357.                 while(P05==0);
  358.                 }
  359.                 if(SC==1)break;
  360.           }
  361.    }
  362. if(SB==1)break;
  363. }
  364. }
  365. if(P05==0)
  366. {
  367. P36=1;
  368. P37=0;
  369. while(1)
  370. {
  371.   if(P04==0)
  372.   {
  373.     P36=0;
  374.     P37=1;
  375.     while(1)
  376.      {
  377.      if(P03==0)
  378.       {                          
  379.         P36=1;
  380.         P37=1;
  381.         SC=1;
  382.         SB=1;
  383.        while(P03==0);
  384.       }
  385. if(SC==1)break;
  386.     }
  387. }
  388. if(SB==1)break;
  389. }
  390. }
  391. v=keys();
  392. }                 
  393. }
  394. void Displayrealtime(void)
  395. {
  396. while(v!=4)
  397. {
  398. UpDate2();
  399. UpDate1();
  400.   v=keys();
  401. }
  402. }
  403. void Display1(void)
  404. {
  405. LCDTestWord(0,0x80);
  406. LCDSendWord("LED1 ON ");
  407. LCDTestWord(1,(Led1onH/10)+0x30);
  408. LCDTestWord(1,(Led1onH%10)+0x30);
  409. LCDTestWord(1,':');
  410. LCDTestWord(1,(Led1onM/10)+0x30);
  411. LCDTestWord(1,(Led1onM%10)+0x30);
  412. LCDTestWord(0,0xc0);
  413. LCDSendWord("LED1 OFF ");
  414. LCDTestWord(1,(Led1offH/10)+0x30);
  415. LCDTestWord(1,(Led1offH%10)+0x30);
  416. LCDTestWord(1,':');
  417. LCDTestWord(1,(Led1offM/10)+0x30);
  418. LCDTestWord(1,(Led1offM%10)+0x30);
  419. }
  420. void Display2(void)
  421. {
  422. LCDTestWord(0,0x80);
  423. LCDSendWord("LED2 ON ");
  424. LCDTestWord(1,(Led2onH/10)+0x30);
  425. LCDTestWord(1,(Led2onH%10)+0x30);
  426. LCDTestWord(1,':');
  427. LCDTestWord(1,(Led2onM/10)+0x30);
  428. LCDTestWord(1,(Led2onM%10)+0x30);
  429. LCDTestWord(0,0xc0);
  430. LCDSendWord("LED2 OFF ");
  431. LCDTestWord(1,(Led2offH/10)+0x30);
  432. LCDTestWord(1,(Led2offH%10)+0x30);
  433. LCDTestWord(1,':');
  434. LCDTestWord(1,(Led2offM/10)+0x30);
  435. LCDTestWord(1,(Led2offM%10)+0x30);
  436. }
  437. void settime(void)
  438. {
  439. LCDClear();
  440. LCDTestWord(0,0x80);
  441. LCDSendWord("Please Set Time");
  442. delay(2000);
  443. LCDClear();
  444. v=keys();
  445. while(v!=4)
  446. {
  447. switch(v)
  448. {       
  449.         case 0: UpDate2();
  450.             UpDate1();
  451.                 v=keys();
  452.                 break;
  453.     case 2: LCDClear();
  454.                     LCDTestWord(0,0x80);
  455.                         LCDSendWord("Set Succes");
  456.                         delay(1000);
  457.                         LCDClear();
  458.                     while(v==0|v==2)
  459.                          {
  460.                UpDate2();
  461.                UpDate1();       
  462.                Hour1=hour/16*10+hour%16;
  463.                Minite1=min/16*10+min%16;                                                       
  464.                       if(Hour1==Led1onH&&Minite1==Led1onM)
  465.                             P36=0;
  466.                       if(Hour1==Led2onH&&Minite1==Led2onM)
  467.                             P37=0;
  468.                       if(Hour1==Led1offH&&Minite1==Led1offM)
  469.                             P36=1;
  470.                   if(Hour1==Led2offH&&Minite1==Led2offM)
  471.                             P37=1;                               
  472.                            v=keys();
  473.                           }
  474.                      break;
  475. case 8: LCDClear();
  476.                 while(v==0|v==8|v==32|v==64|v==128)
  477.               {
  478.                                 Display1();
  479.                                 v=keys();
  480.                                 switch(v)
  481.                            {
  482.                            case 32: count1++;
  483.                                     if(count1==5)
  484.                                       count1=1;
  485.                                       break;
  486.                case 64: if(count1>0&&count1<5)
  487.                                                 {
  488.                                              if(count1==1)
  489.                                       {
  490.                                        Led1onH++;
  491.                                        if(Led1onH==24)
  492.                                            Led1onH=0;
  493.                                       }
  494.                                      if(count1==2)
  495.                                       {
  496.                                       Led1onM++;
  497.                                       if(Led1onM==60)                                                                                                                          
  498.                                                    Led1onM=0;
  499.                                       }
  500.                                      if(count1==3)
  501.                                        {
  502.                                         Led1offH++;
  503.                                         if(Led1offH==24)
  504.                                         Led1offH=0;
  505.                                            }
  506.                                        if(count1==4)
  507.                                            {
  508.                                         Led1offM++;
  509.                                         if(Led1offM==60)
  510.                                        Led1offM=0;
  511.                                         }                                                                                       
  512.                                        }
  513.                                        break;
  514.               case 128: if(count1>0&&count1<5)
  515.                                 {
  516.                                      if(count1==1)
  517.                                       {                                                                                           
  518.                                 Led1onH--;                                                                        
  519.                                 if(Led1onH<0)
  520.                                      Led1onH=23;
  521.                                }
  522.                                   if(count1==2)
  523.                                    {                                                                                               
  524.                                  Led1onM--;
  525.                                        if(Led1onM<0)
  526.                                      Led1onM=59;
  527.                                    }
  528.                                  if(count1==3)
  529.                                   {                                                                                                                                                       
  530.                                                    Led1offH--;                                                                               
  531.                                    if(Led1offH<0)
  532.                             Led1offH=23;
  533.                           }
  534.                       if(count1==4)
  535.                                 {
  536.                                  Led1offM--;
  537.                                  if(Led1offM<0)
  538.                                   Led1offM=59;
  539.                                 }                                                                                               
  540.                                }
  541.                                 break;                                                                                                                       
  542.                                         }
  543.             }
  544.                               break;
  545. case 16: LCDClear();
  546.                 while(v==0|v==16|v==32|v==64|v==128)
  547.           {
  548.                    Display2();
  549.                    v=keys();
  550.                    switch(v)
  551.                    {
  552.                       case 32: count1++;
  553.                                    if(count1==5)
  554.                                         count1=1;
  555.                                        break;
  556.                      case 64: if(count1>0&&count1<5)
  557.                                   {
  558.                                        if(count1==1)
  559.                                          {
  560.                                            Led2onH++;
  561.                                            if(Led2onH==24)
  562.                                             Led2onH=0;
  563.                                          }
  564.                                           if(count1==2)
  565.                                          {
  566.                                           Led2onM++;
  567.                                           if(Led2onM==60)
  568.                                            Led2onM=0;
  569.                                          }
  570.                                           if(count1==3)
  571.                                          {
  572.                                           Led2offH++;
  573.                                           if(Led2offH==24)
  574.                                            Led2offH=0;
  575.                                          }
  576.                                          if(count1==4)
  577.                                          {
  578.                                           Led2offM++;
  579.                                           if(Led2offM==60)
  580.                                        Led2offM=0;
  581.                                          }
  582.                                       }
  583.                                          break;
  584.           case 128: if(count1>0&&count1<5)
  585.                                      {
  586.                                      if(count1==1)
  587.                                       {
  588.                                        Led2onH--;
  589.                                        if(Led2onH<0)
  590.                                         Led2onH=23;
  591.                                       }
  592.                                      if(count1==2)
  593.                                       {
  594.                                        Led2onM--;
  595.                                        if(Led2onM<0)
  596.                                         Led2onM=59;
  597.                                       }
  598.                                       if(count1==3)
  599.                                        {
  600.                                          Led2offH--;
  601.                                          if(Led2offH<0)
  602.                                           Led2offH=23;
  603.                                        }
  604.                                        if(count1==4)
  605.                                       {                                                                                                                                                          
  606.                                            Led2offM--;
  607.                                    if(Led2offM<0)
  608.                                     Led2offM=59;
  609.                                   }                                                                                                                                                         
  610.                                    }
  611.                      break;                                                                                               
  612.            }
  613.       }
  614. break;                                                                 
  615. }
  616. }
  617. }
  618. /*****************主函数程序*************************/
  619. void main(void)
  620. {
  621. LCDInit();
  622. LCDClear();
  623. InitDS1302();
  624. LCDTestWord(0,0x80);
  625. LCDSendWord("Entering system");
  626. LCDTestWord(0,0xc0);
  627. LCDSendWord("Please Waiting");
  628. delay(2000);
  629. LCDClear();
  630. LCDTestWord(0,0x80);
  631. LCDSendWord("Select Menu");
  632. while(1)
  633.   {               
  634. v=keys();                       
  635.         switch(v)
  636.                         {                       
  637.                                  case 1:          count++;
  638.                                                                 if(count==5)
  639.                                      count=1;
  640.                                                                 LCDTestWord(0,0x80);       
  641.                                                                 if(count==1)
  642.                                                                  {
  643.                                  LCDClear();                                                               
  644.                                                                  LCDSendWord("Realtime");
  645.                                                                   }
  646.                                                                 if(count==2)
  647.                                   {  
  648.                                                                         LCDClear();                                                       
  649.                                                                         LCDSendWord("Environment");
  650.                                                                   }
  651.                                                                 if(count==3)
  652.                                                                  {
  653.                                    LCDClear();                               
  654.                                                                    LCDSendWord("Traffic");
  655.                                                                  }       
  656.                                                                 if(count==4)
  657.                                                                  {  
  658.                                    LCDClear();                                                                                                                                  
  659.                                                                    LCDSendWord("Set Time");
  660.                                                                  }       
  661.                                                                 break;
  662.                                 case 2:              if(0<count&&count<5)
  663.                                                                 {       
  664.                                                                   if(count==1)
  665.                                                                         {  
  666.                                                                            P36=1;
  667.                                                                           P37=1;
  668.                                                                   Displayrealtime();                                       
  669.                                                                   LCDClear();
  670.                                                                           LCDTestWord(0,0x80);
  671.                                                                           LCDSendWord("Select Menu");
  672.                                                                           break;
  673.                                                                          }       
  674.                                                                 if(count==2)
  675.                                                                   {
  676.                                     P36=1;
  677.                                                                         P37=1;                                                                                                                                
  678.                                                                         huanjing();
  679.                                     LCDClear();
  680.                                                                     LCDTestWord(0,0x80);
  681.                                                                     LCDSendWord("Select Menu");
  682.                                                                     break;
  683.                                                                         }
  684.                                                                 if(count==3)
  685.                                                                  {
  686.                                     P36=1;
  687.                                                                         P37=1;
  688.                                                                 jiaotong();
  689.                                                                         LCDClear();
  690.                                                                         LCDTestWord(0,0x80);
  691.                                                                         LCDSendWord("Select Menu");
  692.                                                                 break;
  693.                                                                   }
  694.                                                                 if(count==4)
  695.                                                                   {
  696.                                     P36=1;
  697.                                                                         P37=1;
  698.                                                                 settime();
  699.                                                                         LCDClear();
  700.                                                                         LCDTestWord(0,0x80);
  701.                                                                         LCDSendWord("Select Menu");
  702.                                                                 break;
  703.                                                                    }
  704.                                                             }
  705.                                                             break;        
  706.                           case 64:             m++;      
  707.                                  if(m%2==0)
  708.                                                                     P36=1;
  709.                                                                    else
  710.                                                                          P36=0;
  711.                                                                   break;
  712.                           case 128:             n++;      
  713.                                    if(n%2==0)
  714.                                                                          P37=1;
  715.                                                                            else
  716.                                                                                  P37=0;
  717.                                                                    break;
  718.                                 }
  719. }   
  720.                                                           }
  721.                                                           
复制代码

所有资料51hei提供下载:
模拟路灯系统.zip (48.87 KB, 下载次数: 79)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:560167 发表于 2019-6-22 19:09 | 显示全部楼层
很好的资源哦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表