added function to calculate IR pulse times

This commit is contained in:
King Kévin 2013-10-14 13:36:25 +02:00
parent ec8defaa99
commit 9883e2d8d6
2 changed files with 31 additions and 11 deletions

View File

@ -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<<IR))) { /* did the IR pin state changed */
ir = PINB&(1<<IR); /* save new state */
if (pulse_i>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_i<PULSE_MAX-1) { /* prepare to save next pulse */
pulse_i++;
if (pulse<PULSE_MAX-1) { /* prepare to save next pulse */
pulse++;
}
TCNT1 = 0; /* clear timer 1 */
}
@ -65,9 +66,10 @@ ISR(TIMER2_OVF_vect) { /* timer 2 overflow interrupt vector */
/* timer 1 interrupt used to measure IR pulse */
ISR(TIMER1_COMPA_vect) { /* timer 1 OCR1A match interrupt vector */
if (pulse_i>0) {
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<<CS11);
TCCR1B &= ~((1<<CS12)|(1<<CS10));
OCR1A = (uint32_t)(15*F_CPU)/(1000*8); /* set clear time to 15 ms (no IR toggle should be longer) */
uint16_t prescale = TIMER1_PRESCALE[(TCCR1B&((1<<CS12)|(1<<CS11)|(1<<CS10)))>>CS10];
OCR1A = (uint32_t)(15*F_CPU)/(1000*prescale); /* set clear time to 15 ms (no IR toggle should be longer) */
TIFR1 &= ~(1<<OCF1A); /* clear timer 1 overflow interrupt flag */
TIMSK1 |= (1<<OCIE1A); /* enable timer 1 overflow interrupt */
@ -145,6 +148,22 @@ static void ioinit(void)
sei(); /* enable interrupts */
}
/* convert burst to micro-seconds */
static void pulse2us(void)
{
uint16_t prescale = TIMER1_PRESCALE[(TCCR1B&((1<<CS12)|(1<<CS11)|(1<<CS10)))>>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<pulse; i++) {
burst[i] = (burst[i]*1000UL)/tick;
printf("pulse %u: %uus\n",i,burst[i]);
}
}
int main(void)
{
ioinit(); /* initialize IOs */

View File

@ -17,3 +17,4 @@
#define CH2_5 PD7
static void ioinit(void);
static void pulse2us(void);