The example shows the realization of continuously varying pulse width modulation (PWM). The continously modulated output is on the PORTE.0 pin which is monitored by a LED diode. The realization is carried out by using the mikroPascal compiler for dsPIC30F microcontrollers. Fig. 13-6 shows the electric diagram of the realized continuous PWM of a LED diode connected to PORTE.0.
Fig. 13-6 The electric diagram of the realized continuous PWM of a LED diode connected to PORTE.0.
program PWM; var i, duty_50 : word; begin ADPCFG := 0xFFFF; TRISE := 0; PORTB := 0xAAAA; TRISB := 0; Delay_ms(1000); duty_50 := Pwm_Mc_Init(5000,1,0x01,0); // Pwm_Mc_Init returns 50% of the duty i := duty_50; Pwm_Mc_Set_Duty(i, 1); Pwm_Mc_Start(); repeat begin i := i - 1; Pwm_Mc_Set_Duty(i,1); Delay_ms(1); if (i = 0) then i := duty_50 * 2 - 1; // Do not allow the overflow PORTB := i; end; until false; end.