Is it possible to.
Output a pulse train that is spread out evenly in one cycle and is equal to the duration of a PWM cycle.
' the pwm frequency (in Hz)
' high value -> fast refresh, more CPU cycles
' low value -> slow refresh, less CPU cycles (may flicker)
private const _pwm_frequency = 120
' now calculate the timer reload value, given the pwm frequency
private const _fosc as single = _clock * 1000000 / 2 ' instructions per second
private const _update_frequency = _pwm_frequency * 256 ' actual frequency (pwm * steps)
private const _reloadTimerValue as ushort = _fosc / _update_frequency ' reload value
' this event handler is triggered when a timer interval reaches zero...
dim pwmDutyCycle as byte = 0
dim pwmPin as D3
dim pwmCounter as byte = 0
private Interrupt OnTimer(Pic.T1Interrupt)
if pwmCounter = 0 then
high(pwmPin)
elseif pwmCounter = pwmDutyCycle then
low(pwmPin)
end if
pwmCounter += 1
IFS0.3 = 0
End Interrupt
sub SetDuty(pValue as pwmDutyCycle)
T1CON = 0 ' clear Timer 1 - no presccale
PR1 = _reloadTimerValue ' set timer period
IFS0.3 = 0 ' clear timer interrupt flag
T1CON.15 = 1 ' switch timer on
enable(OnTimer) ' enable the interrupt handler
end sub
Sub Main()
setduty(50)
End Sub
Users browsing this forum: No registered users and 1 guest