您的位置:首页 > 其它

STM32单片机学习(11) DS18B20温度传感器实验

2016-07-06 13:14 441 查看


STM32单片机学习(11) DS18B20温度传感器实验

本程序主要实现 DS18B20温度传感器数据获取,并利用串口通信把温度数据传至计算机

注:使用普中科技开发板测试时,需要拔掉Boot1插口,因为用到的是PA15管脚, 由开发板电路图可知,需要改变PA15 管脚的映射,将其设置成普通IO口

参考资料

DS18B20中文手册.pdf http://download.csdn.net/detail/leytton/7742193
STM32-外设篇 视频教程(Cortex-M3)-主讲人:刘洋 http://yun.baidu.com/pclou href="http://www.eeworld.com.cn/mcu/2014/0320/article_15719.html" target=_blank>d/album/info?uk=2853967793&album_id=5492137931588632574

main.c

/*
*
* 软件功能:	 DS18B20温度<strong><a target=_blank href="http://www.eeworld.com.cn/MEMS/" target="_blank" style="color: rgb(1, 88, 167); text-decoration: none;">传感器</a></strong>
*
*/
#include "stm32f10x.h"
#include <stdio h="">
#include "delay.h"
#include "<strong><a target=_blank href="http://www.eeworld.com.cn/mcu/2012/1011/article_10680.html" target="_blank" style="color: rgb(1, 88, 167); text-decoration: none;">ds18b20</a></strong>.h"

void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void Uart1_PutChar(u8 ch);
void Uart1_PutString(u8* buf , u8 len);
int fputc(int ch, FILE *f);

/*
函数: int main(void)
功能: main主函数
参数: 无
返回: 无
/
int main(void)
{
double temperature=0;
RCC_Configuration();
GPIO_Configuration();
delay_init(72);
USART1_Configuration();

while(1)
{
if(!DS18B20_Is_Exist())
{
printf("未检测到DS18B20温度传感器...\n");
delay_ms(500);
}
else
{
printf("检测到DS18B20温度传感器\n获取数据中...\n");
temperature=DS18B20_Get_wd();
printf("当前温度:%0.4lf ℃\n\n",temperature);
}
}
}

/*
函数: void RCC_Configuration(void)
功能: 复位和时钟控制 配置
参数: 无
返回: 无
/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;                    //定义外部高速晶体启动状态枚举变量
RCC_DeInit();                                    //复位RCC外部设备寄存器到默认值
RCC_HSEConfig(RCC_HSE_ON);                       //打开外部高速晶振
HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等待外部高速时钟准备好
if(HSEStartUpStatus == SUCCESS)                  //外部高速时钟已经准别好
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法.位置:RCC初始化子函数里面,时钟起振之后
FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延时

RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)时钟等于==SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)钟==AHB时钟
RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)钟==AHB1/2时钟

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //配置PLL时钟 == 外部高速晶体时钟 * 9 = 72MHz
RCC_PLLCmd(ENABLE);                                   //使能PLL时钟

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL时钟就绪
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //配置系统时钟 = PLL时钟
while(RCC_GetSYSCLKSource() != 0x08)                  //检查PLL时钟是否作为系统时钟
{
}
}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);  //允许 GPIOA、USART1、AFIO时钟
}

/*
函数: void GPIO_Configuration(void)
功能: GPIO配置
参数: 无
返回: 无
/
void GPIO_Configuration(void)
{

GPIO_InitTypeDef GPIO_InitStructure;        //定义GPIO初始化结构体

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复合推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); 	   //PA9串口输出

//把调试设置普通IO口
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);

// 改变指定管脚的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);

}

/*
函数名:USART1_Configuration
输  入:
输  出:
功能说明:
初始化串口硬件设备,启用中断
配置步骤:
(1)打开GPIO和USART1的时钟
(2)设置USART1两个管脚GPIO模式
(3)配置USART1数据格式、波特率等参数
(4)使能USART1接收中断功能
(5)最后使能USART1功能
*/
void USART1_Configuration(void)	  //串口配置   详见《STM32的函数说明(中文).pdf》P346
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;   //波特率为9600
USART_InitStructure.USART_WordLength=USART_WordLength_8b;  //数据位为8
USART_InitStructure.USART_StopBits=USART_StopBits_1; //在帧结尾传输 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_Mode 指定了使能或者失能发送和接收模式:发送使能|接收失能
USART_Init(USART1, &USART_InitStructure);	  //初始化配置

USART_Cmd(USART1,ENABLE);	//使能或者失能 USART 外设
USART_ClearFlag(USART1, USART_FLAG_TC);//清除传输完成标志位,否则可能会丢失第1个字节的数据.USART_FLAG_TC为发送完成标志位
}

//发送一个字符
void Uart1_PutChar(u8 ch)
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}

//发送一个字符串 Input : buf为发送数据的地址 , len为发送字符的个数
void Uart1_PutString(u8* buf , u8 len)
{
u8 i;
for(i=0;i<len;i++) {="" uart1_putchar(*(buf++));="" }="" int="" fputc(int="" ch,="" file="" *f)="" uart1_putchar((u8)ch);="" 此处为自定义函数,参见串口中断通信,请勿盲目="" return="" (ch);="" <="" pre="">

DS18B20.h
<p style="margin-top: 17px; margin-bottom: 17px; padding-top: 0px; padding-bottom: 0px; text-indent: 0em;"> </p>
<p style="margin-top: 17px; margin-bottom: 17px; padding-top: 0px; padding-bottom: 0px; text-indent: 0em;"> </p>
<pre name="code" style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">#ifndef __DS18B20_H
#define __DS18B20_H
#include "stm32f10x.h"

#define DS18B20_Pin GPIO_Pin_15
#define DS18B20_GPIO GPIOA

#define DS18B20_DQ_High() GPIO_SetBits(DS18B20_GPIO,DS18B20_Pin)
#define DS18B20_DQ_Low()  GPIO_ResetBits(DS18B20_GPIO,DS18B20_Pin)

void DS18B20_IO_IN(void);
void DS18B20_IO_OUT(void);
u8 DS18B20_Read_Byte(void);
void DS18B20_Write_Byte(u8 dat);
void DS18B20_Reset(void);
double DS18B20_Get_wd(void);
u8 DS18B20_Is_Exist(void);

#endif


DS18B20.c

#include "stm32f10x.h"
#include "ds<strong><a target=_blank href="http://www.eeworld.com.cn/mcu/2015/0311/article_18675.html" target="_blank" style="color: rgb(1, 88, 167); text-decoration: none;">18b20</a></strong>.h"
#include "delay.h"

void DS18B20_IO_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;        //定义GPIO初始化结构体
GPIO_InitStructure.GPIO_Pin = DS18B20_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //配置成上拉输入;
GPIO_Init(DS18B20_GPIO, &GPIO_InitStructure);
}

void DS18B20_IO_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;        //定义GPIO初始化结构体
GPIO_InitStructure.GPIO_Pin = DS18B20_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //配置成推挽输出;
GPIO_Init(DS18B20_GPIO, &GPIO_InitStructure);
}

u8 DS18B20_Read_Byte(void)
{
u8 i=0,TmpData=0;
for(i=0;i<8;i++)
{
TmpData>>=1;		   //右移

DS18B20_IO_OUT();	   //输出模式

DS18B20_DQ_Low();	   //拉低

delay_us(4);         //延时4us
DS18B20_DQ_High();     //拉高,释放<strong><a target=_blank href="http://www.eeworld.com.cn/qrs/2015/0505/article_22256.html" target="_blank" style="color: rgb(1, 88, 167); text-decoration: none;">总线</a></strong>
delay_us(10);        //延时10us

DS18B20_IO_IN();     //输入模式

if(GPIO_ReadInputDataBit(DS18B20_GPIO,DS18B20_Pin)== 1)  TmpData |=0x80; //读取数据 ,从低位开始

delay_us(45);        //延时45us
}

return 	TmpData;
}

void DS18B20_Write_Byte(u8 dat)
{
u8 i=0;

DS18B20_IO_OUT();	   //输出模式

for(i=0;i<8;i++)
{
DS18B20_DQ_Low();	   //拉低
delay_us(15);         //延时15us

if(dat&0x01==0x01)  DS18B20_DQ_High();
else 	DS18B20_DQ_Low();

delay_us(60);        //延时60us

DS18B20_DQ_High();   //拉高

dat>>=1;  //准备下一位数据的写入
}

}

//复位函数
void DS18B20_Reset(void)
{
DS18B20_IO_OUT();	   //输出模式
DS18B20_DQ_Low();	   //拉低
delay_us(480);        //延时480us
DS18B20_DQ_High();
delay_us(480);        //延时480us

}

//返回温度值
double DS18B20_Get_wd(void)
{
u8 TL=0,TH=0;
u16 temp=0;
double wd=0;

DS18B20_Reset();//复位
DS18B20_Write_Byte(0xCC); //跳过ROM命令
DS18B20_Write_Byte(0x44); //温度转换命令

delay_ms(800);//延时800毫秒
DS18B20_Reset();//复位
DS18B20_Write_Byte(0xCC); //跳过ROM命令
DS18B20_Write_Byte(0xBE); //读温度命令

TL=DS18B20_Read_Byte();//LSB
TH=DS18B20_Read_Byte();//MSB

temp=TH;
temp=(temp<<8)+TL;

if((temp&0xF800)==0xF800)//负温度判断
{
temp=~temp;
temp=temp+1;
wd=temp*(-0.0625);
}
else
{
wd=temp*0.0625;
}
return wd;
}

//等待DS18B20的回应
//返回1:检测到DS18B20的存在
//返回0:不存在
u8 DS18B20_Is_Exist(void)
{
DS18B20_IO_OUT();	   //输出模式
DS18B20_DQ_High();   //默认高电平

DS18B20_DQ_Low();	   //拉低
delay_us(600);        //延时600us
DS18B20_DQ_High();
delay_us(100);        //延时100us

DS18B20_IO_IN();	   //输入模式
if(GPIO_ReadInputDataBit(DS18B20_GPIO,DS18B20_Pin)== 0)	 return 1;
else  return 0;

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: