標(biāo)題: 合泰單片機(jī)交通燈控制程序 [打印本頁(yè)]

作者: 背景    時(shí)間: 2020-3-6 16:31
標(biāo)題: 合泰單片機(jī)交通燈控制程序
  1. #include "HT66F70A.h"
  2. #include <stdlib.h> //隨機(jī)函數(shù)
  3. #include <time.h>
  4. #define LED_Port _pc
  5. #define LED_PortC _pcc
  6. #define LED_PortBSC _pdc
  7. #define LED_PortBS _pd
  8. #define LED1_Port _pg
  9. #define LED_Portc _pgc
  10. #define KEY_PORTC _pec
  11. #define KEY_PORT _pe
  12. #define KeyPortPU _pepu
  13. //下面是函數(shù)聲明部分
  14. void openLedLight(unsigned char Lbit, unsigned char LNum);
  15. void dynamicLed(unsigned char Counter,unsigned char Ligh);
  16. unsigned char const ledTab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x73,0x38,0x3e};//存放LED的點(diǎn)陣碼 對(duì)應(yīng)0~15,HPLV
  17. unsigned char const tabIndex[]={0x01,0x02,0x04,0x08};//存放顯示位對(duì)應(yīng)的碼對(duì)應(yīng)1~4位
  18. unsigned char  Num[4]={0x05,0x09,0x05,0x05};//存放顯示位對(duì)應(yīng)的碼對(duì)應(yīng)1~4位
  19. unsigned int counter =0;
  20. unsigned int time=60;
  21. unsigned int time1=55;
  22. const unsigned short pili_TAB[] = {0x21,0x0C,0x12,0x28,0x05,0x3F};
  23. unsigned short ScanKey();
  24. unsigned char keyNum=0;//最大數(shù)只能是255
  25. unsigned char mode=0;//0
  26. void InitPort()
  27. {
  28. _wdtc=0xaf;
  29. LED_Portc = 0x00;//設(shè)置為L(zhǎng)ED口為輸出;  //關(guān)閉看門狗
  30. _pec = 0x00;//設(shè)置E口輸出
  31. _pepu0 = 1;//開啟上拉模式
  32. _pe0 = 0;//輸出0
  33. }
  34. void main()
  35. {
  36. unsigned short int i;
  37. LED_PortC =0x00;//設(shè)置為L(zhǎng)ED口為輸出
  38. LED_PortBSC=0x00;//設(shè)置LED位選口為輸出
  39. unsigned char temp=0x01;
  40. InitPort();
  41. LED_Portc = 0x00;//設(shè)置為L(zhǎng)ED口為輸出;
  42. LED_Port = temp;//按位取反,關(guān)閉燈顯示;
  43. openLedLight(0,5);
  44. while(1)
  45. {
  46. keyNum=ScanKey();
  47.      if(keyNum==0)
  48.     {
  49.      mode=1;
  50.    
  51.     }
  52.    
  53.         else if(keyNum==1)
  54.     {
  55.      mode=2;
  56.     }
  57.     else if(keyNum==2)
  58.     {
  59.       mode=3;
  60.     }
  61. else if(keyNum==3)
  62.     {
  63.       mode=4;
  64.     }
  65.     else if(keyNum==4)
  66.     {
  67.       mode=5;
  68.     }
  69.     if(mode==1)
  70.    {
  71. counter++;
  72. if(counter>=6000)
  73. {    time--;  
  74.      Num[0]=time/10;
  75.   Num[1]=time%10;
  76.   time1--;      
  77.      Num[2]=time1/10;
  78.   Num[3]=time1%10;
  79.   
  80.   if(time>30){LED1_Port = pili_TAB[0];}
  81.   if(time<=30){LED1_Port = pili_TAB[1];}
  82.    for(i=0;i<80;i++)
  83.    {
  84.     _clrwdt();
  85.     GCC_DELAY(5000);
  86.    }
  87.    
  88.   if(time==0)
  89.   {
  90.    time=60;
  91.   }
  92.   if(time1==0)
  93.   {
  94.    time1=55;
  95.   }
  96.   counter=0;
  97. }
  98.    }
  99.    
  100.        if(mode==2)
  101.    {
  102.        LED1_Port = pili_TAB[2];
  103.    }
  104.    
  105.           if(mode==3)
  106.    {
  107.      LED1_Port = pili_TAB[3];
  108.    }
  109.          if(mode==4)
  110.    {
  111.      LED1_Port = pili_TAB[4];
  112.    }
  113.    if(mode==5)
  114.    {
  115.      LED1_Port = pili_TAB[5];
  116.    }
  117.    
  118. _clrwdt();//喂狗
  119. dynamicLed(4,255);
  120. }
  121. }
  122. /********************************************************************
  123. 函數(shù)名稱:Delay_us
  124. 函數(shù)功能:us秒級(jí)別演示
  125. 輸入?yún)?shù):要延時(shí)的時(shí)間參數(shù)
  126. 輸出參數(shù):無(wú)
  127. *********************************************************************/
  128. void Delay_us(unsigned int time)
  129. {
  130. while(time--);
  131. }
  132. /********************************************************************
  133. 函數(shù)名稱:openLedLight
  134. 函數(shù)功能:實(shí)現(xiàn)單個(gè)LED燈的點(diǎn)亮
  135. 輸入?yún)?shù):Lbit 表示要顯示的是4個(gè)位里面的哪個(gè)位,取值為0~7的數(shù)值
  136.           LNum表示要顯示的數(shù)字的數(shù)值,取值為0~15的的數(shù)值
  137. 輸出參數(shù):無(wú)
  138. *********************************************************************/
  139. void openLedLight(unsigned char Lbit, unsigned char LNum)
  140. {
  141. LED_Port = 0x00;//關(guān)閉端口,停止輸出,避免出現(xiàn)重影
  142. LED_PortBS = tabIndex[Lbit];
  143. LED_Port =ledTab[LNum];
  144. }
  145. /********************************************************************
  146. 函數(shù)名稱:dynamicLed
  147. 函數(shù)功能:實(shí)現(xiàn)LED的循環(huán)動(dòng)態(tài)顯示
  148. 輸入?yún)?shù):Counter 表示要點(diǎn)亮的LED的個(gè)數(shù)取值1~8
  149.           Light 燈的亮度的控制,數(shù)值越大燈越亮,取值范圍0~255
  150. 輸出參數(shù):無(wú)
  151. *********************************************************************/
  152. void dynamicLed(unsigned char Counter,unsigned char Ligh)
  153. {
  154.   unsigned char i;
  155.   for(i=0;i<Counter;i++)
  156. {
  157.    Delay_us(Ligh);//實(shí)現(xiàn)燈亮度的調(diào)整
  158.    openLedLight(i,Num[i]);
  159. }
  160. }

  161. unsigned short ScanKey()
  162. { unsigned char i,key=0;
  163. KEY_PORTC=0x0f;
  164. KeyPortPU=0x0f;      //I/O Mode Config & Pull-up Enable
  165. KEY_PORT=0b11101111;              //Initial Scancode
  166. for(i=0;i<=3;i++)
  167. { if(!(KEY_PORT & 1)) break;        //Check Column 0
  168.   key++;
  169.   if(!(KEY_PORT & 1<<1)) break;      //Check Column 1
  170.   key++;
  171.   if(!(KEY_PORT & 1<<2)) break;      //Check Column 2
  172.   key++;
  173.   if(!(KEY_PORT & 1<<3)) break;      //Check Column 3
  174.   key++;
  175.   KEY_PORT<<=1; KEY_PORT|=0b0000001;    //Scancode for Next Row
  176. }
  177. return key;
  178. }
復(fù)制代碼






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