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 .
/*
* Microsecond delay
*/
void therm_delay(uint16_t nCount)
{
__IO uint16_t TIMCounter = nCount;
uint16_t start = TIM_MST->CNT;
uint16_t end = start + TIMCounter;
__IO uint16_t read;
while (1) {
read = TIM_MST->CNT;
if(read < start)
read = 0xffff - start + read;
if (read >= end)
break;
}
}
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.
//PB8: IO direction setting
#define THERM_INPUT_MODE() { GPIOB->CRH &= 0xFFFFFFF0; GPIOB->CRH |= 0x4; }
#define THERM_OUTPUT_MODE() { GPIOB->CRH &= 0xFFFFFFF0; GPIOB->CRH |= 0x5; }
#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:
// Read the result
typedef struct {
int8_t value;
int8_t error;
} ThermValue;
.
.
.
__IO ThermValue v_ds18b20;
/* ds18b20 */
if (start_conversion) {
v_ds18b20 = therm_read_temperature();
start_conversion = false;
}
else {
therm_start_conversion();
start_conversion = true;
}
if (v_ds18b20.error == 0)
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.
Previous article:stm32f103 reads infrared receiver HS0038A2
Next article:Output Compare of stm32 timer
Recommended ReadingLatest update time:2024-11-15 15:53
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- As shown in the figure, which type of capacitor filtering wiring is better?
- Registration is open | TI Award Live: The industry's most accurate 3D Hall effect position sensor
- Power supply tips you must know: A small negligence can ruin EMI performance!
- What is the inverted state of the transistor and what is its function!
- IAP issue when porting from Qinheng CH32F103 to CH32V103
- Playing with Bluetooth chips on a perforated board, where is the antenna? Come and see Qinheng's 10-pin CH9141K
- Common Faults and Solutions for DC-DC Power Modules
- Design of lightning protection circuit for RS-485 in multifunctional electric energy meter
- Generator re-voltage overcurrent protection
- The program segment of TMS320F28XXX is copied from flash to ram and run