找回密码
 立即注册

QQ登录

只需一步,快速开始

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

Prteus秒表设计LCD实现

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

/*txt1.c*/
  1. #include <reg52.h>
  2. #include<intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. #define DelayNOP(){_nop_();_nop_();_nop_();_nop_();}
  6. uchar Time_Buffer[]={0,0,0,0};
  7. uchar LCD_Display_Buffer[] = {"00:00:00:00"};
  8. void LCD_Initialize();
  9. void LCD_Set_POS(uchar);
  10. void LCD_Write_Data(uchar);
  11. void Display_String(uchar *,uchar);
  12. void Beep();
  13. sbit K1=P1^0;
  14. sbit K2=P1^1;
  15. sbit BEEP=P3^0;
  16. sbit LCD_RS=P2^0;
  17. sbit LCD_RW=P2^1;
  18. sbit LCD_EN=P2^2;
  19. char KeyCount=0;
  20. uchar code msg1[]= {"Second Watch 0 "};
  21. uchar code msg2[]= {"####       "};
  22. uchar code Prompts[][16]=
  23. {{"##1--> "},{"##1-->##2    "},{"##3-->    "},{"##3-->4   "}};

  24. void Beep()
  25. {
  26. uchar i,j=70;
  27. for(i=0;i<180;i++)
  28. {while(--j);BEEP=~BEEP;}
  29. BEEP=0;}
  30. void DelayX(uint MS)
  31. {
  32. uchar i;
  33. while(MS--) for(i=0;i<120;i++);}
  34. void Show_Second()
  35. {
  36. uchar i;
  37. LCD_Set_POS(0X45); //设置 LCD显示起点
  38. for (i=3;i!=0xff;i--)
  39. {
  40. //将两位整数的1、100s, 秒,分,时转换为8位数字字符
  41. LCD_Display_Buffer[2*i+1]=Time_Buffer[i]/10+'0';
  42. LCD_Display_Buffer[2*i]=Time_Buffer[i]%10+'0';
  43. //在i=3,2,1,0时分别显示时,分,秒,1/100s
  44. LCD_Write_Data(LCD_Display_Buffer[2*i+1]);
  45. LCD_Write_Data(LCD_Display_Buffer[2*i]);
  46. LCD_Write_Data(':');
  47. }}
  48. //Time0中断
  49. void Time0() interrupt 1 using 0
  50. {
  51. TH0=-10000/256;
  52. TL0=-10000%256;
  53. Time_Buffer[0]++;
  54. if(Time_Buffer[0]==100)
  55. {
  56. Time_Buffer[0]=0; Time_Buffer[1]++;
  57. }
  58. if(Time_Buffer[1]==60)//秒
  59. {
  60. Time_Buffer[1]=0; Time_Buffer[2]++;
  61. }
  62. if(Time_Buffer[2]==60)  //分
  63. {
  64. Time_Buffer[2]=0; Time_Buffer[3]++;
  65. }
  66. if(Time_Buffer[3]==24)  //时
  67. Time_Buffer[3]=0;
  68. }
  69. void main()
  70. {
  71. uchar i;
  72. IE=0x82;
  73. TMOD=0x01;
  74. TH0=-10000/256;
  75. TL0=-10000%256;
  76. LCD_Initialize();
  77. Display_String(msg1,0x00);
  78. Display_String(msg2,0x40);
  79. while(1)
  80. {
  81. if(K1==0)
  82. {
  83. DelayX(100);
  84. i=++KeyCount;        
  85. switch(i)
  86. {
  87. case 1:        
  88. case 3: TR0=1;
  89. Display_String(Prompts[i-1],0);
  90. break;
  91. case 2:        
  92. case 4: TR0=0;
  93. Display_String(Prompts[i-1],0);
  94. break;
  95. default:TR0=0;
  96. break;
  97. }
  98. while (K1==0);
  99. Beep();
  100. }
  101. else
  102. if(K2==0)
  103. {
  104. TR0=0;
  105. KeyCount=0;
  106. for(i=0;i<4;i++)
  107. Time_Buffer[i]=0;
  108. Display_String(msg1,0);
  109. Beep();
  110. DelayX(100);
  111. while(K2==0);
  112. }
  113. Show_Second();
  114. }
  115. }

  116. /*txt2.c*/
  117. #include <reg52.h>
  118. #include<intrins.h>
  119. #define uchar unsigned char
  120. #define uint unsigned int
  121. #define DelayNOP(){_nop_();_nop_();_nop_();_nop_();}
  122. sbit LCD_RS=P2^0;
  123. sbit LCD_RW=P2^1;
  124. sbit LCD_EN=P2^2;
  125. bit LCD_Busy_Check();
  126. void LCD_Initialize();
  127. void LCD_Write_Data(uchar);
  128. void LCD_Write_Command(uchar);

  129. void DelayMS(unit);
  130. void DelayMS(uint MS)
  131. {
  132. uchar t;
  133. while(MS--) for(t=0;t<120;t++);
  134. }
  135. bit LCD_Busy_Check()
  136. {
  137. bit Result;
  138. LCD_RS=0;LCD_RW=1;LCD_EN=1;
  139. DelayNOP();
  140. Result=(bit)(P0&0x80);
  141. LCD_EN=0;
  142. return Result;
  143. }
  144. void LCD_Write_Command(uchar cmd)
  145. {
  146. while(LCD_Busy_Check());
  147. LCD_RS=0;LCD_RW=0;LCD_EN=0;
  148. _nop_();_nop_();
  149. P0=cmd;DelayNOP();
  150. LCD_EN=1;DelayNOP();
  151. LCD_EN=0;
  152. }
  153. void LCD_Write_Data(uchar str)
  154. {
  155. while(LCD_Busy_Check());
  156. LCD_RS=1;LCD_RW=0;LCD_EN=0;
  157. P0=str;DelayNOP();
  158. LCD_EN=1;DelayNOP();
  159. LCD_EN=0;
  160. }
  161. void LCD_Initialize()
  162. {
  163. DelayMS(5);LCD_Write_Command(0x38);
  164. DelayMS(5);LCD_Write_Command(0x0c);
  165. DelayMS(5);LCD_Write_Command(0x06);
  166. DelayMS(5);LCD_Write_Command(0x01);
  167. DelayMS(5);
  168. }
  169. void LCD_Set_POS(uchar Position)
  170. {
  171. LCD_Write_Command(Position|0x80);
  172. }
  173. void Display_String(uchar*str,uchar LineNo)
  174. {
  175. uchar k;
  176. LCD_Set_POS(LineNo);
  177. for(k=0;k<16;k++)
  178. LCD_Write_Data(str[k]);
  179. }
复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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