Ok, so this combines the working crude regulator code with Henriks example code. I havent programmed the pic yet but it does compile.
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 BYTE 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 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 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, [12,23,34,45,56], DutyCycle SettingChanged = 0 ' Clear flag EndIf If Setting = 0 THEN SETPOINT = 0 endif If SETTING = 1 then Setpoint = 2 endif If Setting = 2 then SETPOINT = 5 endif IF setting = 3 then SETPOINT = 9 endif IF Setting = 4 then SETPOINT = 14 endif If Setting = 5 then Setpoint = 17 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 Goto Main




Bookmarks