John Payson wrote: > [4] One style of "PWM" that I've not seen implemented in hardware > even though a hardware implementation would be easy works > as follows [the MSB of the output value is bit -1; the LSB > is bit -n] > Code to implement this looks like this [assuming that "count" > runs from 0 to 255, incremented somewhere else] > > decf count,w > xorlw 255 > andwf count,w ; Note: only one bit in W will be set! > > andwf value,w > ; Z flag now indicates desired PWM state After much thought and a bit of fiddling, I'm not sure I understand this. I've whipped up a FORTH algorithm that expresses the current PWM value by displaying a series of asterisks or spaces ... but the output I see suggests either that I've misunderstood the algorithm or misunderstood the concept. I agree that the DECF followed by XORLW and ANDWF will yield only one bit set in the result. Here is the FORTH rendition of the PIC code above ... variable value : test 255 0 do ( vary count 0 to 254 ) i 1- ( decf count,w ) 255 xor ( xorlw 255 ) i and ( andwf count,w ) value @ and ( andwf value,w ) 255 and ( force to 8 bits ) 0= if ." *" else space then ( test Z flag ) loop ; And here is the output I get with values ranging from 0 to 4. 0 value ! test ******************************************************************************** ******************************************************************************** ******************************************************************************** *************** 1 value ! test * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 value ! test ** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 3 value ! test * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 value ! test **** ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ** -- James Cameron (cameron@stl.dec.com) Digital Equipment Corporation (Australia) Pty. Ltd. A.C.N. 000 446 800