标题: 51单片机 交通灯修改版 [打印本页]

作者: zhao111    时间: 2017-2-24 16:30
标题: 51单片机 交通灯修改版
喜欢的 可以看一下

  1.   #include<reg52.h>

  2. #define uchar unsigned  char
  3. #define uint unsigned int
  4. #define led P0
  5. uchar tab[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
  6. uchar count, count0;

  7. void delay(uint x);
  8. void play();

  9. void main()
  10. {
  11.         TMOD = 0X01;//定时器0的工作方式为1;
  12.         TH0 = (65536 - 40000) / 256;
  13.         TL0 = (65536 - 40000) % 256;
  14.         EA = 1;//开中断
  15.         ET0 = 1;//开定时器0中断
  16.         TR0 = 1;//启动定时器0

  17.         led = 0x0c;
  18.         while(1)
  19.         {       
  20.                 play();
  21.         }
  22. }

  23. void timer0() interrupt 1//定时器0中断服务程序
  24. {
  25.         TH0 = (65536 - 40000) / 256;
  26.         TL0 = (65536 - 40000) % 256;


  27.         count++;//中断次数累加
  28.         if(count == 50)
  29.         {                                                                  
  30.                 count = 0;//30次的50毫秒为1秒

  31.                 count0++;
  32.                 if((count0>=5)&&(count0<=8))
  33.                 {
  34.                         P2=~tab[8-count0];
  35.                 }
  36.                 else
  37.                 P2=0xff;
  38.                 if((count0>=13)&&(count0<=16))
  39.                 {
  40.                         P3=~tab[16-count0];
  41.                 }
  42.                 else
  43.                 P3=0xff;

  44.                 if(count0 == 16)
  45.                 {
  46.                         count0 = 0;
  47.                 }

  48.         }
  49.                
  50. }                  

  51. void play()
  52. {
  53.         if(count0 == 5)  //5s后南北黄,东西红
  54.         {
  55.                 led = 0x0a;
  56.                 delay(100);
  57.                 led = 0x08;
  58.                 delay(100);
  59.                 led = 0x0a;
  60.                 delay(100);
  61.                 led = 0x08;
  62.                 delay(100);
  63.                 led = 0x0a;
  64.                
  65.         }
  66.         else if(count0 == 8)        //8s后南北红,东西绿
  67.         {
  68.                 led = 0x21;
  69.         }
  70.         else if(count0 == 13)         //13s南北红,东西黄
  71.         {
  72.                 led = 0x11;
  73.                 delay(100);
  74.                 led = 0x01;
  75.                 delay(100);
  76.                 led = 0x11;
  77.                 delay(100);
  78.                 led = 0x01;
  79.                 delay(100);
  80.                 led = 0x11;

  81.         }
  82.         else if(count0 == 0)          //16s后南北绿,东西红
  83.         {
  84.                 led = 0x0c;
  85.         }
  86. }

  87. void delay(uint x)
  88. {
  89.         uint a, b;
  90.         for(a = x; a > 0; a--)
  91.                 for(b = 1000; b > 0; b--);
  92. }
复制代码




单片机 交通灯 修改.rar

53.19 KB, 下载次数: 9, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1