如何让stm8s003f3在timer1上做38khz频率?

发布于 2025-01-09 15:56:42 字数 1298 浏览 0 评论 0原文

我想做一些 rc5 发射器项目。

我已经有一个带有stm8s003的电路来发送数据和ne555来产生38khz频率。将它们连接在一起并发送特定数据后,我的 rc5 发射器工作正常,但没有任何相关消息。

但我想删除 ne555 并仅使用 stm8s003 来完成这两项工作。

我只需要一个代码让TIMER_1发送38khz频率。

这里有一个示例代码,但我不知道。

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++);
          }
                    
     };
}

这段代码似乎可以使 LED 顺利改变亮度

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2025-01-16 15:56:42

使用 TIM1_TimeBaseInit 将预分频值和周期减 1。使用 TIM1_OC1Init 将重复计数设置为 0,将占空比设置为 50%。这是我在 STM8AF/24MHz 中的 1kHz PWM 示例。

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文