Skip to main content
Vertinhol
Associate III
Sep 30, 2020 7:49 PM UTC
Solved

Read/Write on a register

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.
KnarfB
KnarfBBest answer
Super User
Sep 30, 2020 8:02 PM UTC
 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
Sep 30, 2020 8:02 PM UTC
 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
Sep 30, 2020 8:12 PM UTC

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
Sep 30, 2020 8:21 PM UTC

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.