PIC Microcontoller IO Routine

Pulse Width Modulation

From: Scott Dattalo

;------------------------------------------------------------
;pwm_multiple
;
;  The purpose of this routine is to generate 8 pulse width
;modulated waveforms. The algorithm consists of 9 counters
;that change the state of the pwm bits whenever they roll over.
;One of the counters, rising_edge, will drive the pwm bits
;high when it rolls over. The other 8 counters pwm0-pwm7 will
;drive their corresponding bits low when they roll over.
;
;
;RAM:
; pwm0-pwm7 - pwm counters
; rising_edge - rising edge counter
; pwm_state - current state of the pwm outputs.
;ROM
; 23 instructions
;Execution time
; 23 cycles

pwm_multiple

	CLR	W	;Build the bit mask for turning
                                ;off the PWM outputs. Assume that
                                ;all of the outputs will be turned
                                ;off.
                                ;
	DECSZ	pwm0	;If the first counter has not reached 0
	OR	W, #00000001b	;then we don't want to turn it off.
                                ;
	DECSZ	pwm1	;Same for the second one
	OR	W, #00000010b	;
                                ;
	DECSZ	pwm2	;and so on...
	OR	W, #00000100b	;
                                ;
	DECSZ	pwm3	;
	OR	W, #00001000b	;
                                ;
	DECSZ	pwm4	;
	OR	W, #00010000b	;
                                ;
	DECSZ	pwm5	;
	OR	W, #00100000b	;
                                ;
	DECSZ	pwm6	;
	OR	W, #01000000b	;
                                ;
	DECSZ	pwm7	;
	OR	W, #10000000b	;
                                ;
                                ;
	AND	W, pwm_state	; Clear all of those pwm outputs
                                ;that have reached zero.
                                ;
	XOR	W, #11111111b	;Toggle the current state.
	INCSZ	rising_edge	;If the rising edge counter has not
	XOR	W, #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.
                                ;
	MOV	pwm_state, W	;Save the state
	MOV	PWM_PORT, W	;update the outputs



Now this routine has been optimized for speed. It would be difficult (but
not impossible) to modify it to: 1) change the duty cycle on the fly and
2) have different frequencies for each pwm.

One technique is to maintain two counters for each pwm. Call them say,
rising_edge_counter and falling_edge_counter. Make the counters the same
size and do something like this:

  if(--rising_edge_counter == 0) {
    turn_PWM_on();
    rising_edge_counter = PWM_PERIOD;
  }

  if(--falling_edge_counter == 0) {
    turn_PWM_off();
    falling_edge_counter = PWM_PERIOD;
  }

In other words, every time the rising edge counter rolls over, you make
the PWM output high and when the falling edge counter rolls over you make
it low.

Now, to get duty cycle, you simply adjust the phase between the counters:

set_duty_cycle(int new_duty_cyle)
{

  falling_edge_counter = rising_edge_counter + new_duty_cyle;
  if(falling_edge_counter > PWM_PERIOD)
    falling_edge_counter -= PWM_PERIOD;
}

Now this routine assumes that the new_duty_cycle is less than the
PWM_PERIOD.

Scott

See also: