The following PBP3 code is my first attempt at coding for a frequency counter that can read above 500khz. The goal was to see if it could accurately capture frequency below 30mhz without using a frequency-prescaler, or ripple-counter. It does work, but its not accurate enough to be considered anything but educational or a fun project.
Code:' Code for 16F877A to count frequency ' Uses TMR0 and prescaler to gate 1 second counting time, internally clocked. ' Uses Timer1, external clock, from PIN 15 ' WDT, Analog, Comparitors OFF clear define loader_used 1 define osc 4 ' Define LCD registers and bits Define LCD_DREG PORTD Define LCD_RSREG PORTE Define LCD_RSBIT 0 Define LCD_EREG PORTE Define LCD_EBIT 1 define LCD_RWREG PORTE define LCD_RWBIT 2 define LCD_BITS 8 define LCD_LINES 2 define LCD_COMMANDUS 2500 define LCD_DATAUS 100 TRISC = %11110001 'using pin 15 as input for frequency TRISD = 0 'PORTD all outputs PORTD = 0 CMCON = 7 'disable comparators ADCON1 = 7 'PORTA and E digital 'setup option register for TMR0 option_reg.7 = 0 option_reg.6 = 1 '1 for interrupts active option_reg.5 = 0 option_reg.4 = 1 '1 for interrupts active option_reg.3 = 0 'Assign prescaller to TMR0 option_reg.2 = 1 'prescaler 0-15 option_reg.1 = 1 'with this setting TMRO-Rate = 1:256 option_reg.0 = 1 'Prescaller settings for option_reg 'option_reg 01011000 = 1:1 prescaler TMR0 roles over every 1mSec 'option_reg 01010000 = 1:2 prescaler TMR0 roles over every 152 uSec 'option_reg 01010001 = 1:4 prescaler TMR0 roles over every 1mSec 'option_reg 01010010 = 1:8 prescaler TMR0 roles over every 2mSec 'option_reg 01010011 = 1:16 prescaler TMR0 roles over every 4mSec 'option_reg 01010100 = 1:32 prescaler TMR0 roles over every 8mSec 'option_reg 01010101 = 1:64 prescaler TMR0 roles over every 16mSec 'option_reg 01010110 = 1:128 prescaler TMR0 roles over every 32mSec 'option_reg 01010111 = 1:256 prescaler TMR0 roles over ever 64mSec INTCON.7 = 0 'disable interrupt routine on overflow INTCON.5 = 0 'disable interrupt routine on Timer0 overflow INTCON.4 = 0 'disable RB0 external interrupt function INTCON.3 = 0 'disable port change interrupt function INTCON.1 = 0 'not really needed, but clear possible pin change flag T1CON = %00110010 'Timer1 prescaler = 8 - osc off- sync on, external source lcdout $fe,1 'clear the display lcdout "Frequency Counter" pause 1000 Tmr1flg var word counting var byte calc var byte mhz var byte khz var word hz var word PORTD = 0 'We will be setting PORTD.0 high through the 1 second 'gate-period to monitor it with Oscope INTCON.2 = 0 'Clear Timer0 interrupt flag TMR1 = 0 'Reset Timer0 counting value ' Main program loop mainloop: Tmr1flg = 0 'Clear variable which counts Timer1 overflows TMR1H = 0 'clear Timer1 high register TMR1L = 0 'clear Timer1 low register PIR1.0 = 0 'clear Timer1 overflow flag bit INTCON.2 = 0 'Clear Timer0 interrupt flag TMR1 = 0 'Reset Timer0 value counting = 0 PORTD.0 = 1 'raise the 1 second timer flag T1CON.0 = 1 'start Timer1 while counting < 15 'Timer0 has to overflow this many times for 1 second gate period while INTCON.2 = 0 'watch for Timer1 overflow and count if it occurs. if PIR1.0 = 1 then 'if Timer1 overflow flag is up then Tmr1flg = Tmr1flg + 1 'increment overflow count by 1. Each count represents value of 524280 PIR1.0 = 0 'Now reset the Timer1 overflow flag bit endif wend 'end of inner while loop INTCON.2 = 0 'Now reset the Timer0 overflow flag bit counting = counting + 1 'increment counting for gate time wend 'end of outer while loop T1CON.0 = 0 'stop Timer1 PORTD.0 = 0 'lower the 1 second timer flag representing the gate period ' calculations to get the numbers captured to the display ' we have to feed the number of Timer1 overflows into Mhz, Khz, hz ' Then we feed the Timer1 remaining count * 8 in as well ' and parse it out to mhz khz hz hz = 0 khz = 0 mhz = 0 ' Add Timer1 overflow values into Mhz, Khz, hz if Tmr1flg > 0 then for calc = 0 to Tmr1flg khz = khz + 524 hz = hz + 280 while hz > 999 khz = khz + 1 hz = hz -1000 wend while khz > 999 mhz = mhz + 1 khz = khz - 1000 wend next endif ' Add remaining Timer1 count * 8 into Mhz, Khz, hz for calc = 0 to 7 hz = hz + TMR1 while hz > 999 khz = khz + 1 hz = hz -1000 wend while khz > 999 mhz = mhz + 1 khz = khz - 1000 wend next 'Display results if mhz > 0 then lcdout $FE,1,#mhz,".",#khz,".",#hz else lcdout $FE,1,#khz,".",#hz endif pause 2000 goto mainloop ' Go get another reading End




Bookmarks