Ok first of all hi all and +2 for the website, it keeps me surprised every time i open it! (no i'm not kissing ass =P)
i've got a project with my team to build an rc submarine and since we have few channels on the remote but lots of things to control, we want to get the microcontroller to read the PWM signal from the rc reciever and use it to decide what stuff to turn on and off etc. I've included the code in assembler. The microcontroller is from the 8051 series, T89C51AC2 - we didn't pick it, it was just what we had from the equipment we bought for a lab course and we had lots of documentation on it so we just went with it.
We had successfully had the microcontroller produce a software PWM that matches exactly the characteristics of the signal the reciever gives and when we attach a servo to the breadboard and connect the white wire to the microcontroller output we get exactly what we want - the servo moves as expected.
however, when we try to read the input PWM and try to copy it at the output (for proof of concept), we get an output that is erratic and not at all a binary sequence, which is what would be expected if the microcontroller was just doing the math wrong. We've checked the output of the reciever (input to the micro) and its perfectly ok when its connected to the microcontroller. We've also tried stabilizing it by connecting the following circuit:http://mcu-programming.blogspot.com/2006/09/servo-motor-control.html
but although we know it does stabilize the waveform (we tried the successful PWM output and it was somewhat cleaner when we used this circuit) it did little to stabilize the input (we just did it in case it fixed the problem) and less to stabilize the erratic output PWM that was supposed to mirror the input.
we've clipped the code and tried each part seperately and we know it can produce a clean PWM. we also know that whatever else we're doing is not indirectly interfering with the software PWM. To be specific, we use Timer2 to count the full period, Timer1 to count the pulse width and we reload these with values from the PCA counter (unit 0) which, through interrupts and a bit of math, records the pulse width and period of the input. Using just timers 1 and 2 works great, even with the PCA IRQ code in place and running. Its just when we have the timers (timer1 to be exact) reload themselves with the values of the input PWM after these are calculated properly (since the input measures the time and we want them to overflow at FFFFh, we have to subtract from the latter to get a useful reload value for our timers).
What's really odd is that with a steady input we get a really unsteady output. In one case the width of the output pulses seems to almost oscillate rapidly from a small duty cycle to a large one. Also, the pulses at the output seem to be jagged, somewhat like the output voltage isnt steady. we're assuming that is caused both by the fast switching (though to be honest, its only about 50Hz on a 12Mhz crystal) and some instabilities that present themselves even with the good PWM output (there is a slight slope at the low bottom of the pulses, like something is charging slowly from 0 to 1 and gets interrupted early - we're using Visual Analyzer connected to a USB sound card and thats all we have for an oscilloscope - we've already been told we would have to switch to a sound card with a line-in input to get proper readings).
at other times the output was like a very subtle sine wave, with spikes where the pulse should start and end. This is very weird cause it seems like its a PWM but with only spikes to indicate the pulse's position. The period and pulse width change properly and the period and lengths are all exactly right. it's just that its not a pulse train but rather like the PWM integrated (since there is a positive spike where the pulse starts 0->1 and a negative spike where the pulse ends 1->0). The same code has managed to produce both kinds of output in our experience and we haven't linked it to anything yet. We've made sure no other part of the circuit (the rest of the stuff we have on the breadboard) is either connected directly or indirectly to the micro or the reciever or servo.
we cant figure out what's causing the erratic output - we've gone throug the code many many times and found some errors but nothing that fixed this.
the only other thing i can mention that might be of interest is that somehow through magic or something, the servo that we connected to the breadboard seems happy to respond directly to the recievers commands when the red and black wires are properly connected to the reciever (when it's on ofcourse - thankfully we can still apply natural laws to that...) and the white wire is connected to the output circuit of the microcontroller, when the microcontroller is without power, the circuit that stabilizes is connected to absolutely nothing and there is no power to the breadboard. however the servo won't run if any of the 3 wires is disconnected or is connected in any other way other than power and ground at the reciever's end and the white wire either on the microcontroller output (the non-powered microcontroller) or at 5 volts (when we power the breadboard). whenever its connected this way, it responds as if its normally connected to the reciever, directly.
sorry for the extremely long post, but we're pretty desperate - this project has been stabbing us left and right with bizarre issues since the beginning
thanks in advance