<span style="font-family:Arial;background-color: transparent;">
</span>
头文件
*******************************************/
#ifndef __BSP_DS18B20_H
#define __BSP_DS18B20_H
#include "stm32f10x.h"
#include "SysTick.h"
/******************************DS18B20 函数声明**************************************/
void DS18B20_GPIO_Config(void);
void DS18B20_start(void) ;
unsigned int DS18B20_Read_Temp(void) ;
/****************************** DS18B20 函数宏定义***************************************/
/*******************************DS18B20 引脚参数配置******************************/
#define DS18B20_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define DS18B20_GPIO_CLK RCC_APB2Periph_GPIOB
#define DS18B20_GPIO_PORT GPIOB
#define DS18B20_GPIO_PIN GPIO_Pin_0
#endif
驱动文件。c
#include "bsp_DS18B20.h"
void DS18B20_GPIO_Config(void)
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*开启GPIOC的外设时钟*/
RCC_APB2PeriphClockCmd( DS18B20_GPIO_CLK, ENABLE);
/*选择要控制的GPIOC引脚*/
GPIO_InitStructure.GPIO_Pin =DS18B20_GPIO_PIN;
/*设置引脚模式为通用开漏输出*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
/*设置引脚速率为50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*调用库函数,初始化DS18B20_GPIO_PORT*/
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
}
//*************************************************************************
// DS18B20初始化
//*************************************************************************
unsigned char DS18B20_Reset(void) //初始化和复位
{
unsigned char i;
// DQ_OUT;
GPIO_ResetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN); //DQ_CLR;
Delay_us(500); //延时500uS(480-960)
GPIO_SetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN); //DQ_SET;
//DQ_IN;
Delay_us(80); //延时80uS
i = GPIO_ReadInputDataBit(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN);
Delay_us(500); //延时500uS(保持>480uS)
if (i)
{
return 0x00;
}
else
{
return 0x01;
}
}
//*************************************************************************
// 向18B20写一个字节函数
//*************************************************************************
/*DS18B20字节写入函数*/
void DS18B20_Write_Byte(unsigned char value)
{
unsigned char i;
for (i = 8; i != 0; i--)
{
// DQ_OUT;S设置为输出
GPIO_ResetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN);//DQ_CLR;置低电平
Delay_us(4) ; //延时4uS
if (value & 0x01)
{
GPIO_SetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN);//DQ_SET;置高电平
}
Delay_us(80); //延时80uS
GPIO_SetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN); //DQ_SET置高电平 //位结束
value >>= 1;
}
}//*************************************************************************
// DS18B20读一个字节函数
//*************************************************************************
unsigned char DS18B20_Read_Byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i = 8; i != 0; i--)
{
value >>= 1;
// DQ_OUT;
GPIO_ResetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN);// DQ_CLR;置低电平
Delay_us(4); //*延时4uS
GPIO_SetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN); // DQ_SET;置高电平
//DQ_IN;设置为输入模式
Delay_us(10); ; //*延时10uS
if (GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, DS18B20_GPIO_PIN ))
{
value|=0x80;
}
Delay_us(60); //*延时60uS
}
return(value);
}//*************************************************************************
// 发送温度转换命令
//*************************************************************************
/*启动ds1820转换*/
void DS18B20_start(void)
{
DS18B20_Reset();
DS18B20_Write_Byte(0xCC); //勿略地址
DS18B20_Write_Byte(0x44); //启动转换
}
//*************************************************************************
// DS8B20读取温度信息
//*************************************************************************
unsigned int DS18B20_Read_Temp(void)
{
unsigned int i=0;
unsigned int temp_value;
unsigned char buf[9];
DS18B20_Reset();
DS18B20_Write_Byte(0xCC); //勿略地址
DS18B20_Write_Byte(0xBE); //读取温度
for (i = 0; i < 9; i++)
{
buf[i] = DS18B20_Read_Byte();
}
i = buf[1];
i <<= 8;
i |= buf[0];
temp_value=i;
temp_value=(unsigned int)(temp_value*0.625); //不是乘以0.0625的原因是为了把小数点后一位数据也转化为可以显示的数据
//比如温度本身为27.5度,为了在后续的数据处理程序中得到BCD码,我们先放大到275
//然后在显示的时候确定小数点的位置即可,就能显示出27.5度了
return temp_value;
}
如果移植到自己的裸机上,只要替换程序中的头文件的gpio宏定义,和程序中的延时函数