i use this example to use PWM, every thing is ok but when i use low freq (1 to 20 Hz) the o/p is not correct.
any help to generate low freq PWM?
Code: Select all
program Pwm
dim current_duty1, old_duty1,pwm_period1 as word
sub procedure InitMain()
ANSELB = 0
ANSELD = 0
TRISB = 0xFFFF ' configure PORTB pins as input
PORTD = 0 ' set PORTD to 0
TRISD = 0 ' designate PORTD pins as output
end sub
main:
JTAGEN_bit = 0
InitMain()
current_duty1 = 100 ' initial value for current_duty
PPS_Mapping(_RPD0, _OUTPUT, _OC1)
pwm_period1 = PWM_Init(20000 , 1, 1, 2)
PWM_Start(1)
PWM_Set_Duty(current_duty1, 1) ' set current duty for PWM1
while (TRUE) ' endless loop
if RB12_bit = 0 then ' button on RB0 pressed
Delay_ms(1)
current_duty1 = current_duty1 + 5 ' increment current_duty
if (current_duty1 > pwm_period1) then ' if we increase current_duty greater then possible pwm_period1 value
current_duty1 = 0 ' reset current_duty value to zero
end if
PWM_Set_Duty(current_duty1, 1) ' set newly acquired duty ratio
end if
wend
end.