cancel
Showing results for 
Search instead for 
Did you mean: 

Tim 2 does not work well stm32f4 ... why?

orn
Associate II
Posted on November 21, 2012 at 15:37

I'm using the following code to update an output device, in this case the LCD, but when I enable TIM 2 the microcontroller stops working! what is wrong?

void Tim2_LCD_IT(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//Configure TIM2 
TIM_TimeBaseStructure.TIM_Period =((SystemCoreClock / 10000) / 2) - 1; 
TIM_TimeBaseStructure.TIM_Prescaler = 10000 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
/* Enable TIM2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void){
TIM_ClearITPendingBit(TIM2,TIM_IT_Update); 
LCD_command(Seconda_Riga); //Seconda riga 
LCD_PrintData(Sprint,lenght(Sprint));
LCD_PrintData('' % '',13);
}

I also enabled another TIM to transmit data over the CAN, maybe there is some conflict?

void TIM4_IRQHandler(void)
{ //CAN TEST
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); 
TxMessage.Data[0] = 0x0F;
CAN_Transmit(CANx, &TxMessage);
}
void NVIC_Can_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

2 REPLIES 2
Posted on November 21, 2012 at 16:42

I'm using the following code to update an output device, in this case the LCD, but when I enable TIM 2 the microcontroller stops working! what is wrong?

Perhaps what you are trying to do under interrupt is inappropriate. ie Takes too long, has delays, or is dependent on other interrupt running.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
orn
Associate II
Posted on November 21, 2012 at 17:34

you are right, it is the management of the LCD that creates problems, thanks Clive, for all your great advice! :)