当前位置:   article > 正文

单片机Proteus软件仿真二(按键控制LED灯逐个点亮)_proteus仿真按键控制led

proteus仿真按键控制led

1 功能描述

通过2个按键控制8个LED逐个点亮,系统启动默认点亮LED1,按K1则逐次向右点亮灯2、灯3、灯4···,按K2则逐次向左点亮。

 2 电路图

在LED流水灯电路图上直接增加2个按键即可,接P3_0/P3_1口,电路图没有大的变动。上拉电阻统一100欧,Proteus软件感觉用起来比实物开发板要方便点,我之前还买了个普中的开发板,优缺点后面使用再说吧,我现在还不太好评论。Proteus软件用熟练了画图很快,手生的话多用鼠标右键,慢点就慢点,不至于老是画错。

3 代码

上篇文章不是很详细,这边把截图放上。左键单击芯片右键弹出快捷菜单选择编辑源代码或者工具栏找编辑源代码图标都可以进去源码编辑界面。

 3.1头文件的引用

代码有关于P3_0/P3_1口的位定义,因此不能用默认的51引用头文件,这里放REGX52.H头文件。

  1. /*--------------------------------------------------------------------------
  2. AT89X52.H
  3. Header file for the low voltage Flash Atmel AT89C52 and AT89LV52.
  4. Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
  5. All rights reserved.
  6. --------------------------------------------------------------------------*/
  7. #ifndef __AT89X52_H__
  8. #define __AT89X52_H__
  9. /*------------------------------------------------
  10. Byte Registers
  11. ------------------------------------------------*/
  12. sfr P0 = 0x80;
  13. sfr SP = 0x81;
  14. sfr DPL = 0x82;
  15. sfr DPH = 0x83;
  16. sfr PCON = 0x87;
  17. sfr TCON = 0x88;
  18. sfr TMOD = 0x89;
  19. sfr TL0 = 0x8A;
  20. sfr TL1 = 0x8B;
  21. sfr TH0 = 0x8C;
  22. sfr TH1 = 0x8D;
  23. sfr P1 = 0x90;
  24. sfr SCON = 0x98;
  25. sfr SBUF = 0x99;
  26. sfr P2 = 0xA0;
  27. sfr IE = 0xA8;
  28. sfr P3 = 0xB0;
  29. sfr IP = 0xB8;
  30. sfr T2CON = 0xC8;
  31. sfr T2MOD = 0xC9;
  32. sfr RCAP2L = 0xCA;
  33. sfr RCAP2H = 0xCB;
  34. sfr TL2 = 0xCC;
  35. sfr TH2 = 0xCD;
  36. sfr PSW = 0xD0;
  37. sfr ACC = 0xE0;
  38. sfr B = 0xF0;
  39. /*------------------------------------------------
  40. P0 Bit Registers
  41. ------------------------------------------------*/
  42. sbit P0_0 = 0x80;
  43. sbit P0_1 = 0x81;
  44. sbit P0_2 = 0x82;
  45. sbit P0_3 = 0x83;
  46. sbit P0_4 = 0x84;
  47. sbit P0_5 = 0x85;
  48. sbit P0_6 = 0x86;
  49. sbit P0_7 = 0x87;
  50. /*------------------------------------------------
  51. PCON Bit Values
  52. ------------------------------------------------*/
  53. #define IDL_ 0x01
  54. #define STOP_ 0x02
  55. #define PD_ 0x02 /* Alternate definition */
  56. #define GF0_ 0x04
  57. #define GF1_ 0x08
  58. #define SMOD_ 0x80
  59. /*------------------------------------------------
  60. TCON Bit Registers
  61. ------------------------------------------------*/
  62. sbit IT0 = 0x88;
  63. sbit IE0 = 0x89;
  64. sbit IT1 = 0x8A;
  65. sbit IE1 = 0x8B;
  66. sbit TR0 = 0x8C;
  67. sbit TF0 = 0x8D;
  68. sbit TR1 = 0x8E;
  69. sbit TF1 = 0x8F;
  70. /*------------------------------------------------
  71. TMOD Bit Values
  72. ------------------------------------------------*/
  73. #define T0_M0_ 0x01
  74. #define T0_M1_ 0x02
  75. #define T0_CT_ 0x04
  76. #define T0_GATE_ 0x08
  77. #define T1_M0_ 0x10
  78. #define T1_M1_ 0x20
  79. #define T1_CT_ 0x40
  80. #define T1_GATE_ 0x80
  81. #define T1_MASK_ 0xF0
  82. #define T0_MASK_ 0x0F
  83. /*------------------------------------------------
  84. P1 Bit Registers
  85. ------------------------------------------------*/
  86. sbit P1_0 = 0x90;
  87. sbit P1_1 = 0x91;
  88. sbit P1_2 = 0x92;
  89. sbit P1_3 = 0x93;
  90. sbit P1_4 = 0x94;
  91. sbit P1_5 = 0x95;
  92. sbit P1_6 = 0x96;
  93. sbit P1_7 = 0x97;
  94. sbit T2 = 0x90; /* External input to Timer/Counter 2, clock out */
  95. sbit T2EX = 0x91; /* Timer/Counter 2 capture/reload trigger & dir ctl */
  96. /*------------------------------------------------
  97. SCON Bit Registers
  98. ------------------------------------------------*/
  99. sbit RI = 0x98;
  100. sbit TI = 0x99;
  101. sbit RB8 = 0x9A;
  102. sbit TB8 = 0x9B;
  103. sbit REN = 0x9C;
  104. sbit SM2 = 0x9D;
  105. sbit SM1 = 0x9E;
  106. sbit SM0 = 0x9F;
  107. /*------------------------------------------------
  108. P2 Bit Registers
  109. ------------------------------------------------*/
  110. sbit P2_0 = 0xA0;
  111. sbit P2_1 = 0xA1;
  112. sbit P2_2 = 0xA2;
  113. sbit P2_3 = 0xA3;
  114. sbit P2_4 = 0xA4;
  115. sbit P2_5 = 0xA5;
  116. sbit P2_6 = 0xA6;
  117. sbit P2_7 = 0xA7;
  118. /*------------------------------------------------
  119. IE Bit Registers
  120. ------------------------------------------------*/
  121. sbit EX0 = 0xA8; /* 1=Enable External interrupt 0 */
  122. sbit ET0 = 0xA9; /* 1=Enable Timer 0 interrupt */
  123. sbit EX1 = 0xAA; /* 1=Enable External interrupt 1 */
  124. sbit ET1 = 0xAB; /* 1=Enable Timer 1 interrupt */
  125. sbit ES = 0xAC; /* 1=Enable Serial port interrupt */
  126. sbit ET2 = 0xAD; /* 1=Enable Timer 2 interrupt */
  127. sbit EA = 0xAF; /* 0=Disable all interrupts */
  128. /*------------------------------------------------
  129. P3 Bit Registers (Mnemonics & Ports)
  130. ------------------------------------------------*/
  131. sbit P3_0 = 0xB0;
  132. sbit P3_1 = 0xB1;
  133. sbit P3_2 = 0xB2;
  134. sbit P3_3 = 0xB3;
  135. sbit P3_4 = 0xB4;
  136. sbit P3_5 = 0xB5;
  137. sbit P3_6 = 0xB6;
  138. sbit P3_7 = 0xB7;
  139. sbit RXD = 0xB0; /* Serial data input */
  140. sbit TXD = 0xB1; /* Serial data output */
  141. sbit INT0 = 0xB2; /* External interrupt 0 */
  142. sbit INT1 = 0xB3; /* External interrupt 1 */
  143. sbit T0 = 0xB4; /* Timer 0 external input */
  144. sbit T1 = 0xB5; /* Timer 1 external input */
  145. sbit WR = 0xB6; /* External data memory write strobe */
  146. sbit RD = 0xB7; /* External data memory read strobe */
  147. /*------------------------------------------------
  148. IP Bit Registers
  149. ------------------------------------------------*/
  150. sbit PX0 = 0xB8;
  151. sbit PT0 = 0xB9;
  152. sbit PX1 = 0xBA;
  153. sbit PT1 = 0xBB;
  154. sbit PS = 0xBC;
  155. sbit PT2 = 0xBD;
  156. /*------------------------------------------------
  157. T2CON Bit Registers
  158. ------------------------------------------------*/
  159. sbit CP_RL2= 0xC8; /* 0=Reload, 1=Capture select */
  160. sbit C_T2 = 0xC9; /* 0=Timer, 1=Counter */
  161. sbit TR2 = 0xCA; /* 0=Stop timer, 1=Start timer */
  162. sbit EXEN2= 0xCB; /* Timer 2 external enable */
  163. sbit TCLK = 0xCC; /* 0=Serial clock uses Timer 1 overflow, 1=Timer 2 */
  164. sbit RCLK = 0xCD; /* 0=Serial clock uses Timer 1 overflow, 1=Timer 2 */
  165. sbit EXF2 = 0xCE; /* Timer 2 external flag */
  166. sbit TF2 = 0xCF; /* Timer 2 overflow flag */
  167. /*------------------------------------------------
  168. T2MOD Bit Values
  169. ------------------------------------------------*/
  170. #define DCEN_ 0x01 /* 1=Timer 2 can be configured as up/down counter */
  171. #define T2OE_ 0x02 /* Timer 2 output enable */
  172. /*------------------------------------------------
  173. PSW Bit Registers
  174. ------------------------------------------------*/
  175. sbit P = 0xD0;
  176. sbit F1 = 0xD1;
  177. sbit OV = 0xD2;
  178. sbit RS0 = 0xD3;
  179. sbit RS1 = 0xD4;
  180. sbit F0 = 0xD5;
  181. sbit AC = 0xD6;
  182. sbit CY = 0xD7;
  183. /*------------------------------------------------
  184. Interrupt Vectors:
  185. Interrupt Address = (Number * 8) + 3
  186. ------------------------------------------------*/
  187. #define IE0_VECTOR 0 /* 0x03 External Interrupt 0 */
  188. #define TF0_VECTOR 1 /* 0x0B Timer 0 */
  189. #define IE1_VECTOR 2 /* 0x13 External Interrupt 1 */
  190. #define TF1_VECTOR 3 /* 0x1B Timer 1 */
  191. #define SIO_VECTOR 4 /* 0x23 Serial port */
  192. #define TF2_VECTOR 5 /* 0x2B Timer 2 */
  193. #define EX2_VECTOR 5 /* 0x2B External Interrupt 2 */
  194. #endif

sbit P3_0 = 0xB0;
sbit P3_1 = 0xB1;

P3口的字节地址为B0H,位地址为B0~B7H,sbit是C51扩展的4种数据类型之一。

3.2 程序设计

(1)定义一个无符号字符型变量LEDNum,初始值为0,当按键K1按下,则LEDNum自增1,K2按下,LEDNum自减1。

(2)按键K1按下,LEDNum加1,则原先点亮的灯的右边的灯亮,之前的灯灭,这里是通过左移LEDNum位来实现的。例如LEDNum=5,则00000001B左移5位,P2口00100000B取反后为11011111B,第6个灯亮(低电平点亮);如果K2按下,LEDNum减1,LEDNum=4,则P2口左移4位,第5个灯亮。

(3)按K1若LEDNum>=8时,则LEDNum=0,第一个灯亮。这时按K2则使最后一个等亮。读者可自行品味。

  1. #include <REGX52.H>
  2. void Delay(unsigned int xms);
  3. unsigned char LEDNum;
  4. void main()
  5. {
  6. P2=~0x01; //上电默认LED1点亮
  7. while(1)
  8. {
  9. if(P3_1==0) //如果K1按键按下
  10. {
  11. Delay(20);
  12. while(P3_1==0);
  13. Delay(20);
  14. LEDNum++; //LEDNum自增
  15. if(LEDNum>=8) //限制LEDNum自增范围
  16. LEDNum=0;
  17. P2=~(0x01<<LEDNum); //LED的第LEDNum位点亮
  18. }
  19. if(P3_0==0) //如果K2按键按下
  20. {
  21. Delay(20);
  22. while(P3_0==0);
  23. Delay(20);
  24. if(LEDNum==0) //LEDNum减到0后变为7
  25. LEDNum=7;
  26. else //LEDNum未减到0,自减
  27. LEDNum--;
  28. P2=~(0x01<<LEDNum); //LED的第LEDNum位点亮
  29. }
  30. }
  31. }
  32. void Delay(unsigned int xms)
  33. {
  34. unsigned char i, j;
  35. while(xms--)
  36. {
  37. i = 2;
  38. j = 239;
  39. do
  40. {
  41. while (--j);
  42. } while (--i);
  43. }
  44. }

4、注意

程序内不要使用printf打印函数,我之前没有发现,如果使用这个函数执行到此处会出现问题,就是仿真功能全部不能用了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/685685
推荐阅读
相关标签
  

闽ICP备14008679号