Read ds18b20 from stm32f103 under stm32cubemx

Publisher:风清扬yxLatest update time:2017-09-27 Source: eefocusKeywords:stm32cubemx  stm32f103  ds18b20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

environment:

AND 7.4

stm32cubemx 4.13.1

stm32f1 1.3.1

freertos 8.1


Reading ds18b20 is mainly about timing, which is a microsecond delay.

Let’s look at the circuit first:


A pull-up resistor is used here, and DQ is set to OD mode. When reading and outputting, the direction of the PIN is changed. If no change is made, only a value of 0 can be read. 

If you do not want to use pull-up resistors, you can set DQ to PP mode, which the Mbed development board can successfully read out.

Code without circuits is incomplete and difficult to understand.


The delay is implemented by using a timer or a nop instruction. Here, Timer3 is used to provide ticks.

The configuration of GPIO and TIMER uses stm32cubemx, which is not listed here. If you have any questions, you can ask them in the forum .


  1. /* 

  2. * Microsecond delay 

  3. */  

  4. void therm_delay(uint16_t nCount)  

  5. {  

  6.   __IO uint16_t TIMCounter = nCount;  

  7.     

  8.   uint16_t start = TIM_MST->CNT;  

  9.   uint16_t end = start + TIMCounter;  

  10.   __IO uint16_t read;  

  11.     

  12.   while (1) {  

  13.     read = TIM_MST->CNT;  

  14.     if(read < start)  

  15.       read = 0xffff - start + read;  

  16.     if (read >= end)  

  17.       break;  

  18.   }  

  19.     

  20. }  




Frequently called change directions and reads are changed to register operations to reduce the overhead of function calls. 

Modify the port and pin as needed.


  1. //PB8: IO direction setting  

  2. #define THERM_INPUT_MODE()  { GPIOB->CRH &= 0xFFFFFFF0; GPIOB->CRH |= 0x4; }  

  3. #define THERM_OUTPUT_MODE() { GPIOB->CRH &= 0xFFFFFFF0; GPIOB->CRH |= 0x5; }  

  4.   

  5. #define DQ_READ() ((GPIOB->IDR & GPIO_PIN_8) >> 8)  




Putting the reading of ds18b20 in a task that does not require high real-time performance will delay the execution of the task by several milliseconds.

There is CRC verification in the code. If an interrupt or other task interferes with the reading timing, the value will be thrown away when it is retrieved. Therefore, you can integrate it into your own project with confidence.

transfer:


  1. // Read the result  

  2. typedef struct {  

  3.   int8_t value;  

  4.   int8_t error;  

  5. } ThermValue;  

  6. .  

  7. .  

  8. .  

  9. __IO ThermValue v_ds18b20;  

  10. /* ds18b20 */  

  11. if (start_conversion) {  

  12.   v_ds18b20 = therm_read_temperature();  

  13.   start_conversion = false;  

  14. }  

  15. else {  

  16.   therm_start_conversion();  

  17.   start_conversion = true;  

  18. }  

  19. if (v_ds18b20.error == 0)  

  20.   cv = v_ds18b20.value;  


There are a lot of codes and examples for operating ds18b20, which are actually very simple, but some codes work normally for others but not for me. 


The reason is the inconsistency between hardware and software, and there is nowhere to turn when problems occur.


Keywords:stm32cubemx  stm32f103  ds18b20 Reference address:Read ds18b20 from stm32f103 under stm32cubemx

Previous article:stm32f103 reads infrared receiver HS0038A2
Next article:Output Compare of stm32 timer

Recommended ReadingLatest update time:2024-11-15 15:53

51 MCU drives DS18B20 temperature sensor program and experience
Regarding the DS18B20 temperature sensor, it is somewhat difficult to write the internal program without the assistance of hardware equipment, because the actual signal waveform cannot be seen. As for the single-chip microcomputer, I... gradually became a little discouraged. Although I mastered the 1_WIRE bus, I lost a
[Microcontroller]
Design of agricultural environment temperature monitoring system based on AT89C51 microcontroller and DS18B20
Modern facility agriculture is a high-tech, high-efficiency modern agricultural production method that integrates biotechnology, engineering technology, and environmental technology. It is an effective way to adjust the agricultural industry, adapt to the development of market economy and improve the utilization rate
[Microcontroller]
Design of agricultural environment temperature monitoring system based on AT89C51 microcontroller and DS18B20
Design and implementation of intelligent battery temperature monitoring system
  As a convenient, safe and reliable DC power source, batteries have been widely used in the fields of electricity, communication, military, etc. Temperature is an important parameter of batteries. It can indirectly reflect the performance of batteries, and the batteries can be managed intelligently based on this tempe
[Microcontroller]
Design and implementation of intelligent battery temperature monitoring system
stm32f103c8t6 minimum system AD project, package
7*7cm in size, LM1117s for voltage regulation, already printed and soldered, can be used. The pads on both sides cannot be soldered to pin headers. Because it was drawn by a novice, the board is relatively large. Anyway, it can be used. There is a slight problem with the soldering pad of the LED light, the distance be
[Microcontroller]
stm32f103c8t6 minimum system AD project, package
STM32F103 beginner's opinion
1. Preemption priority and response priority are actually two priorities contained in an interrupt. The former is a level division of preemption priority, and the latter is a priority division of the same preemption priority. For example: if the preemption priority of interrupt A is higher than that of B, then the i
[Microcontroller]
Microcontroller Course Design Report - Temperature Alarm
1 Introduction 1.1 Introduction to Temperature Alarm Temperature alarm is an electronic product that uses DS18B20 to collect ambient temperature and a single-chip microcomputer to process data to alarm. There are many types of temperature alarms, including simple circuit products and cost-effective products based on
[Microcontroller]
Microcontroller Course Design Report - Temperature Alarm
STM32F103_TIM3 outputs PWM wave to realize full-color breathing light
1. LED lights 1. Must be full-color RGB_LED light 2. 256 colors are mixed by different red, green and blue LEDs, which is the same as the color mixing RGB888 in computer drawing. Each light uses 8 bits to control its brightness, and all lights off represent black. two, 1. LED brightness level table /* LED br
[Microcontroller]
Key points for implementing USB HID composite device of STM32F103C8T6
1. You can download STM32_USB-FS-Device_Lib_V4.0 from the official website, which contains the Custom_HID example for reference. It can be modified from Custom_HID. Always remember "it is STM32F10X_MD series" and "it is not an official demo board" to remove useless codes, such as ADC and other related codes. 2. Syst
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号