rendered paste body#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#define baud 9600
#define baud_prescale (F_CPU / 16 / baud-1)
#define portio PORTD
#define irrec PD2
#define rxled PD7
volatile uint16_t ir_data = 0;
volatile uint8_t ir_cnt = 0;
volatile uint8_t ir_1st = 0;
volatile uint8_t ir_paste = 0;
volatile uint8_t rx_store = 0;
volatile uint8_t rx_enable = 0;
volatile uint8_t rx_enable_cnt = 0;
void io_init(void)
{
DDRD |= (1 << PD7);
DDRB = 0xff;
PORTB = 0xff;
DDRC = 0xff;
PORTC = 0xff;
}
void int_init(void)
{
GICR |= (1<<INT0);
MCUCR |= (0<<ISC00 | 1<<ISC01);
TIMSK |= (1 << TOIE0)|
(1 << TOIE1);
TCCR0 |= (1 << CS02);
}
void usart_init(void)
{
UCSRB |= (1 << RXEN)| // enable tx and rx
(1 << TXEN)|
(1 << RXCIE); // enable rx interrupt
UCSRC |= (1 << UCSZ0)| // 8 bit character sizes
(1 << UCSZ1);
UBRRL = baud_prescale;
UBRRH = (baud_prescale >> 8);
}
void sendChar(char data)
{
while(!(UCSRA & (1<<UDRE)))
;
UDR = data;
UCSRA=UCSRA|0x40;
}
ISR(INT0_vect)
{
TCCR1B &= ~(1<<CS10);
if(ir_1st == 0) {
ir_1st = 1;
return;
}
if(ir_1st == 1) {
if(TCNT1 > 8000 && TCNT1 < 11200)
ir_data &= ~(1<<ir_cnt++);//ir_data[ir_cnt++] = 0;
if(TCNT1 > 12800 && TCNT1 < 16000)
ir_data |= (1<<ir_cnt++);//ir_data[ir_cnt++] = 1;
}
TCNT1 = 0;
TCCR1B |= (1<<CS10);
portio ^= ( 1 << rxled);
}
ISR(TIMER1_OVF_vect)
{
portio &= ~( 1 << rxled);
}
ISR(TIMER0_OVF_vect)
{
if(rx_enable > 0)
{
if(rx_enable_cnt < 4)
rx_enable_cnt++;
else
{
if(!(portio & (1<<rxled)))
portio |= (1 << rxled);
else
portio &= ~(1 << rxled);
rx_enable_cnt = 0;
rx_enable++;
if(rx_enable >= 20) {
portio &= ~(1 << rxled);
rx_enable_cnt = 0;
rx_enable = 0;
}
}
}
}
ISR(USART_RXC_vect)
{
rx_store = UDR;
rx_enable = 1;
}
int main(void)
{
io_init();
int_init();
usart_init();
sei();
int0cnt = 0;
while(1)
{
if(TCNT1 > 20000)
{
ir_1st = 0;
TCCR1B &= ~(1<<CS10);
TCNT1 = 0;
PORTB = ir_data;
PORTC = (ir_data >> 8);
}
}
}