代碼片段1為“DS18B20.h”
代碼片段2為“DS18B20.c”
在main函數初始化部分直接調用“DQ_GPIO_Init();//初始化函數 使用時直接調用到主函數”
在main函數主循環中調用“Temp_Check(); //溫度獲取”即可獲取溫度
- #ifndef _DS18B20_H_
- #define _DS18B20_H_
- #include "gd32f4xx.h"
- //DQ_GPIO
- #define DQ_GPIO_Port GPIOC //GPIO分組編號 范圍A-E
- #define DQ_Pin GPIO_PIN_13 //GPIO針腳號 范圍0-15
- //#define SET GPIO_PIN_SET
- //#define RESET GPIO_PIN_RESET
- //總線的電平
- typedef enum
- {
- DQ_LEVEL_LOW = 0,
- DQ_LEVEL_HIGHT
- }DQ_LEVEL_Type;
- /* 引腳寫高低電平 */
- #define DQ_Write(Level) if(Level) \
- gpio_bit_set(DQ_GPIO_Port,DQ_Pin); \
- else \
- gpio_bit_reset(DQ_GPIO_Port,DQ_Pin);
-
- #define DQ_Write_LOW gpio_bit_set(DQ_GPIO_Port,DQ_Pin)
-
-
- #define DQ_Write_HIGH gpio_bit_reset(DQ_GPIO_Port,DQ_Pin)
-
- /*讀取總線電平*/
- #define DQ_Read gpio_input_bit_get(DQ_GPIO_Port,DQ_Pin)
- //ROM指令
- #define SKIP_ROM 0xCC //跳過 ROM
- #define CONVERT_TEMP 0x44 //轉換溫度
- #define READ_SCRATCHPAD 0xBE //讀取暫存器內容
-
- /*************************相關函數************************************************/
- void DQ_GPIO_Init(void); //初始化函數 使用時直接調用到主函數
- uint8_t Ds18b20_Init(void); //初始化函數 使用時直接調用到主函數
- float Ds18b20_Get_Temp(void); //獲取溫度函數,使用時直接調用
- void Temp_Check(void); //溫度獲取函數,直接顯示并調用,用于測試
- #endif
復制代碼
- #include "DS18B20.h"
- #include "systick.h"
- float Tem = 0.0;
- char Data[20] = {0};
- //溫度檢測函數 檢測環境溫度 單精度浮點型
- void Temp_Check(void)
- {
- Tem = Ds18b20_Get_Temp();
- // sprintf(Data,"%f",Tem);
- }
- //DS18B20輸出IO口初始化
- void DQ_GPIO_Init(void)
- {
- rcu_periph_clock_enable(RCU_GPIOC); //使能GPIO_C 時鐘
- //DS18B20輸出 輸出模式(推挽輸出) 速度50MHz
- gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
- gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
- }
- //輸出初始化
- void DQ_GPIO_OUT(void)
- {
- rcu_periph_clock_enable(RCU_GPIOC); //使能GPIO_C 時鐘
- //DS18B20輸出 輸出模式(推挽輸出) 速度50MHz
- gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
- gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
-
- }
- //輸入初始化
- void DQ_GPIO_IN(void)
- {
- rcu_periph_clock_enable(RCU_GPIOC); //使能GPIO_C 時鐘
- //DS18B20輸入 輸入模式 速度50MHz
- gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
- gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
-
- }
-
- //DS18B20復位
- void Ds18b20_Reset(void)
- {
-
- DQ_GPIO_OUT();
-
- DQ_Write_LOW;
- delay_us(750);
-
- DQ_Write_HIGH;
- delay_us(15);
- }
- //掃描內存
- uint8_t Ds18B20_CheckPulse(void)
- {
-
- uint8_t Time_Count = 0;
-
- DQ_GPIO_IN();
-
- while (DQ_Read && Time_Count < 100)
- {
- Time_Count++;
- delay_us(1);
- }
-
- if (Time_Count >= 100)
- return 1;
- else
- Time_Count = 0;
-
- while (!DQ_Read && Time_Count < 240)
- {
- Time_Count++;
- delay_us(1);
- }
-
- if (Time_Count >= 240)
- return 1;
- else
- return 0;
- }
- //讀取位
- uint8_t Ds18b20_Read_Bit(void)
- {
- uint8_t dat;
- DQ_GPIO_OUT();
- DQ_Write_LOW;
- delay_us(15);
- DQ_GPIO_IN();
- if (DQ_Read == 1)
- dat = 1;
- else
- dat = 0;
- delay_us(50);
- return dat;
- }
- //讀取字節
- uint8_t Ds18B20_Read_Byte(void)
- {
- uint8_t data = 0x00,mask;
-
- for (mask = 0x01;mask != 0;mask <<= 1)
- {
- if (Ds18b20_Read_Bit() == SET)
- data |= mask;
- else
- data &= ~mask;
- }
- return data;
- }
- //寫字節
- void Ds18B20_Write_Byte(uint8_t data)
- {
- uint8_t mask;
-
- for (mask = 0x01;mask != 0;mask <<= 1)
- {
- DQ_GPIO_OUT();
- if ((data & mask) == RESET)
- {
- DQ_Write_LOW;
- delay_us(70);
-
- DQ_Write_HIGH;
- delay_us(2);
- }
- else
- {
- DQ_Write_LOW;
- delay_us(9);
-
- DQ_Write_HIGH;
- delay_us(55);
- }
- }
- }
- //DS18B20初始化
- uint8_t Ds18b20_Init(void)
- {
- Ds18b20_Reset();
-
- return Ds18B20_CheckPulse();
- }
- //獲取溫度
- float Ds18b20_Get_Temp(void)
- {
- uint8_t tpmsb, tplsb;
- short s_tem;
- float f_tem;
-
- Ds18b20_Reset();
- Ds18B20_CheckPulse();
- Ds18B20_Write_Byte(SKIP_ROM);
- Ds18B20_Write_Byte(CONVERT_TEMP);
-
- Ds18b20_Reset();
- Ds18B20_CheckPulse();
- Ds18B20_Write_Byte(SKIP_ROM);
- Ds18B20_Write_Byte(READ_SCRATCHPAD);
-
- tplsb = Ds18B20_Read_Byte();
- tpmsb = Ds18B20_Read_Byte();
-
- s_tem = tpmsb << 8;
- s_tem = s_tem | tplsb;
-
- if( s_tem < 0 )
- f_tem = (~s_tem+1) * 0.0625;
- else
- f_tem = s_tem * 0.0625;
-
- return f_tem;
- }
復制代碼
|