G update running average of 4 readings current

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: erage + ¼ * new reading Take readings of both Tb and Tc to be extra careful Tc changes with temperature Usually can do Tc just once * !; , 0$ $ , !; Accelerometer duty cycle varies with force 12.5% for each g RSET determines duration of period At 1g duty-cycle will be 62.5% (37.5%) #( 8 $ How big a counter do you need? Assume 7.37MHz clock 1ms period yields a count of 7370 This fits in a 16-bit timer/counter Should you use a prescaler for the counter? Bit precision issues unsigned int positive; unsigned int period; unsigned int pos_duty_cycle; BAD: pos_duty_cycle = positive/period; BAD: pos_duty_cycle = ( positive * 1000 ) / period; OKAY: pos_duty_cycle = ( (long) positive * 1000 ) / period; , 0 Sensitivity (maximum duty cycle change per degree) is highest when accelerometer is perpendicular to gravity / ! Easy to control intensity of light through pulse-width modulation Duty-cycle is averaged by human eye Light is really turning on and off each period Too quickly for human retina (or most video cameras) Period must be short enough (< 1ms is a sure bet) LED output is low to turn on light, high to turn it off Active low output 9 ! 6 #( 8 Varying PWM output volatile uint8_t width; /* positive pusle width */ volatile uint8_t delay; /* used to slow the pulse width changing */ SIGNAL (SIG_OVERFLOW2) { if(delay++ == 20) { OCR2 = width++; delay = 0; } } int main (void) { /* must make OC2 pin an output for the PWM to visible */ DDRD = _BV(DDD7); /* use Timer 2 FastPWM and the overflow interrupt to update duty-cycle */ TCCR2 = _BV (WGM21) | _BV (WGM20) | _BV (COM21) | _BV(COM20) | _BV(CS21) | _BV(CS20); TIMSK = _BV (TOIE2); /* setup initial conditions */ delay = 0; /* enable interrupts */ sei (); for (;;) { ; /* LOOP FOREVER as the interrupt will make necessary adjustment */ } return (0); } 10...
View Full Document

Ask a homework question - tutors are online