adc出现问题

程序如下,adc搬了论坛的liyanan用户的程序,程序是定时器中断中设置adc采集,

        DMA_Cmd(DMA1_Channel1, ENABLE);

        ADC_SoftwareStartConvCmd(ADC1, ENABLE);

        while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

        ADC_SoftwareStartConvCmd(ADC1, DISABLE);

        DMA_Cmd(DMA1_Channel1, DISABLE);

但如上程序在while中运行正常,采集正常,但我放在定时器中断中却无法采到ad值,一直停在 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

中,为啥?如何修改?


#include "debug.h"

void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

u16 TxBuf[3];//ad获取存储

void ADC_Function_Init(void)

{

    ADC_InitTypeDef  ADC_InitStructure = {0};

    GPIO_InitTypeDef GPIO_InitStructure = {0};


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    RCC_ADCCLKConfig(RCC_PCLK2_Div8);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOD, &GPIO_InitStructure);


    ADC_DeInit(ADC1);

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

    ADC_InitStructure.ADC_ScanConvMode = ENABLE;//多通道需要使能,扫描模式

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换

    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//没有外部触发

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

    ADC_InitStructure.ADC_NbrOfChannel = 3;//转换三个通道

    ADC_Init(ADC1, &ADC_InitStructure);


    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_241Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_241Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_241Cycles);


    ADC_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT);

    ADC_DMACmd(ADC1, ENABLE);

    ADC_Cmd(ADC1, ENABLE);

    ADC_ResetCalibration(ADC1);

    while(ADC_GetResetCalibrationStatus(ADC1));

    ADC_StartCalibration(ADC1);

    while(ADC_GetCalibrationStatus(ADC1));

}

u16 Get_ADC_Val(u8 ch)

{

    u16 val;

    ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_241Cycles);

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

    val = ADC_GetConversionValue(ADC1);

    return val;

}

void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize)

{

    DMA_InitTypeDef DMA_InitStructure = {0};

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMA_DeInit(DMA_CHx);

    DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr;

    DMA_InitStructure.DMA_MemoryBaseAddr = memadr;

    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

    DMA_InitStructure.DMA_BufferSize = bufsize;

    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA_CHx, &DMA_InitStructure);

}

void TIM1_Int_Init(u16 arr,u16 psc)

{

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure={0};

    NVIC_InitTypeDef         NVIC_InitStructure={0};


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);       //使能TIM1时钟


    TIM_TimeBaseStructure.TIM_Period = arr;                    //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值。

    TIM_TimeBaseStructure.TIM_Prescaler =psc;                  //指定用于划分TIM时钟的预分频器值。

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //时钟分频因子

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM计数模式,向上计数模式

    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);            //根据指定的参数初始化TIMx的时间基数单位


    //初始化TIM NVIC,设置中断优先级分组

    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;         //TIM1中断

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //设置抢占优先级0

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;         //设置响应优先级3

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //使能通道1中断

    NVIC_Init(&NVIC_InitStructure);                            //初始化NVIC


    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //使能TIM1中断,允许更新中断

    TIM_Cmd(TIM1, ENABLE); //TIM1使能

}

int main(void)

{

    SystemCoreClockUpdate();

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    TIM1_Int_Init(100-1,720-1);

    ADC_Function_Init();

    DMA_Tx_Init(DMA1_Channel1, (u32)&ADC1->RDATAR, (u32)TxBuf,3);

    DMA_Cmd(DMA1_Channel1, ENABLE);

            ADC_SoftwareStartConvCmd(ADC1, ENABLE);

            while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

            ADC_SoftwareStartConvCmd(ADC1, DISABLE);

            DMA_Cmd(DMA1_Channel1, DISABLE);

    while(1)

    {


    }

}


void TIM1_UP_IRQHandler(void)

{



    if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查TIM1中断是否发生。

    {

        DMA_Cmd(DMA1_Channel1, ENABLE);

        ADC_SoftwareStartConvCmd(ADC1, ENABLE);

        while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

        ADC_SoftwareStartConvCmd(ADC1, DISABLE);

        DMA_Cmd(DMA1_Channel1, DISABLE);


        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);      //清除TIM1的中断挂起位。

     }

}


您好,可将ADC初始化放在定时器初始化之前试一下


这个解决了,这个程序是要求每隔2ms采集一次三个通道的值,思路是在定时中断中设置标志位,while中执行adc采集,为判断

是否正常进入中断和采集,分别在中断和主程序ad后设置端口数出使用示波器查看波形,发现一个有意思的问题,在主程序中我将采集到的第一通道值依次赋给一数组a,发现波形很好,但如果我删掉此,却发现主程序执行就不正常了,按道理我删掉应该是节省了时间,程序如下(我没有删掉赋值a数组)


//定时器产生一次中断时间计算方式如下:

//T=(arr+1)*(psc+1)/Tpclk

//定时器计数一次的时间为:

//t=(psc+1)/Tpclk

//(1/1M)Hz=1us  (1/1K)Hz=1ms


#include "debug.h"

void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

volatile u16 TxBuf[3];//ad获取存储

volatile u16 a[400];//ad获取存储

volatile u8 i=0;

volatile u8 c=0;

volatile u8 k=0;

volatile u8 w=0;

void ADC_Function_Init(void)

{

    ADC_InitTypeDef  ADC_InitStructure = {0};

    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    ADC_DeInit(ADC1);

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

    ADC_InitStructure.ADC_ScanConvMode = ENABLE;//多通道需要使能,扫描模式

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换

    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//没有外部触发

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

    ADC_InitStructure.ADC_NbrOfChannel = 3;//转换三个通道

    ADC_Init(ADC1, &ADC_InitStructure);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_241Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_241Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_241Cycles);

    ADC_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT);

    ADC_DMACmd(ADC1, ENABLE);

    ADC_Cmd(ADC1, ENABLE);

    ADC_ResetCalibration(ADC1);

    while(ADC_GetResetCalibrationStatus(ADC1));

    ADC_StartCalibration(ADC1);

    while(ADC_GetCalibrationStatus(ADC1));

}

u16 Get_ADC_Val(u8 ch)

{

    u16 val;

    ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_241Cycles);

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

    val = ADC_GetConversionValue(ADC1);

    return val;

}

void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize)

{

    DMA_InitTypeDef DMA_InitStructure = {0};

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMA_DeInit(DMA_CHx);

    DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr;

    DMA_InitStructure.DMA_MemoryBaseAddr = memadr;

    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

    DMA_InitStructure.DMA_BufferSize = bufsize;

    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA_CHx, &DMA_InitStructure);

}

void TIM1_Int_Init(u16 arr,u16 psc)

{

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure={0};

    NVIC_InitTypeDef         NVIC_InitStructure={0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);       //使能TIM1时钟

    TIM_TimeBaseStructure.TIM_Period = arr;                    //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值。

    TIM_TimeBaseStructure.TIM_Prescaler =psc;                  //指定用于划分TIM时钟的预分频器值。

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //时钟分频因子

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM计数模式,向上计数模式

    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);            //根据指定的参数初始化TIMx的时间基数单位

    //初始化TIM NVIC,设置中断优先级分组

    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;         //TIM1中断

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //设置抢占优先级0

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;         //设置响应优先级3

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //使能通道1中断

    NVIC_Init(&NVIC_InitStructure);                            //初始化NVIC

    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //使能TIM1中断,允许更新中断

    TIM_Cmd(TIM1, ENABLE); //TIM1使能

}


void GPIO_Toggle_INIT(void)

{

    GPIO_InitTypeDef GPIO_InitStructure = {0};


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

}

int main(void)

{

    SystemCoreClockUpdate();

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


    ADC_Function_Init();

    DMA_Tx_Init(DMA1_Channel1, (u32)&ADC1->RDATAR, (u32)TxBuf,3);


    TIM1_Int_Init(96-1,1000-1);

    GPIO_Toggle_INIT();

    while(1)

    {

        if(k==1)

        {


            DMA_Cmd(DMA1_Channel1, ENABLE);

            ADC_SoftwareStartConvCmd(ADC1, ENABLE);

            while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

            ADC_SoftwareStartConvCmd(ADC1, DISABLE);

            DMA_Cmd(DMA1_Channel1, DISABLE);

            a[i]=TxBuf[0];

            i++;

            GPIO_WriteBit(GPIOC, GPIO_Pin_2, (w == 0) ? (w = Bit_SET) : (w = Bit_RESET));

            k=0;

        }



    }

}


void TIM1_UP_IRQHandler(void)


{

    if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查TIM1中断是否发生。

    {


        k=1;

        if(c==0)

        {

            GPIO_ResetBits(GPIOC, GPIO_Pin_1);

            c++;

        }

        else

        {

           GPIO_SetBits(GPIOC, GPIO_Pin_1) ;

           c=0;

        }

        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);      //清除TIM1的中断挂起位

     }


}

1729812645372.png

但如果我删掉主程序赋值语句,

1729812855462.png

1729812890408.png


您好,下帖已回复

https://www.wch.cn/bbs/thread-126123-1.html

 


只有登录才能回复,可以选择微信账号登录