-
Notifications
You must be signed in to change notification settings - Fork 5
/
12f675 MPPT.mpas
258 lines (221 loc) · 5.83 KB
/
12f675 MPPT.mpas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
program _12f675_MPPT;
{ 12F675 MPPT controller
Copyright (C) 2015-2018 rasberryrabbit
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or MPL 1.1
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
}
{$define USE_EEPROM_LM358DIFF}
{$define Write_OPAMP_DIFF}
const
PWM_MAX = 250;
PWM_MIN = 10;
PWM_MID = PWM_MAX div 2-5;
TMR1L_LOAD=$30;
TMR1H_LOAD=$F8; // 2ms, 65536 - 2000
VOLMUL = 25 div 6; // 25V(1024) : 6A(1024), don't exceed 8
_UPDATE_INT = 30 div 2; // consider 90ms
_PWM_CHECK = 4500 div 2;
cLM358_diff = 0;
adc_max_loop = 5; // 8
label
CONTLOOP;
var
LED1:sbit at GP5_bit;
PWM_SIG:sbit at GP2_bit;
Write_OPAMP : sbit at GP3_bit;
ON_PWM, VOLPWM : byte;
TICK_1000 : word;
doADCRead : byte;
LED1_tm : byte;
prevtime, powertime, voltime : word;
adc_vol, adc_cur, vol_prev1, vol_prev2 : word;
power_prev, power_curr : dword;
i, LM358_diff : byte;
flag_inc : boolean;
wtmp, xtmp : word;
procedure Interrupt(); iv 0x0004; ics ICS_AUTO;
begin
if T0IF_bit=1 then begin
PWM_SIG:=not PWM_SIG;
if PWM_SIG=0 then begin
doADCRead:=1;
// on
ON_PWM:=VOLPWM;
TMR0:=255-ON_PWM;
end else begin
// off
TMR0:=255-PWM_MAX+ON_PWM;
end;
T0IF_bit:=0;
end;
if T1IF_bit=1 then begin
TMR1H:=TMR1H_LOAD;
TMR1L:=TMR1L_LOAD;
T1IF_bit:=0;
Inc(TICK_1000);
end;
end;
begin
CMCON:=7;
ANSEL:=%00111001; // ADC conversion clock = fRC, AN3, AN0;
TRISIO0_bit:=1; // AN0
TRISIO1_bit:=1; // VREF
TRISIO2_bit:=0; // PWM
TRISIO4_bit:=1; // AN3
TRISIO5_bit:=0; // LED
VCFG_bit:=1;
CHS1_bit:=1;
ADFM_bit:=1;
LED1:=0;
PWM_SIG:=1;
LED1_tm:=100;
ON_PWM:=0;
VOLPWM:=0;
TICK_1000:=0;
OPTION_REG:=%01011111; // ~4KHz @ 4MHz
TMR0IE_bit:=1;
LM358_diff:=cLM358_diff;
Delay_100ms;
Delay_100ms;
ClrWDT;
{$ifdef Write_OPAMP_DIFF}
if Write_OPAMP=0 then begin
Delay_100ms;
Delay_100ms;
adc_cur:=ADC_Read(0);
EEPROM_Write(0, Lo(adc_cur));
Delay_100ms;
LED1:=1;
Delay_ms(700);
LED1:=0;
end;
ClrWDT;
{$endif}
{$ifdef USE_EEPROM_LM358DIFF}
Delay_100ms;
LM358_diff:=EEPROM_Read(0);
{$endif}
if LM358_diff>$1f then
LM358_diff:=0;
T1CKPS1_bit:=0;
T1CKPS0_bit:=0; // timer prescaler 1:1
TMR1CS_bit:=0;
TMR1L:=TMR1L_LOAD;
TMR1H:=TMR1H_LOAD;
T1IF_bit:=0;
adc_vol:=0;
adc_cur:=0;
power_curr:=0;
TMR1IE_bit:=1;
PEIE_bit:=1;
GIE_bit:=1; // enable Interrupt
TMR1ON_bit:=1;
VOLPWM:=PWM_MIN;
flag_inc:=True;
vol_prev1:=0;
powertime:=0;
prevtime:=0;
voltime:=0;
// starting LED
clrwdt;
delay_ms(300);
LED1:=1;
delay_ms(300);
LED1:=0;
clrwdt;
while True do begin
// LED
wtmp := TICK_1000;
if wtmp - prevtime > LED1_tm then begin
prevtime := wtmp;
LED1 := not LED1;
end;
// save previous adc values
vol_prev2:=vol_prev1;
vol_prev1:=adc_vol;
// Read Sync with PWM clock
doADCRead:=0;
while doADCRead=0 do ;
// adc read
adc_cur:=ADC_Read(0);
adc_vol:=ADC_Read(3);
for i:=0 to adc_max_loop-2 do begin
xtmp:=ADC_Read(0);
wtmp:=ADC_Read(3);
adc_vol:=(adc_vol+wtmp) div 2;
if xtmp > adc_cur then
adc_cur:=xtmp;
//adc_cur:=(adc_cur+xtmp) div 2;
end;
adc_vol:=adc_vol * VOLMUL;
// check time
wtmp:=TICK_1000;
if wtmp - powertime < _UPDATE_INT then
goto CONTLOOP;
clrwdt;
powertime:=wtmp;
power_prev:= power_curr;
power_curr:= dword(adc_vol * adc_cur);
{
// prevent low voltage condition
wtmp:=TICK_1000;
if ON_PWM>PWM_MID then begin
if wtmp - voltime > _PWM_CHECK then begin
voltime:=wtmp;
adc_cur:=LM358_diff;
end;
end else
voltime:=wtmp;
}
if adc_cur>LM358_diff then begin
// power check
if power_curr = power_prev then begin
LED1_tm:=250;
goto CONTLOOP;
end else if power_curr < power_prev then begin
LED1_tm:=150;
flag_inc:=not flag_inc;
end else
LED1_tm:=150;
{
if (adc_vol+vol_prev2+1) div 2 < vol_prev1 then begin
flag_inc:=true;
end;
}
end else begin
LED1_tm:=100;
VOLPWM:=PWM_MIN;
flag_inc:=True;
power_curr:=0;
adc_cur:=0;
goto CONTLOOP;
end;
// PWM
if flag_inc then begin
if VOLPWM<PWM_MAX then
Inc(VOLPWM)
else begin
VOLPWM:=PWM_MAX;
flag_inc:=false;
end;
end else begin
if VOLPWM>PWM_MIN then
Dec(VOLPWM)
else begin
VOLPWM:=PWM_MIN;
flag_inc:=true;
end;
end;
CONTLOOP:
end;
end.