-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_functions.c
executable file
·181 lines (157 loc) · 6.07 KB
/
uart_functions.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//UART Functions
//Roger Traylor 11.l6.11
//For controlling the UART and sending debug data to a terminal as an aid
//in debugging. Note that RX and TX lines are marked relative to the device
//they are located on.
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
//F_CPU should be set in Makefile, don't set it here.
#define USART_BAUDRATE 9600
#define BAUDVALUE ((F_CPU/(USART_BAUDRATE * 16UL)) - 1 )
#define USART1_BAUDRATE 9600
#define BAUDVALUE_1 ((F_CPU/(USART1_BAUDRATE * 16UL)) - 1 )
#include <string.h>
char uart_tx_buf[40]; //holds string to send to crt
char uart_rx_buf[40]; //holds string that recieves data from uart
char uart1_tx_buf[40]; //holds string to send to crt
char uart1_rx_buf[40]; //holds string that recieves data from uart
//******************************************************************
// uart_putc
//
// Takes a character and sends it to USART0
//
void uart_putc(char data) {
while (!(UCSR0A&(1<<UDRE0))); // Wait for previous transmissions
UDR0 = data; // Send data byte
while (!(UCSR0A&(1<<UDRE0))); // Wait for previous transmissions
}
//******************************************************************
//******************************************************************
// uart1_putc
//
// Takes a character and sends it to USART1
//
void uart1_putc(char data) {
while (!(UCSR1A&(1<<UDRE1))); // Wait for previous transmissions
UDR1 = data; // Send data byte
while (!(UCSR1A&(1<<UDRE1))); // Wait for previous transmissions
}
//******************************************************************
//******************************************************************
// uart_puts
// Takes a string and sends each charater to be sent to USART0
//void uart_puts(unsigned char *str) {
void uart_puts(char *str) {
int i = 0;
// Loop through string, sending each character
while(str[i] != '\0') {
uart_putc(str[i]);
i++;
}
}
//******************************************************************
//******************************************************************
// uart1_puts
// Takes a string and sends each charater to be sent to USART0
//void uart_puts(unsigned char *str) {
void uart1_puts(char *str) {
int i = 0;
// Loop through string, sending each character
while(str[i] != '\0') {
uart1_putc(str[i]);
i++;
}
}
//******************************************************************
//******************************************************************
// uart_puts_p
// Takes a string in flash memory and sends each charater to USART0
//void uart_puts(unsigned char *str) {
void uart_puts_p(const char *str) {
// Loop through string, sending each character
while(pgm_read_byte(str) != 0x00) {
uart_putc(pgm_read_byte(str++));
}
}
//******************************************************************
//******************************************************************
// uart_init
//
//RXD0 is PORT E bit 0
//TXD0 is PORT E bit 1
//Jumpers J14 and J16 (mega128.1) or Jumpers J7 and J9 (mega128.2)
//must be in place for the MAX232 chip to get data.
void uart_init(){
//rx and tx enable, receive interrupt enabled, 8 bit characters
UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); //INTERRUPTS ENABLED
// UCSR0B |= (1<<RXEN0) | (1<<TXEN0); //INTERRUPS DISABLED
//async operation, no parity, one stop bit, 8-bit characters
UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);
UBRR0H = (BAUDVALUE >>8 ); //load upper byte of the baud rate into UBRR
UBRR0L = BAUDVALUE; //load lower byte of the baud rate into UBRR
}
//******************************************************************
//******************************************************************
// uart1_init
//
//RXD0 is PORT E bit 0
//TXD0 is PORT E bit 1
//Jumpers J14 and J16 (mega128.1) or Jumpers J7 and J9 (mega128.2)
//must be in place for the MAX232 chip to get data.
void uart1_init(){
//rx and tx enable, receive interrupt enabled, 8 bit characters
//UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1); //INTERRUPTS ENABLED
UCSR1B |= (1<<RXEN1) | (1<<TXEN1); //INTERRUPS DISABLED
//async operation, no parity, one stop bit, 8-bit characters
UCSR1C |= (1<<UCSZ11) | (1<<UCSZ10);
UBRR1H = (BAUDVALUE_1 >>8 ); //load upper byte of the baud rate into UBRR
UBRR1L = BAUDVALUE_1; //load lower byte of the baud rate into UBRR
}
//******************************************************************
//******************************************************************
// uart_getc
//Modified to not block indefinately in the case of a lost byte
//
char uart_getc(void) {
uint16_t timer = 0;
while (!(UCSR0A & (1<<RXC0))) {
timer++;
if(timer >= 16000){ return(0);}
//what should we return if nothing comes in?
//return the data into a global variable
//give uart_getc the address of the variable
//return a -1 if no data comes back.
} // Wait for byte to arrive
return(UDR0); //return the received data
}
//******************************************************************
//******************************************************************
// uart1_getc
//Modified to not block indefinately in the case of a lost byte
//
char uart1_getc(void) {
uint16_t timer = 0;
while (!(UCSR1A & (1<<RXC1))) {
timer++;
if(timer >= 16000){ return(0);}
//what should we return if nothing comes in?
//return the data into a global variable
//give uart_getc the address of the variable
//return a -1 if no data comes back.
} // Wait for byte to arrive
return(UDR1); //return the received data
}
//usage examples:
//uart_puts(".");
//uart_puts(" ");
//uart_puts("strength = ");
//itoa((int)strength, str, 10);
//uart_puts(str);
//uart_puts(" ");
//uart_init();
//uart_putc('\n');
//uart_puts("*****************\n");
//uart_puts("wrote first byte: ");
//uart_puts(str);
//uart_putc('\n');