On Sat, 21 Apr 2001, Bob Blick wrote: > >I'm trying to write some code that takes two radio control servo inputs > (forward/reverse on one channel, left/right on the other) and mixes them > together to provide two PWM outputs suitable for driving FET H-bridges for > a small radio-controlled robot. > > >I suppose my question is: Is generating two PWMs whilst reading two inputs > too much for one 16F84 to cope with or am I looking in the wrong place for > the solution? > > It would require some clever coding to do it glitchlessly. Also adding > other features(like dealing with out-of-range pulses when the transmitter > is off) gets even trickier. Come on Bob, it's trivial :) - well the PWM of it is: http://www.dattalo.com/technical/software/pic/pwm8.asm That'll get you 8 simultaneous PWMs. Two PWMs can be handled with the same code (at that URL). I'd suggest looking at the code just to read the comments, but here's a portion of it pruned for two inputs: pwm_multiple CLRW ;Build the bit mask for turning ;off the PWM outputs. Assume that ;all of the outputs will be turned ;off. ; DECFSZ pwm0,F ;If the first counter has not reached 0 IORLW 00000001b ;then we don't want to turn it off. ; DECFSZ pwm1,F ;Same for the second one IORLW 00000010b ; ; ... ANDWF pwm_state,W ; Clear all of those pwm outputs ;that have reached zero. ; XORLW 11111111b ;Toggle the current state. INCFSZ rising_edge,F ;If the rising edge counter has not XORLW 11111111b ;rolled over then toggle them again. ;Double toggle == no effect. However, ;if the rising edge counter does roll ;over then a single toggle will turn ;the pwm bits on, unless of course the ;pwm counter has just rolled over too. ; MOVWF pwm_state ;Save the state MOVWF PWM_PORT ;update the outputs If you're driving an H-Bridge (and it's NOT Bob Blick's design) then you may want to have two outputs for each PWM; one for each leg of the bridge. A subtle modification of the constants in the above snippet can achieve that. Now, the hard part is calling this at a repeatable rate. You can write an isochronous loop or trigger on TMR0 overflow - it's your choice of the lesser of two evils. If you don't care if the PWM's run at constant frequency, then you can use phase accumulators. This is faster than the above code: movf PWM_duty_0,w addwf PWM_phase_0,f rlf pwm_state,f movf PWM_duty_1,w addwf PWM_phase_1,f rlf pwm_state,w andlw 00000011b movwf PWM_PORT This has the same requirement that it must be called at a constant rate. > > Do you need to use a 16F84? I've done exactly what you want in a 16F876 and > it also has current limiting to protect your h bridge, and am just now > adding temperature sensing for two motors and two h bridges. An '876 is definitely a better processor for this application. Scott -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu