-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.cpp
122 lines (114 loc) · 3.82 KB
/
uart.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/************************************************
* "uart.c": *
* Implementation file for Mega2560 UART driver. *
* Using UART 0. *
* Henning Hargaard, 8/4 2019 *
* Redigeret til brug i PRJ1: Rasmus *
*************************************************/
#include <avr/io.h>
#include <stdlib.h>
#include "uart.h"
// Target CPU frequency
#define XTAL 16000000
/*************************************************************************
USART initialization.
Asynchronous mode.
RX and TX enabled.
No interrupts enabled.
Number of Stop Bits = 1.
No Parity.
Baud rate = Parameter.
Data bits = Parameter.
Parameters:
BaudRate:Wanted Baud Rate (300-115200).
Databit: Wanted number of Data Bits (5-8).
Parity: 'E' (Even parity), 'O' (Odd parity), otherwise No Parity.
*************************************************************************/
void InitUART(unsigned long BaudRate, unsigned char DataBit, unsigned char Rx_int)
{
if ((BaudRate >= 300) && (BaudRate <= 115200) && (DataBit >=5) && (DataBit <= 8))
{
if(Rx_int){
UCSR0B &= (1<<7);
}
// "Normal" clock, no multiprocessor mode (= default)
UCSR0A = 0b00100000;
// No interrupts enabled
// Receiver enabled
// Transmitter enabled
// No 9 bit operation
UCSR0B = 0b00011000;
// Asynchronous operation, 1 stop bit
// Bit 2 and bit 1 controls the number of data bits
UCSR0C = (DataBit-5)<<1;
// Set Baud Rate according to the parameter BaudRate:
// Adding (8*Baudrate) ensures correct rounding (up/down)
UBRR0 = (XTAL+(8*BaudRate))/(16*BaudRate) - 1;
}
}
/*************************************************************************
Returns 0 (FALSE), if the UART has NOT received a new character.
Returns value <> 0 (TRUE), if the UART HAS received a new character.
*************************************************************************/
unsigned char CharReady()
{
return UCSR0A & (1<<7);
}
/*************************************************************************
Awaits new character received.
Then this character is returned.
*************************************************************************/
char ReadChar()
{
// Wait for new character received
while ( (UCSR0A & (1<<7)) == 0 )
{}
// Then return it
return UDR0;
}
/*************************************************************************
Awaits transmitter register ready.
Then send the character.
Parameter :
Tegn : Character for sending.
*************************************************************************/
void SendChar(char Tegn)
{
// Wait for transmitter register empty (ready for new character)
while ( (UCSR0A & (1<<5)) == 0 )
{}
// Then send the character
UDR0 = Tegn;
}
/*************************************************************************
Sends 0 terminated string.
Parameter:
Streng: Pointer to the string.
*************************************************************************/
void SendString(char* Streng)
{
// Repeat until zero-termination
while (*Streng != 0)
{
// Send the character pointed to by "Streng"
SendChar(*Streng);
// Advance the pointer one step
Streng++;
}
}
/*************************************************************************
Converts the integer "Tal" to an ASCII string - and then sends this string
using the USART.
Makes use of the C standard library <stdlib.h>.
Parameter:
Tal: The integer to be converted and sent.
*************************************************************************/
void SendInteger(int Tal)
{
char array[7];
// Convert the integer to an ASCII string (array), radix = 10
itoa(Tal, array, 10);
// - then send the string
SendString(array);
}
/************************************************************************/