-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodigo_Arduino_UNO.c
80 lines (64 loc) · 1.56 KB
/
Codigo_Arduino_UNO.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
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define LED PB5
#ifndef F_CPU
#warning
#define F_CPU 16000000UL
#endif
#define BAUD 9600UL // tasa de baudios
// Cálculos
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // redondeo inteligente
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // tasa real de baudios
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // error en partes por millón, 1000 = no error
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Error in baud rate greater than 1%!
#endif
void uart_init(void) {
UBRR0H = UBRR_VAL >> 8;
UBRR0L = UBRR_VAL & 0xFF;
UCSR0C = (0 << UMSEL01) | (0 << UMSEL00) | (1 << UCSZ01) | (1 << UCSZ00); // asíncronos 8N1
UCSR0B |= (1 << RXEN0); // habilitar UART RX
UCSR0B |= (1 << TXEN0); // habilitar UART TX
UCSR0B |= (1 << RXCIE0); // deshabilitar
}
uint8_t uart_getc(void) {
while (!(UCSR0A & (1 << RXC0)))
// espera a que el símbolo este listo
;
return UDR0; // retorna simbolo
}
uint8_t uart_putc(unsigned char data) {
/* Wait for empty transmit buffer */
while (!(UCSR0A & (1 << UDRE0)))
;
/* asigna el dato en el buffer lo envia */
UDR0 = data;
return 0;
}
void initIO(void) {
DDRD |= (1 << DDD3);
DDRB = 0xff;
}
volatile uint8_t data = 10;
int main(void) {
initIO();
uart_init();
sei();
uint8_t i = 0;
volatile uint8_t pause;
for(;;) {
pause = data;
PORTB |= (1 << LED);
for(i = 0; i < pause; i++)
_delay_us(10);
PORTB &= ~(1 << LED);
for(i = 0; i < 255-pause; i++)
_delay_us(10);
}
return 0;
}
ISR(USART_RX_vect) {
data = UDR0;
}