From 9883e2d8d6c6ca61c39704e56504597c6bc5e63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 14 Oct 2013 13:36:25 +0200 Subject: [PATCH] added function to calculate IR pulse times --- src/main.c | 41 ++++++++++++++++++++++++++++++----------- src/main.h | 1 + 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index dd4b4fa..60fdaf6 100644 --- a/src/main.c +++ b/src/main.c @@ -16,9 +16,10 @@ 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 */ -volatile uint8_t pulse_i = 1; /* pulse index */ -# define PULSE_MAX 64 /* maximum number of pulses to save */ -volatile uint16_t pulse[PULSE_MAX]; /* pulse times (from timer) */ +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 */ @@ -33,12 +34,12 @@ ISR(PCINT0_vect) { /* PCI0 Interrupt Vector for PCINT[7:0] */ } } else if (ir!=(PINB&(1<0) { /* save pulse, except the first */ - pulse[pulse_i-1] = TCNT1; - pulse[pulse_i] = 0; + if (pulse>0) { /* save pulse, except the first */ + burst[pulse-1] = TCNT1; + burst[pulse] = 0; } - if (pulse_i0) { - pulse_i = 0; + if (pulse>0) { + pulse2us(); OCR0A = ~OCR0A; + pulse = 0; } } @@ -127,7 +129,8 @@ 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) */ 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; i