' PIC18F2220
DEFINE OSC 20
TRISC = 0 ' PORTC as output
TRISA = 3 ' RA0 & RA1 as input
PR2=$FF ' load frequency period (PWM freq=1.2 KHZ)
ADCON1 = %00001101 ' Set RA0 & RA1 as analog
ADCON2 = %10000110 ' Right justified result
' Fosc/64
' Tad = 0
ADCON0.0 = 1 ' enable a/d
TMR2DutyVar var word
AdResult var word
Voltage var word
Source var word
SelectedVoltage var word
Quanta con 125 ' (5Volt/1024)*256=0.125 = 125
SelectedVoltage = 110 ' mean 1.1 volt
tmr2dutyvar = 156 ' according duty to get 1.1Volt = (1.1/7.2)*1024
Start:
gosub InitPWM
' Read Motor voltage supply line
' ------------------------------
'
ADCON0 = %00000101 'select channel 1
pauseus 100
ADCON0.1 = 1 'start conversion
while ADCON0.1 ' wait for end of conversion
wend
Adresult.highbyte = ADRESH
adresult.lowbyte = ADRESL
Source = (adresult */ quanta )
Source = source * 288 ' to have a range of 7.2volt
Source = div32 100
' Read feedback motor voltage
' ---------------------------
'
ADCON0 = %00000001 ' select channel 0
pauseus 100
ADCON0.1 = 1
while ADCON0.1 ' wait for end of conversion
wend
Adresult.highbyte = ADRESH
adresult.lowbyte = ADRESL
voltage = (adresult */ quanta )
voltage = voltage * 144 ' to have 7.2V range
voltage = div32 100
voltage = source - voltage
' Voltage regulation
' ------------------
'
if voltage < selectedvoltage then
if tmr2dutyvar < 1023 then TMR2DutyVar=TMR2DutyVar+1
gosub initpwm
endif
if voltage>Selectedvoltage then
if tmr2dutyvar then TMR2DutyVar=TMR2DutyVar-1
gosub initpwm
endif
goto start
InitPWM:
'set Duty cycle
CCP1CON.5=Tmr2dutyvar.1
CCP1CON.4=Tmr2dutyvar.0
CCPR1L =(tmr2dutyvar>>2)
T2CON=%00000110 ' start timer 2
' set prescaler = 16
CCP1CON = %00001100 ' pwm mode for CCP1
return
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Bookmarks