microcontrollerfrequencystm8

how to make stm8s003f3 do 38khz frequency on timer1?


i want to do some rc5 transmitter project.

i already have a circuit with a stm8s003 to send data and a ne555 to make 38khz frequency. with connect them together and send the specific data my rc5 transmitter works fine and its no word about it.

but i want to remove the ne555 and do both work with only use stm8s003.

i only need a code to make TIMER_1 send 38khz frequency.

here a example code but i have no idea about it.

void TIM1_setup(void)
{
     TIM1_DeInit();
                
     TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 1000, 1);
                
     TIM1_OC1Init(TIM1_OCMODE_PWM1, 
                  TIM1_OUTPUTSTATE_ENABLE, 
                  TIM1_OUTPUTNSTATE_ENABLE, 
                  1000, 
                  TIM1_OCPOLARITY_LOW, 
                  TIM1_OCNPOLARITY_LOW, 
                  TIM1_OCIDLESTATE_RESET, 
                  TIM1_OCNIDLESTATE_RESET);
                
    TIM1_CtrlPWMOutputs(ENABLE);
    TIM1_Cmd(ENABLE);
}

void main(void)
{
     int j;
         signed int i = 0;
                
     clock_setup();
     GPIO_setup();
     TIM1_setup();
                
     while(TRUE)
     {
             
          for(i = 0; i < 1000; i += 1)
          {
              TIM1_SetCompare1(i);
              for(j=1;j<0x5FFF;j++);
          }
          for(i = 1000; i > 0; i -= 1)
          {
              TIM1_SetCompare1(i);
              for(j=1;j<0x5FFF;j++);
          }
                    
     };
}

it seems to this code make a led change brightness smoothly


Solution

  • Subtract 1 from the prescale and the period with TIM1_TimeBaseInit. Set the repetition count to 0 and the duty cycle to 50% with TIM1_OC1Init. Here is my 1kHz PWM example in STM8AF/24MHz.

    void TIM1_initialize_with_PWM(void)
    {
      u16 tim1_prescaler = 24 - 1; // 24MHz/24 = 1MHz. See 17.7.17 in ST RM0016
      u16 tim1_period = 1000 - 1;  // 1MHz/1000 = 1kHz.
      u8 repetition = 0;           // 0 is forever
    
      TIM1_DeInit();
      TIM1_TimeBaseInit(tim1_prescaler, TIM1_COUNTERMODE_UP, tim1_period, repetition);
    
      GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_SLOW);
        
      TIM1_OC1Init(TIM1_OCMODE_PWM1,
                   TIM1_OUTPUTSTATE_ENABLE,TIM1_OUTPUTNSTATE_DISABLE,
                   ((tim1_period+1)/2)-1, /* 50% duty cycle, 1 kHz */
                   TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH,
                   TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
    
      TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
      TIM1_Cmd(ENABLE);
      TIM1_CtrlPWMOutputs(ENABLE);
    }
    
    void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void) interrupt 11
    {
      my_isr();
      TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
    }