STM32f103 TIMER IRQ múltiple para generar una base de tiempo diferente usando SPL

0

Estoy tratando de controlar dos LED con diferentes retardos. Usé CUBEMX para la generación de código y usé TIM2 y TIM3 para generar diferentes bases de tiempo. Funcionó sin ningún problema. Simplemente usar TIM2_IRQHandler () y TIM3_IRQHandler () en stm32f1xx_it.c fue suficiente para hacer el trabajo. Pero cuando intento hacer lo mismo con SPL, no funciona. Ambos LED parpadean al mismo tiempo. Mi código es el siguiente. Por favor ayuda.

'GPIO_InitTypeDef gpio,pwm_GPIO;
TIM_TimeBaseInitTypeDef time,time2;

NVIC_InitTypeDef nvic,nvic2;

void sysclock_config(void);
void GPIO_config(void);
void TIMER_Config(void);
void NVIC_Config(void);

void sysclock_config()
{

RCC_HSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET){};

RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);

//DISABLE PLL
RCC_PLLCmd(DISABLE);

RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_4);// change RCC_PLLMul value to set the sysclock. SYSCLCK is 16MHz if RCC_PLLMul value is 4 

//ENABLE PLL
//WAIT FOR IT TO BE READY
//SET SYSCLK SRC AS PLLCLK
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){};
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

//SET HCLK = SYSCLK = 64MHZ
RCC_HCLKConfig(RCC_SYSCLK_Div1);//

//SET PCLK2 = HCLK = SYSCLCK
RCC_PCLK2Config(RCC_HCLK_Div1);//

//SET PCLK1 = HCLK/2 = SYSCLCK/2
RCC_PCLK1Config(RCC_HCLK_Div2);// to set PCLK1 to half of SYSCLOCK


/* If HSI (8MHz) on board crystal is not selected as clock source as abobe step then SYSCLOCK= 64MHz by default */

}


void GPIO_config()
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA,ENABLE);
GPIO_DeInit(GPIOC);
gpio.GPIO_Pin=GPIO_Pin_13;
gpio.GPIO_Mode=GPIO_Mode_Out_PP;
gpio.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&gpio);
GPIO_WriteBit(GPIOC,GPIO_Pin_13,0);

GPIO_DeInit(GPIOA);
pwm_GPIO.GPIO_Pin=GPIO_Pin_1;
pwm_GPIO.GPIO_Mode=GPIO_Mode_Out_PP;
pwm_GPIO.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&pwm_GPIO);
GPIO_WriteBit(GPIOA,GPIO_Pin_1,0);  

}

void TIMER_Config()
{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3,ENABLE);
time.TIM_ClockDivision=TIM_CKD_DIV1;
time.TIM_CounterMode=TIM_CounterMode_Up;
time.TIM_Prescaler=15999;
time.TIM_Period=999;
time.TIM_RepetitionCounter=0;

TIM_TimeBaseInit(TIM2,& time);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);

time2.TIM_ClockDivision=TIM_CKD_DIV1;
time2.TIM_CounterMode=TIM_CounterMode_Up;
time2.TIM_Prescaler=7999;
time2.TIM_Period=499;
time2.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,& time);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);




}




void NVIC_Config()
{


nvic.NVIC_IRQChannel=TIM2_IRQn;         
nvic.NVIC_IRQChannelPreemptionPriority=0;
nvic.NVIC_IRQChannelSubPriority=0;
nvic.NVIC_IRQChannelCmd=ENABLE;
NVIC_EnableIRQ(TIM2_IRQn);
NVIC_Init(&nvic);
nvic2.NVIC_IRQChannel=  TIM3_IRQn;              
nvic2.NVIC_IRQChannelPreemptionPriority=0;
nvic2.NVIC_IRQChannelSubPriority=0;
nvic2.NVIC_IRQChannelCmd=ENABLE;
NVIC_EnableIRQ(TIM3_IRQn);
NVIC_Init(&nvic2);

}





 int main(void)
 {

sysclock_config();

GPIO_config();

TIMER_Config();

NVIC_Config();







while(1);


 }

void TIM2_IRQHandler()
 {

static uint8_t c;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
 {
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);

    c^=1;
    GPIO_WriteBit(GPIOC,GPIO_Pin_13,c);





}





}

 void TIM3_IRQHandler()
 {

static uint8_t k;
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
{
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

    k^=1;
    GPIO_WriteBit(GPIOA,GPIO_Pin_1,k);





}





}'

El código que utiliza CubeMx que funcionó es el siguiente:

#include "main.h"
#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

 /* Private variables ---------------------------------------------------------*/
 TIM_HandleTypeDef htim1;
 TIM_HandleTypeDef htim2;
 TIM_HandleTypeDef htim3;



       /* Private function prototypes -----------------------------------------------*/
     void SystemClock_Config(void);
     static void MX_GPIO_Init(void);
     static void MX_TIM1_Init(void);
     static void MX_TIM2_Init(void);
    static void MX_TIM3_Init(void);
    static void MX_NVIC_Init(void);

    void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);




    int main(void)
   {


   HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
    SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_TIM1_Init();
    MX_TIM2_Init();
    MX_TIM3_Init();

    /* Initialize interrupts */

     MX_NVIC_Init();

     /* USER CODE BEGIN 2 */


     HAL_TIM_Base_Start_IT(&htim1);
     HAL_TIM_Base_Start_IT(&htim2);
     HAL_TIM_Base_Start_IT(&htim3);












/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

}

/ ** Configuración del reloj del sistema * /     void SystemClock_Config (void)    {

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Initializes the CPU, AHB and APB busses clocks 
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Initializes the CPU, AHB and APB busses clocks 
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                          |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure the Systick interrupt time 
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick 
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/** NVIC Configuration

/     estático void MX_NVIC_Init (void)    {      / configuración de interrupción RCC_IRQn /     HAL_NVIC_SetPriority (RCC_IRQn, 0, 0);     HAL_NVIC_EnableIRQ (RCC_IRQn);    / TIM1_UP_IRQn interrumpir la configuración /     HAL_NVIC_SetPriority (TIM1_UP_IRQn, 0, 0);     HAL_NVIC_EnableIRQ (TIM1_UP_IRQn);    / TIM2_IRQn interrumpir la configuración /      HAL_NVIC_SetPriority (TIM2_IRQn, 0, 0);     HAL_NVIC_EnableIRQ (TIM2_IRQn);     / TIM3_IRQn interrumpir la configuración * /     HAL_NVIC_SetPriority (TIM3_IRQn, 0, 0);     HAL_NVIC_EnableIRQ (TIM3_IRQn);     }

 /* TIM1 init function */
  static void MX_TIM1_Init(void)
  {

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

   htim1.Instance = TIM1;
   htim1.Init.Prescaler = 1;
   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
   htim1.Init.Period = 29;
   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim1.Init.RepetitionCounter = 0;
   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
   {
    _Error_Handler(__FILE__, __LINE__);
    }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
 _Error_Handler(__FILE__, __LINE__);
 }

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

/* TIM2 init function */
static void MX_TIM2_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

htim2.Instance = TIM2;
htim2.Init.Prescaler = 47999;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 99;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
  _Error_Handler(__FILE__, __LINE__);
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
  _Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
  _Error_Handler(__FILE__, __LINE__);
}

}

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

 TIM_ClockConfigTypeDef sClockSourceConfig;
 TIM_MasterConfigTypeDef sMasterConfig;
 TIM_OC_InitTypeDef sConfigOC;

 htim3.Instance = TIM3;
 htim3.Init.Prescaler = 24000;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 169;
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
 {
  _Error_Handler(__FILE__, __LINE__);
 }

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
 {
  _Error_Handler(__FILE__, __LINE__);
 }

 if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
 {
  _Error_Handler(__FILE__, __LINE__);
 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
  _Error_Handler(__FILE__, __LINE__);
 }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
  _Error_Handler(__FILE__, __LINE__);
 }

 HAL_TIM_MspPostInit(&htim3);














   }

/ ** Configurar pines como         * Analógico         * Entrada         * Salida         * EVENT_OUT         * EXTI * /       void estático MX_GPIO_Init (void)      {

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(GPIOA, LED_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_RESET);

   /*Configure GPIO pins : LED_Pin LED2_Pin LED3_Pin */
  GPIO_InitStruct.Pin = LED_Pin|LED2_Pin|LED3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   }

/ * EL CÓDIGO DE USUARIO COMIENCE 4 * /

   void toggle()
  {

       HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);

  }

 void toggle_2()
  {

    HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_2);

    }

    void toggle_3()
    {

    HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);

    }
    
pregunta Rhony

0 respuestas

Lea otras preguntas en las etiquetas