I'm trying to figure out how to detect a particular frequency using the 12F683. I've been Googling for two days, and all I can seem to find is something about using some algorythm to check for presence of a tone in an audio signal. All I really need to do is figure out how to count the number of pulses coming in to a pin within a set time period, the rest I can handle in code. I know that the 16-bit timer can be used, I just haven't been able to figure out how (I am totally new to anything PIC escept for the PICAXE chips). The signals to be detected are 1-second 20-khz tones. I already have a circuit for squaring up the 20-khz tones to be able to use them as a TTL-level inputs for the PIC, I just need to find out how to code the PIC to make the count. I'm stuck. Any ideas about how to program the pic to make the count would be appreciated. If this topic has been covered (I looked but didn't see anything directly related), please post a link to the prior discussion. Thanks for all help given!
Last edit: Dave B 2016-04-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes you could possibly do it with timer1. But I think it would be simpler and more reproducible using a tone decoder such as the LM567.
www.ti.com/lit/ds/symlink/lm567.pdf
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
While I appreciate the reply, it fails to shed light on my question. I'm aware of other detection possibilites (I've used the 567 for various tasks for about twenty-five years), but that's not my interest. My interest is programming the PIC to do the job. If you have ideas specifically related to counting pulses received within a specified time period, or links to previous discussions/code examples already posted, I would appreciate you posting them.
Last edit: Dave B 2016-04-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK, I think I have the beginnings of an idea. I'll post my thoughts and see what you guys think. I'll use timer 1 to count the pulses, since it is 16-bit and has the capacity, and since it can be run from an external clock source. I'll use either timer 0 or timer 2 to control the length of time that counter 1 counts. I havent gotten as far as figuring out how to code that idea, but if I understand the 12F683 spec sheet and the GCB documentation regarding timers, it should be possible. Any thoughts?
Edit: I found a PIC-based counter project at http://www.best-microcontroller-projects.com/pic-frequency-counter.html, but for a different PIC. Since I know nothing about programming in C, the files posted don't help me, but the project seems to supoprt my idea of using one timer to set the counting time, and the other to make the count. I also found a more generalized article at the same website regarding use of a PIC as a counter, here: http://www.best-microcontroller-projects.com/frequency-counter.html. Along with supporting the idea of using one timer to control the other, it also mentions using a delay routine as the control for the counting timer. Since that's the simpler solution, and since I don't need pinpoint accuracy, I'm leaning in that direction.
Last edit: Dave B 2016-04-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The timer1 needs to be coordinated with the CCP1 module in the capture mode, to measure the positive/negative ttl pulse width. The CCP1 module will be toggled manually to capture the positive going edge (start TMR1) and then waiting to capture the negative going edge (stop TMR1).
OK, I think I have a workable solution to count pulses-per-second. I havent actually programmed a pic and tested it yet, but here's my code:
#chip 12F683,4
InitTimer1 Ext, PS1_1
Dim TimerValue As Word
dim pulsecount as word
main:
cleartimer 1
startTimer 1
wait 500 ms
stopTimer 1
timervalue = Timer1
pulsecount = timervalue * 2
Again, hyper-accuracy is not important here. Any thoughts?
Last edit: Dave B 2016-04-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Nope, at PS1_1 and clock of 4MHz then one tick of the clock is 1us for the PIC. A 500ms wait would be 500,000 ticks and an overflow condition would exist of the TMR1 word value of 65,535. A PS1_8 would have to be used so as not to overflow TMR1 with a 500ms wait.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I may not understand the timer initialization properly. Doesn't InitTimer1 Ext, PS1_1 tell the timer to run off of an external oscillator (clock pulses into pin 2 of the 12F683 if I'm following the spec sheet properly, the clock pulse source being the train of pulses I wish to count), and specify 1:1 prescale (no prescale)? If true, wouldn't I then be able to count up to 2^16 pulses before a counter 1 overflow occurs? Since The pulse train won't exceed 20khz, and since the time of count is only 500mhz, then 10k pulses is about the max (roughly) count that should be reached. Am I correct, or am I misunderstanding something?
Last edit: Dave B 2016-04-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"wait 500 ms" is waiting 500 milliseconds, or 1/2 a second. Perhaps "wait 500 us" was the intention.
On Monday, April 18, 2016 7:00 AM, Dave B <dtbradio@users.sf.net> wrote:
I may not understand the clock initialization properly. Doesn't InitTimer1 Ext, PS1_1 tell the clock to run off of an external oscillator into pin 2 of the 12F683 (if I'm following the spec sheet properly, the clock signal being the train of pulses I wish to count), and specify 1:1 prescale (no prescale)? If true, wouldn't I then be able to count up to 2^16 pulses before overflow? Since The pulse train won't exceed 20khz, and since the time of count is only 500mhz, then 10k pulses is about the max (roughly) count that should be reached. Am I correct, or am I misunderstanding something?Count pulses received with specific time periodSent from sourceforge.net because you indicated interest in https://sourceforge.net/p/gcbasic/discussion/629990/To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
No, 500 ms is intended. My question still stands about understanding the timer1 initialization clause, IE does "ExtOsc" mean it clocks off of pulses coming into the "T1CKI" pin of the 12F683? I just noticed my error where I typed 500mhz; that should read 500 ms, lol!
I'd appreciate input from other users, as well.
Last edit: Dave B 2016-04-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I appologize, my mind is trapped in the past, totally ignored the external osc command. 10,000 ticks is correct over a 500ms period.
I used the HPWM command to get an approx. 20kHz signal source for the T1CKI input of the 12f683; And verified 97-98 ticks over a 5ms wait period. So, close enough without some manual tweeking.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for confirming. I tested the programming, but I must be missing something because I don't seem to get a count. To be more precise, I don't ever see the count I'm looking for, even though I'm making sure its there. I'll get back on it tomorrow....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Since you specifically asked about counting pulses (time consuming) to detect a specific frequency that's where I will go, except to say that there are better / faster methods and much better chips for the task as hand (such as the 12F1612 w/Signal Measurement Timer) .
There should be no problem at all with the 12F683 counting pulses over a period time to detect a specific frequency such as 20Khz.
With a 20KHz signal applied to T1CKI/Pin2, the code below shows a count of 2013 over a period of 100ms, and 20,128 over a period of 1 second.
Note that I have an I2C 4 x 20 LCD connected to display the data.
'------------------------------------
'Test Code for Counting Pulses - WMR
'------------------------------------
#Chip 12F683, 8
#Config OSC = INTOSCIO, MCLRE = Off
#define I2C_MODE Master
#define I2C_DATA GPIO.4
#define I2C_CLOCK GPIO.2
#define I2C_DISABLE_INTERRUPTS ON
#define LCD_IO 10
#define LCD_I2C_Address_1 0x4E
'Frequency Signal input > GP5/T1CK1/PIN2
Inittimer1 EXT, PS1_1
Cleartimer 1
Do
Read_Signal 100 '// count for 100 ms
Clearline 0
Print Timer1
Read_Signal 1000 '// count for 1 sec
Clearline 1
Print Timer1
Wait 1 s
Loop
SUB READ_SIGNAL(IN Period as WORD)
Starttimer 1
Cleartimer 1
Wait Period ms
Stoptimer 1
END SUB
Sub Clearline (In LCD_TMP)
Locate LCD_TMP,0
LCDSPACE 19
Locate LCD_TMP,0
End Sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks very much for posting, William! I agree that there are better PIC's for this, but I'm trying to work with what I have on hand at the moment. For now, its either the 12F683, or a small assortment of PICAXE chips. I have researched other methods, but I'm NOT good with math in general, and I don't know assembly, so that limits what I personally am able to implement. If I could find something already done in GCB that was easy to port/modify, I would likely lay out the cash for different PIC's.
I'm also limited by my programmer, currently a PIC K-150. It works great, but its chip compatibility list is limited.
Last edit: Dave B 2016-04-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Modify the code I provided above as necessary and get it working in your App,
Then if you are truly interested in optimization, I will post some working example code for PIC16F16xx chips that have a dedicated Signal Measurement Timer (SMT).
This relatively new family of PIC;s, with Core Independant Peripherals (CIP's) opens up lots of new possibilities with signal measurement. Cost? < $2,00 each,
You will need a Pickit 3 to program these chips, or a $20 Curiousity Development Board
Edit: PIcaxe is a nice platform escpecally for beginners. However due to very high procesing overhead as a consequence of using interpreted basic, Picaxe Chips are not well suited for signal measurement duties at anywhere near 20KHz.
Last edit: William Roth 2016-04-20
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I agree 100% on the PICAXE chips. I've used them extensively for many projects not requiring fast processing. As for this project, I've had to set it aside for now until I get more time. I need to modify my pulse shaper as well, as its too susceptible to noise, which is the reason I wasn't getting the expected counts.
Last edit: Dave B 2016-04-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, got it working. Had to re-write the comparisons to make it work. Here's the working code:
#chip 12F683,4
InitTimer1 ExtOsc, PS1_1 'initialize timer1 for external clock and no prescale
dim pulsecount as word 'define pulse count variable
#define led GPIO.0 'set LED output pin
dir led out 'set LED pin for output
main:
'check for possible control pulse burst
cleartimer 1 'clear timer data
starttimer 1 'begin counting operation
wait 100 ms 'count pulses for 100 milli-seconds
'if possible burst detected, then branch off to control burst detection routine
if Timer1 > 1700 then
goto countpulses
end if
goto main
countpulses:
'Take longer count of incoming pulses
cleartimer 1 'clear timer data
startTimer 1 'begin counting pulses
wait 250 ms 'do so for 250 milli-seconds
stopTimer 1 'stop counting
pulsecount = timer1 * 4 'multiply pulse count by 4 to get actual count
'check to see if pulsecount
'reasonably matches one of the control tones;
'if it does, double-check it. If it still matches,
'turn LED either on or off
if 20100 > pulsecount and pulsecount > 19900 then
cleartimer 1 'clear timer data
startTimer 1 'begin counting pulses
wait 250 ms 'do so for 250 milli-seconds
stopTimer 1 'stop counting
pulsecount = timer1 * 4 'multiply pulse count by 4 to get actual count
if 20100 > pulsecount and pulsecount > 19900 then
set led on
end if
end if
if 18100 > pulsecount and pulsecount > 17900 then
cleartimer 1 'clear timer data
startTimer 1 'begin counting pulses
wait 250 ms 'do so for 250 milli-seconds
stopTimer 1 'stop counting
pulsecount = timer1 * 4 'multiply pulse count by 4 to get actual count
if 18100 > pulsecount and pulsecount > 17900 then
set led off
end if
end if
'return to checking for additional control pulse bursts
goto main
Many thanks to those who posted ideas.
Last edit: Dave B 2016-04-22
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm trying to figure out how to detect a particular frequency using the 12F683. I've been Googling for two days, and all I can seem to find is something about using some algorythm to check for presence of a tone in an audio signal. All I really need to do is figure out how to count the number of pulses coming in to a pin within a set time period, the rest I can handle in code. I know that the 16-bit timer can be used, I just haven't been able to figure out how (I am totally new to anything PIC escept for the PICAXE chips). The signals to be detected are 1-second 20-khz tones. I already have a circuit for squaring up the 20-khz tones to be able to use them as a TTL-level inputs for the PIC, I just need to find out how to code the PIC to make the count. I'm stuck. Any ideas about how to program the pic to make the count would be appreciated. If this topic has been covered (I looked but didn't see anything directly related), please post a link to the prior discussion. Thanks for all help given!
Last edit: Dave B 2016-04-16
Yes you could possibly do it with timer1. But I think it would be simpler and more reproducible using a tone decoder such as the LM567.
www.ti.com/lit/ds/symlink/lm567.pdf
While I appreciate the reply, it fails to shed light on my question. I'm aware of other detection possibilites (I've used the 567 for various tasks for about twenty-five years), but that's not my interest. My interest is programming the PIC to do the job. If you have ideas specifically related to counting pulses received within a specified time period, or links to previous discussions/code examples already posted, I would appreciate you posting them.
Last edit: Dave B 2016-04-16
OK, I think I have the beginnings of an idea. I'll post my thoughts and see what you guys think. I'll use timer 1 to count the pulses, since it is 16-bit and has the capacity, and since it can be run from an external clock source. I'll use either timer 0 or timer 2 to control the length of time that counter 1 counts. I havent gotten as far as figuring out how to code that idea, but if I understand the 12F683 spec sheet and the GCB documentation regarding timers, it should be possible. Any thoughts?
Edit: I found a PIC-based counter project at http://www.best-microcontroller-projects.com/pic-frequency-counter.html, but for a different PIC. Since I know nothing about programming in C, the files posted don't help me, but the project seems to supoprt my idea of using one timer to set the counting time, and the other to make the count. I also found a more generalized article at the same website regarding use of a PIC as a counter, here: http://www.best-microcontroller-projects.com/frequency-counter.html. Along with supporting the idea of using one timer to control the other, it also mentions using a delay routine as the control for the counting timer. Since that's the simpler solution, and since I don't need pinpoint accuracy, I'm leaning in that direction.
Last edit: Dave B 2016-04-16
The timer1 needs to be coordinated with the CCP1 module in the capture mode, to measure the positive/negative ttl pulse width. The CCP1 module will be toggled manually to capture the positive going edge (start TMR1) and then waiting to capture the negative going edge (stop TMR1).
Here is an example https://sourceforge.net/p/gcbasic/discussion/629990/thread/a02288e0/#2238 of capturing a single pulse. If you browse around in search for "pulse" then you may find the specific setup for the midrange PIC's.
Thanks very much! I posted an edit to my last reply which shows links to two possible solutions, as well.
OK, I think I have a workable solution to count pulses-per-second. I havent actually programmed a pic and tested it yet, but here's my code:
Again, hyper-accuracy is not important here. Any thoughts?
Last edit: Dave B 2016-04-16
Nope, at PS1_1 and clock of 4MHz then one tick of the clock is 1us for the PIC. A 500ms wait would be 500,000 ticks and an overflow condition would exist of the TMR1 word value of 65,535. A PS1_8 would have to be used so as not to overflow TMR1 with a 500ms wait.
I may not understand the timer initialization properly. Doesn't
InitTimer1 Ext, PS1_1
tell the timer to run off of an external oscillator (clock pulses into pin 2 of the 12F683 if I'm following the spec sheet properly, the clock pulse source being the train of pulses I wish to count), and specify 1:1 prescale (no prescale)? If true, wouldn't I then be able to count up to 2^16 pulses before a counter 1 overflow occurs? Since The pulse train won't exceed 20khz, and since the time of count is only 500mhz, then 10k pulses is about the max (roughly) count that should be reached. Am I correct, or am I misunderstanding something?Last edit: Dave B 2016-04-18
"wait 500 ms" is waiting 500 milliseconds, or 1/2 a second. Perhaps "wait 500 us" was the intention.
I may not understand the clock initialization properly. Doesn't InitTimer1 Ext, PS1_1 tell the clock to run off of an external oscillator into pin 2 of the 12F683 (if I'm following the spec sheet properly, the clock signal being the train of pulses I wish to count), and specify 1:1 prescale (no prescale)? If true, wouldn't I then be able to count up to 2^16 pulses before overflow? Since The pulse train won't exceed 20khz, and since the time of count is only 500mhz, then 10k pulses is about the max (roughly) count that should be reached. Am I correct, or am I misunderstanding something?Count pulses received with specific time periodSent from sourceforge.net because you indicated interest in https://sourceforge.net/p/gcbasic/discussion/629990/To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
No, 500 ms is intended. My question still stands about understanding the timer1 initialization clause, IE does "ExtOsc" mean it clocks off of pulses coming into the "T1CKI" pin of the 12F683? I just noticed my error where I typed 500mhz; that should read 500 ms, lol!
I'd appreciate input from other users, as well.
Last edit: Dave B 2016-04-18
I appologize, my mind is trapped in the past, totally ignored the external osc command. 10,000 ticks is correct over a 500ms period.
I used the HPWM command to get an approx. 20kHz signal source for the T1CKI input of the 12f683; And verified 97-98 ticks over a 5ms wait period. So, close enough without some manual tweeking.
Thanks for confirming. I tested the programming, but I must be missing something because I don't seem to get a count. To be more precise, I don't ever see the count I'm looking for, even though I'm making sure its there. I'll get back on it tomorrow....
Hi Dave,
Since you specifically asked about counting pulses (time consuming) to detect a specific frequency that's where I will go, except to say that there are better / faster methods and much better chips for the task as hand (such as the 12F1612 w/Signal Measurement Timer) .
There should be no problem at all with the 12F683 counting pulses over a period time to detect a specific frequency such as 20Khz.
With a 20KHz signal applied to T1CKI/Pin2, the code below shows a count of 2013 over a period of 100ms, and 20,128 over a period of 1 second.
Note that I have an I2C 4 x 20 LCD connected to display the data.
Thanks very much for posting, William! I agree that there are better PIC's for this, but I'm trying to work with what I have on hand at the moment. For now, its either the 12F683, or a small assortment of PICAXE chips. I have researched other methods, but I'm NOT good with math in general, and I don't know assembly, so that limits what I personally am able to implement. If I could find something already done in GCB that was easy to port/modify, I would likely lay out the cash for different PIC's.
I'm also limited by my programmer, currently a PIC K-150. It works great, but its chip compatibility list is limited.
Last edit: Dave B 2016-04-19
Hi Dave,
Modify the code I provided above as necessary and get it working in your App,
Then if you are truly interested in optimization, I will post some working example code for PIC16F16xx chips that have a dedicated Signal Measurement Timer (SMT).
This relatively new family of PIC;s, with Core Independant Peripherals (CIP's) opens up lots of new possibilities with signal measurement. Cost? < $2,00 each,
You will need a Pickit 3 to program these chips, or a $20 Curiousity Development Board
http://www.mouser.com/new/microchip/microchip-curiosity-dev-board/)
Edit: PIcaxe is a nice platform escpecally for beginners. However due to very high procesing overhead as a consequence of using interpreted basic, Picaxe Chips are not well suited for signal measurement duties at anywhere near 20KHz.
Last edit: William Roth 2016-04-20
I agree 100% on the PICAXE chips. I've used them extensively for many projects not requiring fast processing. As for this project, I've had to set it aside for now until I get more time. I need to modify my pulse shaper as well, as its too susceptible to noise, which is the reason I wasn't getting the expected counts.
Last edit: Dave B 2016-04-21
I just checked the 12F683 data sheet, and it shows no I2C module. How did you implement I2C with the 12F683???
Edit: Never mind, I finally noticed the implementation in the GCB docs. Overlooked that section before.
Last edit: Dave B 2016-04-21
Ok, got it working. Had to re-write the comparisons to make it work. Here's the working code:
Many thanks to those who posted ideas.
Last edit: Dave B 2016-04-22