只需要将STM32的串口的TX线和18B20的DQ相连接,通过4.7K上拉电阻让TX线拉到3.3V即可(PS:就不贴图了)。
/*
*配置串口2
*/
void DS18B20_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = 115200;//默认设置成115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART2, ENABLE); //使能串口
USART_HalfDuplexCmd(USART2, ENABLE);
}
只配置了TX管脚,使能单线半双工模式(USART_HalfDuplexCmd函数)
/*
*复位18B20,
*返回0:找到设备,设备响应。
*返回1:没有找到设备,设置无响应
*/
int DS18B20_Reset()//复位18B20
{
unsigned char status ;
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_SendData(USART2,0xF0); //发送0xF0 低电平时长位 1/9600 * 1000 * 1000 * 5 us = 520us
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) //等待传输完成
{
}
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) //等待发送完成
{
}
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)//等待收到数据,TX和RX内部连接,肯定能收到数据
{
status = USART_ReceiveData(USART2);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
}
USART_InitStructure.USART_BaudRate = 115200;//将波特率改为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
if(status == 0xF0)
return 1;
else
return 0;
}
void DS18B20_WriteBit(unsigned char bit)
{
if(bit)
USART_SendData(USART2,0xFF);
else
USART_SendData(USART2,0x00);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) //等待传输完成
{
}
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) //等待收到数据
{
}
USART_ReceiveData(USART2);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
}
void DS18B20_WriteByte(unsigned char byte)
{
int i;
for(i = 0 ; i < 8 ; i++)//8字节的数据组合成1字节 1-wire数据
{
DS18B20_WriteBit((byte>>i) &0x01);
}
}
char DS18B20_ReadBit()//读取一位数据
{
unsigned char status;
USART_SendData(USART2,0xFF);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) //等待传输完成
{
}
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) //等待传输完成
{
}
status = USART_ReceiveData(USART2);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
if(status == 0xFF)
status = 1;
else
status = 0;
return status;
}
char DS18B20_ReadByte()
{
unsigned char status = 0;
int i;
for(i = 0 ; i < 8 ; i++)
{
status |= (DS18B20_ReadBit() << i);
}
return status;
}
void DS18B20_Start()
{
int i = 0xFFFF;
DS18B20_reset();
DS18B20_WriteByte(0xCC);//跳过网络地址
DS18B20_WriteByte(0x44);//启动转换
while(i--);//等待转换完成
}
float DS18B20_GetTemp()
{
unsigned char TL,TH ,t;
short tem;
float temp;
DS18B20_Start();
DS18B20_reset();
DS18B20_WriteByte(0xCC);//跳过网络地址
DS18B20_WriteByte(0xBE);//读取温度值
TL=DS18B20_ReadByte(); // LSB
TH=DS18B20_ReadByte(); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
t=0;//温度为负
}else
t=1;//温度为正
tem=TH; //获得高八位
tem<<=8;
tem+=TL;//获得底八位
temp = (float)tem*0.0625;
if(t)return temp; //返回温度值
else return -temp;
}