PWM Pseudo Code 40,000 ticks per mS assumes a 40Mhz clock, we will use SysClk/32 for PWM Set 200 Hz frequency so 5mS for the Motor H bridge period Set 20 mS for the Servo period Set 200 Hz frequency so 5mS for the Motor Shooter period Use PWM module 0 for this demo and program it for up/down counting InitPWM() Use volatile to avoid over-optimization Start by enabling the clock to the PWM Module (PWM0) Enable the clock to Port B,E Select the PWM clock as System Clock/32 Make sure that the PWM module clock has gotten going Disable the PWM0 and PWM1 while initializing Program generator0 A to go to 0 at rising compare A, 1 on falling compare A Program generator0 B to go to 0 at rising compare B, 1 on falling compare B PWM0 program generator1 A to go to 0 at rising compare A, 1 on falling compare A PWM0 program generator2 A to go to 0 at rising compare A, 1 on falling compare A Set the PWM period. Since we are counting both up & down, we initialize The load register to 1/2 the desired total period Initialize three generators¡¯ periods(Generator 0,1,2) separately Set the initial Duty cycle on A to 50% by programming the compare value to 1/2 the period to count up (or down) for all three generators separately Set the initial Duty cycle on Generator 0, B to 25% by programming the compare value to 1/4 the period Set changes to the PWM output Enables to be locally syncronized to a zero count Enable the PWM outputs Configure the Port B pins to be PWM outputs Start by selecting the alternate function for PB4, 6 & 7 and PE4 Choose to map PWM to those pins, this is a mux value of 4 that we want to use for specifying the function on bits 4, 6 & 7 Enable pins 4,6 & 7 on Port B and Port E 4 for digital I/O Make pins 4,6 & 7 on Port B and Port E 4 into outputs Set the up/down count mode and enable the PWM generator Set0_DC_L()/Set0_DC_R()/Set0_DC_Shooter()/Set0_DC_Servo()/ When duty cycle is 0 Set specific pin to be low and disable the pin Set100_DC_L()/Set100_DC_R()/Set100_DC_Shooter()/Set100_DC_Servo()/ When duty cycle is 100 Set compare value to the full load value SetDutyCycle_X( uint8_t value ) //Here X can be L, R, Shooter, Servo If value = 0 Set 0_DC_X() If value = 100 Set100_DC_X() Else Set compare period to (value/100)*full load period value