cancel
Showing results for 
Search instead for 
Did you mean: 

writing serial data out for a nucleo f031K6

deep_rune
Associate III

I recently bought a nucleo board for a project im making. I have in the past used 8 bit AVR chips but I want to step up to STM32. My project is some switches, leds, a DAC and an ADC. One thing that is common to all these elements is a serial data out. The way I would usually do this is to use a number (let's say 8 bit) to hold a 'walking bit' to count through each bit in a byte, on each count ANDs with the data (also 8 bit) and the output is then sent to the output pin. I am used to programming in C in AVR GCC so I would write something like this -

for(i=0;i<8;i++){ // loop 8 times
	
	COUNT <<= 1;  // left shift walking bit
 
		if (COUNT & DATA) {  // compare data and walking bit
			PORTA |= 1<<0;  //set output if data is high
			} else {
			PPORTA &= ~(1<<0); // clear output if data is low
		}
		}

How do I achieve the same thing with my nucleo? I am most used to using bare C, but I don't know how to code this for STM. I am happy to use CMSIS or HAL, whatever is closest to what I know already.

any help much appreciated

2 REPLIES 2

Enable the GPIOA clock, configure the GPIO pin as a Push-Pull Output

for(i=0;i<8;i++){ // loop 8 times

 COUNT <<= 1; // left shift walking bit

 if (COUNT & DATA) { // compare data and walking bit

  GPIOA->ODR |= 1<<0; //set output if data is high (PA0=1)

} else {

  GPIOA->ODR &= ~(1<<0); // clear output if data is low

 }

}

More efficient would be to use the Set/Reset register

for(i=0;i<8;i++){ // loop 8 times

 COUNT <<= 1; // left shift walking bit

  if (COUNT & DATA) { // compare data and walking bit

  GPIOA->BSRR = 1<<0; //set output if data is high (PA0=1)

} else {

  GPIOA->BSRR = 1<<(0 + 16); // clear output if data is low (PA0=0)

 }

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
deep_rune
Associate III

thanks! that looks a lot closer to what I know already than I expected.

when you use BSRR for the output, you're not using HAL, does that mean you're using CMSIS? I'm trying to understand the difference between HAL, CMSIS etc.