找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

單片機(jī)小白求助,用msp430單片機(jī)驅(qū)動DS18B20

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1063907 發(fā)表于 2023-2-22 17:01 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
始終顯示讀到高電平,多次檢查時序不知道問題出在哪里,這是我的代碼

單片機(jī)源程序如下:
  1. #include "tempature.h"
  2. #include "msp430f5529_gpio.h"

  3. /*******************************************
  4. 函數(shù)名稱: B20_Init
  5. 功    能: 復(fù)位DS18B20
  6. 參    數(shù): 無
  7. 返回值  : 無
  8. ********************************************/
  9. void B20_Init(void)

  10. {
  11. unsigned char flag=0;
  12.   GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPO);//P6.5輸出
  13.   DS18B20_OUT = 1;//總線拉高
  14.   DELAY_US(5);//稍微延時約5微秒
  15.   DS18B20_OUT = 0;//總線拉低
  16.   DELAY_US(500);//延時500微秒復(fù)位脈沖
  17.   DS18B20_OUT = 1;//總線拉高
  18.   DELAY_US(40);        //等待40微秒
  19.   GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPI);//P6.5輸入
  20.         
  21.         if(DS18B20_IN)//檢測是否為高電平
  22.                 flag=1;
  23.         else
  24.                 flag=0;
  25.         DELAY_US(480);//若讀到了數(shù)據(jù)線上的低電平“做延時,其延時的時間從發(fā)出的高電平算起,延時480微秒。
  26.         GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPO);
  27.         DS18B20_OUT = 1;//將數(shù)據(jù)線再次拉高到高電平“1”后結(jié)束
  28. }



  29. /*******************************************
  30. 函數(shù)名稱: B20_ReadByte
  31. 功    能: 讀取一個字節(jié)的數(shù)據(jù)
  32. 參    數(shù): 無
  33. 返回值  : data--返回的一個字節(jié)數(shù)據(jù)
  34. ********************************************/
  35. uint8_t B20_ReadByte(void)
  36. {
  37. uint8_t i,val=0;
  38. for(i=0;i < 8;i++)     //位計數(shù)值
  39. {
  40. val>>=1;      //右移,準(zhǔn)備接受新的數(shù)據(jù)位
  41. GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPO);
  42.   DS18B20_OUT = 1;  //拉高
  43.   DELAY_US(2);      //等待5微秒
  44. DS18B20_OUT = 0;//拉低,啟動讀數(shù)據(jù)位
  45.    DELAY_US(2);      //等待5微秒
  46.   DS18B20_OUT = 1;  //釋放總線
  47.   DELAY_US(4);      //等待5微秒
  48.   GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPI);         
  49.   if(DS18B20_IN)//該位是否為高
  50.    val|=0x80;     //是就將此位置高
  51.   GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPO);
  52.   DELAY_US(60);     //等待50微秒
  53. }
  54. return val;     //將讀到的一個字節(jié)返回
  55.    

  56. }
  57. /*******************************************
  58. 函數(shù)名稱: B20_WriteByte
  59. 功    能: 寫入一個字節(jié)的數(shù)據(jù)
  60. 參    數(shù): data--要寫入的數(shù)據(jù)
  61. 返回值  : 無
  62. ********************************************/
  63. void B20_WriteByte(uint8_t data)
  64. {
  65. uint8_t i;
  66.         
  67. for(i=0;i<8;i++)            //位計數(shù)值
  68. {
  69.    GPIO_Init(DS18B20_PORT,DS18B20_PIN,GPO);
  70.   DS18B20_OUT = 0;//拉低,啟動寫數(shù)據(jù)位
  71.   DELAY_US(2);      //等待1微秒
  72.   if(data&0x01)     //此位數(shù)據(jù)是否為高
  73.   {
  74.    DS18B20_OUT = 1;//是高則將單總線拉高
  75.   }
  76.   else
  77.   {
  78.    DS18B20_OUT = 0;//是低則將單總線拉低
  79.   }
  80.   DELAY_US(60);      //等待50微秒
  81.   data>>=1;      //右移,為寫入新的數(shù)據(jù)位做準(zhǔn)備
  82. }
  83.   DS18B20_OUT = 1;//釋放總線
  84. }
  85. /*******************************************
  86. 函數(shù)名稱: B20_Read_temp
  87. 功    能: 讀取溫度值
  88. 參    數(shù): 無
  89. 返回值  : 溫度值(單位:攝氏度)
  90. ********************************************/
  91. float B20_Read_temp(void)
  92.         
  93. {
  94. uint8_t templ,temph;
  95. uint16_t temp;
  96. float tempature;
  97. Init_DS18b20();
  98. templ=B20_ReadByte();  
  99. temph=B20_ReadByte();  
  100. temp = (temph<<8)|templ;//合并為16位
  101. tempature = temp*0.0625;//轉(zhuǎn)換為溫度值
  102. temp = (int)(tempature*10+0.5);//精確到小數(shù)點(diǎn)后1位
  103. return temp;        //返回數(shù)據(jù)
  104. }

  105. void Init_DS18b20()
  106. {
  107.         B20_Init();            //復(fù)位18b20
  108.         DELAY_MS(2);
  109.         B20_WriteByte(0xcc);    //跳過ROM
  110.         B20_WriteByte(0x44);    //啟動溫度轉(zhuǎn)換
  111. }
復(fù)制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:1063907 發(fā)表于 2023-2-23 18:32 | 只看該作者
更新:在復(fù)位函數(shù)里怎么調(diào)整時序都沒辦法檢測到應(yīng)答的低電平
  1. /*****************************************************************************
  2. **   對DS18b20進(jìn)行復(fù)位
  3. ******************************************************************************/
  4. void DS_reset()
  5. {
  6.         unsigned char flag=0;
  7.         
  8.         DS_SET_OUT;//設(shè)置為輸出
  9.         DS_1;//輸出高電平
  10.         delay_us(15);
  11.         DS_0;//拉低延時750微秒
  12.         delay_us(750);
  13.         DS_1;
  14.         delay_us(1);
  15.         DS_SET_IN;//讀取電平
  16.         while(DS_IN&&flag<240){
  17.           delay_us(1);
  18.           flag++;
  19.         }
  20.         DS_SET_OUT;
  21.         DS_1;
  22. }
復(fù)制代碼
回復(fù)

使用道具 舉報

板凳
ID:1063907 發(fā)表于 2023-2-23 18:34 | 只看該作者
重寫了一遍,始終讀不到低電平
  1. /*****************************************************************************
  2. **   對DS18b20進(jìn)行復(fù)位
  3. ******************************************************************************/
  4. void DS_reset()
  5. {
  6.         unsigned char flag=0;
  7.         
  8.         DS_SET_OUT;
  9.         DS_1;
  10.         delay_us(15);
  11.         DS_0;
  12.         delay_us(750);
  13.         DS_1;
  14.         delay_us(15);
  15.         DS_SET_IN;
  16.         while(DS_IN&&flag<240){
  17.           delay_us(1);
  18.           flag++;
  19.         }
  20.         DS_SET_OUT;
  21.         DS_1;
  22. }
復(fù)制代碼
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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