利用stm32f103c8t6单片机,ds18b20,串口编写一个测量温度的C语言的代码
时间: 2024-01-05 11:03:17 浏览: 120
很乐意回答您的问题!以下是C语言代码示例:
#include <stdio.h>
#include <stdlib.h>
#include "stm32f10x.h"
#define USART_TX_PIN GPIO_Pin_9
#define USART_RX_PIN GPIO_Pin_10
#define USART_GPIO GPIOA
#define USART USART1
#define RCC_APB2Periph_GPIOA RCC_APB2Periph_GPIOA
#define RCC_APB2Periph_USART1 RCC_APB2Periph_USART1
#define PRESCALER 72
#define BAUD_RATE 9600
#define OW_DQ_PIN GPIO_Pin_3
#define OW_GPIO GPIOB
#define RCC_APB2Periph_GPIOB RCC_APB2Periph_GPIOB
void USART_Config(void);
void delay_us(uint16_t us);
void OW_Pin_Output(void);
void OW_Pin_Input(void);
void OW_Pin_Low(void);
void OW_Pin_High(void);
uint8_t OW_Read_Bit(void);
uint8_t OW_Read_Byte(void);
void OW_Send_Byte(uint8_t byte);
float Read_Temperature(void);
int main(void)
{
float temperature;
USART_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
while (1) {
temperature = Read_Temperature();
printf("Temperature: %0.2fC\n", temperature);
delay_us(500000);
}
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BAUD_RATE;
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_Tx | USART_Mode_Rx;
USART_Init(USART, &USART_InitStructure);
USART_Cmd(USART, ENABLE);
}
void delay_us(uint16_t us)
{
us *= 8;
while (us--) {
asm("NOP");
}
}
void OW_Pin_Output(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = OW_DQ_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OW_GPIO, &GPIO_InitStructure);
}
void OW_Pin_Input(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = OW_DQ_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OW_GPIO, &GPIO_InitStructure);
}
void OW_Pin_Low(void)
{
GPIO_WriteBit(OW_GPIO, OW_DQ_PIN, Bit_RESET);
}
void OW_Pin_High(void)
{
GPIO_WriteBit(OW_GPIO, OW_DQ_PIN, Bit_SET);
}
uint8_t OW_Read_Bit(void)
{
uint8_t bit_status;
OW_Pin_Output();
OW_Pin_Low();
delay_us(2);
OW_Pin_Input();
delay_us(10);
bit_status = GPIO_ReadInputDataBit(OW_GPIO, OW_DQ_PIN);
delay_us(60);
return bit_status;
}
uint8_t OW_Read_Byte(void)
{
uint8_t i, byte = 0;
for (i=0; i<8; i++) {
byte = byte | (OW_Read_Bit() << i);
}
return byte;
}
void OW_Send_Byte(uint8_t byte)
{
uint8_t i;
OW_Pin_Output();
for (i=0; i<8; i++) {
if (byte & (1 << i)) {
OW_Pin_Low();
delay_us(10);
OW_Pin_High();
delay_us(50);
}
else {
OW_Pin_Low();
delay_us(60);
OW_Pin_High();
delay_us(2);
}
}
}
float Read_Temperature(void)
{
uint8_t i;
uint8_t temperature_data[9];
float temperature;
OW_Pin_Output();
OW_Pin_Low();
delay_us(500);
OW_Pin_High();
delay_us(50);
OW_Pin_Input();
while (GPIO_ReadInputDataBit(OW_GPIO, OW_DQ_PIN)) ;
OW_Pin_Low();
delay_us(500);
OW_Pin_High();
OW_Pin_Input();
delay_us(60);
if (!GPIO_ReadInputDataBit(OW_GPIO, OW_DQ_PIN)) {
// Presence pulse
OW_Send_Byte(0xCC); // Skip ROM command
OW_Send_Byte(0x44); // Convert temperature command
while (!GPIO_ReadInputDataBit(OW_GPIO, OW_DQ_PIN)) ;
OW_Send_Byte(0xCC); // Skip ROM command
OW_Send_Byte(0xBE); // Read scratchpad command
for (i=0; i<9; i++) {
temperature_data[i] = OW_Read_Byte();
}
temperature = (float)(temperature_data[1] << 8 | temperature_data[0]) / 16.0;
}
else {
printf("No presence pulse\n");
temperature = 0;
}
return temperature;
}
阅读全文