1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include <avr\io.h>
|
4 | #include <avr/interrupt.h> //for the interupts for receiving via UART
|
5 | #include <string.h> //for building strings of characters
|
6 | #include <avr/pgmspace.h> //for PSTR for handling the location of characters
|
7 |
|
8 | //Set the baud rate of the communication
|
9 |
|
10 | #define BAUD 9600UL // Baudrate
|
11 |
|
12 | // Berechnungen
|
13 | #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden
|
14 | #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate
|
15 | #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
|
16 |
|
17 | #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
|
18 | #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
|
19 | #endif
|
20 |
|
21 |
|
22 | //Four variables for buffering of received messages
|
23 | volatile char c; //The temp place to dump the character we just received
|
24 | volatile char buffer[30]; // The array holding the characters that have been sent
|
25 | volatile int bufferlocation; //The current location in the buffer
|
26 | volatile int bufferflag; //A flag to tell whether there is complete data in the buffer to be processed
|
27 |
|
28 | //Declare the location of the stream for the output
|
29 | void uart_printchar(char c, FILE *stream){
|
30 | // Wait until UDR ready
|
31 | while(!(UCSR0A & (1 << UDRE0)));
|
32 | UDR0 = c; // send character
|
33 | }
|
34 |
|
35 | //USART RX interrupt this code is executed when we recieve a character
|
36 | ISR(USART0_RX_vect){
|
37 | c = UDR0; //Read the value out of the UART buffer
|
38 | buffer[bufferlocation] = c; //Dump that value out into the buffer array
|
39 | bufferlocation++; //Increment the bufferlocation so its ready to write next time
|
40 |
|
41 | if (c == 13) //Number 13 is carrage return
|
42 | bufferflag = 1; //If its a carriage return then mark the string as ready to process
|
43 | }
|
44 |
|
45 | int BaudRateRegister;
|
46 |
|
47 | int main(void){
|
48 | //---------------Start of Initalisation---------------
|
49 |
|
50 | //Configure the UART registers
|
51 | UCSR0B = ((1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0));
|
52 | //RXCIE0 Enable interupt after recieved
|
53 | //RXEN0 Reciever enabled
|
54 | //TXEN0 Transmit enabled
|
55 |
|
56 | UCSR0C = ((1<<UCSZ00)|(1<<UCSZ01));
|
57 | //UCSZ00 and UCSZ01 use 8 bit character size
|
58 |
|
59 | //Set the baud rate
|
60 | BaudRateRegister=F_CPU/16/BAUD-1;
|
61 | UBRR0H=((BaudRateRegister>>8)&0x00FF);
|
62 | UBRR0L=(BaudRateRegister&0x00FF);
|
63 |
|
64 | //Define and initialise stream used for printing to uart
|
65 | FILE uart_str = FDEV_SETUP_STREAM(uart_printchar, NULL, _FDEV_SETUP_RW);
|
66 |
|
67 | //Redirect stdout, so that printf() knows where to go
|
68 | stdout = &uart_str;
|
69 |
|
70 | //Enable interrupts
|
71 | sei();
|
72 | //---------------End of Initalisation---------------
|
73 | printf_P(PSTR("..."));
|
74 | printf_P(PSTR("Running"));
|
75 | printf_P(PSTR("BaudRateRegister set to %u\n"),BaudRateRegister);
|
76 | printf_P(PSTR("Wait"));
|
77 | //Start of infinate while loop
|
78 | while(1){
|
79 | if (bufferflag == 1){
|
80 | printf_P(PSTR("Got instruction"));
|
81 | bufferflag = 0; //Reset the buffer flag to say its been processed
|
82 | bufferlocation =0; //Restart the buffer location
|
83 | }
|
84 | }
|
85 | //End of infinate while loop
|
86 |
|
87 | }
|
88 | //End of main routine
|