STM32f103 ds18b20 驱动程序移植

<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宏定义,和程序中的延时函数

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shenhuaifeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值