Need Help with Square Waves
lardom
Posts: 1,659
in Propeller 1
My goal is to generate a clean 38Khz square wave. I have a Parallax USB oscilloscope and as I increased the frequency the wave would no longer return to zero volts. I have screenshots at 250Hz, 2.22Khz and 6.57Khz. My question is can I solve the problem with a comparator or should I try to write this in PASM?
Comments
edit: just checked the specs but they only say "25Msps", so you may have a setting that lets you change the sample rate.
You have not said how you generate your square wave ?
Those scope traces look strange, as the rise time is much faster than the fall time. - rather like a Open-drain-P-FET would produce.
Do you have a CMOS (push pull) configured output ?
Yes, agreed. I just had a close look at it now and there is that strange output as if it were source drive with a large pull-down resistor.
@lardom - what's the output from the Prop pin itself? Are you using a counter or doing this in software. Either way, you should always post the software that you are using.
Really? For a 38kHz square wave that the Prop can generate in software from any of 8 cogs or in hardware from any of 16 counters?
AD9850 is good/great if you need Sine waves, but the OP asked for square waves, so the NCO mode of the Prop counters should be fine.
Precision of the Counter hardware is ~ 20 millihertz according to AN001.
If the OP is lucky enough to have a 5000001.14111MHz crystal, they will get exactly 38.0000kHz NCO.
ie the point is, the numeric precision, is better than generic crystal tolerance, by far.
@DigitalBob - I protested because you came in with a much more complicated solution when the op is struggling just to do a simple square wave. If we want to be of real help, we should keep it simple. Now if the op needed a very wide range of sine waves etc then DDS is one way to go. But no, anything close to 38kHz will do and the counters are a very simple solution. In fact you don't even have to do anything in particular in Tachyon, just type "26 APIN 38 KHZ" to make pin 26 use counter A set to 38kHz.
I pass a value to a global variable which represents the duty cycle: The code I used to make the screenshots was simple. The first thing I did was connect an LED to the output of an IR receiver and test with a TV remote. That worked fine so I tested my code. The results were inconsistent. I tried to inspect the waveform through my scope and it looked like a 'saw blade'. That's when I decided to ask for help.
In regards to the "pwm" software, you are making it too complicated and the solution is way simpler. You only need to set the counter to nco to output a frequency of 38kHz and just leave it running. All the cog has to do then is modulate it and one of the ways of doing that is simply turn the DIR bit on or off.
BTW, here is Tachyon enabling the counter for 38kHz and then I read the setting. The Prop is running at 96MHz.
You can also modulate it by driving the output high thereby overriding the pin and if it is connected to a PNP then it effectively turns it off. The other way is to directly disable and enable the counter through ctra.
"The other way is to directly disable and enable the counter through ctra."
That sounds like a brilliant idea. JonnyMac's code disables the counter. I'm trying to figure out how to set it up. "How is the duty cycle configured?"
Read AN001 about the Propeller counters.
All you need to do is set the pin value plus the ctr mode into the correct fields. There's plenty of Spin code examples around but you should figure out how to do it yourself, then it becomes so much easier.
edit: here is some Spin code to set the counter to a frequency (it seems to be much simpler in Tachyon )
2 to the 32nd power = 4,294,967,294. I'll need this to calculate FRQA. I'll use the PST to see what the code is doing, line by line. I'll get things working. Thanks.
The thread title specifically says square wave, but your question two posts back makes me wonder, Are the answers here addressing your real question? Also, what exactly are you trying to do?
The funky waveform--It is a separate puzzle. There is something wrong hardware wise with the waveform display, something wrong in the circuit or in the probe setup. If the probe is directly on the Prop pin, then I'd suspect that there is a diode or transistor that connects the Prop to a fat capacitor on the falling edge. Or, p1 is defective and has a high output impedance when sinking current. Try it on a different pin, not connected to anything outside.
My plan is to build an IR joystick controller. If I can do it I'll post a video and the code.
I also wanted to do something similar with 433Mhz modules but the signal is noisy and I'm not sure they're worth the effort. I'll experiment with a Schmitt trigger first, before I abandon the idea.
@DigitalBob, I may take a look at the AD9850 DDS in the future. If I ever get around to experimenting with induction motors a variable frequency drive is something I think I'd like to work on.
the waveform is clearly due to switching from low impedance high output to high impedance
floating (with the exponential sag due to 'scope probe resistance to ground).
Any time you see a signal like this, or reflected version of it you can be sure the signal is
being driven very asymmetrically, ie not by a push-pull output. Its a classic signature of
a open-collector bus signal, for instance (well the reflected version of this).
I have learned a ton of stuff in this thread. I appreciate everybody's input.
After considering what Tracy Allen suggested I have a sneaking suspicion that I have caused similar damage to some of my other boards. Motors are inductive loads. I really think I should start using optocouplers in my designs.
Walter
I've mentioned in the forums in the past that many of the older medical imaging devices are heavy on motors, brakes, low and high voltage systems, video etc. The newer diagrams tend to be too blockish to be as helpful, but the 90s into the 2000s usually have some detailed schematics you can rob for working designs for various project ideas. The GE AMX4 (google ge biomedical selfserve) will bring up lots of products with usable schematics.
I swapped the board instead of testing other pins because it suddenly struck me that maybe you were on to something. It was possible that I damaged the board and I wanted to know right away if I caused the problem. I got a square wave with the second board. When I woke up this morning it occurred to me that I didn't say thank you. Thanks, please forgive my oversight. I will test the pins on the first board and post an update. I hope your heath is improving.
I'm thinking that I have to isolate my controller from my motors. With my nRF24L01 project I used Schmartboard PCB's. (I like the 2" x 2" form factor). Twice the receiver PCB's became unreliable. Finally, I replaced the receiver PCB with a through-hole board and didn't 'see' any more problems. I never had any problems with the transmitter board.
@Peter Jakacki, I have a project in mind that will require two Propellers on the receiving side. I am hoping I can use opto-isolators to allow both Propellers to read data from the same nRF24L01. I have been working on a robotic arm that has five DC motors. I connected potentiometers to the pivot points so I could control the position of the arm. It used up five cogs. I'll need another Prop for the wheels and pan/tilt for a camera. I want the controller to mirror the position of the arm.
Mike R...
An alternative to 20mA is to encode the information as Servo-style pulse width. The Prop can generate and capture very precisely in time - much better than ADC precision.
For optos, the H11L1 is multi sourced, and is the cheapest digital/schmitt opto link I've seen.