PDA

View Full Version : Detecting certain tones



Dwayne
- 5th October 2004, 21:17
Hello folks,

What is the best way to detect certain tones with a PIC?

In RTTY, there are two tones... they are the following:
2295 2125 hz.

I would like the PIC to detect the tones and differentiate between the two.

One idea, is to count the time between the the two "RISES" of a cycle, and if it is between a certain "time" it is a certain frequency.

Another way would be to trigger with a transistor the stage of "positive" side of the wave, and time how long it stayed triggered. (ignoring the negative side of the wave).

What I would like, is using as few components as possible (hopefully none but the PIC) to decern the two frequencies so that I may decode RTTY.

Usage of code such as RCTIME could be used to measure the state of when a pin is positive. And this time could be addressed as a certain frequency.

Even PUSIN can be used similar to RCTIME.


I will be pulling the Tones directly out of the audio jack of any receiver.


I am looking for some suggestions that would likely be the easiest, componentless, and reliable way to accomplish my little fun project.



Dwayne

mister_e
- 6th October 2004, 03:37
Hi Dwayne,
there's a simple way to get it.

Calculate first the period or your frequency.

1/2295=435 uSec half period 217 uSec
1/2125=470 uSec half period 235 uSec

use PULSIN to measure the half of period of High state

PULSIN PORTA.0,1,HalfPeriod

HalfPeriod period is a 10 uSec increment (@4MHZ crystal) so give aprox
21 - 22 for 217 uSec
23 - 24 for 235 uSec

you can connect your input signal directly to capacitor (.47 uF or higher) in serie with 4.7K resistor directly to an input PORT pin. Be sure that your signal input have at least close to 3.5 volt peak of amplitude for great result.

Hope this help.

regards

Dwayne
- 7th October 2004, 16:16
Hello Mister_e,

M>>Be sure that your signal input have at least close to 3.5 volt peak of amplitude for great result.<<


Thanks Mister_e... I was kinda leaning in those directions, but was not sure which would be better. I did not know what the PULSIN would consider as "ON State" Since we have a sign wave, I did not know whether you had to a certain "voltage" for the PULSIN to be triggered as on (Other than the minimum operating voltage of the chip).

I am assuming the PULSIN measures the entire wave, from positive to positive, and the RCTIME measures only 1/2 of the wave. And the PULSIN will operate on the upslope of the sine wave, Is this correct?

Dwayne

Melanie
- 7th October 2004, 17:09
Be careful... RCTime and Pulsin operate on DIGITAL pins of the PIC. those pins will trip from low to High when the threshold voltage is reached. It's NOT going to trip the moment your Sine-wave goes high... so technically the ON period time will be considerably less than the OFF period time. The entire period (ON+OFF) will be correct for the frequency you're measuring, but the ON cycle (or OFF cycle) on it's own will bear absolutely no resemblence to what you expect, and worse, will vary depending on the input level. IMO this is one of those bum applications for a PIC. Better with an NE567!

Dwayne
- 7th October 2004, 19:17
Hello Melanie,


Melanie>>It's NOT going to trip the moment your Sine-wave goes high... so technically the ON period time will be considerably less than the OFF period time. <<

Ok, this is what I thought would happen. Thought if this would happen, I would use a transistor and "Tickle" the base of it, to "Turn on" the PICS probe threshold. (but I didn't really want to do that if I didn't have to).

Melanie>>The entire period (ON+OFF) will be correct for the frequency you're measuring, <<

Thus, you are saying that the PULSIN will read the correct frequency *only* if the wave meets the minumum voltage threshhold for the cycle you are reading. The RCTIME is worthless, because the timeing "ON" will probably be 1/4, 1/8, or even smaller than the timing "OFF" of the RCTIME.

Sounds to me, the PULSIN is the way to go, if I wanted to attempt to proceed with the PIC chip. If that doesn't work, I guess i will play around with the NE567.

Thanks a Million Melanie.

Dwayne

mister_e
- 7th October 2004, 19:50
Absolutely right Melanie,
Dwayne use Transisor, op amp, voltage comparator will not resolve the problem. But in this case what i think... never test but...

do 2 Pulsin reading one for state 1 and 1 for state 0 will give you the entire period.

Pulsin SignalInput,1,TimeHigh
Pulsin SignalInput,0,TimeLow
EntirePeriod=TimeHigh+TimeLow

Results may vary a few depending the speed of intruction execution or triger detection. Why not read a few time (5-10 times) EntirePeriod and do the average of them. Send results to an LCD to see what it gives to you. try to vary the input amplitude too and see how different they are.

Of course you can do the same job with two LM567. But as you request less component as possible. But for time consuming ...

keep us inform

regards

Dwayne
- 7th October 2004, 19:53
Hello Mister_e,


Mister_e>>What i suggest is to take more than 1 reading (why not 10) and do the average of them. Send your result to Lcd and see what happen and how different they are. <<

this may be the way to go. Take about 10 readings, or enough readings to where a "WORD" would not be overloaded.

for x=1 to 10;
Pulsein pin1, 1,Reading;
Accu=Accu+Reading;
next x;
Accu=Accu/10;


if(abs(Accu-22.5)>4) then NoTone
if(Abs(Accu-21.5)<=1) then LowTone
if(Abs(Accu-23.5)<=1) then HighTone


Or possibly leaving it as a larger number with bigger spreads:

for x=1 to 10;
Pulsein pin1, 1,Reading;
Accu=Accu+Reading;
next x;

if(abs(Accu-225)>40) then NoTone
if(Abs(Accu-215)<=10) then LowTone
if(Abs(Accu-235)<=10) then HighTone


Mister_e>>Now for the amplitude problem... in fact it will be a problem if your signal amplitude vary due to the threshold of the PIC input. a voltage comparator or OP AMP to clip the signal will not solve the problem. for the reasons that Melanie already said. But experiment this and let me know.<<

I will try your capacitor thing and see what happens.

the next thing I must find out, is how the PBP handles the switch/case statements. After it finds the case statement that workds, does it jump to the end switch? Or does it continue processing the rest of the case statments to see if another case statement is going to be used. I am goin to have a rather large switch case statement, and "timing" is goin to be critical. I guess Idon't want the compiler to jump to the end switch after the first case statement, and go through a million others before the next case statement is found. <g>. That can throw timing off... Maybe I am being too picky <g>.

Dwayne

mister_e
- 7th October 2004, 20:08
Hi dwayne,
of course you can use bigger spread number. the average calculation is an option and take few Word more + uSec of execution.


Dwayne >> the next thing I must find out, is how the PBP handles the switch/case statements. After it finds the case statement that workds, does it jump to the end switch?

answer is yes.

regards.

Dwayne
- 7th October 2004, 20:13
Hello Mister_e,

M>>Dwayne use Transisor, op amp, voltage comparator will not resolve the problem. But in this case what i think... never test but...

do 2 Pulsin reading one for state 1 and 1 for state 0 will give you the entire period.

Pulsin SignalInput,1,TimeHigh
Pulsin SignalInput,0,TimeLow
EntirePeriod=TimeHigh+TimeLow
<<

If i am reading correctly Mister_e, PULSIN *is* the full cycle of the wave, and RCTIME is 1/2 the cycle.

The problem I think Melanie was pointing out, is the threshold on the pin.

Lets say the threshold is 2 volts. Lets say your wave is 2.1 volts.... that means PULSIN *will* get it on the rise, but it will be at the very *tip* of the wave.. Most of the wave is ignored.

If the sine wave is 5 volts, it will activate less than 1/2 the way up the wave...a different location on the wave than the 2.1 volt wave. RCTIME will give extreme different measurements, but PULSIN should give the same. (unless I am misunderstanding something... which could possibly be the case!)

Dwayne

mister_e
- 7th October 2004, 20:46
Hi Dwayne,
after experimenting with sine wave, i can tell you that Pulsin gives only the ON Time and OFF Time not the full period. RCtime will gives you the time it take to go to ON to OFF or OFF to ON.

so the last solution i post give the whole period. It's an interesting solution when amplitude vary. Test it. I Tell you... THAT'S the solution.

regards

Dwayne
- 7th October 2004, 21:17
Hello Mister_e,

Mister_e>>so the last solution i post give the whole period. It's an interesting solution when amplitude vary. Test it. I Tell you... THAT'S the solution.<<

It sounds like I am better to write my own code for the pin...
somthing like the following:


Maxcounter var word
Frequence var word


Maxcounter=1000
Fequency=0

Loop:
if pin=low, goto loop

while pin=High and Maxcounter>0
Frequency=Frequency+1
Maxcounter=Maxcounter-1
endwhile

while pin=low and Maxcounter>0
Frequency=Frequency+1
Maxcounter-Maxcounter-1
endwhile

if Maxcounter<0 goto badsignal

If abs Frequency-range <tolerance than printout frequency...
etc etc.


Any suggestions or ideas?

Dwayne

mister_e
- 7th October 2004, 21:38
This one is working, already tested.

start:
; 2295 hz=435.7 us or 43-44 pulsin
; 2125 hz 470.5 us or 47-48 pulsin


pulsin bprevious,0,b0
pulsin bprevious,1,b1
b2=b0+b1

if (b2==43) or (b2==44) then
lcdout $fe,1,"2295 Hz detect"
pause 1000
goto start
endif

if (b2==47) or (b2==48) then
lcdout $fe,1,"2125 Hz detect"
pause 1000
goto start
endif
lcdout $fe,1, "waiting for tones"
pause 100
goto start


Easy huh!!! now you can modify for your application.

regards

Dwayne
- 7th October 2004, 22:03
Hello Mister_e,

Mister_e>>This one is working, already tested.

start:
; 2295 hz=435.7 us or 43-44 pulsin
; 2125 hz 470.5 us or 47-48 pulsin


pulsin bprevious,0,b0
pulsin bprevious,1,b1
b2=b0+b1

if (b2==43) or (b2==44) then
lcdout $fe,1,"2295 Hz detect"
pause 1000
goto start
endif

if (b2==47) or (b2==48) then
lcdout $fe,1,"2125 Hz detect"
pause 1000
goto start
endif
lcdout $fe,1, "waiting for tones"
pause 100
goto start
<<

Thank you very much. Am i assumin this is a 8MHZ clock? because I thought the values were 1/2 of these.

I wrote a similar one <g>. Not tested though. I wrote another thread on a question with PULSIN / RECTIME... I am curious what the real difference is between them. I realize you gave me a explanation, but I read your explaination as "the cup is 1/2 full, not 1/2 empy".... Thus I could't differentiate the two apart...yet the manual says RCTIME is about 1/2 of the PULSIN

Dwayne

mister_e
- 7th October 2004, 22:24
Hi Dwayne,
no i test with an 4 MHZ. If you use an 8 MHZ clock results will maybe double. i didn't test but in the manual they says : 4MHZ return 10uSec increment and 20MHZ 2uSec increment. So i figure that 8Mhz will be close to 5Usec increment.

since i never use Rctime, as i see RCtime can be use to read charge and discharge of capacitor. Not really 1/2 Pulsin in your case.

see this :
http://stage.itp.tsoa.nyu.edu/~tigoe/pcomp/code/archives/000494.shtml

and this is an other definition of RCtime

http://www.mikroelektronika.co.yu/english/product/books/picbasicbook/04_03.htm

regards