-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
89 lines (74 loc) · 1.78 KB
/
uart.c
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
/*
************************************************************
* PiMash -
* Communication API over UART
************************************************************
*/
#include <stdio.h>
#include <avr/io.h>
#include "uart.h"
// *************************************************************
void uart_115200 () {
#define BAUD 115200
#include <util/setbaud.h>
// set baud rate
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
// enable transmit and receive
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
// set frame format to 8 data bits 2 stop bits
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
// *************************************************************
unsigned char rx_byteNB(void) {
// wait for data to be received
if(UCSR0A & (1 << RXC0))
return UDR0;
else
return '\0';
}
// *************************************************************
unsigned char rx_byte(void) {
// wait for data to be received
while( !(UCSR0A & (1 << RXC0)))
{}
return '\0';
}
// *************************************************************
void tx_byte(unsigned char data) {
// wait for empty transmit register
while( !(UCSR0A & (1 << UDRE0)) )
{}
// put the data into output buffer
UDR0 = data;
}
// *************************************************************
void tx_inttext(unsigned int num) {
int i ;
char string[7];
if(num !=0) {
for(i = 4;i >=0; i--) {
string[i] = num % 10 + '0';
num /= 10;
}
// send character string
for(i=0;i<=4;i++)
tx_byte(string[i]);
tx_byte('\r');
tx_byte('\n');
}
}
// *************************************************************
void tx_text(char* string) {
while (0 != *string) {
tx_byte (*string);
++string;
}
tx_byte('\r');
tx_byte('\n');
}