DIY 4 Phase Sinewave Generator for Turntable Motor Drive

Member
Joined 2005
Paid Member
I got microprocessors today so could finaly test it. Starts up properly byt the output looks like this. Any suggestions? Frequency seems fine also amplitude is ok but why do it look like this?
IMG_0063.jpeg

I also saw the clipping. Reducing the output level to 127 or lower cured the problem as found out by ForsBackaKalle. Thank you!
 
  • Like
Reactions: 1 user
The problem with 128 as an output level is that +128 = -128 modulo 256, or specifically that +128 is interpreted as -128, in standard 2's complement binary representations of integer values. That puts a big glitch at the top of the waveform.

Actually, this has nothing to do with it at all. The glitch happens when the PWM value is 255 not 128 and it is caused by a design flaw in the AT89LP51RB2 processor (now made by MicroChip). The original AT89C51RB2 (Atmel) does not have this problem. I've confirmed with MicroChip that this is indeed the case; they admitted that it is a well known defect, even though they have no app note or other service bulletin or any other documentation to explain it.

Version 1.06 of the SG4 firmware corrects for this by preventing PWM value 255 from occurring; the range of values is now 0-254. Previously, value 255 would occur when the attenuator value is 128.
 
  • Like
Reactions: 1 users
Thanks for the update Bill(Pyramid). Unfortunately it's getting difficult to source the original AT89C51RB2 chip so the flaw appeared when I started distributing the later chip. Bill has been kind enough to create a new version of the SG4, v1.06,
and new orders will come with this, but it's not worth swapping out the v1.05 as it's quite straightforward to set the maximum value manually to 127.
 
  • Like
Reactions: 1 user
Actually, this has nothing to do with it at all. The glitch happens when the PWM value is 255 not 128 and it is caused by a design flaw in the AT89LP51RB2 processor (now made by MicroChip). The original AT89C51RB2 (Atmel) does not have this problem. I've confirmed with MicroChip that this is indeed the case; they admitted that it is a well known defect, even though they have no app note or other service bulletin or any other documentation to explain it.

Version 1.06 of the SG4 firmware corrects for this by preventing PWM value 255 from occurring; the range of values is now 0-254. Previously, value 255 would occur when the attenuator value is 128.
Well there's no errata section in the AT89LP51RB2/C2 datasheet I found, and searching for "AT89LP51RB2 PWM fault" doesn't find any mention... So I stand by my diagnosis as a microcontroller with broken PWM would be unusable for most cases - the max output level needs to be clamped to 127 and this is a firmware issue only.

Though you say Microchip admit to this, just not officially? And noone's documented it online? Can't really believe that without documentary evidence.
 
Last edited:
Well Mark, you can double down on being wrong all you want, it still won't make you right. Contact me with your e-mail address via PM and I will send you copies of the e-mails from MicroChip (case #01331970) where they confirmed the problem exists just as I have described.

The output PWM on v1.06 goes from 0 to 254; it passes 128 without a problem. I confirmed the design flaw by manually stepping through all PWM values and measuring the output; the PWM value 255 does not work, all others do.

If you've read the datasheet you would know that the PWM register is an 8 bit unsigned value so your premise fails on it's own terms.

The value '128' is the attenuator value and not the PWM value. The attenuator value is used to scale the PWM output while keeping the center of the waveform at midpoint. On v1.05, an attenuator value of 128 would produce a waveform with a peak value of 255 which, according to Microchip, should be avoided because of the design flaw. V1.06 firmware prevents this by producing a waveform with a peak of 254 with attenuator value of 128 and the glitch does not occur.

Better luck next time.
 
Last edited:
  • Like
Reactions: 1 user
Because the PWM register is 8 bit unsigned; it produces variable duty cycle outputs for values between 0-255, although in the LP case, 255 is to be avoided, so 0-254. The filtered PWM isn't bipolar; it ranges between 0-5VDC for PWM values between 0-255 so a value of 128 is 2.5VDC (assuming a 5V supply).

MicroChip is apparently shipping a "C" version of this chip again; maybe that is in response to this problem. Limiting the PWM values to 254 and below wasn't fatal for my applications, but it may have been for so other apps.

I agree it is a major screw up. I use the same algorithm for a commercial application on which I contracted; we caught it before any production units shipped to the field, otherwise it could have been very expensive and embarrassing to clean up.
 
  • Like
Reactions: 1 user
Yes I know about PWM. -128--+128 cannot be offset to 8 bit anything, which is why 127 is normally the amplitude for a PWM signal biased to mid-rail, typically by adding 128 - though for the broken silicon that would have to be a 127 offset, so 0..254 rather than 1..255.
For motor control you'd usually want the maximum PWM range that works with the switching device (there is a minimum pulse width to consider), so sometimes that amplitude would be a bit less anyway.
Conversely with a halfbridge you might want separate PWM for low-side and high-side driver, with a small amount of dead-time between them, perhaps one signal is offset by 125 and the other by 130 so there's a constant deadtime between them - with an amplitude of 125 that means 0..250 and 5..255, again a problem for this chip.
 
OK, I see where you are coming from. The way that I'm scaling the output to the amplitude number works like this:

I have a sinewave table in ROM with two identical half waveforms, 0-179° and 180°-359° with values from 0-255 (y axis); I use the upper 8 bits of the phase accumulator as an index into the table so there are only 256 entries each representing a 1.40625° step (x axis). To scale the table values to the PWM output, multiply the table value by the amplitude value and divide by 256 then add to 128 (<180°) or subtract from 128 (>=180°). This will produce a variable P-P output centered at 128 with values in the range of 1-255 (PWM output). I use integer multiply and divide so any fractional part is discarded. This worked without problem on the "C" version chip, but I had to change the center offset to 127 for the "LP" version making the range of PWM output values 0-254.

128 works as the full scale amplitude value in this scenario; it will produce a P-P PWM output of 1-255 (128 center) or 0-254 (127 center).
PWM Table.jpg
 
For motor control you'd usually want the maximum PWM range that works with the switching device (there is a minimum pulse width to consider), so sometimes that amplitude would be a bit less anyway.
Conversely with a halfbridge you might want separate PWM for low-side and high-side driver, with a small amount of dead-time between them, perhaps one signal is offset by 125 and the other by 130 so there's a constant deadtime between them - with an amplitude of 125 that means 0..250 and 5..255, again a problem for this chip.

All of the PWM switching is done inside the chip and it is single ended; we are not using an H-Bridge. The PWM output is filtered by a 2 pole LPF to produce an analog sinewave of 0-5VPP max, assuming a 5V supply. Your comments about dead time are good info for other apps where that is appropriate.