Author Topic: [Solved]I can't get the CH32V003 output compare to toggle  (Read 895 times)

0 Members and 1 Guest are viewing this topic.

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 215
  • Country: ca
[Solved]I can't get the CH32V003 output compare to toggle
« on: January 01, 2025, 12:30:34 am »
Hello
I'm playing around with the CH32V003 chip and I have problem setting up the PC0 (TIM2_CHANNEL3) to toggle.
this is my code:
Code: [Select]
#include "ch32v003fun.h"



int main()
{
    SystemInit();
    RCC->APB2PCENR |=RCC_AFIOEN | RCC_IOPCEN; //IO auxiliary function clocks
    GPIOC->CFGLR |=0X09;// PC0 is configured as alternate push-pull

    RCC->APB1PCENR |= RCC_TIM2EN;      // enable timer 2 clock

    TIM2->CTLR1 &= ~TIM_CEN;          //Disable timer counter
    TIM2->CTLR1 |=TIM_ARPE;       //autoload of the ATRLR register enabled
    TIM2->PSC = (uint16_t) 48000 - 1; //Prescaler - 48Mhz / (48000 - 1) = 1kHz
    TIM2->ATRLR = (uint16_t)500;    //Auto reload value
    TIM2->CHCTLR2 |=TIM_OC3M_0 | TIM_OC3M_1  ;//TOGGLE 0C1REF TOGGLES

     TIM2->CCER |=TIM_CC1E;  //   COMPARE CAPTURE CHANNEL IS CONFIGURED AS OUTPUT

     TIM2->CTLR1 |=TIM_CEN;  //ENABLE TIMER 2


    while(1)
    {




    }
    }
Any help is very much appreciated
« Last Edit: January 01, 2025, 01:56:06 pm by kgavionics »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15924
  • Country: fr
Re: I can't get the CH32V003 output compare to toggle
« Reply #1 on: January 01, 2025, 01:19:49 am »
If you use channel 3, shouldn't the CCER register be set like this instead?

TIM2->CCER |=TIM_CC3E;
 
The following users thanked this post: kgavionics

Online HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1617
  • Country: gb
Re: I can't get the CH32V003 output compare to toggle
« Reply #2 on: January 01, 2025, 11:09:08 am »
IIRC, PSC is buffered (and so will be ATRLR if you enable TIM_ARPE) and only takes effect when the next update event occurs. So in your timer initialisation you need to generate one using UG in SWEVGR.

To expand on why failing to do this results in the timer 'not working properly', two things are happening:

- PSC doesn't get the right value loaded, so counter runs too fast on the default prescaling of 1.
- ATRLR doesn't get loaded either, so stays with zero value, effectively disabling the timer.
« Last Edit: January 01, 2025, 11:15:00 am by HwAoRrDk »
 

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 215
  • Country: ca
Re: I can't get the CH32V003 output compare to toggle
« Reply #3 on: January 01, 2025, 01:34:51 pm »
If you use channel 3, shouldn't the CCER register be set like this instead?

TIM2->CCER |=TIM_CC3E;
Your are right SiliconWizard! you spotted the mistake as always.Merci pour votre aide!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf