#include /* Standard Integer Types */ #include /* Standard IO facilities */ #include /* General utilities */ #include /* Boolean */ #include /* Strings */ #include /* AVR device-specific IO definitions */ #include /* Convenience functions for busy-wait delay loops */ #include /* Interrupts */ #include "main.h" #include "uart.h" #include "ir_nec.h" /* global variables */ #define INPUT_MAX 255 /* max length for user input string */ char input[INPUT_MAX+2]; /* user input from USART */ volatile uint8_t input_i = 0; /* user input index */ volatile uint8_t pwr_ok; /* is power ok */ volatile uint8_t fan; /* fan signal state, to measure tachometer */ volatile uint8_t timer2_ovf = 0; /* to measure fan speed using timer 2 */ static const uint16_t TIMER2_PRESCALE[8] = {0,1,8,32,64,128,256,1024}; /* timer 2 CS2[2:0] values */ volatile uint16_t tachometer = 0; /* the tachometer time (from timer) */ volatile uint8_t ir; /* IR signal state, to measure IR code */ static const uint16_t TIMER1_PRESCALE[8] = {0,1,8,64,256,1024,0,0}; /* timer 1 CS1[2:0] values */ volatile uint16_t ir_tick; /* number of counter ticks per millisecond */ volatile uint8_t pulse = 0; /* pulse index within the burst */ #define PULSE_MAX 128 /* maximum number of pulses to save */ uint16_t burst[PULSE_MAX]; /* pulse times forming a burst (from timer) */ /* flags, set in the interrupts and handled in the main program */ volatile bool input_flag = false; volatile bool power_flag = false; /* UART receive interrupt */ ISR(USART_RX_vect) { input[input_i] = getchar(); /* save input */ input[input_i+1] = 0; /* always end the string */ if (input_i0) { /* save pulse, except the first */ burst[pulse-1] = (TCNT1*1000UL)/ir_tick; burst[pulse] = 0; } if (pulse0) { time2nec(burst,pulse-1); struct nec ir_data = nec2data(burst,pulse-1); if (ir_data.valid) { if (ir_data.repeat) { printf("IR signal repeated\n"); } else { printf("IR addr: %u, command: %u\n",ir_data.address,ir_data.command); } } pulse = 0; } } static void ioinit(void) { /* configure power */ DDRB |= (1<>CS10]; /* timer 1 presacler */ if (0!=prescale) { ir_tick = F_CPU/(1000*prescale); /* ticks per ms */ OCR1A = (uint32_t)(15*F_CPU)/(1000*prescale); /* set clear time to 15 ms (no IR toggle should be longer) */ } TIFR1 &= ~(1<0) { OCR0A--; } printf("decreasing LED: %u\n",OCR0A); break; case '+': if (OCR0A<0xff) { OCR0A++; } printf("increasing LED: %u\n",OCR0A); break; case 't': if (tachometer) { uint16_t prescale = TIMER2_PRESCALE[TCCR2B&((1<