Skip to main content
Ngai yuk kong
Associate II
May 2, 2018
Question

How to setup timer to trigger ADC using STM32F051

  • May 2, 2018
  • 3 replies
  • 875 views
Posted on May 02, 2018 at 12:34

I know this is an old problem but I really cannot figure out.

My intention is to use timer 15 as a trigger source with setting equal to 1kHz. Then by enabling ADC interrupt using EOC  flag I think I could have an ADC interrupt of 1kHz used to convert 5 channels (they are not sampling at the same rate, so I want to use this ADC interrupt to set another time period.....)

I have the below source code: I could not enter the interrupt routine even one time. 

void initADC(void)

{

ADC1->CFGR1 =

_VAL2FLD(ADC_CFGR1_DISCEN, 1) | // discontinuous mode

_VAL2FLD(ADC_CFGR1_AUTOFF, 0) |

_VAL2FLD(ADC_CFGR1_WAIT, 0) |

_VAL2FLD(ADC_CFGR1_CONT, 0) |

_VAL2FLD(ADC_CFGR1_OVRMOD, 0) |

_VAL2FLD(ADC_CFGR1_EXTEN, 1) |

_VAL2FLD(ADC_CFGR1_EXTSEL, 4) |

_VAL2FLD(ADC_CFGR1_ALIGN, 0) | // data right aligned

_VAL2FLD(ADC_CFGR1_RES, 0) | // 12 bit resolution

_VAL2FLD(ADC_CFGR1_SCANDIR, 0) | // upward scan

_VAL2FLD(ADC_CFGR1_DMACFG, 1) | // DMA circular mode

_VAL2FLD(ADC_CFGR1_DMAEN, 0); // DMA disabled

ADC1->CFGR2 |= ADC_CFGR2_CKMODE_0;

// sampling time set to 28.5 ADC clock cycles:

ADC1->SMPR = _VAL2FLD(ADC_SMPR_SMP, 3);

//I have 5 channels but just test one channel first

ADC1->CHSELR = ADC_CHSELR_CHSEL1; //| //PA1

//ADC_CHSELR_CHSEL8 | //PB0

//ADC_CHSELR_CHSEL9; //PB1

ADC1->IER |= ADC_IER_EOCIE;

cfp_SetBit(&ADC1->ISR, ADC_ISR_ADRDY); // clear ready flag

cfp_SetBit(&ADC1->CR, ADC_CR_ADEN);

while (!cfp_IsBitSet(ADC1->ISR, ADC_ISR_ADRDY)) {

;

}

cpu_EnableInterrupt(ADC1_COMP_IRQn);

//init timer 15

TIM15->PSC = 47;   //48MHz / 48

TIM15->ARR = 999; //set to 1kHz

TIM15->CCR2 |= TIM_CR2_MMS_1;

TIM15->EGR |= TIM_EGR_UG;

TIM15->CR1 |= TIM_CR1_CEN;

cfp_SetBit(&ADC1->ISR, ADC_ISR_EOS); // clear end of sequence flag

cfp_SetBit(&ADC1->CR, ADC_CR_ADSTART);

}

void ADC1_COMP_IRQHandler(void)

{

   if( (ADC1->ISR & ADC_ISR_EOC) )

   {

   ADC1->ISR &= ~(ADC_ISR_EOC);

   digio_ToggleOutputValue(GPIOB, 14);

   }

}

Thank you in advance for any help...........

    This topic has been closed for replies.

    3 replies

    Ngai yuk kong
    Associate II
    May 3, 2018
    Posted on May 03, 2018 at 05:19

    I solve it, thanks all.

    ST Technical Moderator
    May 7, 2018
    Posted on May 07, 2018 at 12:14

    Hello

    paul.ngai

    ,

    It will be more interesting to let us know how you resolved this issue. It may be helpful for forum users.

    Best Regards,

    Imen

    In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
    Apill
    Associate III
    September 17, 2018

    @Imen DAHMEN, There is also a video in youtube with the key word ADC TIMER