找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3989|回复: 1
打印 上一主题 下一主题
收起左侧

单片机智能交通灯仿真设计(带键盘手动调节功能)

[复制链接]
跳转到指定楼层
楼主
智能交通灯的proteus仿真图分享给大伙
1.倒计时到10秒后,黄灯警告,准备交换亮红灯。
2..数码管32和28秒倒计时功能.
3.增加键盘手动调节功能
A:设置键:按一次后,数码管停止倒计数。按三次设置键恢复正常。
B:向上键:每按一次实现加一秒,没有限制秒。
C:向下键:每接一次向下键,实现数码管减一,减到0时恢复到32秒。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)



单片机源程序如下:
  1. #include <reg51.h>
  2. #include <stdio.h>
  3. #define uchar     unsigned char
  4. #define uint      unsigned int
  5. sbit key1=P3^2;
  6. sbit key2=P3^3;
  7. sbit key3=P3^4;
  8. sbit key4=P3^5;

  9. uchar h,count,num,dat,flag,dat1,num1,key1num,temp;
  10. uint r=0;
  11. uint s;
  12. uchar code  dis[]={
  13.                                                        
  14.         0Xc0,/*0*/
  15.         0Xf9,/*1*/
  16.         0Xa4,/*2*/
  17.         0Xb0,/*3*/
  18.         0X99,/*4*/
  19.         0X92,/*5*/
  20.         0X82,/*6*/
  21.         0Xf8,/*7*/
  22.         0X80,/*8*/
  23.         0X90,/*9*/
  24.         0Xff,/*NULL*/
  25. };


  26. void delay(uint c)
  27. {
  28.    uint i,j;
  29.     for(i=0;i<c;i++)
  30.           for(j=0;j<100;j++);
  31.        
  32. }

  33. void init()
  34. {
  35.         TMOD=0x11;//设置定时器0为工作方式1
  36.         TH0=(65536-10000)/256;
  37.         TL0=(65536-10000)%256;
  38.         TH1=(65536-20000)/256;
  39.         TL1=(65536-20000)%256;
  40.         EA=1;//开总中断
  41.         ET0=1;//开定时器0中断
  42.         ET1=1;
  43.         TR0=1;//启动定时器0
  44.         TR1=1;
  45.         num=66;
  46. //        P0=0xeb;
  47. }
  48. void display(dat)   //送显示
  49. {                       
  50.                        
  51.                    if(dat<=33)
  52.                    {
  53.                            if(dat>3&&dat<33)
  54.                         {
  55.                                    P0=0xdd;//红灯
  56.                                 P1=0x00;
  57.                         P2=0xff;
  58.                         P1=0x01;
  59.                         P2=dis[dat/10];
  60.                          delay(1);
  61.                                 P1=0x00;
  62.                         P2=0xff;
  63.                         P1=0x02;
  64.                         P2=dis[dat%10];
  65.                                  delay(1);
  66.                                 P1=0x00;
  67.                         P2=0xff;
  68.                                 P1=0x04;
  69.                         P2=dis[(dat-3)/10];
  70.                                  delay(1);
  71.                                 P1=0x00;
  72.                         P2=0xff;
  73.                                 P1=0x08;
  74.                         P2=dis[(dat-3)%10];
  75.                          delay(1);
  76.                           }
  77.                          if(dat<=3)
  78.                          {
  79.                                          P0=0xd4;//黄灯
  80.                                          P1=0x00;
  81.                                 P2=0xff;
  82.                                 P1=0x02;
  83.                                 P2=dis[dat%10];
  84.                                          delay(1);

  85.                                         P1=0x00;
  86.                                     P2=0xff;
  87.                                     P1=0x08;
  88.                                     P2=dis[(dat-3)%10];
  89.                                     delay(1);
  90.                                        
  91.                                                
  92.                          }
  93. //                         break;
  94.                         }

  95.                         if(dat>=33&&dat<=66)
  96.                         {
  97.                         if(dat>36&&dat<=66)
  98.                         {
  99.                                 P0=0xeb;
  100.                                                 P1=0x00;
  101.                         P2=0xff;
  102.                         P1=0x01;
  103.                         P2=dis[(dat-33)/10];
  104.                          delay(1);
  105.                                 P1=0x00;
  106.                         P2=0xff;
  107.                         P1=0x02;
  108.                         P2=dis[(dat-33)%10];
  109.                                  delay(1);
  110.                                 P1=0x00;
  111.                         P2=0xff;
  112.                                 P1=0x04;
  113.                         P2=dis[(dat-36)/10];
  114.                                  delay(1);
  115.                                 P1=0x00;
  116.                         P2=0xff;
  117.                                 P1=0x08;
  118.                         P2=dis[(dat-36)%10];
  119.                          delay(1);       
  120.                          }
  121.                          if(dat>=33&&dat<=36)
  122.                          {
  123.                                          P0=0xe2;//黄灯
  124.                                          P1=0x00;
  125.                                 P2=0xff;
  126.                                 P1=0x02;
  127.                                 P2=dis[(dat-33)%10];
  128.                                          delay(1);          

  129.                                         P1=0x00;
  130.                                     P2=0xff;
  131.                                     P1=0x08;
  132.                                     P2=dis[(dat-33)%10];
  133.                                     delay(1);          
  134.                          }
  135. //                         break;
  136.                         }
  137.                          
  138. }
  139. void keyscan() //键盘检测
  140. {
  141.         if(key1==0)
  142.         {
  143.                 delay(5);
  144.                 if(key1==0)
  145.                 {
  146.                         key1num++;
  147.                         while(!key1)
  148.                         {
  149.                                 display(num);
  150.                         }
  151.                         if(key1num==1)
  152.                         {
  153.                                 TR0=0;
  154.                                 TR1=0;
  155.                         }
  156.                         if(key1num==2)
  157.                         {
  158.                                 TR0=1;
  159.                                 TR1=1;
  160.                                 P1=temp;
  161.                                 key1num=0;

  162.                         }
  163.                 }
  164.        
  165.         }
  166.                 if(key1num!=0)
  167.                 {       
  168.                         if(key2==0)
  169.                         {
  170.                                 delay(5);
  171.                                 if(key2==0)
  172.                                 {
  173.                                         while(!key2)
  174.                                         {
  175.                                                 display(num);
  176.                                         }
  177.                                         if(key1num==1)
  178.                                         {
  179.                                                 num++;
  180.                                                 display(num);
  181.                                                
  182.                                         }
  183.                                         if(key1num==2)
  184.                                         {
  185.                                                 num++;
  186.                                                 display(num);
  187.                                         }
  188.                                 }
  189.                         }
  190.                       if(key3==0)
  191.                         {
  192.                                 delay(5);
  193.                                 if(key3==0)
  194.                                 {
  195.                                         while(!key3)
  196.                                         {
  197.                                                 display(num);
  198.                                         }
  199.                                        
  200.                                                 if(key1num==1)
  201.                                                 {
  202.                                                         if(num==0)
  203.                                                         num=32;
  204.                                                         num--;
  205.                                                 }
  206.                                                 if(key1num==2)
  207.                                                 {
  208.                                                         if(num1==0)
  209.                                                         num1=56;
  210.                                                         num1--;
  211.                                                 }
  212.                                        

  213.                                 }
  214.                         }
  215.                 }
  216. }
  217. void main()  //主函数
  218. {
  219.         init();
  220.         while(1)
  221.         {       
  222.                 keyscan();
  223. ……………………

  224. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
c40b0a76b361af601ed40d66db712331.rar (144.53 KB, 下载次数: 38)



评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2017-6-20 00:10 | 只看该作者
好资料,51黑有你更精彩!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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