stm32f103C8T6 连接ds18b20的c语言代码和头文件
时间: 2024-03-18 22:16:23 浏览: 93
以下是 STM32F103C8T6 连接 DS18B20 的 C 语言代码和头文件:
头文件部分(ds18b20.h):
```c
#ifndef __DS18B20_H
#define __DS18B20_H
#include "stm32f10x.h"
#define DS18B20_GPIO_PORT GPIOB
#define DS18B20_GPIO_PIN GPIO_Pin_12
#define DS18B20_DQ_OUT() GPIO_Init(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN, GPIO_Mode_Out_PP, GPIO_Speed_50MHz)
#define DS18B20_DQ_IN() GPIO_Init(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN, GPIO_Mode_IPU, GPIO_Speed_50MHz)
void DS18B20_Init(void);
void DS18B20_Reset(void);
void DS18B20_WriteBit(uint8_t dat);
uint8_t DS18B20_ReadBit(void);
void DS18B20_WriteByte(uint8_t dat);
uint8_t DS18B20_ReadByte(void);
float DS18B20_GetTemp_SkipRom(void);
#endif /* __DS18B20_H */
```
代码部分(ds18b20.c):
```c
#include "ds18b20.h"
/* 延时函数 */
void DS18B20_Delay(uint16_t us)
{
uint16_t i;
while(us--)
{
i = 10;
while(i--);
}
}
/* 初始化 DS18B20 */
void DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = DS18B20_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStruct);
DS18B20_Reset();
}
/* 复位 DS18B20 */
void DS18B20_Reset(void)
{
DS18B20_DQ_OUT(); /* 设置为输出模式 */
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出低电平 */
DS18B20_Delay(750); /* 延时 750us */
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出高电平 */
DS18B20_Delay(15); /* 延时 15us */
DS18B20_DQ_IN(); /* 设置为输入模式 */
while(GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN)); /* 等待 DS18B20 响应 */
while(!GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN));
}
/* 向 DS18B20 写 1 位数据 */
void DS18B20_WriteBit(uint8_t dat)
{
DS18B20_DQ_OUT(); /* 设置为输出模式 */
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出低电平 */
if(dat) DS18B20_Delay(5); /* 写 1,延时 5us */
else DS18B20_Delay(80); /* 写 0,延时 80us */
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出高电平 */
}
/* 从 DS18B20 读 1 位数据 */
uint8_t DS18B20_ReadBit(void)
{
uint8_t dat;
DS18B20_DQ_OUT(); /* 设置为输出模式 */
GPIO_ResetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出低电平 */
DS18B20_Delay(2); /* 延时 2us */
GPIO_SetBits(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN); /* 输出高电平 */
DS18B20_DQ_IN(); /* 设置为输入模式 */
DS18B20_Delay(15); /* 延时 15us */
if(GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN)) dat = 1;
else dat = 0;
DS18B20_Delay(45); /* 延时 45us */
return dat;
}
/* 向 DS18B20 写 1 字节数据 */
void DS18B20_WriteByte(uint8_t dat)
{
uint8_t i;
for(i=0; i<8; i++)
{
DS18B20_WriteBit(dat&0x01);
dat >>= 1;
}
}
/* 从 DS18B20 读 1 字节数据 */
uint8_t DS18B20_ReadByte(void)
{
uint8_t i, dat = 0;
for(i=0; i<8; i++)
{
dat >>= 1;
if(DS18B20_ReadBit()) dat |= 0x80;
}
return dat;
}
/* 获取 DS18B20 温度值(跳过 ROM) */
float DS18B20_GetTemp_SkipRom(void)
{
float temp;
uint8_t tl, th;
DS18B20_Reset();
DS18B20_WriteByte(0xcc); /* 跳过 ROM */
DS18B20_WriteByte(0x44); /* 启动温度转换 */
DS18B20_Delay(750); /* 延时 750us */
DS18B20_Reset();
DS18B20_WriteByte(0xcc); /* 跳过 ROM */
DS18B20_WriteByte(0xbe); /* 读取温度寄存器 */
tl = DS18B20_ReadByte(); /* 读取温度低 8 位 */
th = DS18B20_ReadByte(); /* 读取温度高 8 位 */
temp = (float)(th<<8 | tl)/16.0; /* 计算温度值 */
return temp;
}
```
注:以上代码仅供参考,具体实现需要根据实际情况进行修改。
阅读全文
相关推荐


















