STM32單片機學(xué)習(xí)(11) DS18B20溫度傳感器實驗
注:使用普中科技開發(fā)板測試時,需要拔掉Boot1插口,因為用到的是PA15管腳, 由開發(fā)板電路圖可知,需要改變PA15管腳的映射,將其設(shè)置成普通IO口
本文引用地址:http://m.butianyuan.cn/article/201611/318254.htm參考資料
DS18B20中文手冊.pdf http://download.csdn.net/detail/leytton/7742193
STM32-外設(shè)篇 視頻教程(Cortex-M3)-主講人:劉洋 http://yun.baidu.com/pcloud/album/info?uk=2853967793&album_id=5492137931588632574
main.c
/** * 軟件功能: DS18B20溫度傳感器* */#include "stm32f10x.h"#include#include "delay.h"#include "ds18b20.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);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無返回: 無/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獲取數(shù)據(jù)中...n");temperature=DS18B20_Get_wd();printf("當(dāng)前溫度:%0.4lf ℃nn",temperature);}}}/*函數(shù): void RCC_Configuration(void)功能: 復(fù)位和時鐘控制 配置參數(shù): 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit(); //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時鐘準(zhǔn)備好if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經(jīng)準(zhǔn)別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預(yù)讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時鐘等于==SYSCLKRCC_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 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08) //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE); //允許 GPIOA、USART1、AFIO時鐘}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結(jié)構(gòu)體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)合推挽輸出 GPIO_Init(GPIOA, &GPIO_InitStructure); //PA9串口輸出//把調(diào)試設(shè)置普通IO口GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE); // 改變指定管腳的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);}/*函數(shù)名:USART1_Configuration輸 入:輸 出:功能說明:初始化串口硬件設(shè)備,啟用中斷配置步驟:(1)打開GPIO和USART1的時鐘(2)設(shè)置USART1兩個管腳GPIO模式(3)配置USART1數(shù)據(jù)格式、波特率等參數(shù)(4)使能USART1接收中斷功能(5)最后使能USART1功能*/void USART1_Configuration(void) //串口配置 詳見《STM32的函數(shù)說明(中文).pdf》P346{USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate=9600; //波特率為9600USART_InitStructure.USART_WordLength=USART_WordLength_8b; //數(shù)據(jù)位為8USART_InitStructure.USART_StopBits=USART_StopBits_1; //在幀結(jié)尾傳輸 1 個停止位USART_InitStructure.USART_Parity=USART_Parity_No; //校驗?zāi)J?奇偶失能USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //硬件流控制失能USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //USART_Mode 指定了使能或者失能發(fā)送和接收模式:發(fā)送使能|接收失能USART_Init(USART1, &USART_InitStructure); //初始化配置USART_Cmd(USART1,ENABLE); //使能或者失能 USART 外設(shè)USART_ClearFlag(USART1, USART_FLAG_TC);//清除傳輸完成標(biāo)志位,否則可能會丟失第1個字節(jié)的數(shù)據(jù).USART_FLAG_TC為發(fā)送完成標(biāo)志位}//發(fā)送一個字符void Uart1_PutChar(u8 ch){USART_SendData(USART1, (u8) ch);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待發(fā)送完成}//發(fā)送一個字符串 Input : buf為發(fā)送數(shù)據(jù)的地址 , len為發(fā)送字符的個數(shù)void Uart1_PutString(u8* buf , u8 len){ u8 i;for(i=0;i
DS18B20.h#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);#endifDS18B20.c#include "stm32f10x.h"#include "ds18b20.h"#include "delay.h"void DS18B20_IO_IN(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結(jié)構(gòu)體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初始化結(jié)構(gòu)體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); //延時4usDS18B20_DQ_High(); //拉高,釋放總線delay_us(10); //延時10usDS18B20_IO_IN(); //輸入模式 if(GPIO_ReadInputDataBit(DS18B20_GPIO,DS18B20_Pin)== 1) TmpData |=0x80; //讀取數(shù)據(jù) ,從低位開始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); //延時15usif(dat&0x01==0x01) DS18B20_DQ_High(); else DS18B20_DQ_Low();delay_us(60); //延時60usDS18B20_DQ_High(); //拉高dat>>=1; //準(zhǔn)備下一位數(shù)據(jù)的寫入}}//復(fù)位函數(shù)void DS18B20_Reset(void){DS18B20_IO_OUT(); //輸出模式DS18B20_DQ_Low(); //拉低delay_us(480); //延時480usDS18B20_DQ_High();delay_us(480); //延時480us}//返回溫度值double DS18B20_Get_wd(void){u8 TL=0,TH=0;u16 temp=0;double wd=0;DS18B20_Reset();//復(fù)位DS18B20_Write_Byte(0xCC); //跳過ROM命令DS18B20_Write_Byte(0x44); //溫度轉(zhuǎn)換命令delay_ms(800);//延時800毫秒DS18B20_Reset();//復(fù)位DS18B20_Write_Byte(0xCC); //跳過ROM命令DS18B20_Write_Byte(0xBE); //讀溫度命令TL=DS18B20_Read_Byte();//LSBTH=DS18B20_Read_Byte();//MSBtemp=TH;temp=(temp<<8)+TL;if((temp&0xF800)==0xF800)//負(fù)溫度判斷{temp=~temp;temp=temp+1;wd=temp*(-0.0625);}else{wd=temp*0.0625; }return wd;}//等待DS18B20的回應(yīng)//返回1:檢測到DS18B20的存在//返回0:不存在u8 DS18B20_Is_Exist(void) { DS18B20_IO_OUT(); //輸出模式DS18B20_DQ_High(); //默認(rèn)高電平DS18B20_DQ_Low(); //拉低delay_us(600); //延時600usDS18B20_DQ_High();delay_us(100); //延時100usDS18B20_IO_IN(); //輸入模式if(GPIO_ReadInputDataBit(DS18B20_GPIO,DS18B20_Pin)== 0) return 1; else return 0;}
評論