Reset with correct tag DOH! Forwarded from Scott Dattalo > Mike, > > I'm currently not subscribed to the PIC list, but I browse the archives > once a week or so. I saw your message and spent a moment to think of a > very fast way to do PWM's: > > PWM_WIDTH EQU 3 ; 3-bit wide pwm's > movlw 1<<(8-PWM_WIDTH) ;The "lsb" of the PWM counter addwf pwmcount,f ;Clear the outputs when skpnc ;the master counter rolls clrf outport,f ;over > ; addwf pwmset1,f ; PWM # 1 rlf temp,f ;Set the output when it > ;rolls over addwf pwmset2,f ; PWM # 2 rlf temp,w ; andlw 0x03 iorwf output,f > The way it works is that the upper three bits of an 8-bit register are > allocated for the PWM's. These bits are "incremented" by adding 0x20 to > the 8-bit register. When the pwm rolls over, then the carry gets set and > the shadow register picks this up with the rlf. > > Now to adjust the PWM you only need to change the relative counts between > the pwmset's and the pwmcount. > > > One of the benefits with this approach is that it can scale quite well to > 4, 5 or whatever number of bits. Or if you prefer, a non-2^N rollover rate > can be accomodated with phase accumulators. (For example, you can load W > with say 5 and roll over about every 51 iterations). > > ----- > > If you only need 8-bits, then you might consider putting the 8 states into > one register. E.g.: > > two_to_the_N_minus_one: > andlw 7 > addwf pcl,f > retlw 1 > retlw 3 > retlw 7 > retlw 0xf > retlw 0x1f > retlw 0x3f > retlw 0x7f > retlw 0xff > > > start: > movf pw1,w > call two_to_the_N_minus_one > movwf pwmset1 > > > ; etc for N PWM's > > loop > > rlf pwmset1,w > rlf pwmset1,f > rlf output,f > > ; etc for N PWM's > > goto loop > > > Scott > > PS. Feel free to post this to the list if you want. > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads