The code below will flash the LED but not create a pwm output. I'm guessing its because there are fractions and not integers to work with thus creating 0's and other odd results.
The convert to voltage portion works fine - I changed it, but the rest needs work still.
Code:
temp=0 ADCIN 0, temp
'write 40, temp
'convert to voltage
v_feedback=temp 'put ADC in float
'v_supply=v_feedback/1024
v_supply = 1024/v_feedback * 10 'find supply % of vref
'v_supply=v_supply*supply_multiplier 'find supply volts
v_supply = v_supply * supply_multiplier / 10
write 60, v_supply
'calculate rise time
t_rise=L_Ipeak / v_supply 'find rise time @ supply volts for L/Ipeak
' = 68/24 = 2.833uS (example = 24vin)
'calculate CCPR1L:CCP1CON<5:4>
SMPS_duty=t_rise*osc_freq 'find duty cycle value
'2.833*48 = 136
final_duty=SMPS_duty 'convert to byte
'calculate period
t_period=t_rise*133/100 'dutycycle = .75period
'2.833*133/100 = 3.767uS
'calculate PR2
SMPS_period=(t_period/period_multiplier) 'find period value
'3.767/(4/48) = 45.2
final_period=SMPS_period-1 'put in byte
' = 44.2
'put in register masks
high_duty=(final_duty>>2) 'high 6 bits in CCPR1L
low_duty=(final_duty<<6) 'low two bits for CCP1CON
low_duty=(low_duty>>2) 'shift back to CCP1CON<5:4>
low_duty.3=1 'PWM configuration bit
low_duty.2=1 'PWM configuration bit
main:
CCPR1L=high_duty 'flash LED to show alive
CCP1CON=low_duty
portc.0 = 1
pause 100
portc.0 = 0
pause 100
GOTO main
Bookmarks