STM32 Single chip microcomputer learning (one) DS18B20 temperature sensor experiment

Source: Internet
Author: User

STM32 Single chip microcomputer learning (one) DS18B20 temperature sensor experiment The main realization of this program DS18B20 Temperature SensorData acquisition, and leveraging Serial CommunicationTransfer the temperature data to the computer

Note: The use of the general Science and Technology Development Board test, you need to unplug the BOOT1 socket, because the use of the PA15 pin, by the Development Board circuit diagram, you need to change the PA15 pin mapping, set it to normal IO port

Resources

DS18B20 Chinese manual. pdf http://download.csdn.net/detail/leytton/7742193

STM32-Peripherals Video Tutorial (CORTEX-M3)-presenter: Liu Yang Http://yun.baidu.com/pcloud/a lbum/info?uk=2853967793& album_id=5492137931588632574

Main.c

/*** software Features: Ds18b20 temperature <strong><a target=_blank href= "http://www.eeworld.com.cn/MEMS/" target= "_blank" style= " Color:rgb (1, 88, 167); Text-decoration:none; " > Sensor </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);/* Function: int  Main (void) function: Main main function parameter: no return: no/int main (void) {double temperature=0;  Rcc_configuration ();  Gpio_configuration ();  Delay_init (72);    Usart1_configuration (); while (1) {if (!  Ds18b20_is_exist ()) {printf ("DS18B20 temperature sensor not detected ... \ n"); Delay_ms (500); } else {printf ("detected ds18b20 temperature sensor \ n Get data ... \ n"); temperature=ds18B20_GET_WD ();  printf ("Current temperature:%0.4lf℃\n\n", temperature); }}}/* function: void rcc_configuration (void) function: Reset and clock control configuration parameters: No return: no/void rcc_configuration (void) {ErrorStatus Hsestartupstatu                    S                                    Defines an external high-speed crystal starting state enumeration variable rcc_deinit ();                       Reset the RCC external device register to the default value of Rcc_hseconfig (rcc_hse_on);      Open external high-speed crystal oscillator hsestartupstatus = Rcc_waitforhsestartup (); Wait for the external high-speed clock ready if (hsestartupstatus = = SUCCESS)//external high-speed clock is not good {flash_prefetchbuffercmd (flash_prefetch buffer_enable); Flash read-ahead buffering is enabled to speed up flash reading.                    Required usage in all programs. Location: RCC initialization sub-function inside, the clock after the vibration flash_setlatency (flash_latency_2);               Flash operation Delay Rcc_hclkconfig (RCC_SYSCLK_DIV1);                Configuration AHB (HCLK) clock equals ==sysclk rcc_pclk2config (RCC_HCLK_DIV1);                Configure the APB2 (PCLK2) clock ==AHB clock rcc_pclk1config (RCC_HCLK_DIV2);  Configuration APB1 (PCLK1) clock ==ahb1/2 clocks rcc_pllconfig (RCC_PLLSOURCE_HSE_DIV1, rcc_pllmul_9); Configuring the PLL Clock = = External high-speed crystal clock * 9 =72MHz Rcc_pllcmd (ENABLE); Enable PLL clock while (Rcc_getflagstatus (rcc_flag_pllrdy) = = RESET)//wait for PLL clock ready {} rcc_sysclkconfig (RCC_SYSCLK            SOURCE_PLLCLK); Configure the system clock = PLL clock while (Rcc_getsysclksource ()! = 0x08)//check if the PLL clock is a system clock {}} rcc_apb2periph Clockcmd (Rcc_apb2periph_gpioa | Rcc_apb2periph_usart1 |  Rcc_apb2periph_afio, ENABLE); Allow Gpioa, USART1, Afio clock}/* function: void gpio_configuration (void) function: GPIO configuration parameter: No return: no/void gpio_configuration (void) {Gpio_i        Nittypedef gpio_initstructure;   Defines the GPIO initialization structure body Gpio_initstructure.gpio_pin = Gpio_pin_9; Gpio_initstructure.gpio_mode = gpio_mode_af_pp;    Composite push-Pull output gpio_init (Gpioa, &gpio_initstructure);    PA9 serial Output//debug Set normal IO port gpio_pinremapconfig (gpio_remap_swj_disable,enable);   Change the mapping of the specified pin gpio_remap_swj_jtagdisable, JTAG-DP disable + SW-DP enable Gpio_pinremapconfig (gpio_remap_swj_jtagdisable, enable); }/* function Name: usart1_configuration input: Output: Function Description: Initialize serial hardware Device, enable interrupt configuration steps: (1) Turn on Gpio and USART1 clock (2) Set USART1 two pin Gpio mode (3) Configure USART1 data format, baud rate parameters (4) Enable USART1 receive interrupt function (5) finally enable USART1 function */void Usart1_configuration (void)///serial configuration See STM32 's function Description (Chinese). pdf P346{usart_inittypedef Usart_initstructure;   usart_initstructure.usart_baudrate=9600;  Baud rate is 9600usart_initstructure.usart_wordlength=usart_wordlength_8b; Data bits are 8usart_initstructure.usart_stopbits=usart_stopbits_1; Transmit 1 Stop bit usart_initstructure.usart_parity=usart_parity_no at end of frame; Calibration mode: Parity and even loss energy usart_initstructure.usart_hardwareflowcontrol=usart_hardwareflowcontrol_none; Hardware flow control Deactivation Usart_initstructure.usart_mode=usart_mode_tx | USART_MODE_RX;  The Usart_mode specifies the enable or deactivation send and receive modes: Transmit enable | receive the Deactivation usart_init (USART1, &usart_initstructure); Initialize configuration usart_cmd (usart1,enable);//enable or deactivation USART peripheral Usart_clearflag (USART1, USART_FLAG_TC);//clear transfer completion flag bit, Otherwise, a 1th byte of data may be lost.    USART_FLAG_TC sends a character void Uart1_putchar (U8 ch) {Usart_senddata (USART1, (U8) ch) for Send completion flag bit}//; while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET);//wait for Send to complete}//send a string input:buf for the hairThe address of the sending data, Len is the number of characters sent by 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); = "" Here is a custom function, see serial port Interrupt communication, do not blindly = "" 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 G Pioa#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);d OUble 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;   Defines the GPIO initialization structure body Gpio_initstructure.gpio_pin = Ds18b20_pin;     Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;  Gpio_initstructure.gpio_mode = Gpio_mode_ipu;     Configured as pull-up input; Gpio_init (Ds18b20_gpio, &gpio_initstructure);}        void Ds18b20_io_out (void) {gpio_inittypedef gpio_initstructure;   Defines the GPIO initialization structure body Gpio_initstructure.gpio_pin = Ds18b20_pin;     Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;  Gpio_initstructure.gpio_mode = gpio_mode_out_pp;     Configured as push-pull output; Gpio_init (Ds18b20_gpio, &gpio_initstructure);}   U8 Ds18b20_read_byte (void) {U8 i=0,tmpdata=0;for (i=0;i<8;i++) {tmpdata>>=1;   Move right ds18b20_io_out ();   Output mode Ds18b20_dq_low (); Pull down deLay_us (4);     Delay 4us Ds18b20_dq_high (); Pull high, release <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; "        > Bus </a></strong> delay_us (10);     Delay 10us ds18b20_io_in (); Input mode if (Gpio_readinputdatabit (ds18b20_gpio,ds18b20_pin) = = 1) tmpdata |=0x80;        Read data, starting from low Delay_us (45); Delay 45us}return Tmpdata;}   void Ds18b20_write_byte (U8 dat) {U8 i=0;ds18b20_io_out ();   Output mode for (i=0;i<8;i++) {ds18b20_dq_low ();         Pull Low Delay_us (15);   Delay 15us if (dat&0x01==0x01) Ds18b20_dq_high ();  else Ds18b20_dq_low ();        Delay_us (60);   Delay 60us Ds18b20_dq_high ();  Pull High dat>>=1;   Prepare the next data write}}//reset function void Ds18b20_reset (void) {ds18b20_io_out ();   Output mode Ds18b20_dq_low ();        Pull Low Delay_us (480);   Delay 480us Ds18b20_dq_high ();        Delay_us (480); Delay 480us}//return temperature value double ds18b20_get_wd (void) {U8 tl=0,th=0;u16 Temp=0;doubLe Wd=0;ds18b20_reset ();//Reset Ds18b20_write_byte (0xCC); Skip ROM Command ds18b20_write_byte (0x44); Temperature Conversion Command Delay_ms (800);//Delay 800 msec ds18b20_reset ();//Reset Ds18b20_write_byte (0xCC); Skip ROM Command ds18b20_write_byte (0xBE); Read Temperature command tl=ds18b20_read_byte ();//lsbth=ds18b20_read_byte ();//msbtemp=th;temp= (temp<<8) +TL;if ((temp& 0xf800) ==0xf800)//negative temperature judgment {temp=~temp;temp=temp+1;wd=temp* (-0.0625);} else{wd=temp*0.0625;} return WD;}   Wait for DS18B20 response//return 1: Detected DS18B20 presence//Return 0: U8 ds18b20_is_exist (void) {ds18b20_io_out () not present;   Output mode Ds18b20_dq_high ();   Default High Level Ds18b20_dq_low ();        Pull low Delay_us (600);   Delay 600us Ds18b20_dq_high ();        Delay_us (100);   Delay 100US ds18b20_io_in ();    Input mode if (Gpio_readinputdatabit (ds18b20_gpio,ds18b20_pin) = = 0) return 1; else return 0;}

STM32 Single chip microcomputer learning (one) DS18B20 temperature sensor experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.