Code:
Define OSC 20
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
PORTA = %00000000 ' Turn off all PORTA
PORTB = %00000000 ' Turn off all PORTB
ADCON0 = %00000001
ADCON1 = %01111000
ADCON2 = %10000111
TRISA = %11100111
TRISB = %11000011
UpButton VAR PortB.0 ' Active high button
DnButton VAR PortB.1 ' Active high button
Setting VAR BYTE
SettingChanged VAR BIT
DutyCycle VAR WORD
SETPOINT var byte 'Output current level in 0.1A increments)100 = 1A)
CURRENT VAR WORD
LEDCURRENT VAR WORD
LEDCURRENT1 VAR WORD
high_duty var word
low_duty var word
Main:
If UpButton THEN
pause 75
Setting = Setting + 1 ' Increment setting
If Setting = 6 THEN Setting = 5 ' Make sure it doesn't go beyond 5
SettingChanged = 1 ' Set flag indicating new setting
EndIf
If DnButton THEN
pause 75
Setting = Setting - 1
If Setting = 255 THEN Setting = 0 ' Make sure it doesn't go below 0
SettingChanged = 1 ' Set flag indicating new setting
EndIf
If SettingChanged THEN
LOOKUP Setting, [0,100,150,200,240,254], DutyCycle
LOOKUP Setting, [0, 2, 5, 9, 14, 17], SetPoint ' Assign value to setpoint variable
SettingChanged = 0 ' Clear flag
EndIf
ADCIN 0, CURRENT
LEDCURRENT = CURRENT * 49 /100 *2
LEDCURRENT1 = LEDCURRENT/10
CCP1CON = %00111100
PR2 = 99
high_duty=(dutycycle>>2) 'high 6 bits in CCPR1L
low_duty=(dutycycle<<6) 'low two bits for CCP1CON
'low_duty=(low_duty>>2) 'shift back to CCP1CON<5:4>
low_duty.4=0 'PWM configuration bit
low_duty.5=1 'PWM configuration bit
CCPR1L=high_duty
CCP1CON = $0C
T2CON = 4
if LEDCURRENT1 < setpoint then
dutycycle=dutycycle+1
if dutycycle => 300 then
dutycycle = 300
endif
ENDIF
if LEDCURRENT1 > setpoint then
dutycycle=dutycycle-1
IF dutycycle > 301 then
dutycycle = 0
ENDIF
endif
pause 100
Goto Main
Ok seem to be stuck. It no longer responds to button presses, turns off the LED if I raise the input voltage to 22V - should operate from 19 - 42V, and the pwm output is a constant low %. I have no idea what is causing this.
The pause 100 at the end is just to slow things down so I can see whats happening on the scope.
Bookmarks