• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need Frequency Counter Oshonsoft Basic example code

Status
Not open for further replies.
I need a frequency counter (20 - 50 mhz or so) using any of the 16F chips -- 16F877A) Can someone point
me to Oshonsoft BASIC example code ? I do not want ASM code (lots of that on the Internet).
I've Googled all over and can't find any BASIC examples.

Thanks

Chuck Templeman
 

ericgibbs

Well-Known Member
Most Helpful Member
hi,
Try this in your Oshonsoft Sim
The external frequency input is T1 clock
Eric

Code:
'PIC 16F876A with 4 line * 16 char LCD
'used for freq measurement dev 26/07/08
'external freq input on T1 Clk

Define SIMULATION_WAITMS_VALUE = 1

AllDigital

'ignore these 4 lines, old Menu
Symbol menukey = PORTA.1
Symbol inckey = PORTA.2
Symbol deckey = PORTA.3
Symbol acceptkey = PORTA.5

'buffer for bin to ascii conv
Dim b2avall As Byte
Dim b2avalm As Byte
Dim b2avalh As Byte

'ascbuffer
Dim ascbfr7 As Byte
Dim ascbfr6 As Byte
Dim ascbfr5 As Byte
Dim ascbfr4 As Byte
Dim ascbfr3 As Byte
Dim ascbfr2 As Byte
Dim ascbfr1 As Byte
Dim ascbfr0 As Byte

Dim temp1 As Byte
Dim temp2 As Byte
Dim cntr1 As Byte
Dim cntr2 As Byte

Dim tmr0_cnt As Byte
Dim tmr1_cnt As Byte

Dim sim As Byte

TRISB = 0x01

T1CON = 0x30
T1CON.T1OSCEN = 1
'T1CON.t1sync = 0
T1CON.TMR1CS = 0

Define LCD_LINES = 4
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 3
Define LCD_EREG = PORTB
Define LCD_EBIT = 2
Define LCD_RWREG = PORTB
Define LCD_RWBIT = 1

Define LCD_READ_BUSY_FLAG = 1

INTCON.GIE = 0
INTCON.PEIE = 0

PIE1.TMR1IE = 0
PIR1.TMR1IF = 0

Lcdinit

frequency:
Lcdout "Freq Cntr"
ASM:        movlw 0x06  'T1 ext osc sync off,timer1 off
ASM:        movwf t1con  't1 ext clock input via RC0
ASM:        bsf STATUS,RP0
ASM:        movlw 0x86  'timer 1:128, pu off
ASM:        movwf OPTION_REG
ASM:        bcf STATUS,RP0

nxt_freq:
ASM:        movlw 0x07  'T1 ext osc sync off,timer1 on
ASM:        movwf T1CON

tmr0_cnt = 25  '1 sec count 25

ASM:        clrf tmr1_cnt
ASM:        clrf TMR0
ASM:        bcf INTCON,2
ASM:        bcf PIR1,0  'clr tmr1 INTF
ASM:        clrf TMR1L  'zero timer1
ASM:        clrf TMR1H

timer1:
ASM:        btfss PIR1,0
Goto timer0  'no timer1 overflow
ASM:        bcf PIR1,0  'clr tmr1 INTF
ASM:        incf tmr1_cnt,F  'inc tmr1 cntr
timer0:
ASM:        btfss INTCON,2
Goto timer1  'no timer0 overflow
ASM:        bcf INTCON,2  'clr TMR0 INTF
ASM:        decfsz tmr0_cnt,F  'dec tmr0 cntr
Goto timer1  'Not zero so loop again
ASM:        movlw 0x00  'its zero
ASM:        movwf T1CON  'stop tmr1

b2avall = TMR1L
b2avalm = TMR1H
b2avalh = tmr1_cnt

Gosub bin2asc
Lcdcmdout LcdLine1Home
Lcdout ascbfr7, ascbfr6, ascbfr5, ascbfr4, ascbfr3, ascbfr2, ascbfr1, ascbfr0, "Hz"
WaitMs 500
Goto nxt_freq

End                                              

'this is used for tempr
'convert 24bit bin To 8 asci in ascbfr0
bin2asc:
ASM:        clrf ascbfr7
ASM:        clrf ascbfr6
ASM:        clrf ascbfr5
ASM:        clrf ascbfr4
ASM:        clrf ascbfr3
ASM:        clrf ascbfr2
ASM:        clrf ascbfr1
ASM:        clrf ascbfr0
ASM:        movlw .24
ASM:        movwf temp1
bitlp2:
ASM:        rlf b2avall,F
ASM:        rlf b2avalm,F
ASM:        rlf b2avalh,F
ASM:        movlw ascbfr0
ASM:        movwf FSR
ASM:        movlw 0x8
ASM:        movwf temp2
adjlp2:
ASM:        rlf INDF,F
ASM:        movlw 0x0a
ASM:        subwf INDF,W
ASM:        btfsc STATUS,C
ASM:        movwf INDF
ASM:        decf FSR,F
ASM:        decfsz temp2,F
Goto adjlp2
ASM:        decfsz temp1,F
Goto bitlp2

ASM:        movlw 0x30
ASM:        iorwf ascbfr7,F
ASM:        iorwf ascbfr6,F
ASM:        iorwf ascbfr5,F
ASM:        iorwf ascbfr4,F
ASM:        iorwf ascbfr3,F
ASM:        iorwf ascbfr2,F
ASM:        iorwf ascbfr1,F
ASM:        iorwf ascbfr0,F

'leading zero suppresion
If ascbfr7 = 0x30 Then
ascbfr7 = 0x20
Else
Goto skipz
Endif
If ascbfr6 = 0x30 Then
ascbfr6 = 0x20
Else
Goto skipz
Endif
If ascbfr5 = 0x30 Then
ascbfr5 = 0x20
Else
Goto skipz
Endif
If ascbfr4 = 0x30 Then
ascbfr4 = 0x20
Else
Goto skipz
Endif
If ascbfr3 = 0x30 Then
ascbfr3 = 0x20
Else
Goto skipz
Endif
If ascbfr2 = 0x30 Then
ascbfr2 = 0x20
Else
Goto skipz
Endif
If ascbfr1 = 0x30 Then
ascbfr1 = 0x20
Endif
   

skipz:

Return
 
Yes, I could use a pre-divider but I assumed that the 16F877A would be equal to the lowly 16F84 which can
count to around 50 Mhz according to all that I've googled. I'm not limited to just the 16F877 tho if others might
be more appropriate.

Thanks Chuck Templeman
 
EricGibbs, thanks for the code example. I'll fix up a PCB with the 16F876 for a test run. BTW what
frequency is the PIC operating at ? I assume 20 MHZ. I can deduce a schematic from the code.
 

jjw

Member
Are you going to use a ripple counter to divide down the frequency... The humble pic16f877a has a maximum internal clock of 5Mhz..... so 20...50Mhz will need to be slowed somewhat...
Do you mean an external prescaler?
The timer0 prescaler runs with ~ 50MHz input and does not depend on clock frequency.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
jjw said:
Do you mean an external prescaler?
The timer0 prescaler runs with ~ 50MHz input and does not depend on clock frequency.
I was going off the datasheet.... The recommendations are T0CK is synchronized with the osc....

I was trying to find the fastest input speed.... There is no entry about max timer clock...
 

jjw

Member
From memory it is in electrical specs that t0ck pulse width minimum is 10ns.
There are a lot of frequency counter projects which are based on AN592 from Microchip
 
Thanks for the input. What I'm looking for is a BASIC vs ASM code means of
reading/setting the timers. I suspect Oshonsoft is capable of doing so, but the
Oshonsoft documentation is very limited on many PIC native functions. ( like timer0 )
The solutions I've seen so far are pieces of ASM code embedded in BASIC code.

Thanks again for confirming the timer0 max frequency limit. If one wanted to
count with "programmable counter loops" then maybe 5 mhz or less is probably
the max limit.

Chuck Templeman
 

jjw

Member
If you open the alternate SFR viewer from Tools menu, you can see the official register names, which can be used from Basic.

For example:
TMR0 = 123
OPTION_REG= ...
t= TMR0
.....
The editor changes the names to uppercase, when they are correct
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Heres a snippet from the datasheet... It misleadingly reads that the external pulse must be high at least 2 tosc and low 2 tosc...
datasheet said:
11.5 Using Timer0 with an External Clock
When an external clock input is used for Timer0, it must meet certain requirements as detailed
in
11.5.1 “External Clock Synchronization.”
These requirements ensure the external clock
can be synchronized with the internal phase clock (T
OSC
). Also, there is a delay in the actual
incrementing of Timer0 after synchronization.
11.5.1 External Clock Synchronization
When no prescaler is used, the external clock input is the same as the prescaler output. The syn-
chronization of T0CKI with the internal phase clocks is accomplished by sampling the prescaler
output on the Q2 and Q4 cycles of the internal phase clocks (
Figure 11-5
). Therefore, it is nec-
essary for T0CKI to be high for at least 2Tosc (and a small RC delay of 20 ns) and low for at least
2Tosc (and a small RC delay of 20 ns). Refer to parameters 40, 41 and 42 in the electrical spec-
ification of the desired device.
When a prescaler is used, the external clock input is divided by an asynchronous ripple-counter
type prescaler so that the prescaler output is symmetrical. For the external clock to meet the
sampling requirement, the ripple-counter must be taken into account. Therefore, it is necessary
for T0CKI to have a period of at least 4Tosc (and a small RC delay of 40 ns) divided by the pres-
caler value. The only requirement on T0CKI high and low time is that they do not violate the min-
imum pulse width requirement of 10 ns. Refer to parameters 40, 41 and 42 in the electrical
specification of the desired device.
 
Status
Not open for further replies.

EE World Online Articles

Loading

 
Top