forked from Elektron2016/key_copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.c
151 lines (125 loc) · 2.97 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
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
/*
* Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sfr_defs.h>
#include <avr/sleep.h>
#include "uart.h"
#define BAUD 57600
#define UBRR_VALUE (((F_CPU) + 8UL * (BAUD)) / (16UL * (BAUD)) -1UL)
#define UBRRL_VALUE (UBRR_VALUE & 0xff)
#define UBRRH_VALUE (UBRR_VALUE >> 8)
#define USE_SLEEP 0
void uart_init()
{
DDRD |= 1<<PD1;
DDRD &= ~(1<<PD0);
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
/* set frame format: 8 bit, no parity, 1 bit */
UCSRC = UCSRC_SELECT | (1 << UCSZ1) | (1 << UCSZ0);
/* enable serial receiver and transmitter */
#if !USE_SLEEP
UCSRB = (1 << RXEN) | (1 << TXEN);
#else
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
#endif
}
void uart_putc(uint8_t c)
{
//if(c == '\n')
// uart_putc('\r');
/* wait until transmit buffer is empty */
while(!(UCSRA & (1 << UDRE)));
/* send next byte */
UDR = c;
}
void uart_putc_hex(uint8_t b)
{
/* upper nibble */
if((b >> 4) < 0x0a)
uart_putc((b >> 4) + '0');
else
uart_putc((b >> 4) - 0x0a + 'A');
/* lower nibble */
if((b & 0x0f) < 0x0a)
uart_putc((b & 0x0f) + '0');
else
uart_putc((b & 0x0f) - 0x0a + 'A');
}
void uart_putw_hex(uint16_t w)
{
uart_putc_hex((uint8_t) (w >> 8));
uart_putc_hex((uint8_t) (w & 0xff));
}
void uart_putdw_hex(uint32_t dw)
{
uart_putw_hex((uint16_t) (dw >> 16));
uart_putw_hex((uint16_t) (dw & 0xffff));
}
void uart_putw_dec(uint16_t w)
{
uint16_t num = 10000;
uint8_t started = 0;
while(num > 0)
{
uint8_t b = w / num;
if(b > 0 || started || num == 1)
{
uart_putc('0' + b);
started = 1;
}
w -= b * num;
num /= 10;
}
}
void uart_putdw_dec(uint32_t dw)
{
uint32_t num = 1000000000;
uint8_t started = 0;
while(num > 0)
{
uint8_t b = dw / num;
if(b > 0 || started || num == 1)
{
uart_putc('0' + b);
started = 1;
}
dw -= b * num;
num /= 10;
}
}
void uart_puts(const char* str)
{
while(*str)
uart_putc(*str++);
}
void uart_puts_P(const char *str)
{
uint8_t b = 0;
while( (b = pgm_read_byte(str++)) ) uart_putc(b);
}
uint8_t uart_getc()
{
/* wait until receive buffer is full */
#if USE_SLEEP
uint8_t sreg = SREG;
sei();
while(!(UCSRA & (1 << RXC)))
sleep_mode();
SREG = sreg;
#else
while(!(UCSRA & (1 << RXC)));
#endif
uint8_t b = UDR;
// if(b == '\r')
// b = '\n';
return b;
}
//EMPTY_INTERRUPT(USART_RXC_vect)