cancel
Showing results for 
Search instead for 
Did you mean: 

Timer enable and disable

fiulala
Associate II
Posted on September 15, 2015 at 12:32

Hi,

I have a stm32f4-disco, and I wanted to enable and disable a timer using external interrupt. When the external signal arrives I enable the timer, and when 400 ticks have passed I disable the timer. I thought that it would make the timer work when every interrupt arrives and stop after 400 ticks, but it only works one time. Here's the code:

// Assumptions per system_stm32f4xx.c CPU @ 168 MHz, APB2 @ 84 MHz (/2), APB1 @ 42 MHz (/4)
// TIM13 40KHz PWM (PA.06 TIM13_CH1)
#include ''stm32f4xx.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4_discovery.h''
#include ''stm32f4xx_tim.h''
#include ''misc.h''
#include ''stm32f4xx_exti.h''
#include ''stm32f4xx_syscfg.h''
void ClockSetUp(void) {
// Initialize system
SystemInit();
RCC_DeInit();
// PLL Setup
RCC_HSEConfig(RCC_HSE_ON);
RCC_WaitForHSEStartUp();
RCC_PLLConfig(RCC_PLLSource_HSE,5,210,2,4);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_PCLK2Config(RCC_HCLK_Div2);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
}
/**************************************************************************/
void RCC_Configuration(void)
{
/* GPIO's clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* TIM's clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);
}
/**************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Configuration: TIM13 CH1 (PA6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM13 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM13);
/* Configure PA0 pin as input floating (EXTI, interrupcio arduino)*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM13 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************/
uint32_t Period;
void TIM13_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//Periode en ticks, (168Mhz/2)/40kHz
Period = (SystemCoreClock / 2) / 40000; // 40 KHz, from APB1 clock *2, nominally 84 MHz
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM13, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50/50 duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM13, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);
/* TIM Interrupts enable */
TIM_ITConfig(TIM13, TIM_IT_Update, ENABLE);
}
/**************************************************************************/
#define TimerON 400
void TIM8_UP_TIM13_IRQHandler(void) // 40KHz
{
static int modulatingcount = 0;
// 1 Sec On - 40000 ticks// 10ms - 400 ticks
if (TIM_GetITStatus(TIM13, TIM_IT_Update) != RESET) // Validate source
{
TIM_ClearITPendingBit(TIM13, TIM_IT_Update); // Clear source
modulatingcount++;
if (modulatingcount > TimerON){
TIM_Cmd(TIM13, DISABLE);
}
}
}
/**************************************************************************************/
static void EXTILine0_Config(void);
void GPIO_Initialize(void);
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
static void EXTILine0_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
/* Connect EXTI Line0 to PA0 pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
/* Configure EXTI Line0 */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
STM_EVAL_LEDToggle(LED4);
TIM_Cmd(TIM13, ENABLE);
/* Clear the EXTI line 0 pending bit */
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
/**************************************************************************************/
int main(void)
{
ClockSetUp();
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM13_Configuration();
EXTILine0_Config();
STM_EVAL_LEDInit(LED4);
while(1){
}
}

1 REPLY 1
Posted on September 15, 2015 at 16:19

but it only works one time.

Yes, because you'd have to clear the count, otherwise it would still be >400

To clear it externally, outside the IRQ Handler, it would need to be global in scope. You'd probably pick a less generic name in that case too.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..