diff --git a/src/main.c b/src/main.c index a2e51bf..7e1783d 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,9 @@ #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 */ @@ -19,25 +22,34 @@ static const uint16_t TIMER2_PRESCALE[8] = {0,1,8,32,64,128,256,1024}; /* timer 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; + burst[pulse-1] = (TCNT1*1000UL)/ir_tick; burst[pulse] = 0; } if (pulse0) { - pulse2time(burst,pulse-1); time2nec(burst,pulse-1); struct nec ir_data = nec2data(burst,pulse-1); if (ir_data.valid) { @@ -79,7 +90,6 @@ ISR(TIMER1_COMPA_vect) { /* timer 1 OCR1A match interrupt vector */ printf("IR addr: %u, command: %u\n",ir_data.address,ir_data.command); } } - OCR0A = ~OCR0A; pulse = 0; } } @@ -140,8 +150,11 @@ static void ioinit(void) /* clock/8 prescaler, offers most precision for 15ms (up to 28.5ms) */ TCCR1B |= (1<>CS10]; - OCR1A = (uint32_t)(15*F_CPU)/(1000*prescale); /* set clear time to 15 ms (no IR toggle should be longer) */ + uint16_t prescale = TIMER1_PRESCALE[(TCCR1B&((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<>CS10]; /* timer 1 presacler */ - uint16_t tick = F_CPU/(1000*prescale); /* ticks per ms */ - - if (0==prescale) { - return; - } - for (uint8_t i=0; i