cancel
Showing results for 
Search instead for 
Did you mean: 

Hello I'm Using STM8S003F3PU mini-eval Board. My software is STVD and Cosmic. Why My TIM1 don't work truly? It should measure the input signal HIGH_Duration on TIM1_CH1

Jhabi
Associate II

Hello

I'm Using STM8S003F3PU mini-eval Board. My software is STVD and Cosmic.

Why My TIM1 don't work truly? It should measure the input signal HIGH_Duration on TIM1_CH1 (= PC6) and my led on PA3 turned off. But that's not so. Furthermore, I use debug mode but it doesn't enter my condition( if(duty_cycle_updated))

Part of my code:

       void TIM1_setup(void){
    TIM1_DeInit();
       TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);
       TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
       TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
       TIM1_ITConfig(TIM1_IT_CC1, ENABLE);   
       TIM1_Cmd(ENABLE);
       enableInterrupts();
}

stm8s_it.c:

void TIM1_CH1_CCP_IRQHandler(void){
       if(TIM1_GetFlagStatus(TIM1_FLAG_CC1))       {
          if(state == FALSE)          {
              start_time = TIM1_GetCapture1();
              TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING,                     TIM1_ICSELECTION_DIRECTTI, 1, 1);
          }
          else {
              end_time = TIM1_GetCapture1();
              TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
              duty_cycle = (end_time - start_time);
							duty_cycle_updated=TRUE;
          }
          state = ~state;
       }
       TIM1_ClearITPendingBit(TIM1_IT_CC1);
       TIM1_ClearFlag(TIM1_FLAG_CC1);
}
 

Full project attached.

1 REPLY 1
Jhabi
Associate II
Main.c:
 
        #include "stm8s.h"
  
    
    //Pulse width measurment
    unsigned char data_value;
    unsigned long duty_cycle = 0;
    unsigned long duty_cycle_temp = 0;
    
    unsigned long start_time = 0;
    unsigned long end_time = 0;
    bool duty_cycle_updated=FALSE;
    bool state=FALSE;
    void clock_setup(void);
    void GPIO_setup(void);
    void TIM1_setup(void);
    void TIM2_setup(void);
    void delay_cycles(uint16_t cy);
    
    void main() {
      //Set PinMode
 
         GPIOA->DDR |= 0xFF;  // PD.0 as Output
         GPIOA->CR1 |= 0xFF;  // PD.0 as Push Pull Type Output
    		 GPIOA->ODR |=1ODR &= ~(1