当前位置:   article > 正文

283 基于单片机八路抢答系统仿真设计

283 基于单片机八路抢答系统仿真设计

完整版 电路图和程序代码 下载地址

https://pan.baidu.com/s/1UEx-nh1Dg-pAJSUGoDCkaQ?pwd=8888

部分代码展示
  1. #include <at89x51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define M 10
  5. char s;
  6. uchar num = 0;
  7. char time = 30; /* 抢答时间 */
  8. char datitime = 30; /* 答题限时时间 */
  9. uint tt, t1; /* T0,T1定时器定一秒时 */
  10. bit flag, s_flag = 1, b_flag, fall_flag; /* 标志位 */
  11. bit K_startcountflag, K_timecountflag; /* 时间调整标志位 */
  12. sbit K0 = P3 ^ 0;
  13. sbit beep = P3 ^ 7; /* 蜂鸣器 */
  14. sbit rled = P3 ^ 1; /* 指示灯 */
  15. sbit K1 = P1 ^ 0;
  16. sbit K2 = P1 ^ 1;
  17. sbit K3 = P1 ^ 2;
  18. sbit K4 = P1 ^ 3;
  19. sbit K5 = P1 ^ 4;
  20. sbit K6 = P1 ^ 5;
  21. sbit K7 = P1 ^ 6;
  22. sbit K8 = P1 ^ 7;
  23. sbit scl = P0 ^ 0;
  24. sbit sda = P0 ^ 1;
  25. sbit K_Time = P3 ^ 2; /*复位键 */
  26. sbit K_startcount = P3 ^ 3; /* 开始抢答时间调整键 */
  27. sbit add = P3 ^ 4; /* 答题计时时间调整键 */
  28. sbit sub = P3 ^ 5; /* 答题计时时间调整键 */
  29. void delay( uchar ms )
  30. {
  31. uchar y;
  32. for (; ms > 0; ms-- )
  33. for ( y = 120; y > 0; y-- )
  34. ;
  35. }
  36. uchar code tabledu[] = { 0x3f, 0x06, 0x5b, 0x4f,
  37. 0x66, 0x6d, 0x7d, 0x07,
  38. 0x7f, 0x6f, 0x77, 0x7c,
  39. 0x39, 0x5e, 0x79, 0x71 };
  40. uchar code tablewe[] = { 0XFE, 0XFD, 0XFB, 0XF7 };
  41. void T0_Init( void )
  42. {
  43. TMOD = 0X01;
  44. TH0 = (65536 - 2000) / 256;
  45. TL0 = (65536 - 2000) % 256;
  46. TH1 = (65536 - 2000) / 256;
  47. TL1 = (65536 - 2000) % 256;
  48. ET0 = 1;
  49. ET1 = 1;
  50. EA = 1;
  51. P0 = 0;
  52. }
  53. void Key_Scan( void ) /* 开始键扫描 */
  54. {
  55. if ( K0 == 0 )
  56. {
  57. delay( 10 );
  58. if ( K0 == 0 )
  59. {
  60. while ( !K0 )
  61. ;
  62. TR0 = 1;
  63. s = time;
  64. tt = 0;
  65. flag = 1;
  66. s_flag = 1;
  67. b_flag = 1;
  68. num = 0;
  69. beep = 1;
  70. rled = 1;
  71. fall_flag = 0; /* 清除违规标志位 */
  72. K_startcountflag = 0;
  73. K_timecountflag = 0;
  74. }
  75. }
  76. if ( K_Time == 0 )
  77. {
  78. delay( 10 );
  79. if ( K_Time == 0 )
  80. {
  81. TR0 = 0;
  82. TR1 = 1;
  83. s = time;
  84. flag = 0;
  85. s_flag = 1;
  86. num = 0;
  87. rled = 1;
  88. fall_flag = 0; /* 清除违规标志位 */
  89. K_startcountflag = 0;
  90. K_timecountflag = 0;
  91. }
  92. }
  93. }
  94. void WRITE_A_BYTE( uchar input )
  95. {
  96. uchar t;
  97. for ( t = 8; t != 0; t-- )
  98. {
  99. sda = (bit) (input & 0x80);
  100. delay( M );
  101. scl = 1;
  102. delay( M );
  103. input = input << 1;
  104. scl = 0;
  105. delay( M );
  106. }
  107. }
  108. void Scan( void ) /* 八路热键扫描(哪个键先按下,哪个优先级最高) */
  109. {
  110. if ( K1 == 0 )
  111. {
  112. delay( 10 );
  113. if ( K1 == 0 )
  114. {
  115. while ( !K1 )
  116. ;
  117. num = 1; /* 数码管显示1"1" */
  118. TR0 = 0; /* 关闭定时器0,时间停止 */
  119. TR1 = 1; /* 打开定时器1,使扬声器响一声 */
  120. s_flag = 0; /* 关闭开始键标志位,使再按其他七个键不会响应 */
  121. }
  122. }
  123. if ( K2 == 0 ) /*下面七个键的处理同上 */
  124. {
  125. delay( 10 );
  126. if ( K2 == 0 )
  127. {
  128. while ( !K2 )
  129. ;
  130. num = 2;
  131. TR0 = 0;
  132. TR1 = 1;
  133. s_flag = 0; /* 重要 */
  134. }
  135. }
  136. if ( K3 == 0 )
  137. {
  138. delay( 10 );
  139. if ( K3 == 0 )
  140. {
  141. while ( !K3 )
  142. ;
  143. num = 3;
  144. TR0 = 0;
  145. TR1 = 1;
  146. s_flag = 0;
  147. }
  148. }
  149. if ( K4 == 0 )
  150. {
  151. delay( 10 );
  152. if ( K4 == 0 )
  153. {
  154. while ( !K4 )
  155. ;
  156. num = 4;
  157. TR0 = 0;
  158. TR1 = 1;
  159. s_flag = 0;
  160. }
  161. }
  162. if ( K5 == 0 )
  163. {
  164. delay( 10 );
  165. if ( K5 == 0 )
  166. {
  167. while ( !K5 )
  168. ;
  169. num = 5;
  170. TR0 = 0;
  171. TR1 = 1;
  172. s_flag = 0;
  173. }
  174. }
  175. if ( K6 == 0 )
  176. {
  177. delay( 10 );
  178. if ( K6 == 0 )
  179. {
  180. while ( !K6 )
  181. ;
  182. num = 6;
  183. TR0 = 0;
  184. TR1 = 1;
  185. s_flag = 0;
  186. }
  187. }
  188. if ( K7 == 0 )
  189. {
  190. delay( 10 );
  191. if ( K7 == 0 )
  192. {
  193. while ( !K7 )
  194. ;
  195. num = 7;
  196. TR0 = 0;
  197. TR1 = 1;
  198. s_flag = 0;
  199. }
  200. }
  201. if ( K8 == 0 )
  202. {
  203. delay( 10 );
  204. if ( K8 == 0 )
  205. {
  206. while ( !K8 )
  207. ;
  208. num = 8;
  209. TR0 = 0;
  210. TR1 = 1;
  211. s_flag = 0;
  212. }
  213. }
  214. }
  215. void display( void )
  216. {
  217. if ( flag == 1 ) /* 开始键按下,开始计时抢答 */
  218. {
  219. if ( num != 0 ) /* 如果有人抢答,则显示相应的几号 */
  220. {
  221. P0 = tabledu[num]; /* 显示几号抢到了 */
  222. P2 = tablewe[0];
  223. delay( 2 );
  224. P0 = 0;
  225. P2 = 0XFF;
  226. }else { /* 否则没人抢答,则前面不显示几号 */
  227. P0 = 0;
  228. P2 = 0XFF;
  229. }
  230. P0 = tabledu[s / 10]; /*下面为显示抢答倒计时 */
  231. P2 = tablewe[2];
  232. delay( 2 );
  233. P0 = 0;
  234. P2 = 0XFF;
  235. P0 = tabledu[s % 10];
  236. P2 = tablewe[3];
  237. delay( 2 );
  238. P2 = 0XFF;
  239. P0 = 0;
  240. }else { /* 如果开始键没有按下,则显示F FF(若有违规者,则显示违规号码及 FF)或时间调整 */
  241. if ( fall_flag == 1 ) /* 违规显示 */
  242. {
  243. if ( num != 0 )
  244. {
  245. P0 = tabledu[num]; /* 显示几号违规了 */
  246. P2 = tablewe[0];
  247. delay( 2 );
  248. P0 = 0;
  249. P2 = 0XFF;
  250. P0 = tabledu[15]; /*下面显示 FF,表示违规了 */
  251. P2 = tablewe[2];
  252. delay( 2 );
  253. P0 = 0; /* 消隐 */
  254. P2 = 0XFF;
  255. P0 = tabledu[15];
  256. P2 = tablewe[3];
  257. delay( 2 );
  258. P0 = 0;
  259. P2 = 0XFF;
  260. }else {
  261. P0 = 0;
  262. P2 = 0XFF;
  263. }
  264. }else { /* 没有人违规才显示调整时间 */
  265. if ( K_startcountflag == 1 )
  266. {
  267. P0 = 0X6D; /* 第一位数码管显示"5(S)"(表示抢答时间调整) */
  268. P2 = tablewe[0];
  269. delay( 2 );
  270. P0 = 0;
  271. P2 = 0XFF;
  272. P0 = tabledu[time / 10]; /*下面显示调整的抢答时间 */
  273. P2 = tablewe[2];
  274. delay( 2 );
  275. P0 = 0;
  276. P2 = 0XFF;
  277. P0 = tabledu[time % 10];
  278. P2 = tablewe[3];
  279. delay( 2 );
  280. P0 = 0;
  281. P2 = 0XFF;
  282. }else { /* 否则显示0 00 */
  283. P0 = tabledu[0];
  284. P2 = tablewe[0];
  285. delay( 2 );
  286. P0 = 0;
  287. P0 = tabledu[0];
  288. P2 = tablewe[2];
  289. delay( 2 );
  290. P0 = 0; /* 消隐 */
  291. P2 = 0XFF;
  292. P0 = tabledu[0];
  293. P2 = tablewe[3];
  294. delay( 2 );
  295. P0 = 0;
  296. P2 = 0XFF;
  297. }
  298. }
  299. }
  300. }

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

闽ICP备14008679号