当前位置:   article > 正文

【单片机】定时炸弹倒计时

【单片机】定时炸弹倒计时
  1. #include <reg52.h>
  2. #include "./delay/delay.h"
  3. #define TIMEPORT P0
  4. sbit bit_select = P2^0;
  5. sbit seg_select = P2^1;
  6. sbit beep = P2^2;
  7. code unsigned char timedata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  8. code unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
  9. unsigned disdata[8];
  10. unsigned char ms = 1;
  11. unsigned char sec = 10;
  12. unsigned char min = 0;
  13. void timer0_init();
  14. void timer1_init();
  15. void display();
  16. void change_time();
  17. void main()
  18. {
  19. unsigned char i;
  20. unsigned char cont;
  21. timer0_init();
  22. timer1_init();
  23. while(1)
  24. {
  25. for(cont = 9; cont > 0; cont--)
  26. {
  27. if(cont == sec && 99 == ms)
  28. {
  29. for(i = 0; i < 200; i++)
  30. {
  31. beep = ~beep;
  32. delay_us(100);
  33. }
  34. }
  35. }
  36. if(0 == sec)
  37. {
  38. for(i = 0; i < 200; i++)
  39. {
  40. beep = ~beep;
  41. delay_us(200);
  42. }
  43. }
  44. }
  45. }
  46. /*
  47. ===================
  48. 功能:初始化中断0
  49. ===================
  50. */
  51. void timer0_init()
  52. {
  53. EA = 1;
  54. TMOD |= 0x01;
  55. TH0 = (65536 - 1000) / 256;
  56. TL0 = (65536 - 1000) % 256;
  57. ET0 = 1;
  58. TR0 = 1;
  59. }
  60. /*
  61. ===================
  62. 功能:初始化中断1
  63. ===================
  64. */
  65. void timer1_init()
  66. {
  67. EA = 1;
  68. TMOD |= 0x10;
  69. TH1 = (65536 - 2000) / 256;
  70. TL1 = (65536 - 2000) % 256;
  71. ET1 = 1;
  72. TR1 = 1;
  73. }
  74. /*
  75. ==========================
  76. 功能:中断1服务函数,显示
  77. ==========================
  78. */
  79. void timer1_isr() interrupt 3
  80. {
  81. TH1 = (65536 - 2000) / 256;
  82. TL1 = (65536 - 2000) % 256;
  83. display();
  84. }
  85. /*
  86. ==========================
  87. 功能:中断0服务函数,计数
  88. ==========================
  89. */
  90. void timer0_isr() interrupt 1
  91. {
  92. static unsigned char i = 0;
  93. TH0 = (65536 - 1000) / 256;
  94. TL0 = (65536 - 1000) % 256;
  95. i++;
  96. if(10 == i)
  97. {
  98. i = 0;
  99. ms--;
  100. if(0 == ms)
  101. {
  102. ms = 99;
  103. sec--;
  104. if(0 == sec)
  105. {
  106. sec = 0;
  107. ms = 0;
  108. TR0 = 0;
  109. }
  110. }
  111. }
  112. change_time(); //修改显示时间
  113. }
  114. /*
  115. ======================
  116. 功能:修改时间显示值
  117. ======================
  118. */
  119. void change_time()
  120. {
  121. disdata[0] = timedata[min / 10];
  122. disdata[1] = timedata[min % 10];
  123. disdata[2] = 0x40;
  124. disdata[3] = timedata[sec / 10];
  125. disdata[4] = timedata[sec % 10];
  126. disdata[5] = 0x40;
  127. disdata[6] = timedata[ms / 10];
  128. disdata[7] = timedata[ms % 10];
  129. }
  130. /*
  131. ==========================
  132. 功能:动态扫描,显示数据
  133. ==========================
  134. */
  135. void display()
  136. {
  137. static unsigned char i = 0;
  138. TIMEPORT = 0x00; //清空数据,防止重影
  139. seg_select = 1;
  140. seg_select = 0;
  141. TIMEPORT = 0xff; //清空数据,防止重影
  142. bit_select = 1;
  143. bit_select = 0;
  144. TIMEPORT = disdata[i]; //取出显示值
  145. seg_select = 1;
  146. seg_select = 0;
  147. TIMEPORT = bitdata[i]; //取出位码
  148. bit_select = 1;
  149. bit_select = 0;
  150. i++;
  151. if(8 == i)
  152. {
  153. i = 0;
  154. }
  155. }

 

 

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

闽ICP备14008679号