找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2030|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

AD轉(zhuǎn)換,單片機(jī)C程序現(xiàn)在還缺什么步驟?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主


  1. #include <reg51.h>
  2. #include <intrins.h>   //空操作函數(shù)_nop_()必須的頭文件
  3. #include <stdio.h>
  4. #define uint unsigned int
  5. #define uchar unsigned char
  6. #ifndef __1602_H__
  7. #define __1602_H__
  8. sbit lcdrs=P2^0; //1602:0寫指令,1寫數(shù)據(jù)
  9. sbit lcdwr=P2^1; //1602讀寫信號(hào)
  10. sbit lcden=P2^2; //1602片選信號(hào)
  11. sbit TLC549_CS = P3^1;  //TLC549片選,低電平有效
  12. sbit TLC549_DO = P3^5;  //TLC549數(shù)字量輸出
  13. sbit TLC549_CLK = P3^0;  //TLC549片外獨(dú)立時(shí)鐘
  14. sbit sta7=P0^7;
  15. unsigned char data1_convert(unsigned char dat_temp)
  16. {
  17. unsigned char data1;  
  18. data1=(unsigned char)(((float)dat_temp/255)*5); //換算為電壓值的個(gè)位值
  19. data1=data1+48; //轉(zhuǎn)換為對(duì)應(yīng)的ASCII碼,因?yàn)?對(duì)應(yīng)ASCII碼的48,以此類推
  20. return data1;   
  21. }
  22. unsigned char data0_convert(unsigned char dat_temp)
  23. {  
  24. unsigned char data0,data1;  data1=(unsigned char)(((float)dat_temp/255)*5);     //換算為電壓值的個(gè)位值
  25. data0=(unsigned char)((((float)dat_temp/255)*5-data1)*10);//換算為為電壓值的小--  
  26.                                                         //--數(shù)點(diǎn)后第一位的那個(gè)?
  27. data0=data0+48;            
  28. return data0;             //轉(zhuǎn)換為對(duì)應(yīng)的ASCII碼
  29. }
  30. void delayus(unsigned int t);  //聲明 延時(shí)t個(gè)機(jī)器 周期
  31. //**********************************
  32. //AD轉(zhuǎn)換
  33. //**********************************
  34. unsigned char TLC549_read()   
  35. {  
  36. unsigned char i , temp;  
  37. TLC549_CS = 1;    //關(guān)閉片選  
  38. TLC549_CLK = 0;    //時(shí)鐘線拉低  
  39. TLC549_CS = 0;    //打開片選  
  40. _nop_();  //空操作等待兩個(gè)機(jī)器周期待總線穩(wěn)定
  41. _nop_();
  42. for( i = 0; i < 8; i++ )  //循環(huán)8次讀出8位轉(zhuǎn)換結(jié)果
  43. {   
  44. temp <<= 1;   
  45. temp |= TLC549_DO;  
  46. TLC549_CLK = 1;
  47. _nop_();   
  48. TLC549_CLK = 0;
  49. }
  50. TLC549_CS = 1;  
  51. delayus(14);  //TLC549轉(zhuǎn)換速度小于14微秒,大致延時(shí)14微秒等待轉(zhuǎn)換結(jié)束  
  52. return temp; //返回轉(zhuǎn)換值
  53. }
  54. //**********************************
  55. //AD轉(zhuǎn)換
  56. //**********************************
  57. /*uint n1,ad1;  
  58. uint read_549(void)
  59. {  
  60. uchar i,value;
  61. TLC549_CS=0;
  62. TLC549_CLK=0;
  63. for(i=0;i<8;i++)
  64. {
  65. value=value<<1;
  66. value|=TLC549_DO;
  67. TLC549_CLK=1;
  68. delayus(1);
  69. TLC549_CLK=0;
  70. }
  71. TLC549_CLK=1;
  72. delayus(2);  
  73. return value;
  74. }  
  75. void zhh()
  76. {
  77. uint temp,n2;
  78. n2=(n1/10)%10;
  79. temp=read_549();
  80. n1=temp*3;
  81. if((n1/10)%10-n2==1)
  82. {  
  83. ad1=800;  
  84. }   
  85. }
  86. */
  87. /***********************1ms延時(shí)函數(shù)**********************/
  88. void delayms(uint ms)
  89. {
  90. uint y;
  91. while(ms--)
  92. {
  93. for(y=0;y<125;y++);
  94. }
  95. }
  96. //******************判忙*********************
  97. void lcd_busy_check(void)   //1602忙信號(hào)檢測(cè),忙則等待
  98. {  
  99. P0=0xff;   
  100. do  
  101. {   
  102. lcdrs=0;   //讀狀態(tài)操作,為0   
  103. lcdwr=1;      //讀操作為1   
  104. lcden=0;   
  105. lcden=1;   //讀狀態(tài),需為高電平
  106. }   
  107. while (sta7==1);  //如果為1則忙,等待...直到為0   
  108. lcden=0;
  109. }
  110. /***********************LCD寫指令 寫數(shù)據(jù)**********************/
  111. void LCD_write_cmd(unsigned char cmd) //液晶寫?
  112. {  
  113. lcd_busy_check(); //每次操作之前都要進(jìn)行忙信號(hào)檢測(cè)
  114. lcdrs=0;   //執(zhí)行數(shù)據(jù)操作,為1  
  115. lcdwr=0;   //寫操作,為0  
  116. P0=cmd;    //送數(shù)據(jù)到液晶數(shù)據(jù)端口P0,準(zhǔn)備執(zhí)行數(shù)據(jù)操?
  117. lcden=1;   //高電平,數(shù)據(jù)送入液晶液晶數(shù)據(jù)RAM  
  118. _nop_();  //保持一會(huì)兒,使顯示數(shù)據(jù)可靠地送入液晶數(shù)據(jù)RAM  
  119. lcden=0;   //低電平,顯示數(shù)據(jù)
  120. }
  121. void LCD_write_data(unsigned char Data) //液晶寫數(shù)據(jù)函數(shù)
  122. {  
  123. lcd_busy_check(); //每次操作之前都要進(jìn)行忙信號(hào)檢測(cè)
  124. lcdrs=1;   //執(zhí)行數(shù)據(jù)操作,為1  
  125. lcdwr=0;   //寫操作,為0  
  126. P0=Data;    //送數(shù)據(jù)到液晶數(shù)據(jù)端口P0,準(zhǔn)備執(zhí)行數(shù)據(jù)操作

  127. lcden=1;   //高電平,數(shù)據(jù)送入液晶液晶數(shù)據(jù)RAM  
  128. _nop_();   //保持一會(huì)兒,使顯示數(shù)據(jù)可靠地送入液晶數(shù)據(jù)RAM  
  129. lcden=0;   //低電平,顯示數(shù)據(jù)
  130. }
  131. /***********************LCD初始化*********************/
  132. void LCD_init()  
  133. {  
  134. delayms(15);
  135. LCD_write_cmd(0x38);
  136. delayms(5);
  137. LCD_write_cmd(0x38);//8位總線、雙行5×7點(diǎn)陣
  138. LCD_write_cmd(0x08);
  139. LCD_write_cmd(0x01);
  140. LCD_write_cmd(0x06);
  141. delayms(5);
  142. LCD_write_cmd(0x0c);
  143. }
  144. /**********************主函數(shù)*********************/
  145. void main()  
  146. {  
  147. unsigned char dat;   //用于接收從8591中讀到的數(shù)據(jù)
  148. LCD_init();
  149. LCD_write_cmd(0x80);  
  150. LCD_write_data('C');  
  151. LCD_write_data('H');  
  152. LCD_write_data('1');  
  153. LCD_write_data(':');  
  154. LCD_write_cmd(0x85);
  155. LCD_write_data('.');
  156. LCD_write_cmd(0x87);
  157. LCD_write_data('V');
  158. while(1)  
  159. {   
  160. LCD_write_cmd(0x84);                 //將要顯示的字符顯示在此地址處
  161. LCD_write_data(data1_convert(dat));  //顯示電壓的整數(shù)部分
  162. LCD_write_cmd(0x86);                //將要顯示的字符顯示在此地址處
  163. LCD_write_data(data0_convert(dat));  //顯示電壓的小數(shù)部分
  164. }
  165. }
  166. #endif
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:276150 發(fā)表于 2018-1-17 11:53 | 只看該作者
主要是main函數(shù)怎么寫
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表