找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机交通灯设计 原理图与代码

[复制链接]
跳转到指定楼层
楼主
proteus交通信号灯仿真原理图

  吉首大学单片机交通灯设计
原理图
局部放大

单片机源程序如下:
  1. <font color="rgb(0, 0, 0)">#include<reg51.H>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. uchar code table[]={     //共阴极数码管码表
  5. 0x3f,0x06,0x5b,0x4f,
  6. 0x66,0x6d,0x7d,0x07,
  7. 0x7f,0x6f,0x77,0x7c,
  8. 0x39,0x5e,0x79,0x71,

  9. 0xC9,0xFF,0x40};//设置码,测试码,不计时码

  10. void delay(uint x);//延时函数
  11. void display(uchar,uchar,uchar,uchar);    //数码管显示函数
  12. void mkeys();    //键盘函数
  13. void traffic();    //交通灯函数

  14. uchar num,num1,num2,  //1南北 2东西
  15. shi1,ge1,shi2,ge2,
  16. value1,value2,//南北 绿灯时间 黄灯时间
  17. value3,value4,//东西 绿灯时间 黄灯时间
  18. count1,count2,flag1,flag2; //南北标记 东西标记

  19. void main()
  20. {   
  21.         
  22.         TMOD=0x01;
  23.         TH0=(65536-45872)/256;
  24.         TL0=(65536-45872)%256;
  25.         EA=1;
  26.         ET0=1;
  27.         TR0=1;
  28.         /*初状态*/
  29.         value1=15;         //南北 黄绿灯默认值
  30.         value2=5;      
  31.         value3=10;      //东西 黄绿灯默认值
  32.         value4=5;

  33.         num1=value1; //南北数码管先绿灯时间
  34.         num2=value2+value1;//东西红灯时间
  35.         shi1=num1/10;
  36.         ge1=num1%10;
  37.         shi2=num2/10;
  38.         ge2=num2%10;
  39.         P1=0x41;//初始状态:东西红灯 南北绿灯      20 15

  40.         while(1){
  41.             if(num==20)       //定时器1s
  42.             {
  43.                 num=0;
  44.                 num1--;
  45.                 num2--;
  46.                 traffic();
  47.                     
  48.                 shi1=num1/10;
  49.                 ge1=num1%10;

  50.                 shi2=num2/10;
  51.                 ge2=num2%10;

  52.             }
  53.               mkeys();
  54.                   display(shi1,ge1,shi2,ge2);
  55.               
  56.         }

  57. }

  58. void traffic() //红绿灯主控制程序
  59. {
  60.     if(num1==0){
  61.             count1++;
  62.             if(count1==1){
  63.                 P1=0x42;//东西红灯 南北黄灯    5 5
  64.                 num1=value2;
  65.             }
  66.             if(count1==2){
  67.                 num1=value3+value4;//东西绿灯 南北红灯    10 15
  68.                 P1=0x14;
  69.             }
  70.             if(count1==3){
  71.                 P1=0x41;// 东西黄灯 南北红灯    5 5
  72.                 num1=value4;
  73.                 count1=0;                           
  74.             }
  75.     }
  76.     if(num2==0){
  77.             count2++;
  78.             if(count2==1){
  79.                 //P1=0x14;//东西绿灯 南北红灯
  80.                 num2=value3;
  81.             }
  82.             if(count2==2){
  83.                 P1=0x24;//东西黄灯 南北红灯
  84.                 num2=value4;
  85.             }
  86.             if(count2==3){
  87.                 num2=value1+value2; //东西红灯 南北绿灯
  88.                 num1=value1;
  89.                 count2=0;
  90.             }
  91.                     
  92.     }
  93. }

  94. void display(uchar shi1,uchar ge1,uchar shi2,uchar ge2)    //数码管显示子函数
  95. {        
  96.         uchar temp;
  97.         temp=P2;
  98.         P2=0xfe;
  99.         P0=table[shi1];
  100.         delay(5);
  101.         
  102.         P2=0xfd;
  103.         P0=table[ge1];
  104.         delay(5);
  105.    
  106.         P2=0xfb;
  107.         P0=table[shi2];
  108.         delay(5);
  109.         
  110.         P2=0xf7;
  111.         P0=table[ge2];
  112.         delay(5);        
  113. }

  114. void delay(uint x)//延时子函数
  115. {
  116.     uint i,j;
  117.     for(i=x;i>0;i--)
  118.         for(j=110;j>0;j--);
  119. }

  120. void mkeys()  //4*4矩阵键盘功能子函数
  121. {
  122.     uchar temp,key;
  123.     P3=0xfe;//第一行线
  124.     temp=P3;
  125.     temp=temp&0xf0;
  126.     if(temp!=0xf0)
  127.     {
  128.         delay(10);
  129.         temp=P3;
  130.         temp=temp&0xf0;
  131.         if(temp!=0xf0){
  132.             temp=P3;
  133.             switch(temp)
  134.             {
  135.                 case 0xee:
  136.                     key=0;
  137.                     break;
  138.                 case 0xde:
  139.                     key=1;
  140.                     break;
  141.                 case 0xbe:
  142.                     key=2;
  143.                     break;
  144.                 case 0x7e:
  145.                     key=3;
  146.                     break;
  147.             }
  148.         while(temp!=0xf0)
  149.         {
  150.             temp=P3;
  151.             temp=temp&0xf0;
  152.         }
  153.         if(key==0) {//按键1:暂停
  154.             TR0=~TR0;  //定时器取反
  155.             flag1=~flag1;//南北能够设置标志 0有效
  156.             flag2=~flag2;//东西能够设置标志
  157.         }

  158.         if(key==1&&flag1==0){    //按键2:设置时间按钮
  159.             TR0=0;
  160.             P1=0x44;//禁止东南西北车辆 全为红灯 可以设置
  161.             shi1=ge1=shi2=ge2=16;
  162.         }

  163.         if(key==2&&flag2==0){//按键3:设置完成 重启
  164.             TR0=1;
  165.             num=0;     //定时器 初始化
  166.             P1=0x41; //重新开始初状态
  167.             num1=value1; //南北数码管先绿灯时间
  168.             num2=value2+value1;//东西红灯时间
  169.             shi1=num1/10;
  170.             ge1=num1%10;
  171.             shi2=num2/10;
  172.             ge2=num2%10;
  173.         }

  174.         if(key==3&&P1==0x44){ //按键4:测试交通灯各个设备的好坏
  175.             P1=0xff;
  176.             delay(1000);
  177.             P1=~P1;
  178.             shi1=ge1=shi2=ge2=17;
  179.             P1=0x44;
  180.         }

  181.         }
  182.     }

  183.     P3=0xfd;//第二行线
  184.     temp=P3;
  185.     temp=temp&0xf0;
  186.     if(temp!=0xf0)
  187.     {
  188.         delay(10);
  189.         temp=P3;
  190.         temp=temp&0xf0;
  191.         if(temp!=0xf0){
  192.             temp=P3;
  193.             switch(temp)
  194.             {
  195.                 case 0xed:
  196.                     key=0;
  197.                     break;
  198.                 case 0xdd:
  199.                     key=1;
  200.                     break;
  201.                 case 0xbd:
  202.                     key=2;
  203.                     break;
  204.                 case 0x7d:
  205.                     key=3;
  206.                     break;
  207.             }
  208.         while(temp!=0xf0)
  209.         {
  210.             temp=P3;
  211.             temp=temp&0xf0;
  212.         }
  213.          
  214.         if(key==0&&P1==0x44){    //按键5:设置南北绿灯时间+

  215.             num1=value1;
  216.             if(num2!=159){ //@@@@保证交通合理,红灯最大值计时159s,绿灯不再增加
  217.                 num1++;
  218.                 value1=num1;
  219.             }

  220.             shi1=num1/10;
  221.             ge1=num1%10;

  222.             num2=value1+value2;//显示东西红灯时间
  223.             shi2=num2/10;
  224.             ge2=num2%10;


  225.         }
  226.         if(key==1&&P1==0x44){    //按键6:设置南北黄灯时间+
  227.             num1=value2;
  228.             if(num2!=159){
  229.                 num1++;
  230.                 value2=num1;
  231.             }

  232.             shi1=num1/10;
  233.             ge1=num1%10;
  234.             num2=value1+value2;//显示东西红灯时间
  235.             shi2=num2/10;
  236.             ge2=num2%10;

  237.         }
  238.         if(key==2&&P1==0x44&&value1>3){    //按键7:设置南北绿灯时间- @@@@保证交通合理,绿灯最小值计时3s,绿灯不再减少
  239.             num1=value1;

  240.             num1--;
  241.             value1=num1;

  242.             shi1=num1/10;
  243.             ge1=num1%10;
  244.             num2=value1+value2;//显示东西红灯时间
  245.             shi2=num2/10;
  246.             ge2=num2%10;

  247.         }
  248.         if(key==3&&P1==0x44&&value2>3){    //按键8:设置南北黄灯时间-      
  249.             num1=value2;

  250.             num1--;
  251.             value2=num1;

  252.             shi1=num1/10;
  253.             ge1=num1%10;
  254.             num2=value1+value2;//显示东西红灯时间
  255.             shi2=num2/10;
  256.             ge2=num2%10;

  257.         }
  258.         }
  259.    
  260.     }
  261.    ////||||||||||||||||||

  262.     P3=0xfb;//第三行线
  263.     temp=P3;
  264.     temp=temp&0xf0;
  265.     if(temp!=0xf0)
  266.     {
  267.         delay(10);
  268.         temp=P3;
  269.         temp=temp&0xf0;
  270.         if(temp!=0xf0){
  271.             temp=P3;
  272.             switch(temp)
  273.             {
  274.                 case 0xeb:
  275.                     key=0;
  276.                     break;
  277.                 case 0xdb:
  278.                     key=1;
  279.                     break;
  280.                 case 0xbb:
  281.                     key=2;
  282.                     break;
  283.                 case 0x7b:
  284.                     key=3;
  285.                     break;
  286.             }
  287.         while(temp!=0xf0)
  288.         {
  289.             temp=P3;
  290.             temp=temp&0xf0;
  291.         }
  292.         if(key==0&&P1==0x44){    //按键9:设置东西绿灯时间+
  293.    
  294.             num2=value3;
  295.             if(num1!=159){
  296.                 num2++;
  297.                 value3=num2;
  298.             }

  299.             shi2=num2/10;
  300.             ge2=num2%10;

  301.             num1=value3+value4;//显示南北红灯时间
  302.             shi1=num1/10;
  303.             ge1=num1%10;



  304.         }
  305.         if(key==1&&P1==0x44){    //按键10:设置东西黄灯时间+
  306.             num2=value4;

  307.             if(num1!=159){
  308.                 num2++;
  309.                 value4=num2;
  310.             }

  311.             shi2=num2/10;
  312.             ge2=num2%10;
  313.             num1=value3+value4;//显示南北红灯时间
  314.             shi1=num1/10;
  315.             ge1=num1%10;

  316.         }
  317.         if(key==2&&P1==0x44&&value3>3){    //按键11:设置东西绿灯时间-
  318.             num2=value3;

  319.             num2--;
  320.             value3=num2;

  321.             shi2=num2/10;
  322.             ge2=num2%10;

  323.             num1=value3+value4;//显示南北红灯时间
  324.             shi1=num1/10;
  325.             ge1=num1%10;


  326.         }
  327.         if(key==3&&P1==0x44&&value4>3){    //按键12:设置东西黄灯时间-
  328.             num2=value4;

  329.             num2--;
  330.             value4=num2;

  331.             shi2=num2/10;
  332.             ge2=num2%10;
  333.             num1=value3+value4;//显示南北红灯时间
  334.             shi1=num1/10;
  335.             ge1=num1%10;


  336.         }
  337.         }
  338.    
  339.     }

  340.     P3=0xf7;//第四行线          2未用
  341.     temp=P3;
  342.     temp=temp&0xf0;
  343.     if(temp!=0xf0)
  344.     {
  345.         delay(10);
  346.         temp=P3;
  347.         temp=temp&0xf0;
  348.         if(temp!=0xf0){
  349.             temp=P3;
  350.             switch(temp)
  351.             {
  352.                 case 0xe7:
  353.                     key=0;
  354.                     break;
  355.                 case 0xd7:
  356.                     key=1;
  357.                     break;
  358.                 case 0xb7:
  359.                     key=2;
  360.                     break;
  361.                 case 0x77:
  362.                     key=3;
  363.                     break;
  364.             }
  365.         while(temp!=0xf0)
  366.         {
  367.             temp=P3;
  368.             temp=temp&0xf0;
  369.         }
  370.         if(key==0&&P1==0x44){    //按键13:南北紧急情况:南北绿灯常亮 东西红灯常亮
  371.             P1=0x41;
  372.             shi1=ge1=shi2=ge2=18;   

  373.         }
  374.         if(key==1&&P1==0x44){    //按键14:东西紧急情况:东西绿灯常亮 南北红灯常亮
  375.             P1=0x14;
  376.             shi1=ge1=shi2=ge2=18;   
  377.         }
  378.         if(key==2&&P1==0x44){//按键15:
  379.             
  380.         }
  381.         if(key==3&&P1==0x44){//按键16:
  382.             
  383.         }
  384.         }
  385.    
  386.     }



  387. }
  388. void T0_time() interrupt 1     //定时器T0 中断子程序
  389. {
  390.     TH0=(65536-45872)/256;
  391.     TL0=(65536-45872)%256;
  392.     num++;
  393. }
  394. </font>
复制代码

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

使用道具 举报

沙发
ID:297634 发表于 2018-3-30 00:13 | 只看该作者
获益良多!!
回复

使用道具 举报

板凳
ID:411657 发表于 2018-10-18 18:35 | 只看该作者
想获得全部代码
回复

使用道具 举报

地板
ID:447674 发表于 2018-12-17 09:17 | 只看该作者
楼主学的是否是通用式单片机
回复

使用道具 举报

5#
ID:552378 发表于 2019-6-10 10:32 | 只看该作者
要是有仿真文件就好了,自己连的仿真有点小问题
回复

使用道具 举报

6#
ID:728994 发表于 2020-4-14 14:55 来自手机 | 只看该作者
想要全部代码
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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