-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.c
48 lines (42 loc) · 866 Bytes
/
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
#define BAUD 9600L
#define UBRR_value F_CPU / (BAUD * 16) - 1
#include <avr/io.h>
void UART_setup()
{
// uart setup
UCSR0B |= (1 << TXEN0);
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00);
UBRR0 = UBRR_value;
}
void UART_send_byte(uint8_t u8Data)
{
// Wait until last byte has been transmitted
while ((UCSR0A & (1 << UDRE0)) == 0)
;
// Transmit data
UDR0 = u8Data;
}
char digit_to_ASCII(char d)
{
if (0 <= d && d <= 9)
{
return d + '0';
}
if (0xA <= d && d <= 0xF)
{
return d - 10 + 'A';
}
return 0;
}
void byto_to_hex_ASCII(uint8_t byte, char *result)
{
result[1] = digit_to_ASCII(byte / 16);
result[0] = digit_to_ASCII(byte % 16);
}
void UART_send_int(uint8_t tmp)
{
char out[2];
byto_to_hex_ASCII(tmp, out);
UART_send_byte(out[1]);
UART_send_byte(out[0]);
}