When I run this code on my STM32L1 board, the debugger hangs once it reaches to a Float (in this case the variable 'Distance') or Delay, I mean it seems like it's processing, however it never goes through, and I have to close it and run the debugger again, which hangs of course again. Note that the code builds without errors or warnings. Is there's any special configuration for float ?
The following is the related part of the code:
void Delay(int x){ //input milliseconds, delay that number of milliseconds int a,b; for(a=0; a<x; a++){ for(b=0; b<1000; b++){ } } } . . . volatile int timespan = 0; // Total pulse width int main(void){ float Distance; // actual distance in cm . . . while(1){ TIM4_Echo_Read(); Distance = (timespan / 58.0); if (Distance <= 100){ GPIOB->BSRRL = (1<<7); } else { GPIOB->BSRRH = (1<<7); } Delay(10); }
timespan is declared as a global int. Note that none of this happens when I declare Distance as int instead of float.
Any idea why the debugger keeps getting stuck on float instructions?
You can't just randomly enable interrupts without the TIMx_IRQHandler() code to service them. Code you don't show, so assuming it is missing.
Your code is stuck in the Default_Handler code because you don't deal with the interrupt with your own routine.
The TIM will work without the interrupt being enabled.
When you first published your code none of this detail was presented, it is important to make a complete presentation.
I thought I already enabled TIM4 interrupt via the following lines:
TIM4->DIER |= TIM_DIER_UIE; // UPDATE INTERRUPT ENABLE
TIM4->CCER |= TIM_CCER_CC1E; // ENABLE COUNTER CAPTURE
TIM4->DIER |= TIM_DIER_CC1IE; // ENABLE CH1 CAPTURE/COMPARE INTERRUPT
TIM4->CR1 |= TIM_CR1_CEN; // Enable the counter
NVIC_SetPriority(TIM4_IRQn, 1); // SET PRIORITY TO 1
NVIC_EnableIRQ(TIM4_IRQn); //ENABLE TIM4 INTERRUPT IN NVIC
What I'm I missing here..
void TIM4_IRQHandler(void) { // Handle and clear the interrupt you've caused. if (TIM4->SR & TIM_SR_UIF) { TIM4->SR = ~TIM_SR_UIF; // Perhaps read registers or do something related to why you wanted interrupt? } }
I don't believe these concepts are entirely unique to ARM or STM32 parts.
I can't believe I missed that. I changed the function name from TIM4_IRQHandler to TIM4_Echo_Read without changing it on the NVIC_EnableIRQ() line. just changed the function name and everything worked, well the debugger doesn't hang anymore, however I still don't get the Distance value, I guess (according to the guys in Stackoverflow) it's because STM32L1 doesn't have FPU, But the LED did turn on when an object closer that 1 meter is detected, even with the float declaration.
@Westonsupermare Pier thank you for your patience & knowledge, you sure have a lot of both.