✨本工程以上面一篇的工程为模板,在此基础上实现ADC电压采样。

📝本案例采用ADC单次采样模式,本示例代码提供不同精度的采集方式,根据需求选择对应的接口函数。
AIN0---->PB6
AIN1---->PB5
AIN2---->PB4
AIN3---->PD3
AIN4---->PD5
AIN5---->PD6
⚡需要注意的是当选择通道AIN5时,需要重新映射串口端口。串口发送引脚和通道5都在一个引脚。当使能了通道5后,如果串口不重新映射将无法打印信息。

🔨映射方法:(将串口映射到PF4,PA3引脚上)

🍁数据对齐

● 🔰在左对齐模式下,先读高位字节寄存器(ADC_DRH),再读低位字节寄存器(ADC_DRL)。
● 🔰在右对齐模式下,先读低位字节寄存器(ADC_DRL),再读高位字节寄存器(ADC_DRH) 。
🎉此时,用户也可以选择使用LDW指令来读取整个ADC_DR,因为该指令和右对齐模式采用同样的读取顺序。
u16 ReadVol_CH3( void )
{
u16 voltage = 0;
if( ADC_flag )
{
ADC_flag = 0;
if ((ADC_CR2 & 0x08) != 0) /* Right alignment */
{
voltage = (uint16_t)(DATAL + (uint16_t)(DATAH << (uint8_t)8)) ;
}
else
{
voltage = ( DATAH << 2 ) + DATAL ; //得到十位精度的数据 0--1024
}
ADC_CR1 = ADC_CR1 | 0x01; //再次将CR1寄存器的最低位置1,
};
return voltage;
}
🔖STM8S903K3/F3系列产品包含10位逐次逼近A /D转换器(ADC1),具有多达7个外部和1个内部多路输入通道.


🌿采集的引脚是PD3,AIN4的电压值。
void main()
{
unsigned int VALUE;
CLK_CKDIVR = 0x00; //主频16MHz
_asm("SIM");
GPIO_Init();
ADC_GPIO_Init();
UART1_Init(115200);
Init_Timer5();
_asm("RIM");
while (1)
{
TIM_Delay_ms( 800);
ADC_CH_Init(3);
if(ADC_flag == 1)
{
// VALUE = ADC1_GetConversionValue();//8bit精度adc
VALUE = ReadVol_CH3();//10bit精度
My_print_Information_uint("ADC_Value= ", VALUE);
ADC_flag = 0; // ADC中断标志 置0
}
}
}

struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, TIM5_UPD_OVF_IRQHandler}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, UART1_Receive_Interrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, ADC_Handle}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
u16 ReadVol_CH3( void )
{//10BIT精度adc
u16 voltage = 0;
if( ADC_flag )
{
ADC_flag = 0;
voltage = ( DATAH << 2 ) + DATAL ; //得到十位精度的数据 0--1024
ADC_CR1 = ADC_CR1 | 0x01; //再次将CR1寄存器的最低位置1,
};
return voltage;
}
链接:https://pan.baidu.com/s/179Hjd-OGmSUzaprnd5aVSA
提取码:xil8