cancel
Showing results for 
Search instead for 
Did you mean: 

TIM INTERRUPT IRQ STM32F3DISCOVERY

andreaspiezia9
Associate III
Posted on January 04, 2016 at 16:17

Hi all,

I'd like to have e PWM signal, starting with a duty cycle of 10% and incrementing up to 100%. I'm using next code, I see the waveform with 10%, but it never increase, it seems that it never enter in the TIM IRQ, and I don't understand why, can you help me? In file stm32f30x_it.c the IRQ is:

1.
void
TIM3_IRQHandler (
void
) 
2.
{ 
3.
SetTrue(); 
4.
5.
}


#include ''stm32f30x.h'' 

#include ''main.h'' 


static
volatile
unsigned 
int
CapCom_PULSE; 

__IO uint32_t IncDuty = 0; 
//boolean, if true increment duty cycle. It has set in ISRQ withSetTrue() func.




/************************************************/


void
TIM_Interr_Config(
void
) 

{ 

NVIC_InitTypeDef NVIC_InitStructure; 


/* Enable the TIM3 gloabal Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 

NVIC_Init(&NVIC_InitStructure); 


} 


void
RCC_Configuration(
void
) 

{ 

/* Enable GPIO clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 
// 


/* Enable TIM3 clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 

} 

void
GPIO_Configuration(
void
) 

{ 

GPIO_InitTypeDef GPIO_InitStructure; 

GPIO_DeInit(GPIOA);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); 
// TIM3_CH1 

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); 
// TIM3_CH2 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; 

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_NOPULL; 

GPIO_Init(GPIOA, &GPIO_InitStructure); 

} 



void
TIM_Configuration(
void
) 

{ 

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 

TIM_OCInitTypeDef TIM_OCInitStructure; 


TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; 
// 72 to 1 MHz 

TIM_TimeBaseStructure.TIM_Period = 1000 - 1; 
// 1 MHz to 1 KHz 

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 

TIM_OCInitStructure.TIM_Pulse = 900; 
// 10% duty cycle 

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 


TIM_OC1Init(TIM3, &TIM_OCInitStructure); 

TIM_Cmd(TIM3, ENABLE); 

} 


/*****************************************************/


int
main(
void
) 

{ 

IncDuty = 0; 
//boolean. If true increment duty cycle. It has set to TRUE in TIM3_IRQHandler, read in while loop and reset 

CapCom_PULSE = 500; 



RCC_Configuration(); 


TIM_Interr_Config(); 


GPIO_Configuration(); 


TIM_Configuration(); 



while
(1){ 

if
(IncDuty){ 

CapCom_PULSE--; 

TIM_SetCompare1(TIM3, CapCom_PULSE); 

IncDuty = 0; 

} 

} 


} 


void
SetTrue(){ 

IncDuty = 1; 

} 

#st
2 REPLIES 2
Posted on January 04, 2016 at 19:37

You'd need to enable a specific IT source in the TIM, like Update or CC1, etc.

You will also want to clear that source in the IRQ Handler, otherwise it will keep re-entering.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andreaspiezia9
Associate III
Posted on January 05, 2016 at 09:46

Thank you Clive, now it works! I'm sorry for my trivial error, but I'm a beginner.

Here below the correct code for everyone interested.

// STM32 TIM3/4 PWM (PA11/12,6/7) STM32F3-Discovery - sourcer32@gmail.com
// for TIMER INTERRUPT SEE DM00068049 PG 495
#include ''stm32f30x.h''
#include ''main.h''
//static volatile unsigned int TimingDelay;
static
volatile
unsigned 
int
CapCom_PULSE;
__IO uint32_t IncDuty = 0; 
//boolean, if true increment duty cycle. It has written in ISRQ and read in while loop 
__IO uint32_t TimingDelay = 0;
/**************************************************************************************/
void
TIM_Interr_Config(
void
)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} 
void
RCC_Configuration(
void
)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 
//see DM00068049 pg. 235
/* Enable TIM3 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
/**************************************************************************************/
void
GPIO_Configuration(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOA); 
// Free pins located in STM32F3-Discovery manual, UM1570
//for AF deteils see DM00058181 Table 14 pg 44/134
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); 
// TIM3_CH1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); 
// TIM3_CH2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
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_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
// General-purpose timers (TIM2/TIM3/TIM4) description DM00043574 pg. 603
// Capture/Compare Registers (TIMx_CCRx), Auto-Reload Register (TIMx_ARR) 
void
TIM_Configuration(
void
)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; 
// 72 to 1 MHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; 
// 1 MHz to 1 KHz
/* Set the Autoreload value 
TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ; */
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 900; 
// 90% duty cycle
/* Set the Capture Compare Register value 
TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse; */
//TIM_SetCompare1(TIM1_CapCom);
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
/**************************************************************************************/
int
main(
void
)
{
IncDuty = 0; 
//boolean. If true increment duty cycle. It has set to TRUE in TIM3_IRQHandler, read in while loop and reset 
CapCom_PULSE = 500;
RCC_Configuration();
TIM_Interr_Config();
GPIO_Configuration();
TIM_Configuration();
while
(1){ 
if
(IncDuty){
CapCom_PULSE--;
TIM_SetCompare1(TIM3, CapCom_PULSE);
IncDuty = 0;
} 
}
}
void
SetTrue(){ 
IncDuty = 1; 
}