Continue to Site

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.

  • 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.

counting 1 second

Status
Not open for further replies.

jumpjack

New Member
I need to build a loop which elapses exactly one second, and while time passes, a separate counter must be incremented.
At the end of the second, I need to know the value of the counter, which started from 0.
I'm not very familiar with PIC instruction set, can anybody give me some tips?
How do I know how long any instruction take to be executed?
How do I test if 1 second is passed?
 
I'm not very familiar with PIC instruction set, can anybody give me some tips?
How do I know how long any instruction take to be executed?
How do I test if 1 second is passed?

Read the datasheet. Why do you expect anyone to help you, if you don't make an effort to help yourself?
 
Read the datasheet. Why do you expect anyone to help you, if you don't make an effort to help yourself?

I need a starting point. A tip, an example source, a suggestion, a link.... Just as blueroomelectronics did... :rolleyes:

If you don't want to help, just don't reply....
 
I need a starting point. A tip, an example source, a suggestion, a link.... Just as blueroomelectronics did... :rolleyes:

If you don't want to help, just don't reply....

correct jumpjack, we are the beginners asking help ,but these people things they are very smart
 
Without a better description of what you are trying to do it is very difficult to suggest a solution.

Mike.
 
Without a better description of what you are trying to do it is very difficult to suggest a solution.

Mike.
better?!?
I can't imagine a better description than this... :confused:

I'll try to be LESS precise:
I need to count how many pulses I see on a pin in one second.
 
Last edited:
better?!?
I can't imagine a better description than this... :confused:

I'll try to be LESS precise:
I need to count how many pulses I see on a pin in one second.

hi jack,
Just observing this thread, this is NOT what your opening post said.:)

This is quite different.

Basically IIUC you want to count the frequency input to a PIC pin over a 1 second period.

EDIT:

Use the PIC's Timer 0 to give an integer count based on the PIC's crystal/cycle time.
Loop around this Timer 0 interval, the number of times it takes to make a total of 1 second.

Within this timer0 loop, use timer 1 to count the incoming pulses.

At the end of 1 second, read the contents of timer1 high/low for the count frequency.
 
Last edited:
hi jack,
Just observing this thread, this is NOT what your opening post said.:)

This is quite different.

Basically IIUC you want to count the frequency input to a PIC pin over a 1 second period.
If I asked "how do I write a frequency counter", somebody would have replied "readt the **** datasheet", so I just asked how to count, to write the frequency counter by myself....
But, guess what?... :rolleyes:

To be more precise, I need to count how many times C1OUT in pic 16f628a passes from low to high, upon detecting an analog signal on the RA0 pin (a 440 Hz sinusoid coming from an audio output).
 
If I asked "how do I write a frequency counter", somebody would have replied "readt the **** datasheet", so I just asked how to count, to write the frequency counter by myself....
But, guess what?... :rolleyes:

To be more precise, I need to count how many times C1OUT in pic 16f628a passes from low to high, upon detecting an analog signal on the RA0 pin (a 440 Hz sinusoid coming from an audio output).

hi jack,
Chill Out.:)

Read my EDIT
 
hi jack,
This is an extract the way I do it, using Timer0 and Timer 1.
The asm: prefix is just to keep my Basic compiler happy.

Just square up the analog signal and drive PORTB.0


Code:
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
 
Last edited:
To be more precise, I need to count how many times C1OUT in pic 16f628a passes from low to high, upon detecting an analog signal on the RA0 pin (a 440 Hz sinusoid coming from an audio output).

Your description is still confusing. At what frequency will C1Out change? Will the signal on RA0 be there for 1 second. Will any other signal appear on RA0 or will it always be 440Hz? Can you simply wait for any signal on RA0 and then count C1Out transitions for 1 second?

Mike.
 
I need a starting point. A tip, an example source, a suggestion, a link.... Just as blueroomelectronics did...

Part of your original post read:

I'm not very familiar with PIC instruction set, can anybody give me some tips?
How do I know how long any instruction take to be executed?

This is information found in the datasheet , which I suggested that you read. I assumed that you could Google for the link. You don't state which PIC you are using, but here it is one for the 16F627A/628A/648A:

https://www.electro-tech-online.com/custompdfs/2008/10/40044D.pdf

Timing depends on the clock frequency, which again you don't state. Are you using the internal oscillator or a crystal or resonator? If so, what frequency?

Section 3.1 of the datasheet explains clocking scheme and instruction cycle, while Table 15-2 has the number of cycles each instruction takes.

Had you read the datasheet you would have found sections 6, 7, and 8 deal with the timer modules, which you might find useful for your project.

Show that you've done your homework, and I think you will find many people here ready to help.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top