按键led

第三次单片机编程挑战

第一次用状态机的思想编程。
  1. #include <reg52.h>
     

  2.  
  3. #define key_state_0        0        //按键初始态
     
  4. #define key_state_1        1                 //按键确认态
     
  5. #define key_state_2        2                 //按键松手态
     

  6.  
  7. typedef unsigned char uchar;
     
  8. typedef bit bool;
     

  9.  
  10. /*我使用的是STC89C52RC单片机,按键1 2 3分别对应P3口的P3.4 P3.5 P3.5,三个LED对应P1口的P1.1 P1.3 P1.5。*/
     
  11. sbit led1 = P1^1;
     
  12. sbit led2 = P1^3;
     
  13. sbit led3 = P1^5;
     
  14. sbit key1 = P3^4;
     
  15. sbit key2 = P3^5;
     
  16. sbit key3 = P3^6;
     

  17.  
  18. uchar key1_counter, key2_counter, key3_counter;
     

  19.  
  20. void init();
     
  21. bool key1_scan();
     
  22. bool key2_scan();
     
  23. bool key3_scan();
     
  24. void light_led1();
     
  25. void light_led2();
     
  26. void light_led3();
     

  27.  
  28. void main()
     
  29. {
     
  30.         init();
     
  31.         while(1)
     
  32.         {
     
  33.                 light_led1();
     
  34.                 light_led2();
     
  35.                 light_led3();
     
  36.         }               
     
  37. }
     

  38.  
  39. /*初始化函数,初始化定时器*/
     
  40. void init()
     
  41. {
     
  42.         TMOD = 0x01;                       //定时器0工作方式1
     
  43.         TH0 = (65536 - 10000) / 256;       //定时10ms
     
  44.         TL0 = (65536 - 10000) % 256;
     
  45.         EA = 1;                            //开总中断
     
  46.         ET0 = 1;                           //开定时器中断
     
  47.         TR0 = 1;                           //启动定时器
     
  48. }
     

  49.  
  50. /*led1以10HZ闪烁*/
     
  51. void light_led1()
     
  52. {
     
  53.         if (10 == key1_counter)
     
  54.         {
     
  55.                 key1_counter = 0;
     
  56.                 led1 = ~led1;
     
  57.         }
     
  58. }
     

  59.  
  60. /*led2以100HZ闪烁*/
     
  61. void light_led2()
     
  62. {
     
  63.         if (1 == key2_counter)
     
  64.         {
     
  65.                 key2_counter = 0;
     
  66.                 led2 = ~led2;
     
  67.         }       
     
  68. }
     

  69.  
  70. /*led1以50HZ闪烁*/
     
  71. void light_led3()
     
  72. {
     
  73.         if (5 == key3_counter)
     
  74.         {
     
  75.                 key3_counter = 0;
     
  76.                 led3 = ~led3;
     
  77.         }
     
  78. }
     

  79.  
  80. /*控制led1的按键被按下*/
     
  81. bool key1_scan()
     
  82. {
     
  83.         static uchar key_state = 0;
     
  84.         static bool key_press = 0;
     
  85.        
     
  86.         switch (key_state)
     
  87.         {
     
  88.                 case key_state_0 :
     
  89.                         if (0 == key1)                                        // 键被按下,状态转换到键确认态,同时有去抖作用
     
  90.                                 key_state = key_state_1;
     
  91.                         break;
     

  92.  
  93.                 case key_state_1:
     
  94.                         if (0 == key1)                                       
     
  95.                         {
     
  96.                                 key_press = ~key_press;                //按键仍按下,按键确认输出为"1"
     
  97.                                 key_state = key_state_2;        // 状态转换到松手态
     
  98.                         }
     
  99.                         else
     
  100.                                 key_state = key_state_0;        // 抖动,转换到按键初始态
     
  101.                         break;
     

  102.  
  103.                 case key_state_2 :
     
  104.                         if (1 == key1)
     
  105.                                 key_state = key_state_0;        //按键已释放,转换到按键初始态
     
  106.                         break;                       
     
  107.         }
     

  108.  
  109.         return key_press;
     
  110. }
     

  111.  
  112. bool key2_scan()
     
  113. {
     
  114.         static uchar key_state = 0;
     
  115.         static bool key_press = 0;
     

  116.  
  117.         switch (key_state)
     
  118.         {
     
  119.                 case key_state_0 :
     
  120.                         if (0 == key2)
     
  121.                                 key_state = key_state_1;
     
  122.                         break;
     

  123.  
  124.                 case key_state_1:
     
  125.                         if (0 == key2)
     
  126.                                 key_state = key_state_2;
     
  127.                         else
     
  128.                                 key_state = key_state_0;
     
  129.                         break;
     

  130.  
  131.                 case key_state_2 :
     
  132.                         if (1 == key2)
     
  133.                         {
     
  134.                                 key_press = ~key_press;
     
  135.                                 key_state = key_state_0;
     
  136.                         }
     
  137.                         break;                       
     
  138.         }
     

  139.  
  140.         return key_press;
     
  141. }
     

  142.  
  143. bool key3_scan()
     
  144. {
     
  145.         static uchar key_state = 0;
     
  146.         static bool key_press = 0;
     
  147.        
     
  148.         switch (key_state)
     
  149.         {
     
  150.                 case key_state_0 :
     
  151.                         if (0 == key3)
     
  152.                                 key_state = key_state_1;
     
  153.                         break;
     

  154.  
  155.                 case key_state_1:
     
  156.                         if (0 == key3)
     
  157.                         {
     
  158.                                 key_press = ~key_press;
     
  159.                                 key_state = key_state_2;
     
  160.                         }
     
  161.                         else
     
  162.                                 key_state = key_state_0;
     
  163.                         break;
     

  164.  
  165.                 case key_state_2 :
     
  166.                         if (1 == key3)
     
  167.                         {
     
  168.                                 key_press = ~key_press;
     
  169.                                 key_state = key_state_0;
     
  170.                         }
     
  171.                         break;                       
     
  172.         }
     

  173.  
  174.         return key_press;
     
  175. }
     

  176.  
  177. /*中断处理程序*/
     
  178. void timer0() interrupt 1
     
  179. {
     
  180.         TH0 = (65536 - 10000) / 256;
     
  181.         TL0 = (65536 - 10000) % 256;
     
  182.        
     
  183.         if (1 == key1_scan())
     
  184.                 key1_counter ++;
     
  185.         else
     
  186.                 led1 = 1;
     

  187.  
  188.         if (1 == key2_scan())
     
  189.                 key2_counter ++;       
     
  190.         else
     
  191.                 led2 = 1;
     

  192.  
  193.         if (1 == key3_scan())
     
  194.                 key3_counter ++;       
     
  195.         else
     
  196.                 led3 = 1;
     
  197.        
     
  198. }
复制代码
硬件电路:
截图00.png
下载 (21.7 KB)
2010-11-25 17:23


 

你可能感兴趣的:(职场,单片机,休闲,led,按键)