-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialTest.c
94 lines (79 loc) · 1.89 KB
/
serialTest.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
/*
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "utils/pinFunc.h"
#include <stdlib.h>
#define POLLING
//#define INTERRUPT
#define BAUDRATE 2400
#define UBRR F_CPU / BAUDRATE / 16 - 1
volatile char received;
static void init_USART(unsigned int ubrr) {
// set UBRR (USART baud rate register) aka set baud rate
UBRR0H = (unsigned char) (ubrr >> 8);
UBRR0L = (unsigned char) (ubrr);
// enable receiver and transmitter
// RXCIE0 = receiver interrupt
// RXEN0 = receiver enable
// TXEN0 = transmitter enable
#ifdef POLLING
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
#endif
#ifdef INTERRUPT
UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
#endif
// set frame - 8 bits, 1 stop bit, no parity
UCSR0C = 3 << UCSZ00;
}
static void USART_transmit(char data) {
// wait for empty transmit buffer
while (!(UCSR0A & (_BV(UDRE0))));
// put data into buffer, sends the data
UDR0 = data;
if (data == 'd') toggle_status_led2();
}
static char USART_receive(void) {
// wait for data to be received
while(!(UCSR0A & _BV(RXCIE0)));
// get and return received data from buffer
toggle_status_led1();
return UDR0;
}
#ifdef INTERRUPT
ISR(USART_RX_vect) {
toggle_status_led3();
received = USART_receive();
// a 'get' signal from the computer
if ((int) received > 120) {
USART_transmit((char) 100);
toggle_status_led4();
} else {
USART_transmit(received);
toggle_status_led5();
}
}
#endif
int main(void) {
init_leds();
init_USART(UBRR);
sei();
while(1) {
#ifdef POLLING
if (UCSR0A & _BV(RXC0)) {
toggle_status_led3();
received = USART_receive();
// a 'get' signal from the computer
if ((int) received > 120) {
USART_transmit((char)100);
toggle_status_led4();
} else {
USART_transmit(received);
toggle_status_led5();
}
}
#endif
};
return 0;
}
*/