cancel
Showing results for 
Search instead for 
Did you mean: 

STM8L151G3 TIM4 not working

zin10
Associate II

MCU: STM8L151G3U6
ISSUE: TIM4 interrupt doesnt seem working 
Connections:
                 1. LED on pin PB0
                 2. Push button/ switch on pin PC5

Scenario:
1. User press the push button

2. Toggles the LED

Observation:
Without using the TIM4 interrupt, the code works perfectly fine, when the TIM4 is used, the excutions reaches the first wait_50ms(); (red colour) and never go beyond this line, I would appreciate if any one would address the issue. BTW: I have used IRQHandler, 23  also but same result.

/===== Header files ======
#include "stm8l15x.h"
 
// Software timing counters
volatile uint16_t tick_1ms = 0;
volatile uint8_t flag_50ms = 0;
volatile uint8_t flag_100ms = 0;
volatile uint8_t flag_1min = 0;
volatile uint16_t soft_50ms = 0;
volatile uint16_t soft_100ms = 0;
volatile uint16_t soft_1min = 0;
 
//==== System Clock Configuration =====
void CLK_Config(void)
{
    CLK_DeInit(); // Reset the clock to its default setting
    CLK_HSICmd(ENABLE);// Enable the internal clock speec osscilator ( HSI = 16 MHz)
    while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET);// Wait until HSI is ready and stable
    CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);// Set HSI as a system clock source
    CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); // Set system clock prescaler: devide by 1(full 16 MHz)
}
 
void TIM4_Config(void)
{
    TIM4_DeInit();
    TIM4_TimeBaseInit(TIM4_Prescaler_128, 125); // 16MHz / 128 = 125kHz; 125 -> 1ms
    TIM4_ClearFlag(TIM4_FLAG_Update);
    TIM4_ITConfig(TIM4_IT_Update, ENABLE);
    TIM4_Cmd(ENABLE);
}
 
INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler, 25)
{
    TIM4_ClearITPendingBit(TIM4_IT_Update);
 
    tick_1ms++;
 
    if (++soft_50ms >= 50) {
        soft_50ms = 0;
        flag_50ms = 1;
    }
 
    if (++soft_100ms >= 100) {
        soft_100ms = 0;
        flag_100ms = 1;
    }
 
    if (++soft_1min >= 60000) {
        soft_1min = 0;
        flag_1min = 1;
    }
//TIM4_ClearITPendingBit(TIM4_IT_Update);
}
 
// Utility: wait for 50ms debounce
void wait_50ms(void)
{
    flag_50ms = 0;
    while (!flag_50ms);
}
 
void wait_100ms(void)
{
    flag_100ms = 0;
    while (!flag_100ms);
}
 
 
//==== Main Function =====
int main()
{
     uint8_t system_on = 0;          // 0 = off, 1 = on
    
 
    GPIO_Init(GPIOC, GPIO_Pin_5, GPIO_Mode_In_PU_No_IT); // Initialize Buttons Input
    GPIO_Init(GPIOB, GPIO_Pin_0, GPIO_Mode_Out_PP_Low_Fast); // Initialize LEDS
 
    CLK_Config();        // System clock
    TIM4_Config();
    enableInterrupts();
 
 
 
    while(1)
    {
       
        if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == RESET)
        {
            wait_50ms();
            if(system_on == 0)
            {
                // Turn on system
                 system_on = 1;
 
                 GPIO_SetBits(GPIOB, GPIO_Pin_0);
 
                 // Blink twice
                  GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
                  wait_100ms();
                  GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
                  wait_100ms();
              }
             else
             {
                   // Turn off system
                  system_on = 0;
                 GPIO_ResetBits(GPIOB, GPIO_Pin_0);
              }
 
              while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == RESET)
              wait_50ms();
        }
   }
}
0 REPLIES 0