UART-LF.c 2.47 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
/*
Petter Breedveld's avatar
Petter Breedveld committed
2
 * UART-LF.c
Petter Breedveld's avatar
Petter Breedveld committed
3
4
5
6
7
 *
 * Created: 29.01.2017 23:43:39
 * Author : Petter
 */ 

BuildTools's avatar
BuildTools committed
8
9
#define F_CPU 3333333
#define BAUD_9600 4*F_CPU/9600
Petter Breedveld's avatar
Petter Breedveld committed
10
11
12
13
14

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

spookysys's avatar
spookysys committed
15
16
17
/*
In this exercise we will set up and use UART communication.
The embedded debugger has a virtual com port that we will use to communicate with the computer.
BuildTools's avatar
BuildTools committed
18
*/ 
Petter Breedveld's avatar
Petter Breedveld committed
19

BuildTools's avatar
BuildTools committed
20
21
#define TX_PORT		PORTB
#define TX_PIN		0
22
23
24


void uart_init(unsigned long baud){
Petter Breedveld's avatar
Petter Breedveld committed
25
	
BuildTools's avatar
BuildTools committed
26
	//From chapter 22.3.1 in datasheet
spookysys's avatar
spookysys committed
27
	
BuildTools's avatar
BuildTools committed
28
29
	TX_PORT.OUTSET = (1 << TX_PIN); 	//Setting up TX pin as output
	TX_PORT.DIRSET = (1 << TX_PIN); 	//Setting up TX pin as output
spookysys's avatar
spookysys committed
30
	
BuildTools's avatar
BuildTools committed
31
32
33
	//Set baud rate register
	USART3.BAUDL = (uint8_t) baud;			//Set baud rate without shifting to get the 8 low bits
	USART3.BAUDH = (uint8_t)(baud >> 8);	//Shift register right by 8 bits to get the 8 high bits
spookysys's avatar
spookysys committed
34
35
	
	
36
	//USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size
BuildTools's avatar
BuildTools committed
37
38
	//Since all bits are default 0 we only need to change the character size part
	USART3.CTRLC = (0x3 << USART_CHSIZE0_bp);
spookysys's avatar
spookysys committed
39
	
BuildTools's avatar
BuildTools committed
40
41
	//Enable RX and TX
	USART3.CTRLB = (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp);
42

BuildTools's avatar
BuildTools committed
43
44
	//Enable interrupts on incoming data
	USART3.CTRLA |= (1 << USART_RXCIE_bp); 
Petter Breedveld's avatar
Petter Breedveld committed
45
46
47
}

// function to transmit data
BuildTools's avatar
BuildTools committed
48
void uart_transmit(char data){
spookysys's avatar
spookysys committed
49
50
51
52
53
	
	//In this function we will be send data.
	
	//First we should check that there isn't already data being sent
	//	if there is, we should probably wait for it to finish first
BuildTools's avatar
BuildTools committed
54
	while (!(USART3.STATUS & (1 << USART_DREIF_bp))){
55
56
		//wait for previous transmit to finish
	};
spookysys's avatar
spookysys committed
57
	
BuildTools's avatar
BuildTools committed
58
59
60
61
62
63
64
65
66
67
68
	//Put our new data into tx data register
	USART3.TXDATAL = data;
}


//To send a string we can do this
void uart_transmit_string(char* data) {
	while (*data != '\0') {
		uart_transmit(*data);
		data++;
	}
Petter Breedveld's avatar
Petter Breedveld committed
69
70
71
72
73
}


int main(void)
{
spookysys's avatar
spookysys committed
74
75
76
	//Initialize the UART with our function. 
	//We will be using a baudrate of 9600 (defined as BAUD_9600 at the top of the file)
	uart_init(BAUD_9600);
Petter Breedveld's avatar
Petter Breedveld committed
77
	
spookysys's avatar
spookysys committed
78
	sei(); //Enable inerrupt, important for anything here to work
Petter Breedveld's avatar
Petter Breedveld committed
79
80
81
	
    while (1) 
    {
spookysys's avatar
spookysys committed
82
83
		//We don't really need to do anything here.
		//the ISR will handle receiving. 
BuildTools's avatar
BuildTools committed
84
85
		uart_transmit_string("Dette er en test\n");
		_delay_ms(200);
Petter Breedveld's avatar
Petter Breedveld committed
86
87
    }
}
Petter Breedveld's avatar
Petter Breedveld committed
88
89
90


//Interrupt service routine for the receiver.
BuildTools's avatar
BuildTools committed
91
92
ISR (USART3_RXC_vect) {
	//In the interrupt we will read the data in the receive buffer
Petter Breedveld's avatar
Petter Breedveld committed
93
	
BuildTools's avatar
BuildTools committed
94
95
96
97
98
99
100
	//First we should check that new data has arrived the receive buffer
	while (!(USART3.STATUS & (1 << USART_RXCIF_bp))){
		//wait for previous transmit to finish
	};
	
	//Store the data in a temporarily variable
	uint8_t tmp = USART3.RXDATAL; 
Petter Breedveld's avatar
Petter Breedveld committed
101
}