cancel
Showing results for 
Search instead for 
Did you mean: 

Read/Write on a register

Vertinhol
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III
  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.

View solution in original post

4 REPLIES 4
KnarfB
Principal III
  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.

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?

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.

CMSIS has a macros for this:

int inmsel = _FLD2VAL(COMP_CSR_INMSEL, COMP1->CSR);