找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1907|回复: 0
打印 上一主题 下一主题
收起左侧

单片机方波发生器无法产生信号?

[复制链接]
跳转到指定楼层
楼主


单片机源码:
  1. #include<reg52.h>
  2. sbit LCD_RS=P1^0;
  3. sbit LCD_RW=P1^1;
  4. sbit LCD_E=P1^2;
  5. #define LCD_DATA P2
  6. #define LCD_BUSY 0x80
  7. #define uchar unsigned char
  8. #define uint unsigned int
  9. uint speed;
  10. uchar test=1;
  11. uchar normal=0;
  12. uchar nor_speed;
  13. uchar exc_speed;
  14. uchar cnt_ovtime;
  15. uint data dis[5]={0x00,0x00,0x00,0x00,0x00};
  16. void lcd_init(void);
  17. void display_string(uchar x,uchar y,uchar *s);
  18. void disp_selec(uchar bit_selec,uchar seg);
  19. void init_timer0();
  20. void init_timer1();
  21. void delay_ms(uint i)
  22. {
  23. uint j;
  24. while(i--)
  25. {
  26. for(j=0;j<125;j++);
  27. }
  28. }
  29. void dellay(uint h)
  30. {
  31. while(h--);
  32. }
  33. void init_timer0()
  34. {
  35. TMOD=0x51;
  36. TH0=0xD8;
  37. TL0=0xF0;
  38. ET0=1;
  39. EA=1;
  40. TR0=1;
  41. }
  42. void init_timer1()
  43. {
  44. TMOD=0x51;
  45. TH1=0x00;
  46. TL1=0x00;
  47. ET1=1;
  48. EA=1;
  49. TR1=1;
  50. }
  51. void it_timer0() interrupt 1
  52. {
  53. uchar counter;
  54. TF0=0;
  55. TH0=0xD8;
  56. TL0=0xF0;
  57. counter++;
  58. if(counter==100)
  59. {
  60. speed=(TH1*256+TL1+65536*cnt_ovtime)/100;
  61. if(speed>=1200)
  62. {
  63. exc_speed=1;
  64. nor_speed=0;
  65. }
  66. else
  67. {
  68. nor_speed=1;
  69. exc_speed=0;
  70. }
  71. TH1=TL1=0x00;
  72. counter=0;
  73. cnt_ovtime=0;
  74. }
  75. }
  76. void it_timer1()interrupt 3
  77. {
  78. TF1=0;
  79. cnt_ovtime++;
  80. }
  81. void WriteDataLcd(uchar wdata)
  82. {
  83. LCD_DATA=wdata;
  84. LCD_RS=1;
  85. LCD_RW=0;
  86. LCD_E=0;
  87. dellay(100);
  88. LCD_E=1;
  89. }
  90. void WriteCommandLcd(uchar wdata)
  91. {
  92. LCD_DATA=wdata;
  93. LCD_RS=0;
  94. LCD_RW=0;
  95. LCD_E=0;
  96. dellay(100);
  97. LCD_E=1;
  98. }
  99. void lcd_init(void)
  100. {
  101. LCD_DATA=0;
  102. WriteCommandLcd(0x38);
  103. dellay(1000);
  104. WriteCommandLcd(0x38);
  105. WriteCommandLcd(0x08);
  106. WriteCommandLcd(0x01);
  107. WriteCommandLcd(0x06);
  108. WriteCommandLcd(0x0c);
  109. }
  110. void display_xy(uchar x,uchar y)
  111. {
  112. if(y==1)
  113. x+=0x40;
  114. x+=0x80;
  115. WriteCommandLcd(x);
  116. }
  117. void display_char(uchar x,uchar y,uchar dat)
  118. {
  119. display_xy(x,y);
  120. WriteDataLcd(dat);
  121. }
  122. void display_string(uchar x,uchar y,uchar *s)
  123. {
  124. display_xy(x,y);
  125. while(*s)
  126. {
  127. WriteDataLcd(*s);
  128. s++;
  129. }
  130. }
  131. void main()
  132. {
  133. lcd_init();
  134. init_timer0();
  135. init_timer1();
  136. if(test)
  137. {
  138. P3 |=0x07;
  139. display_string(0,0,"Life's precious!");
  140. display_string(0,1,"Safe Driving!");
  141. delay_ms(2000);
  142. P3 &=~0x07;
  143. lcd_init();
  144. test=0;
  145. normal=1;
  146. delay_ms(2000);
  147. }
  148. if(normal)
  149. {
  150. while(1)
  151. {
  152. if(nor_speed)
  153. {
  154. P3 |=0x01;
  155. P3 &=~0x06;
  156. display_string(0,0,"Speed:");
  157. dis[3]=speed/1000;
  158. dis[4]=speed%1000;
  159. dis[2]=dis[4]/100;
  160. dis[4]=dis[4]%100;
  161. dis[1]=dis[4]/10;
  162. dis[0]=dis[4]%10;
  163. if(dis[3]!=0)
  164. {
  165. display_char(6,0,dis[3]+0x30);
  166. }
  167. else
  168. {
  169. display_char(6,0,0xA0);
  170. }
  171. display_char(7,0,dis[2]+0x30);
  172. display_char(8,0,dis[1]+0x30);
  173. display_char(10,0,dis[0]+0x30);
  174. display_string(9,0,".");
  175. display_string(11,0,"km/h");
  176. display_string(0,1,"Status:Normal");
  177. }
  178. if(exc_speed)
  179. {
  180. display_string(0,0,"Speed:");
  181. dis[3]=speed/1000;
  182. dis[4]=speed%1000;
  183. dis[2]=dis[4]/100;
  184. dis[4]=dis[4]%100;
  185. dis[1]=dis[4]/10;
  186. dis[0]=dis[4]%10;
  187. if(dis[3]!=0)
  188. {
  189. display_char(6,0,dis[3]+0x30);
  190. }
  191. else
  192. {
  193. display_char(6,0,0xA0);
  194. }
  195. display_char(7,0,dis[2]+0x30);
  196. display_char(8,0,dis[1]+0x30);
  197. display_char(10,0,dis[0]+0x30);
  198. display_string(9,0,".");
  199. display_string(11,0,"km/h");
  200. display_string(0,1,"Status:Speeding!");
  201. P3 &=~0x01;
  202. P3 |=0x06;
  203. delay_ms(500);
  204. display_string(0,1,"Warning!");
  205. P3 &=~0x06;
  206. dellay(500);
  207. }
  208. }
  209. }
  210. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表