This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
outp(compare >> 8,OCR1H); // set the compare1 register to the outp(compare,OCR1L); // required value
outp(0x40,TCCR1A); // set OC1 bit to toggle on compare
sbi(TIFR,OCF1A); // clear overflov/compare flags
if (compare == 15625) outp(0x0C,TCCR1B); // start with fClk/256 (15625 Hz) and compare clear else outp(0x0A,TCCR1B); // start with fClk/8 (500 kHz) and compare clear
while ( ! (unsigned char) ( inp(TIFR) & BV(OCF1A)) ); // wait for bit sbi(TIFR,OCF1A); // clear flags
// counter input now enabled // for the specified time
while ( ! (unsigned char) ( inp(TIFR) & BV(OCF1A)) ); // wait again for bit
outp(0,TCCR1B); // stop timer
// counter input disabled }
/****************************************************************************/ /* main *******************************************************************/ /****************************************************************************/
int main(void) { int i,j; unsigned char dp,ms; unsigned long lv; unsigned int count;
// first make sure the OC1 pin is in a controlled state // we want it to be HIGH initially
// There's no way to set/clear it directly, but it can be forced to // a defined state by a compare match, se by setting a low compare value // and start the timer, it can be forced into set state
outp(0,TCNT1H); // clear timer outp(0,TCNT1L);
outp(0,OCR1H); // set compare to 200 outp(200,OCR1L);
outp(0xC0,TCCR1A); // set OC1 bit to set on compare
// start timer and wait for one compare match outp(0x01,TCCR1B); // start with fClk/1 (4 MHz) while ( ! (unsigned char) ( inp(TIFR) & BV(OCF1A)) ); // wait for bit sbi(TIFR,OCF1A); // clear flags
outp(0,TCCR1B); // stop timer
// compare bit no HI, start // doing some useful work
while (1) { // try a capture at min gate capture(500); // 1 mS // get the data count = read_counters(); dp = 3; // decimal point ms = 2; // indicate MHz
if (count < 4096) // less than 4.096 MHz { // try a capture at next gate value capture(5000); // 10 mS // get the data count = read_counters(); dp = 4; // decimal point ms = 2; // indicate MHz
if (count < 4096) // less than 409.6 kHz { // try a capture at next gate value capture(50000); // 100 mS // get the data count = read_counters(); dp = 3; // decimal point ms = 1; // indicate kHz
if (count < 4096) // less than 40.96 kHz { // try a capture at next gate value capture(15625); // 1 S // get the data count = read_counters(); dp = 0; // decimal point ms = 0; // indicate Hz } } }
// convert BINARY counter_value (int) to BCD in led_value (long) lv = 0; for (j=0;j<8;j++) { i = count % 10; lv >>= 4; lv |= ((unsigned long)i << 28); count /= 10; }