User manual
mikroBasic PRO for PIC32
MikroElektronika
375
Library Example
The example changes PWM duty ratio on channels 1 and 2 continuously. If LEDs are connected to channels 1 and 2, 
a gradual change of emitted light will be noticeable
Copy Code To Clipboard 
program Pwm_Demo
dim current_duty, old_duty, current_duty1, old_duty1 as word
 pwm_period1, pwm_period2 as word
sub procedure InitMain()
 CHECON = 0x32
 AD1PCFG = 0xFFFF ‘ initialize AN pins as digital
 TRISB = 0xFFFF   ‘ congure PORTB pins as input
 PORTD = 0  ‘ set PORTD to 0
 TRISD = 0  ‘ designate PORTD pins as output
end sub
main:
 InitMain()
 current_duty = 100  ‘ initial value for current_duty
 current_duty1 = 100 ‘ initial value for current_duty1
 pwm_period1 = PWM_Init(5000 , 1, 1, 2)
 pwm_period2 = PWM_Init(10000, 2, 1, 3)
 PWM_Start(1)
 PWM_Start(2)
 PWM_Set_Duty(current_duty, 1) ‘ set current duty for PWM1
 PWM_Set_Duty(current_duty1, 2) ‘ set current duty for PWM2
 while (TRUE)  ‘ endless loop
 if RB0_bit = 1 then  ‘ button on RB0 pressed
 Delay_ms(1)
 current_duty = current_duty + 5 ‘ increment current_duty
 if (current_duty > pwm_period1) then ‘ if we increase current_duty greater then 
possible pwm_period1 value
 current_duty = 0  ‘ reset current_duty value to zero
  end if
 PWM_Set_Duty(current_duty, 1)  ‘ set newly acquired duty ratio
  end if
 if RB1_bit = 1 then  ‘ button on RB1 pressed
 Delay_ms(1)
 current_duty = current_duty - 5 ‘ decrement current_duty
  if (current_duty > pwm_period1) then ‘ if we decrease current_duty greater then 
possible pwm_period1 value (overow)
 current_duty = pwm_period1 ‘ set current_duty to max possible value
 end if
 PWM_Set_Duty(current_duty, 1)  ‘ set newly acquired duty ratio
 end if










