找回密码
 立即注册

QQ登录

只需一步,快速开始

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

左右转向交通灯仿真及源程序

[复制链接]
跳转到指定楼层
楼主
这是带左右转向的交通灯,采用每一个方向9个二极管灯的设计。这是根据设计任务做的。代码 仿真都是自己做的。
设计内容:1、东西干道和南北干道的通行分左行、右行、直行,其中左行、右行固定15秒;直行固定30
                        2、信号灯分绿灯(3种)、红灯、黄灯,每次绿灯换红灯时,黄灯亮3秒钟
                        3、东西干道和南北干道交替控制,每次干道绿灯交替时,有3秒钟所有干道的交通灯都是黄灯闪烁3秒钟,提示已经进入路口的车辆迅速通过



  1. #include<reg52.h>
  2. #define  uchar unsigned char
  3. #define  uint unsigned int
  4. sbit wei1=P3^0;
  5. sbit wei2=P3^1;
  6. sbit num1=P3^7;
  7. sbit e2=P3^4;
  8. sbit f2=P3^5;
  9. uchar num,num2,num3,flag;
  10. uchar code table[]=
  11. {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  12. void delayms(uint x)
  13. {
  14.         uint i,j;
  15.         for(i=x;i>0;i--)
  16.                 for(j=110;j>0;j--);
  17. }
  18. void lamp()
  19. {        if(num1==0)
  20.   {
  21.         if(num3<=13)
  22.         {
  23.           P0=0x81;
  24.           P2=0x0f;
  25.         }
  26.         if((num3>13)&&(num3<=15))
  27.         {
  28.           P0=0x80;
  29.           P2=0x0f;
  30.           delayms(400);
  31.           P2=0x1f;
  32.           delayms(400);
  33.         }
  34.         if((num3>15)&&(num3<=27))
  35.         {
  36.           P0=0x46;
  37.           P2=0x0e;
  38.         }
  39.         if((num3>27)&&(num3<=30))
  40.         {
  41.           P0=0X42;
  42.           P2=0X4e;
  43.           delayms(400);
  44.           P2=0X0e;
  45.           delayms(400);       
  46.         }
  47.         if((num3>30)&&(num3<=42))
  48.         {
  49.           P0=0x42;
  50.           P2=0x0f;
  51.         }
  52.         if(num3>42)
  53.         {
  54.           P0=0x00;
  55.           P2=0xf0;
  56.           e2=1;
  57.           f2=1;
  58.           delayms(400);
  59.           P2=0x00;
  60.           e2=0;
  61.           f2=0;
  62.           delayms(400);
  63.         }
  64.   }
  65. else
  66. {
  67.         if(num3<=13)
  68.         {
  69.           P0=0xc8;
  70.           P2=0x0d;
  71.         }
  72.         if((num3>13)&&(num3<=15))
  73.         {
  74.           P0=0xc0;
  75.           P2=0x8d;
  76.           delayms(400);
  77.           P2=0x0d;
  78.           delayms(400);
  79.         }
  80.         if((num3>15)&&(num3<=27))
  81.         {
  82.           P0=0xf0;
  83.           P2=0x03;
  84.         }
  85.         if((num3>27)&&(num3<=30))
  86.         {
  87.           P0=0Xd0;
  88.           P2=0X03;
  89.           f2=1;
  90.           delayms(400);
  91.           f2=0;
  92.           delayms(400);       
  93.         }
  94.         if((num3>30)&&(num3<=42))
  95.         {
  96.           P0=0xd0;
  97.           P2=0x0b;
  98.         }
  99.         if(num3>42)
  100.         {
  101.           P0=0x00;
  102.           P2=0xf0;
  103.           e2=1;
  104.           f2=1;
  105.           delayms(400);
  106.           P2=0x00;
  107.           e2=0;
  108.           f2=0;
  109.           delayms(400);
  110.         }
  111. }
  112. }
  113. void xs()
  114. {
  115.         P1=table[num/10];
  116.         wei1=1;
  117.         delayms(10);
  118.         wei1=0;
  119.         P1=table[num%10];
  120.         wei2=1;
  121.         delayms(10);
  122.         wei2=0;
  123.    
  124. }
  125. void main()
  126. {
  127.         P0=0x00;
  128.         num1=0;num3=0;
  129.         P1=P2=P3=0x00;
  130.         TMOD=0x01;
  131.         TH0=(65536-45872)/256;
  132.         TL0=(65536-45872)%256;
  133.         EA=1;
  134.         ET0=1;
  135.         TR0=1;
  136.         while(1)
  137.         {
  138.         lamp();
  139.         xs();
  140.         }
  141. }
  142. void T0_time() interrupt 1
  143. {
  144.         TH0=(65536-45872)/256;
  145.         TL0=(65536-45872)%256;
  146.         num2++;
  147.         if(num2==20)
  148.         { num2=0;
  149.         num3++;
  150.         if(num3==45)
  151.         {       
  152.         num3=0;
  153.         num1=~num1;
  154.         }
  155.         num=45-num3;
  156.         }
  157. }
复制代码



这是用proteus的仿真图 三刀.rar (53.7 KB, 下载次数: 25)
这是用kellc编写的程序 程序.rar (12.85 KB, 下载次数: 25) 。初次发帖请各位大神关照。


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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