AVR SPI without the ASF files

Go To Last Post
54 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I mean I worked with Arduino DUE. Not really a different source code from that I wrote above. I just had to change them accotding to the Arduino IDE standard. It would have been better,if printf was easily accesible in Atmel Studio 7

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pika wrote:
It would have been better,if printf was easily accesible in Atmel Studio 7
Err it is:

 

http://www.nongnu.org/avr-libc/u...

 

So a minimal AVR program to use something like printf() might be:

#define F_CPU 16000000UL
#define BAUD 9600
#include <stdio.h>
#include <avr/io.h>
#include <util/setbaud.h> // uses BAUD and F_CPU as input, outputs UBRR_VALUE

void uart_init(void) {
	UBRR = UBRR_VALUE;
	UCSRB = (1 << TXEN);
}

int	uart_putchar(char c, FILE *stream) {
	loop_until_bit_is_set(UCSRA, UDRE);
	UDR = c;

	return 0;
}

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

int main(void) {
        uart_init();
	stdout =  &uart_str;

	while(1) {
		printf("Hello world\n");
	}
}

The only "trick" in any of that is the FDEV_SETUP_STREAM and the connection to stdout. But as soon as you have made a connection between stdout and a uart_putchar() routine then all the usual stdio.h output routines are connected and ready to use.

 

The reason it's done like this is that the output could just as easily have been an LCD with LCD_init() and an LCD_putchar() instead. It's up to you what output device you'd like to use.

 

The same technology also allows for a uart_getchar() and then connecting that to stdin after which all the things like scanf() could be used too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oops. I wanted to print the 'result ' from my post #47 . Because, I could not get to understand if I have receieved the value that is supposed to be read. The ASF and drivers aree too confusing for such a simple operation, so I tried to do SPI on my own.

Last Edited: Thu. Aug 17, 2017 - 02:11 PM

Pages