{"id":2948,"date":"2024-09-18T14:00:28","date_gmt":"2024-09-18T14:00:28","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2948"},"modified":"2024-09-18T14:56:07","modified_gmt":"2024-09-18T14:56:07","slug":"timer-basic-digital-temperature-sensor","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2948","title":{"rendered":"Timer Basic : Digital Temperature Sensor"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-b43db203b773d59dd8b46d2ba9440df1 wp-block-paragraph\" style=\"color:#5c5c5c\">In STM32 there are different types of timer i.e. Advanced-control timer, General-purpose timers, Independent watchdog timer, Window watchdog timer. Example in STM32F030x8 mcu uses APB1 Timer Clocks for Advanced-control timer &amp; General-purpose timers. In HAL_Delay function we can only have 1ms delay system delay. We will use TIM17 general purpose time to create 1us delay. Let\u2019s use system clock as 48MHz f<sub>sys<\/sub> = 48MHz and prescaler value 47, so the new clock frequency for TIM17 is-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5fc8a5770dedc0c730b41c0af30c3b7c wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f<sub>TIM17 <\/sub>= f<sub>sys <\/sub>\/ (prescaler +1) = 48MHz\/(47+1) = 1MHz &amp; t<sub>TIM17<\/sub> = 1us<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color wp-elements-afbe23901b2b7d6b70afb5fbe02efb07 wp-block-paragraph\" style=\"color:#5c5c5c\">So that the Counter Period (Auto Reload Register) will be update in every 1us value. The Couter Period has a maximum value of 2<sup>16<\/sup>-1 or 65535. So the logic is-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid _delay_us(uint8_t delay)\n{\nHAL_TIM_Base_Start(&amp;htim17);\n__HAL_TIM_SET_COUNTER(&amp;htim17,0);  \/\/ set the counter value a 0\nwhile (__HAL_TIM_GET_COUNTER(&amp;htim17) &lt; delay);  \/\/ wait for ARR value\n} \n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-35c4c7df3e692ced58e66d8c8616528e wp-block-paragraph\" style=\"color:#5c5c5c\">Some useful function of timer as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_TIM_Base_Start(TIM_HandleTypeDef *htim);      \/\/ start timer \nHAL_TIM_Base_Stop(TIM_HandleTypeDef *htim);       \/\/ stop timer\nHAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim); \/\/ start timer with interrupt\nHAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim);  \/\/ stop timer with interrupt\n\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"781\" height=\"748\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function1.jpg\" alt=\"\" class=\"wp-image-2951\" style=\"width:487px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function1.jpg 781w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function1-300x287.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function1-768x736.jpg 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2031a682c2491b63bf65d601b6b6a6fc wp-block-paragraph\" style=\"color:#5c5c5c\">Here we interface DS18B20 digital temperature sensor with PC0 pin of STM32F0308-DISCO board. Before start please go through my <a href=\"https:\/\/iotthinghub.com\/?p=1079\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20(Digital Temperature Sensor)<\/a> interfacing with GPIO article, because we will not discuss the interfacing logic. Here we just change some parameter according to STM32 HAL Library. In STM32CubeMX we just initialize the GPIO as input or output configuration. But in DS18B20 sensor interface we need to set the same GPIO pin as input and output. So to define a GPIO pin as output or input will be-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define DS18B20_GPIO              GPIOC\n#define DS18B20_PIN               GPIO_PIN_0\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid output(void)\n{\n  GPIO_InitTypeDef GPIO_InitStruct = {0};\n  GPIO_InitStruct.Pin = DS18B20_PIN;\n  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;\n  GPIO_InitStruct.Pull = GPIO_NOPULL;\n  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;\n  HAL_GPIO_Init(DS18B20_GPIO, &amp;GPIO_InitStruct);\n} \n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid input(void)\n{\n  GPIO_InitTypeDef GPIO_InitStruct = {0};\n  GPIO_InitStruct.Pin =  DS18B20_PIN;\n  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;\n  GPIO_InitStruct.Pull = GPIO_PULLDOWN;\n  HAL_GPIO_Init(DS18B20_GPIO, &amp;GPIO_InitStruct);\n\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-1024x288.jpg\" alt=\"\" class=\"wp-image-2968\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-1024x288.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-300x84.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-768x216.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20.jpg 1100w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-a1b2375c07b6a87362c0140ab40da7e0 wp-block-paragraph\" style=\"color:#5c5c5c\">So change in reset function as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define CONFIG_OUTPUT             HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_RESET)\n#define READ_INPUT                HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_0)\nuint8_t DS18B20Reset(void)\n{\n\t\/\/Pull Output line low and wait for 480uS\n    output();\/\/Output low for 480us   \n    CONFIG_OUTPUT;\/\/Configure as Output\n\t_delay_us(480);\n\t\/\/Release line and wait for 60uS\n    input(); \/\/Configure as Input   \n\t_delay_us(60);\n\t\/\/Store line value and wait until the completion of 480uS period\n\tuint8_t i;\n\ti=(READ_INPUT);\n\t_delay_us(420);\n\treturn i; \/\/Return the value read from the presence pulse (0=OK, 1=WRONG)\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-2dab2fc5b301d0cf2c76cfeb2acdc9dd wp-block-paragraph\" style=\"color:#5c5c5c\">Similarly change in sensor read &amp; sensor write functions-<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid DS18B20Write(uint8_t byte)\n{\n\tfor(uint8_t i=0;i&lt;8;i++)\n\t{\n\t  \/\/Pull line low for 2uS   \n      output();  \/\/Configure as Output  \n      CONFIG_OUTPUT;\/\/Output low \n\t  _delay_us(2);\n\t  \/\/If we want to write 1, release the line (if not will keep low)\n\t  if(byte &amp; 0x01) input(); \n      \/\/Configure as Input\n\t  _delay_us(60);  \n      input(); \/\/Configure as Input\n\t  _delay_us(1);\n\t  byte=byte&gt;&gt;1;\n\t}\n}\n\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t DS18B20Read()\n{\n\tuint8_t byte=0x00;\n\tfor(uint8_t i=0;i&lt;8;i++)\n\t{\n\t  byte=byte&gt;&gt;1;\n\t  \/\/Pull line low for 1uS  \n      output(); \/\/Configure as Output\n      CONFIG_OUTPUT; \/\/Output low for 2us\n\t  _delay_us(2);\n\t  \/\/Release line and wait for 14uS\n      input();\n\t  _delay_us(14); \/\/Configure as Input\n\t  if(READ_INPUT) byte|=0x80;\n\t  _delay_us(45);\n\t}\n\treturn byte;}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-63aea4a86f6a803f4757668aa6650050 wp-block-paragraph\" style=\"color:#5c5c5c\">No change in DS18B20ReadTemp function. All functions are ready, now it is time to download the driver file and display it. In display part we use .91 &#8221; OLED. You can change GPIO pins in the definition section &amp; use the driver library.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"STM32 DS18B202\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/pea7Hmv5M2k?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/TIMER-DS18B20-STM32F0308.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--50)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color has-medium-font-size wp-elements-62a82f2717195af29d4e1e7a1715fe88 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/TIMER-DS18B20-STM32F0308.rar\">TIMER DS18B20 STM32F0308.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In STM32 there are different types of timer i.e. Advanced-control timer, General-purpose timers, Independent watchdog timer, Window watchdog timer. Example in STM32F030x8 mcu uses APB1 Timer Clocks for Advanced-control timer &amp; General-purpose timers. In HAL_Delay function we can only have 1ms delay system delay. We will use TIM17 general purpose time to create 1us delay. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-2948","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2948"}],"version-history":[{"count":19,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2948\/revisions"}],"predecessor-version":[{"id":2980,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2948\/revisions\/2980"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}