#include /* Standard Integer Types */ #include /* Standard IO facilities */ #include /* General utilities */ #include /* Strings */ #include /* AVR device-specific IO definitions */ #include /* Convenience functions for busy-wait delay loops */ #include /* Interrupts */ #include "main.h" #include "uart.h" /* global variables */ 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 uint8_t pulse = 1; /* pulse index within the burst */ #define PULSE_MAX 128 /* maximum number of pulses to save */ volatile uint16_t burst[PULSE_MAX]; /* pulse times forming a burst (from timer) */ /* power ok interrupt */ ISR(PCINT0_vect) { /* PCI0 Interrupt Vector for PCINT[7:0] */ if (pwr_ok!=(PINB&(1<0) { /* save pulse, except the first */ burst[pulse-1] = TCNT1; burst[pulse] = 0; } if (pulse0) { pulse2us(); OCR0A = ~OCR0A; pulse = 0; } } static void ioinit(void) { /* configure power */ DDRB |= (1<>CS10]; 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 */ //uint16_t time; /* pulse time in us */ if (0==prescale) { return; } for (uint8_t i=0; i0) { 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<