This is the 24th day of my participation in the August Text Challenge.More challenges in August

When debugging the ADC analog watchdog function of STM8 microcontroller, I found that the register description of setting the upper and lower limits of analog watchdog voltage in The Chinese manual was wrong.

First look at the introduction of analog watchdog on the chip manual

It is stated in the manual that the ADC_HTR and ADC_LTR registers are used to set the upper and lower voltages of the analog watchdog.

The register ADC_HTRH is used to set the upper limit by 2 bits.

The ADC_HTRL register is used to set the lower 8 bits of the upper limit.

The ADC_LTRH register is used to set the upper 2 bits of the lower limit.

Set the upper and lower limit registers according to the register mode in the document

    // Set the upper threshold value
    ADC_HTRH = 0x03;    // Store the top 8 bits of 10 bits of data
    ADC_HTRL = 0xe8;
    
    // Set the lower threshold
    ADC_LTRH = 0x00;    // Store the top 8 bits of 10 bits of data
    ADC_LTRL = 0x64;
Copy the code

Set the upper limit to 0x03e8 (1000 in decimal) and the lower limit to 0x0064 (100). The watchdog is triggered when the sampled analog voltage is less than 100 or more than 1000.

According to the documentation, the ADC_HTRH register stores the high 2 bits value and the ADC_HTRL stores the low 8 bits value.

The calculator shows that 1000 is exactly 10 bits. The highest two bits are 1 (3) and the lowest eight bits are 1110 and 1000 (E8), so the ADC_HTRH register is set to 0x03 and the ADC_HTRL register is set to 0xE8.

The highest two bits of the number 100 are 0 and the lowest eight bits are 0110 01000 or 0x64, so the ADC_LTRH register is set to 0x00 and the ADC_LTRL register value is set to 0x64. Then the IAR software is used for online debugging to observe whether the values of these two registers are set successfully.

Before setting the register, the ADC_HTRH register defaults to 0x03, the ADC_HTRL register defaults to 0xFF, and both the ADC_LTRH register and the ADC_LTRL register default to 0.

Register Settings are then performed

You can see that after these four lines of code are executed, the ADC_HTRH register value is 0x03, the ADC_HTRL register value is 0x00, and both the ADC_LTRH and ADC_LTRL registers are 0.

That is, the values of these four registers are not the values set in the code, so why is this?

So I found an official library function code, in the library function to find the operation of these four registers.

In the stm8s_adc1.c file, there are two functions that are specifically used to set the upper and lower limits of the simulated watchdog.

Through the observation of these lines of code, we can find a problem, that is, the high level of the register stores the value of the data set after the right shift of 2 bits, that is to say, the value of the high level register is the high 8 bits of the value of the 10-bit register, and the low level register stores the low 2 bits of the value of the 10-bit register. Is this the case? Test the Settings of library functions directly in the code.

Before setting the register, the ADC_HTRH register defaults to 0x03, the ADC_HTRL register defaults to 0xFF, and both the ADC_LTRH register and the ADC_LTRL register default to 0.

Next, set the register value using the method in the library function

After setting, you can see that the ADC_HTRH value is 0xC8, ADC_HTRL value is 0x00,ADC_LTRH value is 0x4B, and the ADC_LTRL register value is 0x00.

Is this the set value? Display the values of these four registers in binary mode.

Combine the values of this set of registers according to the combination of high 8 bits and low 2 bits, then

The value of the ADC_HTR register is 11 0010 0000

Convert to base 10

The value of the ADC_LTR register is 01 0010 1100

The upper limit is 800 and the lower limit is 300, the same values as set in the code

It is correct to operate registers in this way. In order to verify this idea, I found the English version of STM8 MCU.

Through the English version of the data can see clear, high storage register bit2 to combination, also is the high 8-bit data, low of data is stored in a register bit0 and bit1, also is the low two data, this and the program verification, English version of the manual is correct, and in the official download the Chinese version of the manual, The description of these registers is incorrect.