Hi Nick,
Using the PIC hardware instead of a "divide by 1000" external prescaler would allow for more frequent LCD updates. Would that be an advantage for your application?
Here's one way I thought about implementing a frequency counter in a HLL (BoostC in this case). I might use periodic 1-msec timer 2 interrupts to check for TMR0 register overflow and gate the counter on or off every 200 msecs by toggling the T0CKI data direction pin. Timer 0 would alternate between counting for 200-msecs or being idle for 200-msecs.
Extracting the 8-bit timer 0 prescaler value and adding it to the 24 bit result is tricky. After T0CKI has been set to an output (gate 'off') we pulse the timer 0 edge select bit (in option_reg) which bumps the prescaler and we decrement the CountL byte until the prescaler overflows into the TMR0 register.
Maybe something like this;
Code:; ; unsigned char msctr = 200; // ; unsigned char CountL = 0; // ; unsigned char CountH = 0; // ; unsigned char CountU = 0; // ; unsigned long result = 0; // ; ; void interrupt() // 1-msec TMR2 interrupts ; { pir1.TMR2IF = 0; // clear TMR2 interrupt flag ; msctr--; // decrement 200-msec timer ; if(msctr == 0) // if end of 200-msec interval ; { trisio.0 ^= 1; // toggle GP2/T0CKI direction ; msctr = 200; // reset 200-msec timer ; } ; if(intcon.T0IF) // if TMR0 overflow ; { CountU++; // bump b23..b16 byte ; intcon.T0IF = 0; // clear the overflow flag ; } // ; } ; ; void main() // ; { init(); // initialize ; while(1) // main.loop ; { while(trisio.2 == 1); // while 200-msec gate "on" ; CountH = tmr0; // result b15..b8 bits ; do // extract the prescaler bits ; { option_reg.T0SE = 1; // pulse TMR0 edge select bit ; option_reg.T0SE = 0; // to increment the prescaler ; CountL--; // adjust b7..b0 result bits ; } while(tmr0 == CountH); // until overflow into TMR0 ; result = CountU * 65536; // build 24-bit result ; result += CountH * 256; // ; result += CountL; // ; PutResult(); // put result on LCD ; CountU = 0; // prep for next count cycle ; CountH = 0; // ; CountL = 0; // ; while(trisio.2 == 0); // while 200-msec gate "off" ; } // ; } ;




Bookmarks