Having a thinking issue with the code below. For the most part it works but I know there is a problem with the way I'm getting the feedback to adjust the output. I need it to be adjustable by the push buttons, and hold at that level. At the moment it will run and settle at some random number first, then I can adjust it with the push buttons. I believe the problem is this:
It will try to get LEDCURRENT = SETPOINT even if SETPOINT = 0.Code:if LEDCURRENT < setpoint then pwm_signal=pwm_signal+1
Obviously when the circuit starts, LEDCURRENT = 0 but it reduces the PWM_SIGNAL by 1 which then means it goes to 65535 and counts down by 1 until LEDCURRENT = SETPOINT.
SETPOINT and LEDCURRENT should be the same, so if I want 1A at the output, LEDCURRENT should read 100 and SETPOINT would equal 100.
Code:include "modedefs.bas" Define OSC 48 DEFINE LCD_DREG PORTB ' Set LCD Data port DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus DEFINE LCD_RSREG PORTB ' Set LCD Register Select port DEFINE LCD_RSBIT 3 ' Set LCD Register Select bit DEFINE LCD_EREG PORTB ' Set LCD Enable port DEFINE LCD_EBIT 2 ' Set LCD Enable bit DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) DEFINE LCD_LINES 2 ' Set number of lines on LCD DEFINE LCD_COMMANDUS 2000' Set command delay time in us DEFINE LCD_DATAUS 50 ' Set data delay time in us DEFINE ADC_BITS 10 DEFINE ADC_CLOCK 3 DEFINE ADC_SAMPLEUS 50 DEFINE CCP1_REG PORTC DEFINE CCP1_BIT 2 ADCON1 = %00001011 ADCON2 = %10000111 TRISA = %00001111 TRISB = %00000000 TRISC = %00000011 CMCON = 7 PORTA.5 = 0 high_duty var word low_duty var word PWM_SIGNAL VAR word LEDVOLT var word LEDCOUNT var word CURRENT var word LEDCURRENT var word LCUR0 var word LCUR1 var word SETPOINT var word 'Output current level in 0.1A increments (100 = 1A) tb0 VAR WORD tb1 VAR WORD tb2 VAR WORD LED0 var word LED1 var word LED2 var word LEDV0 var word LEDV1 var word IOUT VAR byte but_0 var porte.3 but_1 var portc.0 PWMDUTY var word PWMDISP var word 'IOUT = 0 PORTC.6 = 0 Pause 1500 PWM_SIGNAL = 0 setpoint = 0 test: 'setpoint = 150 CCP1CON = %00111100 PR2 = 239 high_duty=(PWM_SIGNAL>>2) 'high 6 bits in CCPR1L low_duty=(PWM_SIGNAL<<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 !but_0 then if SETPOINT>0 then SETPOINT=SETPOINT-1 endif endif if !but_1 then if SETPOINT<150 then SETPOINT=SETPOINT+1 endif endif PWMDUTY = PWM_SIGNAL * 100 PWMDISP = PWMDUTY / 1040 if LEDCURRENT < setpoint then pwm_signal=pwm_signal+1 if PWM_SIGNAL = 550 then PWM_SIGNAL = PWM_SIGNAL endif ENDIF if LEDCURRENT > setpoint then pwm_signal=pwm_signal-1 IF PWM_SIGNAL = 0 then PWM_SIGNAL = PWM_SIGNAL endif ENDIF ADCIN 1, LEDCOUNT LEDVOLT = LEDCOUNT * 49 /90 LEDV0 = LEDVOLT/10 LEDV1 = LEDVOLT//100 ADCIN 0, CURRENT LEDCURRENT = CURRENT * 49 /100 *2 LCUR0 = LEDCURRENT/100 LCUR1 = LEDCURRENT//1000 'LCDOUT $FE,1,"PCB ",DEC2 tb0,".",DEC1 tb1,"C ", DEC2 PWMDISP, "%" 'LCDOUT $FE,$C0,"LED ",DEC2 LED0,".",DEC1 LED1,"C ", DEC4 PWM_SIGNAL PORTC.6 = 1 pause 100 PORTC.6 = 0 pause 100 LCDOUT $FE,1, "LED V ", DEC2 LEDV0, ".", DEC1 LEDV1,"V ", DEC2 PWMDISP, "%" LCDOUT $FE,$C0,"C", DEC1 LCUR0, ".", DEC2 LCUR1,"A ", dec4 current, " ", DEC4 PWM_SIGNAL goto test




Bookmarks