-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart.c
executable file
·121 lines (88 loc) · 2.69 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
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
//Crystal frequency
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "uart.h"
char Read_UART = 0;
/*****************************************************************
** Name:void init_uart(void)
** Function:atmega328p UART initialization
** Entrance parameters: None
** Outlet parameters: None
** Instructions for use: send & read, interrupt reception, 8 data bits, 1 stop bit, 57600 baud
*****************************************************************/
void init_uart(void)
{
UCSRB = 0x00;
UCSRA = 0x00;
UCSRC = 3 << UCSZ0;
//Configure the baud rate
UBRRH = (F_CPU / BAUD / 16 - 1) / 256;
UBRRL = (F_CPU / BAUD / 16 - 1) % 256;
//Enabled receive interrupt, receiver, transmit
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);
DDRD |= 0x02;
}
/*****************************************************************
** Name:void send_Char(char data)
** Function:atmega328p UART send character
** Entrance parameters:the character to be sent
** Outlet parameters:None
** Instructions for use:Send single character
*****************************************************************/
void send_char(char data)
{
while(!(UCSRA & _BV(UDRE)));
UDR = data;
}
/************************************************************
** Name:void send_string(char *str)
** Function:atmega328p UART send string
** Entrance parameters:the point of string to be sent
** Outlet parameters:None
** Instructions for use:Send string
************************************************************/
void send_string(char *str)
{
while(*str)
{
send_char(*str++);
}
}
/************************************************************
** Name:void uart_printf(char *str)
** Function:atmega328p UART send string with parameters
** Entrance parameters:the point of string to be sent
** Outlet parameters:None
** Instructions for use:Send string
************************************************************/
void uart_printf(char *fmt,...)
{
va_list ap;
unsigned char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
send_string(string);
va_end(ap);
}
/*****************************************************************
** Name:void read_char(char data)
** Function:atmega328p UART read character
** Entrance parameters:None
** Outlet parameters:the character read from RX
** Instructions for use:Read single character
*****************************************************************/
char read_char(void)
{
while(!(UCSRA & (1<<RXC)) );
Read_UART = UDR;
send_char(Read_UART);
return Read_UART;
}