cancel
Showing results for 
Search instead for 
Did you mean: 

Why is this code not being able to change the default SPI value of 0 to 1, and also the bit shift on timer is not working

AHald
Associate

#define SPI2_CR2 *(__IO uint32 *)0x40003800

#define timer4_CR2 *(__IO uint32 *)0x40000800

void setup() 

{

 Serial.begin(115200);

}

void loop() 

{

 Serial.println("SPI");

 Serial.println(SPI2_CR2,BIN);

 SPI2_CR2=SPI2_CR2|(1<<0);

 Serial.println(SPI2_CR2,BIN);

 Serial.println("Timer");

 Serial.println(timer4_CR2,BIN);

 timer4_CR2 = timer4_CR2 | (1<<1);

 Serial.println(timer4_CR2,BIN);

 delay(1000);

}

I assumed that all registers initially has 0 as its value, so I tried to get the default ,i.e, 0 first from the Serial Monitor, then update that to 1, then get the updated value as 1 on the Serial Monitor. Same for the timer4, I first wanted to display the default value for the timer 4, then left-shift 1 bit, then display that. Can anyone please give me the rectified code? Just one constraint, not using any library functions.

3 REPLIES 3

CR2 offset in SPI is 0x04, so what you change is CR1. You change CPHA as that's the 0th bit. I don't exactly know what do you mean by "default SPI value", but if you mean the value of SPI_CR1.CPHA and it's not changing, then it's maybe because you did not enable the SPI clock in RCC.

JW

AHald
Associate

I assumed that all registers initially has 0 as its value, so I tried to get the default ,i.e, 0 first from the Serial Monitor, then update that to 1, then get the updated value as 1 on the Serial Monitor. Same for the timer4, I first wanted to display the default value for the timer 4, then left-shift 1 bit, then display that. Can anyone please give me the rectified code? Just one constraint, not using any library functions.

Do you have the SPI and TIM clocks enabled in RCC?

JW