-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart_several_to_1.c
More file actions
77 lines (65 loc) · 1.17 KB
/
uart_several_to_1.c
File metadata and controls
77 lines (65 loc) · 1.17 KB
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
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#define F_CPU 1000000UL // 1 MHz
#define BUFF_LENGTH 100
int position = 0;
ISR(USART_RXC_vect)
{
uint8_t MYUDR = UDR;
if ((MYUDR >= 'a') && (MYUDR <= 'z'))
{
if ('a' + position == MYUDR)
{
position++;
}
else
{
position = 0;
}
}
else if (MYUDR == '\r')
{
if (position <= 9)
{
UDR = position + '0';
}
else
{
UDR = '-';
}
position = 0;
}
else
{
position = 0;
}
}
void setup_io()
{
//disable interrupts:
cli();
//set the baud rate:
UBRRH = 0;
UBRRL = 12; // for 4800 bps baud rate
//set asynchronous mode, disable parity mode, 1 stop bit, 8-bit character:
UCSRC |= (1 << URSEL) | (0 << UMSEL) | (0 << UPM1) | (0 << UPM0) | (0 << USBS) | (1 << UCSZ1) | (1 << UCSZ0) | (0 << UCPOL);
//enable RX Complete, no yet USART Data Register Empty Interrupts:
UCSRB |= (1 << RXCIE) | (0 << UDRIE) | (0 << UCSZ2);
//enable transmitter, receiver
UCSRB |= (1 << RXEN) | (1 << TXEN);
UCSRA |= (1 << U2X);
//enable interrupts globally:
sei();
}
int main()
{
setup_io();
sleep_enable();
while (true)
{
sleep_cpu();
}
}