动态扫描显示.c
上传用户:schina
上传日期:2021-05-07
资源大小:39k
文件大小:3k
源码类别:

扫描程序

开发平台:

Visual C++

  1. #include<reg52.h>
  2. #define uchar unsigned char 
  3. #define uint unsigned int 
  4. #define KEY_PORT P1
  5. #define dat P0
  6. sbit w1=P2^0;
  7. sbit w2=P2^1;
  8. sbit w3=P2^2;
  9. sbit w4=P2^3;
  10. uchar num,count,temp;
  11. uint t;
  12. uchar code table[]={ //共阴数码管码表
  13. 0x3f,0x06,0x5b,0x4f,
  14. 0x66,0x6d,0x7d,0x07,
  15. 0x7f,0x6f,0x77,0x7c,
  16. 0x39,0x5e,0x79,0x71
  17. };
  18. //延时函数
  19. void delay(uint z)
  20. {
  21. uint x;
  22. uchar y;
  23. for(x=z;x>0;x--)
  24. for(y=110;y>0;y--);
  25. }
  26. //键盘扫描
  27. void keyscan()
  28. {
  29. KEY_PORT=0xfe; //扫描第一行
  30. temp=KEY_PORT;
  31. temp=temp&0xf0;      
  32. while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
  33. {  
  34. delay(5) ;
  35. temp=KEY_PORT;
  36. temp=temp&0xf0;
  37. while(temp!=0xf0)
  38. {
  39. temp=KEY_PORT;
  40. switch(temp)
  41. {
  42. case 0xee :   num=0;
  43. break;
  44. case 0xde :   num=1;
  45. break;
  46. case 0xbe :   num=2;
  47. break;
  48. case 0x7e :   num=3;//对键赋值,定义用户想要的值
  49. break;
  50. }
  51. while(temp!=0xf0)
  52. {
  53. temp=KEY_PORT;
  54. temp=temp&0xf0;   //松手检测,对temp赋新值,使跳出扫描循环
  55. }
  56. }
  57. }
  58. KEY_PORT=0xfd;    //扫描第二行
  59.     temp=KEY_PORT;
  60. temp=temp&0xf0;      
  61. while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
  62. {  
  63. delay(5) ;
  64. temp=KEY_PORT;
  65. temp=temp&0xf0;
  66. while(temp!=0xf0)
  67. {
  68. temp=KEY_PORT;
  69. switch(temp)
  70. {
  71. case 0xed :   num=4;
  72. break;
  73. case 0xdd :   num=5;
  74. break;
  75. case 0xbd :   num=6;
  76. break;
  77. case 0x7d :   num=7; //对键赋值,定义用户想要的值
  78. break;
  79. }
  80. while(temp!=0xf0)
  81. {
  82. temp=KEY_PORT;
  83. temp=temp&0xf0;   //松手检测,对temp赋新值,使跳出扫描循环
  84. }
  85. }
  86. }
  87. KEY_PORT=0xfb; //扫描第三行
  88. temp=KEY_PORT;
  89. temp=temp&0xf0;      
  90. while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
  91. {  
  92. delay(5) ;
  93. temp=KEY_PORT;
  94. temp=temp&0xf0;
  95. while(temp!=0xf0)
  96. {
  97. temp=KEY_PORT;
  98. switch(temp)
  99. {
  100. case 0xeb :   num=8;
  101. break;
  102. case 0xdb :   num=9;
  103. break;
  104. case 0xbb :   num=10;
  105. break;
  106. case 0x7b :   num=11; //对键赋值,定义用户想要的值
  107. break;
  108. }
  109. while(temp!=0xf0)
  110. {
  111. temp=KEY_PORT;
  112. temp=temp&0xf0;   //松手检测,对temp赋新值,使跳出扫描循环
  113. }
  114. }
  115. }
  116. KEY_PORT=0xf7; //扫描第四行
  117. temp=KEY_PORT;
  118. temp=temp&0xf0;      
  119. while(temp!=0xf0) // 如何有键按下再扫描值,有键按下了temp值就不为0xf0了
  120. {  
  121. delay(5) ;
  122. temp=KEY_PORT;
  123. temp=temp&0xf0;
  124. while(temp!=0xf0)
  125. {
  126. temp=KEY_PORT;
  127. switch(temp)
  128. {
  129. case 0xe7 :   num=12;
  130. break;
  131. case 0xd7 :   num=13;
  132. break;
  133. case 0xb7 :   num=14;
  134. break;
  135. case 0x77 :   num=15; //对键赋值,定义用户想要的值
  136. break;
  137. }
  138. while(temp!=0xf0)
  139. {
  140. temp=KEY_PORT;
  141. temp=temp&0xf0;   //松手检测,对temp赋新值,使跳出扫描循环
  142. }
  143. }
  144. }
  145. }
  146. //计数显示函数
  147. void display(uchar val)
  148. {
  149. w1=0;
  150. dat=table[val];
  151. delay(1);
  152. w1=1;
  153. }
  154. //按键显示函数
  155. void display2(uchar val)
  156. {
  157. w3=0;
  158. dat=table[val/10];
  159. delay(1);
  160. w3=1;
  161. w4=0;
  162. dat=table[val%10];
  163. delay(1);
  164. w4=1;
  165. }
  166. //主函数
  167. void main()
  168. {
  169. num=0;
  170. t=0;
  171. count=0;
  172. P2=0xff;
  173. TMOD=0x02;
  174. TH0=6;
  175. TL0=6;
  176. EA=1;
  177. ET0=1;
  178. TR0=1;
  179. while(1)
  180. {
  181. keyscan();
  182. display(num);
  183. display2(count);
  184. }
  185. }
  186. //计时中断函数
  187. void t0() interrupt 1
  188. {
  189. t++;
  190. if(t==4000)
  191. {
  192. t=0;
  193. count++;
  194. if(count==100)
  195. count=0;
  196. }
  197. }