forked from andygock/avr-uart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
130 lines (113 loc) · 3.85 KB
/
example.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
/*************************************************************************
Example program, based on Peter Fluery's example.
*************************************************************************/
/*************************************************************************
Title: Example program for the Interrupt controlled UART library
Author: Peter Fleury <pfleury@gmx.ch> http://tinyurl.com/peterfleury
File: $Id: test_uart.c,v 1.7 2015/01/31 17:46:31 peter Exp $
Software: AVR-GCC 4.x
Hardware: AVR with built-in UART/USART
DESCRIPTION:
This example shows how to use the UART library uart.c
*************************************************************************/
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "uart.h"
/* Define CPU frequency in Hz in Makefile or toolchain compiler configuration */
#ifndef F_CPU
#error "F_CPU undefined, please define CPU frequency in Hz in Makefile or compiler configuration"
#endif
/* Define UART baud rate here */
#define UART_BAUD_RATE 9600
int main(void)
{
uint16_t c;
char buffer[7];
int8_t num = 134;
/*
* Initialize UART library, pass baudrate and AVR cpu clock
* with the macro
* UART_BAUD_SELECT() (normal speed mode)
* or
* UART_BAUD_SELECT_DOUBLE_SPEED() (double speed mode)
*/
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU));
/*
* Now enable interrupt, since UART library is interrupt controlled
*/
sei();
/*
* Transmit string to UART
* The string is buffered by the uart library in a circular buffer
* and one character at a time is transmitted to the UART using interrupts.
* uart_puts() blocks if it can not write the whole string to the circular
* buffer
*/
uart_puts("String stored in SRAM\n");
/*
* Transmit string from program memory to UART
*/
uart_puts_P("String stored in FLASH\n");
/*
* Use standard avr-libc functions to convert numbers into string
* before transmitting via UART
*/
itoa(num, buffer, 10); // convert integer into string (decimal format)
uart_puts(buffer); // and transmit string to UART
/*
* Transmit single character to UART
*/
uart_putc('\r');
for (;;)
{
/*
* Get received character from ringbuffer
* uart_getc() returns in the lower byte the received character and
* in the higher byte (bitmask) the last receive error
* UART_NO_DATA is returned when no data is available.
*
*/
c = uart_getc();
if (c & UART_NO_DATA)
{
/*
* No data available from UART
*/
}
else
{
/*
* New data available from UART
* Check for Frame or Overrun error
*/
if (c & UART_FRAME_ERROR)
{
/* Framing Error detected, i.e no stop bit detected */
uart_puts_P("UART Frame Error: ");
}
if (c & UART_OVERRUN_ERROR)
{
/*
* Overrun, a character already present in the UART UDR register was
* not read by the interrupt handler before the next character arrived,
* one or more received characters have been dropped
*/
uart_puts_P("UART Overrun Error: ");
}
if (c & UART_BUFFER_OVERFLOW)
{
/*
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
*/
uart_puts_P("Buffer overflow error: ");
}
/*
* Send received character back
*/
uart_putc((unsigned char)c);
}
}
}