Skip to main content
Vertinhol
Associate III
September 30, 2020
Solved

Read/Write on a register

  • September 30, 2020
  • 4 replies
  • 5973 views

Hello, I'm having a trouble to read or write bits on registers, for example I would like to change the comparator input minus pin, according to the RM I have to change the INMSEL bits in the COMP1_CSR register, but when I write this line code:

COMP1_CSR = 0xF7854876 // just an example

the IDE tells me that "COMP1_CSR is not declared", and it's the same thing with other registers, I cannot directly manipulate them...

I'm using STM32 Nucleo L476.

This topic has been closed for replies.
Best answer by KnarfB
 COMP1->CSR |= COMP_CSR_INMSEL_0;

or such works. The componentes are pointer to structures, the registers are struct members. This is favorable considering the generated machine code.

4 replies

KnarfB
KnarfBBest answer
Super User
September 30, 2020
 COMP1->CSR |= COMP_CSR_INMSEL_0;

or such works. The componentes are pointer to structures, the registers are struct members. This is favorable considering the generated machine code.

Vertinhol
VertinholAuthor
Associate III
September 30, 2020

Thank you for help, how can I check for example if the pin number 5 is set to 0 or 1 without alternating the other register bits?

KnarfB
Super User
September 30, 2020

You can check for a single bit set by

 if( COMP1->CSR & COMP_CSR_INMSEL_0 ) {
	... 
 }

or extract one or more bits by using the Pos and Msk

int inmsel = (COMP1->CSR & COMP_CSR_INMSEL_Msk) >> COMP_CSR_INMSEL_Pos; // yields 0..7

In STM32CubeIDE press F3 (open declaration) to see the header file with all of its defines.