-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
81 lines (56 loc) · 1.12 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
#include <Arduino.h>
#include <avr/io.h>
#include <util/delay.h>
#include "uart.h"
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif
#include <util/setbaud.h>
void init_uart(void) {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0B |= (1<<RXCIE0); //RX complete enable interrupt
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
void uart_putchar(char c) {
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}
char uart_getchar() {
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}
void transmit() {
uart_putchar('A');
_delay_ms(50);
}
void txString(char *str){
while(*str != '\0'){
uart_putchar(*str);
str++;
}
}
/*
int main(void) {
char myChar='a';
pinMode(13,OUTPUT);
while(1) {
myChar = uart_getchar();
if(myChar == 'A') {
digitalWrite(13,HIGH);
_delay_ms(500);
digitalWrite(13,LOW);
}
// transmit();
}
}
*/