-
Notifications
You must be signed in to change notification settings - Fork 12
/
IR_Receiver_Test.cpp
94 lines (79 loc) · 2.11 KB
/
IR_Receiver_Test.cpp
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
/*
* Tests IR Receiver library.
*
* Created: 28.04.2017 22:29:35
* Author: Sergei Biletnikov
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include "IR_Receiver.h"
// define some macros
#define BAUD 9600 // define baud
#define UBRR ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR
// function to initialize UART
void uart_init (void)
{
//UBRR0H = (UBRR>>8); // shift the register right by 8 bits
//UBRR0L = UBRR; // set baud rate
UBRR0 = UBRR;
UCSR0B|= (1<<TXEN0)|(1<<RXEN0); // enable receiver and transmitter
UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01); // 8bit data format
}
void usart_transmit(uint8_t data)
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
void usart_transmit_str(const char* str)
{
int len = strlen(str);
int i;
for (i = 0; i < len; i++) {
usart_transmit(str[i]);
}
}
struct IR_Packet received_packet;
int main(void)
{
// init uart
uart_init();
init_receiver();
sei();
while (1)
{
cli();
uint8_t check_result = check_new_packet(&received_packet);
sei();
if (check_result)
{
char buff[10];
if (received_packet.repeat > 0)
{
utoa(received_packet.repeat, buff, 10);
usart_transmit_str(" R: "); // Command repeat counter
usart_transmit_str(buff);
} else
{
usart_transmit_str("\n\r");
utoa(received_packet.addr, buff, 16);
usart_transmit_str("A: "); // Address of the sending device
usart_transmit_str(buff);
usart_transmit_str(" ");
utoa(received_packet.command, buff, 16);
usart_transmit_str("C: "); // Command code
usart_transmit_str(buff);
usart_transmit_str(" ");
utoa(received_packet.repeat, buff, 10);
usart_transmit_str("R: "); // Command repeat counter
usart_transmit_str(buff);
}
}
_delay_ms(50);
}
}