2013-12-11 05:33 AM
Hello everyone. I'm new to programming, just got this stm32f3 board. I tried to run TIM_PWM_Input example ant it seems to work just fine. That's what i want to do now - measure external signal frequency. However, when i created new project, and copied absolutely everything from TIM_PWM_Input example, with all libs and directories, the code doesn't work. When i connect external signal and run the code, it jumps to HardFault_handler. So i want to ask is there a way to fix it and why it happens? i mean i just coppied example project and it doesn't work. Will appreciate any help. Thanks in advance.
2013-12-13 01:07 AM
Here is my code. Hope this will help you to explain me what im doing whrong and why it always stops in TIM2_IRQHandler
#include ''main.h''#include ''stdio.h''TIM_ICInitTypeDef TIM_ICInitStructure;static void TIM_Config(void);int main(void){ //uint32_t freq; // uint32_t duty; // uint32_t k,k1; TIM_Config(); // Frequency = 72000000/TIM2->CCR2 (Hz) TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // connect signal to PA1 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM2, &TIM_ICInitStructure); TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); // Select the slave Mode: Reset Mode TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable); TIM_Cmd(TIM2, ENABLE); // Enable counter and interrupt request TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); /* freq = 72000000/(TIM2->CCR2);k=(TIM2->CCR1*100);k1=TIM2->CCR2;duty=k/k1;printf(''%d %d'', freq, duty);*/ while (1) { }}static void TIM_Config(void){ GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // Timer 2 clock on /* GPIOA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // GPIOA clock // Timer 2 channel2 PA1 settings GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); // Connect TIM pin to AF1 GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1); // Enable Timer 2 global Interrupt NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}So i dont need to connect external quartz to measure high frequency correctly?2013-12-16 03:15 AM
Hey guys. Still working to solve these problems. Could anyone explain me, if it's possible to make external signal as a timer/counter, then with an another timer measure time, and like that i cant measure external signal frequency? Was it exactly what you were saying, clive? Thanks for answers and help.
2013-12-16 04:17 AM
why it always stops in TIM2_IRQHandler
Yet it is the one piece of code we don't see that code? Make sure you qualify and clear the source of the interrupt. Try measuring low frequencies, say 50 Hz, once that works try higher frequencies to understand where the code saturates. TIM2 is 32-bit as I recall, so should be capable of measuring slow frequencies with high precision.Input Capture and PWM Input should permit analysis for period/duty, the latter resets the counter at each pulse, good for things like measuring servo control inputs. The counters can also take an external input, and be gated by other timers. I've posted a bunch of examples in the past, probably not using the F3, as it's not a part I spend any time with.2013-12-16 08:20 AM
Thanks for your help clive. I finally measured frequency and duty cycle and managed to make printf work. But still when i measure high frequencies, they are not correct. Is there any way to calculate in what bounds my measurement will fall? Or i should be able to measure it exactly correct with the way you told me earlier? ''...
for higher frequencies you want to count pulses, and integrate over a fixed period, 1 ms, a second, whatever is appropriate for the bit width of the counter, modulation and range being measured.''
Thanks for your help.2013-12-16 08:31 AM
Beyond a few hundred KHz I think you'd want to be using the counter to count pulses, and sample the count periodically.
If you are counting individual interrupts, at some point the processor runs out of bandwidth, it will always be in the interrupt routine, and the measurements for Input Capture will be wrong, ie perhaps the frequency appears to halve or something.2013-12-17 06:09 AM
Thanks for your help Clive. I managed to do everything i wanted, so now moved on and got a question. I did'nt find an answer in forum so maybe you could help me. Let's say i want to measure external signal frequency this way: i use external signal as a counter, then with another timer i give the value, for which the counting should be done. How can i configure Timer to know exact value of time? Example of what i want to do: Lets say i count pulses for 2.5ms. so i configure timer like this?:
TIM_TimeBaseInitStructure.TIM_Period = 10000-1;// so every 10k pulses cnt value goes +1? TIM_TimeBaseInitStructure.TIM_Prescaler =2-1;// so total count of pulses 10000*2=20000? that gives frequency of 8MHz/20000=400Hz. so t=1/400=2.5ms? TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure );}I need to know if i understant this configuration correct. Thank you very much for your help.2013-12-17 06:41 AM
Where does the 8 MHz come from?
2013-12-17 06:48 AM
Oh yeah. that should be 36Mhz, the frequency of the APB1 bus, to which Timer2 or Timer3 are conected?
2013-12-17 06:55 AM
They are probably 72 MHz, if your APB is divided down the TIMCLK = APBCLK * 2, please check Clock Tree diagram in reference manual
400 Hz Prescaler = 3 - 1; Period = 60000 - 1; Prescaler = 18 - 1; Period = 10000 - 1;2013-12-17 07:10 AM
once again you are absolutely correct. Many thanks for your help. Even though i cant make my idea work yet. For some reason the counter for external signal always stays at 0. Anyways, i hope ill figure this one out by myself. Thanks again.