蓝桥杯STM32F103RB扩展板DS18B20

STM32F103RB扩展板(温度检测+LCD显示)

DS18B20介绍(转载)link

硬件电路

蓝桥杯STM32F103RB扩展板DS18B20

程序设计


#include "stm32f10x.h"
#include "ds18b20.h"

#define delay_us(X)  delay((X)*72/5)

void delay(unsigned int n)
{
	while(n--);
}

void ds18b20_init_x(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;
  	/* Enable  clock */
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  , ENABLE);
  
  	/* Configure Ports */
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
}

//
void mode_input1(void )
{
  	GPIO_InitTypeDef GPIO_InitStructure;

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void mode_output1(void )
{
  	GPIO_InitTypeDef GPIO_InitStructure;

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
}

//
uint8_t ow_reset(void) //先复位
{ 
	uint8_t err;

   	OW_DIR_OUT(); // pull OW-Pin low for 480us
   	OW_OUT_LOW(); // disable internal pull-up (maybe on from parasite)  //输出低电平

   	delay_us(400);	  //about 480us   
   
   	// set Pin as input - wait for clients to pull low
   	OW_DIR_IN(); // input         输入引脚

   	delay_us(66);
   	err = OW_GET_IN();		// no presence detect
   	// nobody pulled to low, still high


   	// after a delay the clients should release the line
  	// and input-pin gets back to high due to pull-up-resistor
   	delay_us(480-66);
   	if( OW_GET_IN() == 0 )		// short circuit
      err = 1;

   	return err;
 }

uint8_t ow_bit_io( uint8_t b ) //低bit io
{ 
 	OW_DIR_OUT(); // drive bus low   //输出
 	OW_OUT_LOW(); // 输入
 	delay_us(1);  // Recovery-Time wuffwuff was 1  

 	if ( b ) OW_DIR_IN(); // if bit is 1 set bus high (by ext. pull-up)
	//如果位为1,则设置总线高(通过外部上拉)
	
#define  OW_CONF_DELAYOFFSET  5
	//OW\配置\延迟偏移量
 	delay_us(15-1-OW_CONF_DELAYOFFSET);
      
 	if( OW_GET_IN() == 0 ) b = 0;  // sample at end of read-timeslot
	                              //读取时序结束时的样本
	
 	delay_us(60-15);
 	OW_DIR_IN();

 	return b;
}


uint8_t ow_byte_wr( uint8_t b )   //读取一个字节
{ 	
	uint8_t i = 8, j;
   	do 
    { 
		  j = ow_bit_io( b & 1 ); //取最低位  (0xFF)
      b >>= 1;
      	if( j ) b |= 0x80;  //最高位进行置1.
    } 
   	while( --i );
   	return b;
}


//
uint8_t ow_byte_rd( void )
{
   	return ow_byte_wr( 0xFF ); 
}

//

unsigned int dsb_read(void)
{
  unsigned char th, tl;
	
  ow_reset(); //复位
  ow_byte_wr(0xCC); //写入0xCC   跳过ROM操作指令  
  ow_byte_wr(0x44); //温度转换
	
  ow_reset(); //复位
  ow_byte_wr(0xCC); // 跳过ROM操作指令  
  ow_byte_wr(0xBE); //
  tl = ow_byte_wr(0xFF);//温度低位(低八位)
  th = ow_byte_wr(0xFF);//温度高位(高八位)
	return (th<<8)+tl;// 返回(拼接的高八位和低八位)整数,和小数
}

/*
返回数值组成
b15-b11 符号位
b10-b4  整数位   
b3-b0   小数(补码)  舍弃


主函数中返回值需要除以16.0   除以16是为了移除小数位(补码)  返回值除以16(2的四次方)等同于返回值<<4   
除以16.0是为了进行浮点运算
*/



main.c

#include "led.h"
#include "lcd.h"
#include "usart.h"
#include "dht11.h"
#include "ds18b20.h"

unsigned int  dht11_Val,uiDsb_Val;
unsigned char ucled, ucSec1;
unsigned char pucStr[21];
unsigned long ulTick_ms;

void DHT_Proc(void);
void DSB_Proc(void);

int main(void)
{
  SysTick_Config(72000);			// 定时1ms(HCLK = 72MHz)
  LED_Init();
  ucled=0;
	
  STM3210B_LCD_Init();
  LCD_Clear(Blue);
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);

  dht11_init();
  ucled=0;//关闭所有LED
  ds18b20_init_x();
	
  while(1)
  {
    LED_Disp(ucled);
    DHT_Proc();
	DSB_Proc();
  }
}  

void DHT_Proc(void)//这里显示了dht11的温度为了做对比(湿度无所谓)
{
   sprintf((char *)pucStr, " Humidity:    %2d%%", dht11_Val>>24);
   LCD_DisplayStringLine(Line2, pucStr);//数据左移24位得到湿度数据高八位(整数位)
   sprintf((char *)pucStr, " DHTTem:      %2dC", (dht11_Val>>8)&0xff);
   LCD_DisplayStringLine(Line4, pucStr);// &0xff的目的是为了只得到温度高八位数据(整数位)
}


void DSB_Proc(void)
{
	sprintf((char *)pucStr, "DSBTem: %7.2fC", uiDsb_Val/16.0);//  /16.0是为了进行浮点运算,
	//(/16.0在整数中取小数部分)
	// uiDsb_Val<<4; 等同于 uiDsb_Val/16; //此处不能用,仅仅说明
	
	
  LCD_DisplayStringLine(Line6, pucStr);
}



// SysTick中断处理程序
void SysTick_Handler(void)
{
  ulTick_ms++;
	
   if(ulTick_ms%1500 == 0) //每1.5s读取一次
  {
	dht11_Val = dht11_read();
    uiDsb_Val = dsb_read();
  }
}


完整工程下载

选择第10个工程

作者:江多多(在校学生)
版权所有,欢迎保留原文链接进行转载:)

不忘初心,牢记使命,励志成为一名优秀的嵌入式工程师! (我的第十一篇博客)

上一篇:51单片机系列--1Wrie


下一篇:华大单片机HC32L136J8TA读取DS18B20温度(源码+时钟配置)