Three minutes to understand stm8s microcontroller to achieve multi-channel conversion

Today, using the AD function of the stm8s microcontroller, there are ten channels from AN0 to AN9 on the microcontroller. I want to use only three of them AN5 to AN7.

Three minutes to understand stm8s microcontroller to achieve multi-channel conversion

//ADC initialization

Void ADC_Init(void)

{

ADC_CR1 = 0x50;

ADC_CR2 = 0x38;

ADC_TDRL = 0xE0; / / disable Schmitt trigger

}

Unsigned int Read_ADC_Temp(unsigned channel)

{

Unsigned int i, ADtemp;

ADC_Init();

//ADC_CSR = 0x00;

ADC_CSR = channel;

ADC_CR1 = 0x01; // The lowest bit of the CR1 register, 1 enables ADC conversion

For(i = 0;i "300;i ++);// Delay for a period of time, at least 7uS, to ensure that the ADC module is powered on.

ADC_CR1 = ADC_CR1 | 0x01; // Set the lowest position of the CR1 register again

/ / Enable ADC conversion

While(!(0x80&ADC_CSR)); // Wait for the ADC to end

ADtemp = (unsigned int) (ADC_DRH "8" | ADC_DRL;

ADC_CR1 = 0x00;

Return ADtemp;

}

When using a channel alone

Adtemp = read_a2d_dis(0x07);

Printf("%d", adtemp);

The value read is correct. The ground is 0 and connected to VDDA is 1023.

but

Adtemp = read_a2d_dis(0x07);

Adtemp1 = read_a2d_dis(0x06);

Printf("%d", adtemp);

Printf("%d", adtemp1);

After switching channels, the data read is incorrect. When connected to the ground, it is 768, 254. It is not possible to reinitialize after switching channels. At first it was thought that it should be delayed after each sample read and play data. Later, when reading data, delay 1S or not. The measured voltage of the hardware is also correct. The settings on the datasheet are also correct. Later, I had no choice but to read it a few times to see if the result was the same.

Later it was found that as long as it was read twice in the same channel. The result of the removal is correct.

Unsigned int read_a2d_dis(unsigned char channel)

{

Unsigned int tempTab[5];

Unsigned int i,j,temp;

Temp = 0;

For(i = 0;i " 5;i ++)

{

tempTab[i] = Read_ADC_Temp(channel);

}

For(j=0;j"5;j++)

{

For (i=0;i"5-j;i++)

{

If (tempTab[i]》tempTab[i+1])

{

Temp=tempTab[i];

tempTab[i]=tempTab[i+1];

tempTab[i+1]=temp;

}

}

}

Return tempTab[2];

}

After making the above changes, read a total of five data, taking the median. There is no problem with raising the value, but why do you have to read it twice to get the correct data and still can't find the answer. It is only suspected that after the channel is switched, the MCU stores the data into ADC_DRH, and the shift register is misaligned when it is in ADC_DRL.

200*300mm Anti-Blue Light Film

The Imported Anti-Blue Light For ipad is made of high-quality imported TPU material. It is precisely cut by the Screen Protector Cutting Machine. Protect your device screen from scratches and cracks. The flexible TPU Screen Protector can completely cover the iPad screen, providing the maximum protection for the iPad.

Advanced blue light blocking technology can protect your eyes, can effectively filter blue light, prevent glare, reduce eye fatigue, and help you fall asleep better.

Super clear and transparent, will not reduce the screen brightness, maintain the original screen brightness, and reduce eye fatigue.

The Protective Film adopts an oleophobic and waterproof coating, which can ensure anti-fingerprint, waterproof, dustproof and no air bubbles. Moreover, it is sensitive to touch, bringing natural touch, fast response and flawless precise touch.

If you want to know more about Imported Anti-Blue Light For ipad products, please click product details to view the parameters, models, pictures, prices and other information about Imported Anti-Blue Light For ipad products.

Whether you are a group or an individual, we will try our best to provide you with accurate and comprehensive information about Imported Anti-Blue Light For ipad!

Imported Anti-Blue Light Protective Film, Imported Anti-Blue Screen Protective Film, Imported Anti-Blue Screen Protector, Imported Anti-Blue Light Screen Protector

Shenzhen Jianjiantong Technology Co., Ltd. , https://www.mct-sz.com