找回密码
 立即注册

QQ登录

只需一步,快速开始

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

51单片机 12864显示温湿度

[复制链接]
跳转到指定楼层
楼主
ID:125275 发表于 2016-9-13 14:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. #define noACK 0
  6. #define ACK 1
  7. #define STATUS_REG_W 0x06
  8. #define STATUS_REG_R 0x07
  9. #define MEASURE_TEMP 0x03
  10. #define MEASURE_HUMI 0x05
  11. #define RESET 0x1e
  12. enum {TEMP,HUMI};
  13. typedef union  
  14. {
  15. unsigned int i;
  16. float f;
  17. }
  18. value;
  19. sbit lcdrs=P2^0;
  20. sbit lcdrw=P2^1;
  21. sbit lcden=P2^2;
  22. sbit SCK = P1^0;
  23. sbit DATA = P1^1;
  24. uchar table2[]="SHT11 ";
  25. uchar table3[]="";
  26. uchar table4[]="";
  27. uchar table5[]=".";
  28. uchar wendu[6];
  29. uchar shidu[6];
  30. void delay(int z)
  31. {
  32. int x,y;
  33. for(x=z;x>0;x--)
  34. for(y=125;y>0;y--);
  35. }

  36. void delay_50us(uint t)
  37. {
  38. uint j;
  39. for(;t>0;t--)
  40. for(j=19;j>0;j--);
  41. }
  42. void delay_50ms(uint t)
  43. {
  44. uint j;
  45. for(;t>0;t--)
  46. for(j=6245;j>0;j--);
  47. }

  48. void write_12864com(uchar com)
  49. {
  50. lcdrs=0;
  51. lcdrw=0;
  52. delay_50us(1);
  53. P0=com;
  54. lcden=1;
  55. delay_50us(10);
  56. lcden=0;
  57. delay_50us(2);
  58. }
  59. void write_dat(uchar dat)
  60. {
  61. lcdrs=1;
  62. lcdrw=0;
  63. delay_50us(1);
  64. P0=dat;
  65. lcden=1;
  66. delay_50us(10);
  67. lcden=0;
  68. delay_50us(2);
  69. }

  70. void init12864lcd(void)
  71. {
  72. delay_50ms(2);
  73. write_12864com(0x30);
  74. delay_50us(4);
  75. write_12864com(0x30);
  76. delay_50us(4);
  77. write_12864com(0x0f);
  78. delay_50us(4);
  79. write_12864com(0x01);
  80. delay_50us(240);
  81. write_12864com(0x06);
  82. delay_50us(10);
  83. write_12864com(0x0c);
  84. delay_50us(10);
  85. }

  86. void display1(void)
  87. {
  88. uchar i;
  89. write_12864com(0x80);
  90. for(i=0;i<18;i++)
  91. {
  92. write_dat(table2[i]);
  93. delay_50us(1);
  94. }
  95. }

  96. void display2(void)
  97. {
  98. uchar i;
  99. write_12864com(0x90);
  100. for(i=0;i<18;i++)
  101. {
  102. write_dat(table3[i]);
  103. delay_50us(1);
  104. }
  105. }
  106. void display3(void)
  107. {
  108. uchar i;
  109. write_12864com(0x88);
  110. for(i=0;i<8;i++)
  111. {
  112. write_dat(table4[i]);
  113. delay_50us(1);
  114. }
  115. }


  116. void displaywendu(void)
  117. {
  118. uchar i;
  119. write_12864com(0x94);
  120. for(i=0;i<3;i++)
  121. {
  122. write_dat(wendu[i]);
  123. delay_50us(1);
  124. }
  125. for(i=0;i<1;i++)
  126. {
  127. write_dat(table5[i]);
  128. delay_50us(1);
  129. }
  130. for(i=4;i<5;i++)
  131. {
  132. write_dat(wendu[i]);
  133. delay_50us(1);
  134. }
  135. }

  136. void displayshidu(void)
  137. {
  138. uchar i;
  139. write_12864com(0x8C);
  140. for(i=0;i<3;i++)
  141. {
  142. write_dat(shidu[i]);
  143. delay_50us(1);
  144. }
  145. for(i=0;i<1;i++)
  146. {
  147. write_dat(table5[i]);
  148. delay_50us(1);
  149. }
  150. for(i=4;i<5;i++)
  151. {
  152. write_dat(shidu[i]);
  153. delay_50us(1);
  154. }
  155. }
  156. char s_write_byte(unsigned char value)
  157. {
  158. unsigned char i,error=0;
  159. for (i=0x80;i>0;i>>=1)
  160. {
  161. if (i&value) DATA=1;
  162. else DATA=0;
  163. SCK=1;
  164. _nop_();
  165.   _nop_();
  166.   _nop_();
  167. SCK=0;
  168. }
  169. DATA=1;
  170. SCK=1;
  171. error=DATA;  
  172. _nop_();
  173. _nop_();
  174. _nop_();
  175. SCK=0;
  176. DATA=1;
  177. return error;
  178. }

  179. char s_read_byte(unsigned char ack)
  180. {
  181. unsigned char i,val=0;
  182. DATA=1;
  183. for(i=0x80;i>0;i>>=1)
  184. {
  185. SCK=1;
  186. if(DATA) val=(val|i);  
  187. SCK=0;
  188. }
  189. DATA=!ack;
  190. SCK=1;
  191. _nop_();
  192. _nop_();
  193. _nop_();
  194. SCK=0;
  195. _nop_();
  196. _nop_();
  197. _nop_();
  198. DATA=1;
  199. return val;
  200. }

  201. void s_transstart(void)
  202. {
  203. DATA=1; SCK=0;
  204. _nop_();
  205. SCK=1;
  206. _nop_();
  207. DATA=0;
  208. _nop_();
  209. SCK=0;
  210. _nop_();
  211. _nop_();
  212. _nop_();
  213. SCK=1;
  214. _nop_();
  215. DATA=1;
  216. _nop_();
  217. SCK=0;
  218. }

  219. void s_connectionreset(void)
  220. {
  221. unsigned char i;
  222. DATA=1; SCK=0;
  223. for(i=0;i<9;i++)
  224. {
  225. SCK=1;
  226. SCK=0;
  227. }
  228. s_transstart();
  229. }
  230. char s_measure(unsigned char *p_value, unsigned char *p_checksum,unsigned char mode)
  231. {
  232. unsigned error=0;
  233. unsigned int i;
  234. s_transstart();
  235. switch(mode)  
  236.   {
  237. //case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
  238. case HUMI : error+=s_write_byte(MEASURE_HUMI); break;  
  239. default : break;
  240. }
  241. for (i=0;i<65535;i++) if(DATA==0) break;
  242. if(DATA) error+=1;
  243. *(p_value) =s_read_byte(ACK);
  244. *(p_value+1)=s_read_byte(ACK);
  245.   *p_checksum =s_read_byte(noACK);
  246. return error;
  247. }
  248. void calc_sth10(float *p_humidity ,float *p_temperature)
  249. {
  250. const float C1=-4.0;
  251. const float C2=+0.0405;
  252. const float C3=-0.0000028;  
  253. const float T1=+0.01;
  254. const float T2=+0.00008;
  255. float rh=*p_humidity;
  256. float t=*p_temperature;
  257. float rh_lin;  
  258. float rh_true;
  259. float t_C;  
  260. t_C=t*0.01 - 40;
  261. rh_lin=C3*rh*rh + C2*rh + C1;
  262. rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;  
  263. if(rh_true>100)rh_true=100;
  264. if(rh_true<0.1)rh_true=0.1;
  265. *p_temperature=t_C;
  266. *p_humidity=rh_true;
  267. }
  268. void main(void)
  269. {
  270. unsigned int temp,humi;
  271. value humi_val,temp_val;
  272. unsigned char error;
  273. unsigned char checksum;  
  274. init12864lcd();
  275. display1();
  276. display2();
  277. display3();
  278. s_connectionreset();  
  279. while(1)
  280. {
  281. error=0;
  282. error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP);
  283. error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI);  
  284. if(error!=0) s_connectionreset();
  285. else
  286. {
  287. humi_val.f=(float)humi_val.i;
  288. temp_val.f=(float)temp_val.i;
  289. calc_sth10(&humi_val.f,&temp_val.f);
  290. temp=temp_val.f*10;
  291. humi=humi_val.f*10;
  292. wendu[0]=temp/1000+'0';
  293. wendu[1]=temp%1000/100+'0';
  294. wendu[2]=temp%100/10+'0';
  295. wendu[3]=0x2E;
  296. wendu[4]=temp%10+'0';
  297. displaywendu();
  298. shidu[0]=humi/1000+'0';
  299. shidu[1]=humi%1000/100+'0';
  300. shidu[2]=humi%100/10+'0';  
  301. shidu[3]=0x2E;
  302. shidu[4]=humi%10+'0';
  303. displayshidu();
  304. }
  305. delay(800);
  306. }
  307. }


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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