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

QQ登錄

只需一步,快速開始

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

STM32單片機(jī)DS18B20測(cè)溫?cái)?shù)碼管顯示程序(不帶CRC、不帶零度以下溫度)有注釋

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:814785 發(fā)表于 2020-11-24 11:20 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

DS1302驅(qū)動(dòng)和數(shù)碼管驅(qū)動(dòng)程序如附件!
單片機(jī)源程序如下:
  1. #include "ds18b20.h"
  2. #include "display.h"
  3. #include "delay.h"
  4. void Ds18B20_Output()
  5. {
  6.         GPIO_InitTypeDef GPIO_InitStructure;
  7.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);                //使能PG時(shí)鐘
  8.         
  9.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;                                        //PG11口
  10.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;                                //設(shè)定I/O口推挽輸出
  11.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  12.         GPIO_Init(GPIOG,&GPIO_InitStructure);
  13. }
  14. void Ds18B20_Input()
  15. {
  16.         GPIO_InitTypeDef GPIO_InitStructure;
  17.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);                //使能PG時(shí)鐘
  18.         
  19.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;                                        //PG11口
  20.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;                        //浮空輸入
  21.         GPIO_Init(GPIOG,&GPIO_InitStructure);        
  22. }

  23. void Ds18b20_Reset(void)
  24. {
  25.         Ds18B20_Output();                //將PG11配置為推挽輸出
  26.         DQ_Write_1;                                //拉高總線
  27.         delayus(1);
  28.         DQ_Write_0;                                //拉低總線
  29.         delayus(480);                        //延時(shí),拉低總線480~960us
  30.         DQ_Write_1;                                //釋放總線
  31.         Ds18B20_Input();                //DQ改為輸入模式
  32.         delayus(40);                        //延時(shí)約60us
  33.         while((DQ_ReadBit));        //等待從機(jī)DS18B20應(yīng)答
  34.         while(!(DQ_ReadBit));        //等待應(yīng)答信號(hào)結(jié)束,釋放總線        
  35. }
  36. void Ds18b20_Write(u8 dat)
  37. {
  38.         u8 m0;
  39.         Ds18B20_Output();                //將PG11配置為推挽輸出
  40.         for(m0=0;m0<8;m0++)
  41.         {
  42.                 DQ_Write_0;                                //拉低總線
  43.                 delayus(10);                        //延時(shí)10us,最大不超過(guò)15us
  44.                 if(dat&0x01)
  45.                         DQ_Write_1;
  46.                 else
  47.                         DQ_Write_0;
  48.                 delayus(30);                        //延時(shí)40us
  49.                 DQ_Write_1;                                //釋放總線
  50.                 delayus(1);                                //兩個(gè)寫之間,間隔至少1us
  51.                 dat>>=1;                                //右移1位,
  52.         }
  53. }
  54. u8 Ds18b20_Read(void)
  55. {
  56.         u8 m0,temp0;
  57.         for(m0=0;m0<8;m0++)
  58.         {
  59.                 temp0>>=1;                //數(shù)據(jù)右移一位
  60.                 Ds18B20_Output();                //將PG11配置為推挽輸出
  61.                 DQ_Write_0;                                //拉低總線,啟動(dòng)
  62.                 DQ_Write_1;                                //釋放總線
  63.                 if(DQ_ReadBit==1)
  64.                         temp0|=0x80;
  65.                 delayus(40);
  66.         }
  67.         return temp0;
  68. }
  69. void temperature(void)
  70. {
  71.         u16 temp1,temp2;
  72.         Ds18b20_Reset();
  73.         Ds18b20_Write(0xcc);                //跳過(guò)ROM
  74.         Ds18b20_Write(0x44);                //溫度轉(zhuǎn)換

  75.         Ds18b20_Reset();
  76.         Ds18b20_Write(0xcc);                //跳過(guò)ROM
  77.         Ds18b20_Write(0xbe);                //讀取RAM
  78.         
  79.         temp1=Ds18b20_Read();                //讀取低8位
  80.         temp2=Ds18b20_Read();                //讀取高8位
  81.         Ds18b20_Reset();                        //復(fù)位,表示讀取結(jié)束
  82.         display(((temp2<<8)|temp1)*0.0625);
  83. }
復(fù)制代碼



注意缺少main函數(shù)等,介意的就不要下載了: HARDWARE.rar (2.22 KB, 下載次數(shù): 30)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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