[PYBOARD] CTCSS TONES generation

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

[PYBOARD] CTCSS TONES generation

Post by roland_vs » Thu Mar 15, 2018 3:05 pm

Dear all,

I would like to generate some odd frequencies with my PYBOARD like:

Code: Select all

CTCSS_FREQS =[67.0, 69.3, 71.9, 74.4, 77.0, 79.7, 82.5, 85.4, 88.5, 91.5, 94.8, 97.4, 100.0,
103.5,  107.2, 110.9, 114.8, 118.8, 123.0, 127.3, 131.8, 136.5, 141.3, 146.2, 151.4, 
        156.7, 162.2, 167.9, 173.8, 179.9, 186.2, 192.8, 203.5, 206.5,  210.7, 218.1, 225.7, 
        229.2, 233.6, 241.8, 250.3, 254.1]
These frequencies are used worldwide with HAM radio transceivers to have a receiving station block or unblock the incoming audio.

So far I use a pyb.Timer(1) and have a clock of 168MHz, prescaler at 168 and using a value for the period that is based on essentially 20* the selected CTCSS frequency. The output on a Pin outputs a square wave of the desired CTCSS frequency.

Code: Select all

t = pyb.Timer(1)
t.period(int(1000000/(20*CTCSS_FREQS[1]))
t.prescaler(168)

Is there a better way?

Regards,

Roland

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: [PYBOARD] CTCSS TONES generation

Post by dhylands » Thu Mar 15, 2018 5:36 pm

Keep in mind that the actual prescaler and period are 1 more than the value passed in. So if you want a timer tick rate of 1 MHz, then you would use a prescaler of 167.

If you're trying to get a callback called at a frequency of 69.3 Hz, then you would set the period to be 1/69.3 = 0.014430 seconds or 14430 clock ticks. You'd set the period to 14429 and then the callback would be called 69.3 times per seconds.

If you want your timer frequency output on a pin, then you need to get a channel involved. Pin Y6 is connected to Timer 1, channel 1N (the N means that the output is negated, which won't affect the frequency). I'd probably pick an output mode of Timer.OC_TOGGLE. To get the output signal to be 69.3 MHz, we want a full cycle of 14430 microseconds or a toggle period of 14430/2 = 7215 microseconds. For example:

Code: Select all

t1 = pyb.Timer(1, prescaler=167, period=7214)
t1ch1 = t1.channel(1, mode=pyb.Timer.OC_TOGGLE, pin=pyb.Pin('Y6'))
You could also just let MicroPython do the calculations for the period and prescaler.

Code: Select all

t1 = pyb.Timer(1, freq=69.3 * 2)
t1ch1 = t1.channel(1, mode=pyb.Timer.OC_TOGGLE, pin=pyb.Pin('Y6'))
When I tried, MicroPython picked a prescaler of 29 and period of 40403. (168000000 / 30) / 40404 = 138.6001386 (which is the toggle frequency) Dividing by 2, we get the full cycle frequency of 69.3000693 Hz.

In either, case I see this output on pin Y6:
Logic-69.3.png
Logic-69.3.png (42.21 KiB) Viewed 3216 times

User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

Re: [PYBOARD] CTCSS TONES generation

Post by roland_vs » Thu Mar 15, 2018 10:52 pm

@dhylands : again Dave thanks for the reply. I will try this. Indeed I realized that the prescaler was one higher.
I'm working on hooking up a PYBOARD to my transceiver to be able to access some radio repeaters over here.... and add some morse code for callsign identification.

Regards,

Roland

Post Reply