STM32單片機學習(11) DS18B20溫度感應器實驗

來源:互聯網
上載者:User

標籤:

STM32單片機學習(11) DS18B20溫度感應器實驗本程式主要實現 DS18B20溫度感應器資料擷取,並利用串口通訊把溫度資料傳至電腦

注:使用普中科技開發板測試時,需要拔掉Boot1插口,因為用到的是PA15管腳, 由開發板電路圖可知,需要改變PA15 管腳的映射,將其設定成普通IO口

參考資料

DS18B20中文手冊.pdf   http://download.csdn.net/detail/leytton/7742193

STM32-外設篇 視頻教程(Cortex-M3)-主講人:劉洋   http://yun.baidu.com/pcloud/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;   //傳輸速率為9600USART_InitStructure.USART_WordLength=USART_WordLength_8b;  //資料位元為8USART_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();//LSBTH=DS18B20_Read_Byte();//MSBtemp=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;}

 

STM32單片機學習(11) DS18B20溫度感應器實驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.