Code:
'****************************************************************
'* Name : pic 18f4523 *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 28/04/2014 *
'* Version : 1.0 *
'* Notes : modificare file 18f4523 nella cartella PBP *
'* : per il config ... oscillatore MCLR etc *
'* picbasic pro 2.6 : long used. 12294 BYTES USED. *
'* 50 WATT SOLAR PANEL, 27 AH 12 VDC SLA BATTERY *
'****************************************************************
' WARNING:
' HSPLL OSCILLATOR HAS BEEN DEFINED IN THE PBP FOLDER > 18F4523 FILE
DEFINE OSC 40 ' 10 MHz XTAL AND x4 PLL GIVES 40 MHz CLOCK SPEED.
INCLUDE "MODEDEFS.BAS"
'DEBUG DEFINITION> WITH_OUT MAX 232...USE INVERTED=1, 57600 BAUD 8N1
' Set Debug pin port ________________________________________
DEFINE DEBUG_REG PORTC
' Set Debug pin bit _________________________________________
DEFINE DEBUG_BIT 6
' Set Debug baud rate _________________________________________
DEFINE DEBUG_BAUD 57600
' Set Debug mode: 0 = true, 1 = inverted ______________________
DEFINE DEBUG_MODE 1
' LCD CONFIGURATION ...HD_4470 LIKE > 4 LINES BY 20 CHRS IS USED HERE
' Set LCD Data port _________________________________________
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus ________
DEFINE LCD_DBIT 0
' Set LCD Register Select port (R/S) ________________________
DEFINE LCD_RSREG PORTD
' Set LCD Register Select bit _______________________________
DEFINE LCD_RSBIT 5
' Set LCD Enable port (EN_DCDC)__________________________________
DEFINE LCD_EREG PORTD
' Set LCD Enable bit ____________________________________
DEFINE LCD_EBIT 4
' Set LCD bus size (4 or 8 bits) _____________________________
'DEFINE LCD_BITS 4
' Set number of lines on LCD ______________________________
DEFINE LCD_LINES 4
' Set command delay time in us ______________________________
DEFINE LCD_COMMANDUS 2500
' Set data delay time in us _________________________________
DEFINE LCD_DATAUS 50
PAUSE 500
' SOME CONSTANTS TO CONTROL THE LCD.
INS CON 254 ' $FE: INSTRUCTION COMMAND MODE
CLR CON 1 ' CLEAR LCD , GOTO FIRST LINE, FIRST CHARACTER
DG CON 223 ' SIMBOLO DEL GRADO °
LINE1 CON 128 ' $80: GOTO LINE 1
LINE2 CON 192 ' $C0: GOTO LINE 2
LINE3 CON 148 ' $94: GOTO LINE 3
LINE4 CON 212 ' $D4: GOTO LINE 4
' eg.: LCDOUT INS, LINE1, "PIBPO TXT=",........
' ADC CONFIGURATION
ADCON1=%00001010 ' AN0,AN1,AN2,AN3,AN4 ENABLED.THE REMAINING PINS ARE DIGITAL
VB_ADC VAR WORD ' AN0 PIC_PIN 2 ' BATTERY VOLTAGE
VP_ADC VAR WORD ' AN1 PIC_PIN 3 ' PANEL VOLTAGE
IP_ADC VAR WORD ' AN2 PIC_PIN 4 ' PANEL CURRENT
IB_ADC VAR WORD ' AN3 PIC_PIN 5 ' BATTERY VOLTAGE
POT_ADC VAR WORD ' AN4 PIC_PIN 7 ' POT VOLTAGE FOR MANUAL DUTY INPUT...
' FILTERS '........................................................
IPT VAR WORD
VPT VAR WORD
VBT VAR WORD
IBT VAR WORD
AVG VAR BYTE
AVG=8 ' MAX AVG=16 4096*16=65536 IN A WORD ! PANEL data
AVG_B VAR BYTE
AVG_B=4 ' MAX AVG_B=16 4096*16=65536 IN A WORD ! BATTERY data
I VAR BYTE
Y VAR BIT
Y=0
Z VAR BYTE
Z=0
DLY_FLG VAR BIT
DLY_FLG =1
IPF_ADC VAR WORD
VPF_ADC VAR WORD
VBF_ADC VAR WORD
IBF_ADC VAR WORD
'...................................
VP VAR LONG
IP VAR LONG
VB VAR LONG
IB VAR LONG
WP VAR LONG
WB VAR LONG
IBSIGN VAR BYTE ' BATTERY CURRENT SIGN
DUTY VAR WORD ' PIC PWM DUTY CYCLE CONTROL
DUTY=150 ' START AT MINIMUM DUTY
GOSUB SET_PWM
DUTY_S VAR BYTE ' DUTY CYCLE STEP for MPPT
DUTY_S=1 ' MUST BE WIDE ENOUGH TO OVERCOME PANEL DATA NOISE !
' IF MPP IS NOT FOUND USE 2, 3, 4 OR 5...
LD_EN VAR PORTC.1 ' PIC_PIN 16 LOAD POWER MOSFET CONTROL PIN
LOW LD_EN ' DISABLE LOAD POWER MOSFET
EN_DCDC VAR PORTC.3 ' PIC_pin 18 DC/DC CONVERTER ENABLE
low EN_DCDC ' DISABLE DC/DC CONVERTER
LOAD VAR BIT
LOAD=1
MODE VAR BYTE
MODE=0
STATE VAR BYTE
STATE=0
' STATE 0= OFF
' STATE 1= ON
' STATE 3= FLOAT
' STATE 4= BULK
DV VAR BIT ' PANEL DELTA VOLT USED FOR HILL CLIMBING
DP VAR BIT ' PANEL DELTA POWER
VP_OLD VAR LONG ' OLD PANEL VOLTAGE
WP_OLD VAR LONG ' OLD PANEL POWER
' SCAN FOR MPP ......................
WPMAX VAR LONG
WPMAX=0
VPMAX VAR LONG
VPMAX=0
IPMAX VAR LONG
IPMAX =0
VBMAX VAR LONG
IBMAX VAR LONG
WBMAX VAR LONG
DUTYMAX VAR BYTE
DUTYMAX=0
' BACK LIGHTING ......................................................
BCK_LT VAR PORTA.4 ' PIC_PIN 6
FOR I=3 TO 0 STEP -1 ' LCD BACK LIGHT TEST AT POWER ON
HIGH BCK_LT
pAUSE 100
LOW BCK_LT
PAUSE 100
NEXT I
HIGH BCK_LT ' LEAVE BACK LIGHT ON AT THE END ...
' EXTERNAL POWER ........................................................
EXT_PWR_RLY VAR PORTC.0 ' PIC_PIN 15 OUTPUT CONTROL FOR EXT_PWR SOURCE RELAY
HIGH EXT_PWR_RLY ' DISABLE EXT PWR_RELAY HIGH= DISABLE
EXTPWR_OK VAR PORTC.4 ' PIC_PIN 23 INPUT > POWER_LINE STATUS; 1=OK
U_VOLT VAR BIT ' EXT POWER MONITOR
U_VOLT=0 ' EXT POWER GOOD
U_FLG VAR BIT ' BATTERY UNDERVOLTAGE FLAG
U_FLG=1
EX VAR BIT
EX=0
' SPLASH SCREEN:
Lcdout INS,line1, "Ambrogio 06/04/2014 " ' LCD CHECK
Lcdout INS,line2, "PV PIC18F4523 "
debug 13,10,13,10
DEBUG " STARTING H E R E ! ! ! ! " ' SERIAL LINK CHECK
PAUSE 1000
LCDOUT INS,CLR ' TURN LCD OUT
' PUSH BUTTONS PIN ASSIGNEMENT
PB_1 VAR PORTE.0 'PIC_PIN 8 STEPS THROUGH THE MODES
PB_2 VAR PORTE.1 'PIC_PIN 9
PB_3 VAR PORTE.2 'PIC_PIN 10 TOGGLE LOAD MOSFET
'********************************************************************
MAIN: ' ************************************************************
'********************************************************************
' PB_1 SELECTS THE MODES ...
' mode =0 is THE normal OPERATING MODE
' mode =1 pot input controls the duty cycle
' mode =2 scan duty cycle to search for the MPP CONDITION
' mode =3 TOGGLES the duty cycle to see the system response caracteristics
' MODES SELECTION....................
IF PB_1=0 THEN ' MODE SELECTION...
MODE=MODE+1
IF MODE >=4 THEN
MODE=0
ENDIF
IF MODE=1 THEN
LCDOUT INS, CLR
HIGH EN_DCDC
ENDIF
IF MODE=2 THEN
LCDOUT INS, CLR
WPMAX=0 ' reset maximum registers
VPMAX=0
IPMAX=0
VBMAX=0
IPMAX=0
WBMAX=0
DUTYMAX=0
HIGH EN_DCDC
DUTY=150 ' THIS IS MY MINIMUM DUTY CYCLE
GOSUB SET_PWM
PAUSE 1000
ENDIF
IF MODE=3 THEN
LCDOUT INS, CLR
LCDOUT INS, LINE1, " DC STEP TEST "
ENDIF
WHILE PB_1=0: WEND
ENDIF
' LOW VOLTAGE PROTECTION ...and load control..................................
IF ( VB<11800 ) THEN ' BATT LOWER THAN 11.8 Vdc
U_VOLT=1 ' SET UNDER_VOLTAGE FLAG
LOW LD_EN ' DISABLE LOAD
LOAD=0
U_FLG=1
ELSE
IF ( VB>12600 ) THEN ' IF VBATT MORE THAN 12.6 Vdc
U_VOLT=0 ' RESET UNDER_VOLTAGE FLAG
IF U_FLG=1 THEN
HIGH LD_EN ' ENABLE LOAD
LOAD=1
U_FLG=0
ENDIF
ENDIF
ENDIF
IF (U_VOLT=1) AND (EXTPWR_OK=1) THEN
LOW EXT_PWR_RLY ' ACTIVATE EXT POWER RELAY
EX=1
ELSE
IF U_VOLT=0 THEN
HIGH EXT_PWR_RLY ' RELASE EXT POWER RELAY
EX=0
ENDIF
ENDIF
IF U_VOLT=0 THEN
IF PB_3=0 THEN
if LOAD=0 THEN
LOAD=1
ELSE
LOAD=0
ENDIF
IF LOAD=0 THEN
LOW LD_EN
ENDIF
IF LOAD=1 THEN
HIGH LD_EN
ENDIF
WHILE PB_3=0 : WEND
ENDIF
ENDIF
' MODES MANAGEMENT BY PUSH BUTTONS ...........................................
IF MODE=0 THEN ' NORMAL MODE ........................................
GOTO NORMAL_MODE
ENDIF
IF MODE=1 THEN ' POT CONTROLLED DUTY CYCLE............................
LCDOUT INS, LINE1,"POT CONTROLLED DUTY:"
GOSUB ACQ_POT ' ACQ TAKES ABOUT 2.3 mSEC
DUTY=((POT_ADC/39)+149) ' POT CONTROLS DUTY 0>4096 = 150 to 254
GOSUB SET_PWM
GOSUB GET_PANEL_DATA
GOSUB GET_BATTERY_DATA
LCDOUT INS, LINE3,DEC VP/1000,".",DEC1 VP/100," ",DEC IP/1000,_
".",DEC2 IP/10," ",DEC WP/1000,".",DEC1 WP/100," ", DEC DUTY," "
LCDOUT INS, LINE4,DEC VB/1000,".",DEC1 VB/100," ",IBSIGN,DEC IB/1000,_
".",DEC2 IB/10, " ",IBSIGN,DEC WB/1000,".",DEC1 WB/100," "
GOSUB SERIAL_OUT
GOTO MAIN
ENDIF
IF MODE=2 THEN ' SCAN DUTY CYCLE FOR MPP SEARCHING.................
DUTY=DUTY+1 ' INCREMENT DUTY CYCLE
GOSUB SET_PWM
PAUSE 10 ' NOT REALLY NEEDED .. LCDOUT PROVIDES TIME DLY
GOSUB GET_PANEL_DATA
GOSUB GET_BATTERY_DATA
IF WP> WPMAX THEN ' STORE MAX READINGS
WPMAX=WP
VPMAX=VP
IPMAX=IP
VBMAX=VB
IBMAX=IB
WBMAX=WB
DUTYMAX=DUTY
ENDIF
IF DUTY >= 254 THEN
LCDOUT INS, CLR
LCDOUT INS, LINE1, " MAX POWER : "
LCDOUT INS, LINE3,DEC VPMAX/1000,".",DEC1 VPMAX/100," ",_
DEC IPMAX/1000,".",DEC2 IPMAX/10," ",DEC WPMAX/1000,_
".",DEC1 WPMAX/100," ", DEC DUTYMAX
LCDOUT INS,LINE4,DEC VBMAX/1000,".",DEC1 VBMAX/100," ",_
DEC IBMAX/1000,".",DEC2 IBMAX/10," ",DEC WBMAX/1000,_
".",DEC1 WBMAX/100
DUTY=150
GOSUB SET_PWM
while PB_2=1 :wend ' WAIT FOR PB_2 TO BE PRESSED
ENDIF
lcdout ins, line1, "MAX PWR SEARCHING:"
LCDOUT INS, LINE3,DEC VP/1000,".",DEC1 VP/100," ",DEC IP/1000,_
".",DEC2 IP/10," ",DEC WP/1000,".",DEC1 WP/100," ", DEC DUTY," "
LCDOUT INS, LINE4,DEC VB/1000,".",DEC1 VB/100," ",IBSIGN,DEC IB/1000,_
".",DEC2 IB/10, " ",IBSIGN,DEC WB/1000,".",DEC1 WB/100," "
GOSUB SERIAL_OUT
GOTO MAIN
ENDIF
IF MODE=3 THEN 'DUTY STEP CHANGE RESPONSE TEST > SET TWO DUTY VALUES
'SET PAUSE FOR STEP TIME_WIDTH ,,,,,,,,,,,,,,,,,,,,,,,,,
' system response time to duty change is about 15 msec
' measured with a scope
STEP_DUTY:
IF Y=0 THEN
HIGH BCK_LT ' CONNECT TO SCOPE TRIGGER INPUT
DUTY=170 ' FIRST DUTY VALUE
GOSUB SET_PWM
Y=1
PAUSE 50 ' WIDTH
GOSUB GET_PANEL_DATA
GOSUB SERIAL_OUT
ENDIF
IF Y=1 THEN
low bck_lt
DUTY=175 ' SECOND DUTY VALUE > DUTY_step is 5 now
GOSUB SET_PWM
Y=0
PAUSE 50 ' step time WIDTH IS 50 MSEC
GOSUB GET_PANEL_DATA
GOSUB SERIAL_OUT
GOTO MAIN
ENDIF
ENDIF
NORMAL_MODE: '*************************************************************
' BATTERY STATE CONTROL ......................................................
GOSUB GET_PANEL_DATA ' 2.3 msec x avg
GOSUB GET_BATTERY_DATA ' 2.3 msec x avg_b
SELECT CASE STATE ' MODIFIED TIM NOLAND PROGRAM IN c LANGUAGE... ( ! )
CASE 0 ' OFF *** * * * * * * * * * * * * * * * * * * * * * * * * * *
Z=Z+1
IF Z>=254 THEN ' wait sometime for the battery to settle down
DLY_FLG=0
ENDIF
DUTY=150 ' MINIMUM DUTY CYCLE.
low EN_DCDC ' DISABLE DC / DC CONVERTER
GOSUB SET_PWM
debug dec z,13,10
IF DLY_FLG=0 THEN
IF VP > VB+1500 THEN ' day light is coming ...
IF VB < 13800 THEN
DUTY=150
HIGH EN_DCDC
GOSUB SET_PWM
PAUSE 1000
STATE=1 ' >> ON
ENDIF
ENDIF
ENDIF
CASE 1 ' ON *** * * * * * * * * * * * * * * * * * * * * * * * * * *
HIGH EN_DCDC
IF (VP < VB) and ( IP<80 ) THEN ' it is dark
DLY_FLG=1 : Z=0
DUTY=150
GOSUB SET_PWM
STATE= 0 ' >> OFF
ENDIF
IF VB > 14800 THEN ' MAX BATTERY VOLTAGE IS 14.8 VDC
STATE=2 ' >> FLOAT
DUTY=150
GOSUB SET_PWM
ENDIF
IF ( WP < 5000 ) THEN
DUTY=254
GOSUB SET_PWM
ENDIF
IF WP> 5000 THEN
DUTY=240
GOSUB SET_PWM
STATE=3 ' >> BULK
ENDIF
CASE 2 ' FLOAT *** * * * * * * * * * * * * * * * * * * * * * * * * *
HIGH EN_DCDC
IF VB > 14200 THEN ' KEEP FLOAT VOLTAGE @ 13.8 TO 14.2 VDC
DUTY=DUTY-1 ' DECREASE DUTY
GOSUB SET_PWM
ENDIF
IF VB < 13800 THEN
DUTY=DUTY+1 ' INCREASE DUTY
GOSUB SET_PWM
ENDIF
IF DUTY >= 254 THEN
DUTY=200
GOSUB SET_PWM
STATE=3 ' >> BULK
ENDIF
IF (VP < VB) and ( IP<80 ) THEN
DLY_FLG=1 : Z=0
DUTY=150
GOSUB SET_PWM
STATE=0 ' >> OFF
ENDIF
CASE 3 ' BULK
HIGH EN_DCDC
IF (WP < 5000) THEN
STATE= 1 ' >> ON
ENDIF
IF WP >= 5000 THEN
GOSUB MPPT
ENDIF
IF (VP < VB) and ( IP<80 ) THEN
DLY_FLG=1 : Z=0
DUTY=150
GOSUB SET_PWM
STATE=0 ' >> OFF
ENDIF
IF VB > 14800 THEN
DUTY=150
GOSUB SET_PWM
STATE=2 ' >> FLOAT
ENDIF
END SELECT
' DATA OUTPUT TO LCD, SERIAL AND RADIO.......................................
GOSUB DIS_LCD ' execution time is 15 msec
GOSUB SERIAL_OUT ' execution time is 25 msec
GOTO MAIN ' main loop execution time is 65 msec if avg=8 and
' avg_b=4
'============================================================================
'======================== SUBROUTINES =====================================
'============================================================================
ACQ_PV: 'ACQUIRE PV ANALOG DATA ... 2.3 msec ........................
' READ ANALOG 0 VP .........................
ADCON0=%00000000 ' SELECT AN0, VP
ADCON2=%10100110 ' ACQ TIME AND FOSC SELECTION
ADCON0=%00000001 ' ON, ACCESO
PAUSE 1 ' INPUT RESISTANCE AND CAP FOR DC ACQISISTION
ADCON0=%00000011 ' ON + GO
WHILE ADCON0.1=1 ' WAIT FOR CONVERSION COMPLETED , DONE
WEND
VP_ADC=(ADRESH<<8) + ADRESL ' ADC RESULT 12 BIT
ADCON0=%00000000 ' TURN OFF,
'READ ANALOG 2 IP ........................
ADCON0=%00001000 ' SELECT AN0, IP
ADCON2=%10100110 ' ACQ TIME AND FOSC SELECTION
ADCON0=%00001001 ' ON, ACCESO
PAUSE 1 ' INPUT RESISTANCE AND CAP FOR DC ACQISISTION
ADCON0=%00001011 ' ON + GO
WHILE ADCON0.1=1 ' WAIT FOR CONVERSION COMPLETED , DONE
WEND
IP_ADC=(ADRESH<<8)+ ADRESL ' ADC RESULT 12 BIT
ADCON0=%00000000 ' TURN OFF,
RETURN
ACQ_BATT: ' ACQUIRE BATTERY ANALOG DATA ........ 2.3 msec ................
' READ ANALOG 1 VB ..........................
ADCON0=%00000100 ' SELECT AN0, VB
ADCON2=%10100110 ' ACQ TIME AND FOSC SELECTION
ADCON0=%00000101 ' ON, ACCESO
PAUSE 1 ' INPUT RESISTANCE AND CAP FOR DC ACQISISTION
ADCON0=%00000111 ' ON + GO
WHILE ADCON0.1=1 ' WAIT FOR CONVERSION COMPLETED , DONE
WEND
VB_ADC=(ADRESH<<8) + ADRESL ' ADC RESULT 12 BIT
ADCON0=%00000000 ' TURN OFF,
'READ ANALOG 3 IB ........................
ADCON0=%00001100 ' SELECT AN0, IB
ADCON2=%10100110 ' ACQ TIME AND FOSC SELECTION
ADCON0=%00001101 ' ON, ACCESO
PAUSE 1 ' INPUT RESISTANCE AND CAP FOR DC ACQISISTION
ADCON0=%00001111 ' ON + GO
WHILE ADCON0.1=1 ' WAIT FOR CONVERSION COMPLETED , DONE
WEND
IB_ADC=(ADRESH<<8)+ ADRESL ' ADC RESULT 12 BIT
ADCON0=%00000000 ' TURN OFF,
RETURN
ACQ_POT: 'ACQUIRE POT ANALOG VOLTAGE ....................................
'READ ANALOG 4 POT
ADCON0=%00010000 ' SELECT AN0, VPOT
ADCON2=%10100110 ' ACQ TIME AND FOSC SELECTION
ADCON0=%00010001 ' ON, ACCESO
PAUSE 1 ' INPUT RESISTANCE AND CAP FOR DC ACQISISTION
ADCON0=%00010011 ' ON + GO
WHILE ADCON0.1=1 ' WAIT FOR CONVERSION COMPLETED , DONE
WEND
POT_ADC=(ADRESH<<8)+ ADRESL ' ADC RESULT 12 BIT
ADCON0=%00000000 ' TURN OFF,
RETURN
DIS_LCD: '.................... 15 msec ...................................
IF STATE=0 THEN LCDOUT INS, LINE1,"OFF "
IF STATE=1 THEN LCDOUT INS, LINE1,"ON "
IF STATE=2 THEN LCDOUT INS, LINE1,"FLOAT"
iF STATE=3 THEN LCDOUT INS, LINE1,"BULK "
IF U_VOLT=1 THEN LCDOUT LINE1,"LOAD> OFF > UV"
IF LOAD=0 THEN LCDOUT LINE1,"LOAD> OFF "
IF LOAD=1 THEN LCDOUT LINE1,"LOAD> ON "
IF EXTPWR_OK=1 THEN LCDOUT INS,LINE2,"LINE> AVL "
IF EXTPWR_OK=0 THEN LCDOUT INS,LINE2,"LINE> NOT AVL"
IF EX=1 THEN LCDOUT INS,LINE2+14,"EXT_PWR"
IF EX=0 THEN LCDOUT INS,LINE2+14," "
LCDOUT INS, LINE3,DEC VP/1000,".",DEC1 VP/100," ",DEC IP/1000,_
".",DEC2 IP/10," ",DEC2 WP/1000,".",DEC1 WP/100," ", DEC DUTY
LCDOUT INS, LINE4,DEC VB/1000,".",DEC1 VB/100," ",IBSIGN,DEC IB/1000,_
".",DEC2 IB/10, " ",IBSIGN,DEC2 WB/1000,".",DEC1 WB/100," "
RETURN
SERIAL_OUT: ' .................. 25 msec.....................................
DEBUG "VP ",DEC VP/1000,".",DEC1 VP/100," IP ",DEC IP/1000,_
".",DEC2 IP/10, " WP ", DEC WP/1000,".",DEC1 WP/100,_
" DC ", DEC DUTY, " VB ",DEC VB/1000,".",DEC1 VB/100,_
" IB ",IBSIGN,DEC IB/1000,".",DEC2 IB/10, " WB ",IBSIGN,_
DEC WB/1000,".",DEC1 WB/100," M ",DEC MODE," S ",DEC STATE,13,10
RETURN
MPPT: '...............................................................
' PANEL DATA CHANGE
IF WP>WP_OLD THEN ' POWER INCREASES
DP=1
ELSE
IF WP<WP_OLD THEN ' POWER DECREASES
DP=0
ENDIF
ENDIF
if wp=WP_OLD THEN RETURN
IF VP>VP_OLD THEN ' VOLTAGE INCREASES
DV=1
ELSE
IF VP<VP_OLD THEN ' VOLTAGE DECREASES
DV=0
ENDIF
ENDIF
' HILL CLIMBING.... FOR MPP SEARCH
' CLIMBING FROM LEFT
IF DV=1 AND DP=1 THEN
DUTY=DUTY-DUTY_S ' DECREASE LOAD TO INCREASE P_VOLTAGE
ENDIF
'DESCEND TO THE RIGHT
IF DV=1 AND DP=0 THEN
DUTY=DUTY+DUTY_S ' INCREASE LOAD TO REDUCE P_VOLTAGE
ENDIF
' CLIMBING FROM RIGHT
IF DV=0 AND DP=1 THEN
DUTY=DUTY+DUTY_S ' INCREASE LOAD TO REDUCE P_VOLTAGE
ENDIF
' DESCEND TO THE LEFT
IF DV=0 AND DP=0 THEN
DUTY=DUTY-DUTY_S ' DECREASE LOAD TO INCREASE P_VOLTAGE
ENDIF
GOSUB SET_PWM
WP_OLD=WP
VP_OLD=VP
RETURN
SET_PWM: '...................................................................
IF DUTY >= 254 THEN ' MAX DUTY FOR IR2104
DUTY=254
ENDIF
if DUTY <=150 THEN ' CONSIDERING ABOUT 12 VDC OUTPUT..
DUTY=150
ENDIF
HPWM 1,DUTY,75000 ' 75 KHz CLOCK FREQUENCY @ DUTY....
RETURN
GET_PANEL_DATA: ' ........................................................
' GET PANEL DATA
FOR I=1 TO AVG ' VP AND IP FILTERING LOOP
GOSUB ACQ_PV ' ACQ_PV TAKES ABOUT 2.3 mSEC...
IPT=IPT+IP_ADC
VPT=VPT+VP_ADC
NEXT I
IPF_ADC=IPT/AVG
VPF_ADC=VPT/AVG
IPT=0 : VPT=0
VP=(VPF_ADC*7200)/1000 ' MILLIVOLTS
IF IPF_ADC>2052 THEN ' ACS_712 BIAS IS ABOUT 2.5 VDC... 12 BIT adc
IPF_ADC=IPF_ADC-2052
ELSE
IF IPF_ADC =< 2052 THEN
IPF_ADC=2052-IPF_ADC
ENDIF
ENDIF
IP=(IPF_ADC*6790)/1000 ' MILLIAMPS
WP=(IP*VP)/1000 ' MILLIWATT
RETURN
GET_BATTERY_DATA: '........................................................
' GET BATTERY DATA
FOR I=1 TO AVG_B
GOSUB ACQ_BATT ' ACQ_BATT TAKES ABOUT 2.3 mSEC
VBT=VBT+VB_ADC
IBT=IBT+IB_ADC
NEXT I
VBF_ADC=VBT/AVG_B
IBF_ADC=IBT/AVG_B
IBT=0 :VBT=0
IF IBF_ADC>2053 THEN
IBF_ADC=IBF_ADC-2053
IBSIGN="-"
ELSE
IF IBF_ADC=<2053 THEN
IBF_ADC=2053-IBF_ADC
IBSIGN="+"
ENDIF
ENDIF
VB=(VBF_ADC*7000)/1000 ' MILLIVOLTS
IB=(IBF_ADC*6790)/1000 ' MILLIAMPS
WB=(IB*VB)/1000 ' MILLIWATT
RETURN
Bookmarks