標(biāo)題: 51單片機(jī)定時器控制交通燈 [打印本頁]

作者: Yimning    時間: 2020-4-11 20:10
標(biāo)題: 51單片機(jī)定時器控制交通燈
  1. /***************   writer:shopping.w   ******************/
  2. #include <reg52.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char

  5. sbit RED_A = P0^0;
  6. sbit YELLOW_A = P0^1;
  7. sbit GREEN_A = P0^2;
  8. sbit RED_B = P0^3;
  9. sbit YELLOW_B = P0^4;
  10. sbit GREEN_B = P0^5;

  11. uchar Time_Count = 0,Flash_Count = 0,Operation_Type = 1;

  12. void T0_INT() interrupt 1
  13. {
  14.          TH0 = -50000/256;
  15.         TL0 = -50000%256;
  16.         switch(Operation_Type)
  17.         {
  18.                  case 1:
  19.                         RED_A=0;YELLOW_A=0;GREEN_A=1;
  20.                         RED_B=1;YELLOW_B=0;GREEN_B=0;
  21.                         if(++Time_Count != 100) return;
  22.                         Time_Count=0;
  23.                         Operation_Type = 2;
  24.                         break;
  25.                 case 2:
  26.                         if(++Time_Count != 8) return;
  27.                         Time_Count=0;
  28.                         YELLOW_A=!YELLOW_A;
  29.                         GREEN_A=0;
  30.                         if(++Flash_Count != 10) return;
  31.                         Flash_Count=0;
  32.                         Operation_Type = 3;
  33.                         break;
  34.                 case 3:
  35.                         RED_A=1;YELLOW_A=0;GREEN_A=0;
  36.                         RED_B=0;YELLOW_B=0;GREEN_B=1;
  37.                         if(++Time_Count != 100) return;
  38.                         Time_Count=0;
  39.                         Operation_Type = 4;
  40.                         break;        
  41.                 case 4:
  42.                         if(++Time_Count != 8) return;
  43.                         Time_Count=0;
  44.                         YELLOW_B=!YELLOW_B;
  45.                         GREEN_B=0;
  46.                         if(++Flash_Count !=10)
  47.                                 return;
  48.                         Flash_Count=0;
  49.                         Operation_Type = 1;
  50.                         break;        
  51.         }
  52. }

  53. void main()
  54. {
  55.          TMOD = 0x01;
  56.         IE = 0x82;
  57.         TR0 = 1;
  58.         while(1);
  59. }
復(fù)制代碼


{CP]R_~C1$$6UT1]H@NVYDH.png (7.52 KB, 下載次數(shù): 31)

目錄圖

目錄圖

41 定時器控制交通指示燈.zip

26.58 KB, 下載次數(shù): 19, 下載積分: 黑幣 -5

51下載






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1