picaxe 1khz squarewave generation

peter howarth

New Member
can any of the picaxes be programmed to give any squarewave,50% duty cycle, at any frequency between 500 hz and 2500 hz?can anyone give any examples please.
 

AllyCat

Senior Member
Hi,

It depends how you define "any frequency" and how you want to set that frequency, but probably the easiest way is with one of the PWM outputs, which are available for most PICaxes. Not tested on hardware, but the following should sweep up from 500 Hz to 2.5 kHz in steps, on pin c.2 of an 08M2. You can use the "PWM Wizard" in the PE to find the values for other PICaxes and frequencies.
Code:
#picaxe 08m2
#no_data
setfreq m8
symbol opin = c.2		; Only pin for an 08m2
symbol pulswid = w1
symbol period = b0
   for pulswid = 500  to 100 step -2
		period = pulswid / 2 - 1
		pwmout pwmdiv16, opin, period, pulswid
		pause 2000    ; Step once per second
	next
Or you might write a "program" to do it for almost any PICaxe, for example:
Code:
setfreq m32				         ; 1 PIC instruction = 0.125 us, pulses at 2kHz = 250 us = 2000 ICs 
	for w1 = 4800 to 0 step -10	  ; min loop =~2000 PIC instructions max = 8000
		time = 0
		do
			toggle opin		; Any output port.pin
			pauseus w1
		loop while time = 0
	next
Again untested and I estimate the above will probably only go to about 2.0 kHz with an M2. For a higher frequency, use an X2 or change the loop to contain a HIGH opin and LOW opin, but then the duty cycle won 't be exactly 50%.

Cheers, Alan.
 

bag57pipe

New Member
The all picaxe are quite capable of producing frequencies and duty cycle you specify either in software or using pwmout, it just depends on how critical you're application is and how much accuracy you require, using pwmout.
500Hz & 32mHz axe clock pwmout pwmdiv64, B.1, 249, 500
2.5kHz & 32mHz axe clock pwmout pwmdiv16, B.1, 199, 400
The same could be done in software toggling a pin with the same on and off time.
The fly in the ointment is the time software takes to execute causing delays and the pwmout command may not produce the exact frequency you require if you require a particular range of frequencies within your'e specified limits. What is the project you have in mind?
 

peter howarth

New Member
The all picaxe are quite capable of producing frequencies and duty cycle you specify either in software or using pwmout, it just depends on how critical you're application is and how much accuracy you require, using pwmout.
500Hz & 32mHz axe clock pwmout pwmdiv64, B.1, 249, 500
2.5kHz & 32mHz axe clock pwmout pwmdiv16, B.1, 199, 400
The same could be done in software toggling a pin with the same on and off time.
The fly in the ointment is the time software takes to execute causing delays and the pwmout command may not produce the exact frequency you require if you require a particular range of frequencies within your'e specified limits. What is the project you have in mind?
just want to design square wave frequencies,50% duty cycle/symettrical,in the audio spectrum say 500 hz to 2500 hz, then just convert the squarewaves to sinewaves using passive capacitor resistor network, so that i can then decode using ne567 tone decoders
 

AllyCat

Senior Member
Hi,

Hmm, why Tone Decoders which are very "Old Technology"? Almost every modern system of control/data transfer would use some form of serial data stream.

Also, IMHO generating the square wave is the "Easy Part"; to convert it to anything representing a sine wave over that frequency range will need multiple/tuned and/or variable frequency filters. A 500 Hz square wave has strong harmonics at 1.5 kHz and 2.5 kHz, so I believe when tone decoders were the only practical solution, it was common to limit the frequency range to one octave (e.g. 1 kHz - 2 kHz) to avoid issues with harmonics/distortion. Simple low pass R-C filters don't do much to the close harmonics, so a "tuned" filter might be more appropriate, I vaguely remember using a Wein Bridge.

Personally, if trying to generate a simple variable frequency "Sine Wave", I'd start with a sawtooth (triangular) wave (or derive it from a square wave by integration). Then you can "crush" the peaks with a non-linear device, for example back-to-back diodes, to give a reasonable representation of a sine wave, and then maybe "tidy it up" with an analogue filter.

Cheers, Alan.
 

erco

Senior Member
Are you sure you need a sine wave input to a 567? I've used square waves with fine results, never worried about waveform too much. I didn't find much in a quick Google except this post near the bottom at https://www.electro-tech-online.com/threads/lm567-vs-lm567cn.146094/page-2 Sounds like the guy had better results with a square wave.

On My Test Circuit, Putting in a Sine Wave, It Triggers at Two different Frequencies.
It is set for 1100 Hz, But also triggers at 350 Hz.
I Tested this with Both the Square and Sine wave Signals between 0 and 20 KHz.
It Only Triggers at 1100 With the Square wave.
 

erco

Senior Member
Speaking of 567 chips, here's a link to a 2004 vintage robot kit with full instructions and schematics. You could duplicate the robot with this info. Several 567s are used to make IR reflectance sensors. Each 567 is tuned to a different frequency and both generates the IR signal and detects its reflection... clever! https://www2.cose.isu.edu/~perealba/courses/OctoBot_Instructions_1.2.pdf Worth downloading & saving for future reference before it disappears. 567 circuit portion only attached here.
 

Attachments

Last edited:

bag57pipe

New Member
Peter tone decoding in a digital world seems old hat, but in the right place it has it's advantages, what is it you are building? If you are using say less than six tones the tolerances would be pretty you wide could skip the NE567 and go for using a charge pump/pulse counter to convert frequency to voltage with either comparators or a picaxe to convert the voltages to discrete outputs. I was able to decode 50 baud RTTY using this method though it was fairly hard work for the picaxe! There is a fable about a bunch of university graduates were asked in their job interview to produce something like a 4mHz signal source. The graduates got down to it and produced rubidium/caesium stabilised, phase lock loop, microprocessor controlled circuits, the job when the bloke who built a colpitts oscillator!
 

bag57pipe

New Member
Oh forgot to mention on the plus side a pulse counter/discriminator/ratemeter call it what you like only works with a good solid square pulse! If you are going to feed a square or triangular waveform to a low pass filter/integrator to get a sine wave the amplitude of the sin(ish) wave decreases with frequency which is not what you want if you are going over say an RF link. The DAC on the picaxe could be used to produce a rough outline of a sine and then filtered but at full tilt the picaxe would be taxed to produce frequencies above a few hundred Hz. That said a sine approximated with only say eight steps would probably give a better approximation than direct filtering of a square wave.
 
Top