I have a problem with STM32 register in Arduino IDE

I have to work with STM32f103C8T6 (Black pills) and I want to setting a time period using interrupt. I searching setting method on the internet and found some code below.So,he access the register by write like this TIMER2_BASE->CR1 = 0x01 but i have a question why they write TIMER_BASE ->CR1 not this TIM2_CR1 |= 0x01 .Is there anyway to access the register.Thank you. :slight_smile:

int t = 0;
void setup() {
  //timer 2 setup
  //TIMER2_BASE->CR1 = 0x0000; //Tdts=Tint, upcounter, timer disabled
  //TIMER2_BASE->CR2 = 0x0000; //already set to 0 by default
  //TIMER2_BASE->CR1 = 0x00000084;

  
  TIMER2_BASE->CR1 |= 0x01;
  TIMER2_BASE->PSC = 72; //72MHz/72=1,000,000 Hz 
  //f = 1,000,000 , T = 0.000001
  TIMER2_BASE->ARR = 1000; //20000/10000=2s ,1000 * 0.000001 = 1mS
  TIMER2_BASE->CNT = 0;  //clear counter
  timer_attach_interrupt(TIMER2, 0, handler_tim2); //interrupt on timer update
  TIMER2_BASE->CR1 |= 0x0001; //enable timer.
  
  
  //GPIO setup
  GPIOB_BASE->CRH = 0x44424444; //enable PB12 as output
  //0100 0100 0100 0010 0100 0100 0100 0100
  GPIOB_BASE->BSRR = 0x10000000; //reset PB12 (low)
  //0001 0000 0000 0000 0000 0000 0000 0000
}

void loop() {
  if (t>=10){
    
    GPIOB_BASE->ODR ^= 0x1000; //toggle PB12
    t = 0;
  }
}
//timer 2 interrupt

void handler_tim2(void){
  t = t + 1;
  
  //0001 0000 0000 0000 
}

I have a problem with STM32 encoder interface mode .how can i config input pin from encoder to timer and
show counter value from register Thank you.

//Timer4 as encoder interface mode
  TIMER4_BASE->ARR = 0xFFFF;//value to count up

  TIMER4_BASE->CCMR1 |=0x0001;
  TIMER4_BASE->CCER &=0x0022;
  TIMER4_BASE->SMCR |= 0x0003;
  TIMER4_BASE->CR1 |= 0x0001;

There is a forum especially for stm32 and arduino at https://www.stm32duino.com/index.php
You will probably get more help there.

It is because the timer registers are organized as structs, to avoid repetition where there are multiple timers with the same register types. What do you mean, "is there any way to access the register"? Does the example not do this?

duplicate post

Duplicate topics merged

Multiple posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated multiple posting will result in a suspension from the forum.

Thanks in advance for your cooperation.