My first timer (TIM1) is acting as the start of ADC conversion. I know I can bind some timers on the ADC events but I choose to separate that. The code snippet is:
/* Time 1 Base configuration 25µs */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 4;
TIM_TimeBaseStructure.TIM_Prescaler = 360;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_CKD_DIV1 is actually the main cpu clock and in my case 72MHz. The precaler is dividing that frequency to have the final period.
72MHz/360 = 200kHZ or 5µs period unit.
The minimum period you can set up is 5µs because the STM needs one complete cycle first to start counting. The TIM_Period struct member defines the final timer period you want to work with in my case:
5µs + 4 x (5µs) = 25µs or 40kHz sampling frequency.
We want our timer to just count up until it's overflowed and gives an interrupt on overflow that will be done by taking TIM_CounterMode_Up as the value for CounterMode member. To enable the interrupt on that timer following snippet is needed:
/* Enable and configure TIM1 IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
The only thing you need to do now is simply put some code in your int handler known as vector address. In the ride environment this is done in a separate file [target]_it.c
void TIM1_UP_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update );
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
You can change the interrupt priority here but I will go into details on general interrupt handling in an other blog. Don't forget to enable the clock for the timer and the interrupt:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update , ENABLE);
That's basically it so pretty simple and straightforward and easy to learn if you are familiar with other targets.
do we need to configure it as GPIO then use it??
ReplyDeletei have try your setting but i get 0.0105ms between pulse why is this so i am so confused
ReplyDelete