This is a multi-part message in MIME format. ------=_NextPart_000_00DA_01CAA671.4D21DAA0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit Joe, Try a Microchip search for PWM in the application notes http://search.microchip.com/searchapp/searchhome.aspx?q=pwm&resperpage=10&id=26 For example http://ww1.microchip.com/downloads/en/AppNotes/00564b.pdf and (not personally viewed) http://ww1.microchip.com/downloads/en/AppNotes/01050a.pdf http://ww1.microchip.com/downloads/en/AppNotes/00654a.pdf http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf This code does do what is intended at the PIClist link. I've included some of the original text. Attached is what comes out of C2 with various values for duty (500, 100 and 900) and either /4 or /16 pre-scaler on TMR2 Note that the first crystal I grabbed was a 4.096MHz, so the frequency is slightly more than 1000Hz (ie 1000 * 4.096/4.000 or 0.976ms period). I used a 452, which works just like the 4520 wbr =========================== Definition - duty = d500 d_lo = low(duty) & b'00000011' d_hi = high(duty << 6) ;d_lo = two lowest bits need for the duty cycle 'on' time ;d_hi = highest 8 bits. It's left-shifted 6 times to get it into the duty ;high byte at assembly time ;If duty = 500 => 0111 1101 00 ;This needs splitting into 0111 1101 and 00, not 01 and 1111 0100, ;which would happen if you used high(duty) and low(duty), ;therefore ; the left shifts ======================= ;Set the PWM period (1000Hz = 0.001s) by writing to the PR2 register. ;PWM Period = [PR2 + 1]*4*Tosc*TMR2 PreScale value ;Use 4 for TMR2 prescale. You can try other values but make sure it ;works with the duty cycle calcs as well. ; ;Tosc = 1/4000000 = 2.5*10-7 ;Rearranging the equation gives... ; ;PR2 = (Period/(4 * Tosc * TMR2 Prescale)) - 1 ;Therefore... ; ;PR2 = (0.001/(4 * 2.5 * 10-7 * 4)) - 1 = 249 movlw .249 ;TMR2 period register movwf pr2 movlw d_hi ;set duty cycle bits 9-2 movwf ccpr1l movlw d_lo ;put duty cycle bits 1-0 swapf wreg iorwf ccp1con ;into bits 5-4 of CCP1CON movlw b'00000101' ; 1 ;TMR2 on ; 01 ;pre-scaler = 1:4 movwf t2con movf ccp1con,w andlw b'00110000' iorlw b'00001111' ;enable PWM mode movwf ccp1con here bra here ------=_NextPart_000_00DA_01CAA671.4D21DAA0 Content-Type: image/gif; name="duty_examples.gif" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="duty_examples.gif" R0lGODlhkwIDAbMAAAAAAAAAhAAA/wCEAACEhAD/AAD//3NrY4QAALVNTa2tnP8A/8bGxu/v3v// AP///ywAAAAAkwIDAQAE/vDJSau9OOvNu/9gKI5kaZ5oqq5s675wLM9tY994ru987//AoHBILBqP yKRyyWw6n9CodEqtWq/YrHbL7Xq/4LB4TC6bz+i0es1uu9/wuFx6qNvvV4R+z58rB4CBgn5JBIaH iISKi4yNjjwOkZKSdVZ8l3qPQQWcnZ2AmkAGo6SkhqGoqaqrWpOulVWYl6w6nragtDilu6e5vr/A wTYOAMXFlAeWxst7wZ3Fnri/pMYGAAa9wtrb3ISRAJLgDrBUCAB65+bNwJ4A0QPBo9fy19nd9/j5 ZAsLAPz8/hIksLQnXR92nNy54yTNl7Vd9Qjom0ix4hWA//otEEgQHSZn/gkLLCzQMNfDXdgkWlzJ sqURjP8CDoxV8CNCkTgZwgN2kpdKl0CDCm0AEyNHmh5nIVy2sCStnqXsDZ1KFV9Rfxtnlqup9FfI r05ZySsVsarZs9oEWBAgIECAjsa6+hIJzRjJndPoFRslFa3fv5rYCh7stqMsBCBt6YyHku9PwJAj KxosuHCew9oU39XWuK/kz6DZULaMRRa3W3iF+QzNuvUawV7W3euUj5Tr27jNCACDWF+BiQZyCx/u ZfeX3vl+6wtOvLlzKsZjT1Re+7n169iza98+XIH37+DDix9Pvrz58+jTq1/Pvr379/Djy59Pv779 +/jz69/Pv7////nd/iHggAQWaOCBCCao4IIMNujggxBGKOGEFFZo4YUYZqjhhhx26OGHIILI1Igk lmjiiSaag+KKLLbo4osr0gXjjDTWOKM1Nuao44489ujjj0AGKeSQRBZp5JEoqojkkjDKyOSTK+II 5ZRUVmnllVhmqeW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ffc3rAiXzR4//ePL+4j Qu7hdPKSXzBsW8CLX++wLzaHX8CNTOqCnudA7Oilr/RhjzZjD/NKrvoNnvYq/+80n1ahT1ldH+Oq 3/sv0i5IX8HCP9+q/vWVD/XzmfC3tfy6P/ns7vyd9PsoH/wvP/7V/i/z18/2QZX8kzX6lO/9js8U qA9O02/6TGHxaX5e7I/97OT+9g8BQD5Z7cVZb969prTlI0vz5EJMRVv3tdgLgWtbIm4712G+BwZL ColBeLTJKkpkk8SUjJzTEhRKxcY2tCzy1818wRfx2PxRKB7Gc6V8fMTlc1ybjJznH1LL2+x30aPj AAQr/BCU4/o4tHN8hIzsUMhZbGvEwqTSBCrA4HQCbRL1amJjlExVXU2lBLA8Iz2SFaKF8byDtAXa 7enlOPX4ZSUuNp6UgD1eZtbAbYaOLpIOixzeoaYies3uNn72DpcM7r6uMfcRP9pWVncfA3+X7yLP Rn+5d8lXd203FLQGCO9TwEf7UBgEUG8DwnkNX0QAADs= ------=_NextPart_000_00DA_01CAA671.4D21DAA0 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist ------=_NextPart_000_00DA_01CAA671.4D21DAA0--