Author Topic: ch32v003fun / can not get timer2 to work  (Read 1464 times)

0 Members and 1 Guest are viewing this topic.

Offline SaschkoTopic starter

  • Newbie
  • Posts: 2
  • Country: de
ch32v003fun / can not get timer2 to work
« on: December 07, 2025, 10:59:38 pm »
Hallo,
I am playing around with ch32v003fun from cnlohr, and like it a lot :-)

I have started to play with timerinterrupts, and use the following code to get a timer1 interrupt, which is working well.

 void init_timer1() {
  RCC->APB2PCENR |= RCC_APB2Periph_TIM1;
  TIM1->CTLR1 |= TIM_CounterMode_Up | TIM_CKD_DIV1;
  TIM1->CTLR2 = TIM_MMS_1;
  TIM1->ATRLR = 3000-1;
  TIM1->PSC = 0;
  TIM1->RPTCR = 0;
  TIM1->SWEVGR = TIM_PSCReloadMode_Immediate;

  NVIC_EnableIRQ(TIM1_UP_IRQn);
  TIM1->INTFR = ~TIM_FLAG_Update;
  TIM1->DMAINTENR |= TIM_IT_Update;
  TIM1->CTLR1 |= TIM_CEN;
}

void TIM1_UP_IRQHandler(void) __attribute__((interrupt));
void TIM1_UP_IRQHandler() {
  if(TIM1->INTFR & TIM_FLAG_Update) {
    TIM1->INTFR = ~TIM_FLAG_Update;
    // Toggle a pin here
  }
}

But when I change to the following, it does not work at all, and I have no more ideas  |O. Any help whould be preciated.

void init_timer2() {
  RCC->APB2PCENR |= RCC_APB1Periph_TIM2;
  TIM2->CTLR1 |= TIM_CounterMode_Up | TIM_CKD_DIV1;
  TIM2->CTLR2 = TIM_MMS_1;
  TIM2->ATRLR = 3000-1;
  TIM2->PSC = 0;
  TIM2->RPTCR = 0;
  TIM2->SWEVGR = TIM_PSCReloadMode_Immediate;

  NVIC_EnableIRQ(TIM2_IRQn);
  TIM2->INTFR = ~TIM_FLAG_Update;
  TIM2->DMAINTENR |= TIM_IT_Update;
  TIM2->CTLR1 |= TIM_CEN;
}

void TIM2_IRQHandler(void) __attribute__((interrupt));
void TIM2_IRQHandler(){
  if(TIM2->INTFR & TIM_FLAG_Update) {
    TIM2->INTFR = ~TIM_FLAG_Update;
    // Toggle a pin here
  }
}

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17826
  • Country: fr
Re: ch32v003fun / can not get timer2 to work
« Reply #1 on: December 07, 2025, 11:05:09 pm »
The culprit may be here:
Code: [Select]
RCC->APB2PCENR |= RCC_APB1Periph_TIM2;
Refer to the reference manual to make sure on which APB TIM2 is, but if the RCC_APB1Periph_TIM2 constant is correct, then it suggests it is on APB1 and not APB2.
 

Online HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1933
  • Country: gb
Re: ch32v003fun / can not get timer2 to work
« Reply #2 on: December 08, 2025, 03:15:34 am »
Refer to the reference manual to make sure on which APB TIM2 is, but if the RCC_APB1Periph_TIM2 constant is correct, then it suggests it is on APB1 and not APB2.

Yes, that is correct. TIM2 is enabled in APB1PCENR, not APB2PCENR.
 

Offline SaschkoTopic starter

  • Newbie
  • Posts: 2
  • Country: de
Re: ch32v003fun / can not get timer2 to work
« Reply #3 on: December 08, 2025, 06:48:01 am »
Yes that was the Problem.

Thank you so much  :-+
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->