Rev Author Line No. Line
1045 jacho 1 //Mereni energie
2 //(c) Jan Chroust 2013
3  
4  
5 #include "C:main.h"
6  
7 #define EEPROM_SDA PIN_C4
8 #define EEPROM_SCL PIN_C3
9 //rele v blok sch. REL1 a REL2
10 #define REL1_IN1 PIN_D6
11 #define REL1_IN2 PIN_D7
12 //rele v blok sch. REL5
13 #define REL2_IN1 PIN_B2
14 #define REL2_IN2 PIN_B3
15 //rele v blok sch.REL3
16 #define REL3_IN1 PIN_A0
17 #define REL3_IN2 PIN_A1
18 //rele v blok sch.REL3
19 #define REL4_IN1 PIN_A2
20 #define REL4_IN2 PIN_A3
21  
22 #define NOC PIN_B1 //pro povoleni moznosti nabijet
23  
24  
25 #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
26 #use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD
27 #include "BQ34Z100.h"
28  
29 //BAT3 - pro pohyblivý panel - LION1CELLB-1
30 //BAT2 - pro fixní panel - LION1CELLB-2
31 //BAT1 - hlavní baterie - LION1CELLB-3
32 int16 bat1STAV,bat2STAV,bat3STAV; //zbyvajici eneregie akumulátoru v %
33 signed int16 bat1PROUD,bat2PROUD,bat3PROUD; //proud akumulatorem
34 int16 bat1NAP,bat2NAP,bat3NAP; //napeti akumulatoru
35 signed int16 bat1P,bat2P, bat3P; //vykon z nebo do akumulatoru
36  
37 //pro odeslání
38 signed int16 rozdil;
39 char baterka[40];
40  
41  
42 void batMODE(int d) //nastavi rele do prislusneho modu
43 //hodnota udava jaky port bude pripojen na master I2C
44 //zde je nastaveno propojeni jen jedne brany, je mozne propojit i vice bran naraz
45 {
46 switch (d) {
47  
48 case 1: output_high(REL1_IN2); //pro mereni
49 output_high(REL2_IN1);
50 output_high(REL3_IN2);
51 output_high(REL4_IN2);
52 delay_ms(100);
53 output_low(REL1_IN1);
54 output_low(REL1_IN2);
55 output_low(REL2_IN1);
56 output_low(REL2_IN2);
57 output_low(REL3_IN1);
58 output_low(REL3_IN2);
59 output_low(REL4_IN1);
60 output_low(REL4_IN2);
61  
62 break;
63  
64 case 2: output_high(REL1_IN2); //dobijeni Hl. clanku z bat3
65 output_high(REL2_IN1);
66 output_high(REL3_IN1);
67 output_high(REL4_IN2);
68 delay_ms(100);
69 output_low(REL1_IN2);
70 output_low(REL2_IN1);
71 output_low(REL3_IN1);
72 output_low(REL4_IN2);
73 break;
74  
75 case 3: output_high(REL1_IN2); //dobijeni Hl. clanku z bat2
76 output_high(REL2_IN1);
77 output_high(REL3_IN2);
78 output_high(REL4_IN1);
79 delay_ms(100);
80 output_low(REL1_IN2);
81 output_low(REL2_IN1);
82 output_low(REL3_IN2);
83 output_low(REL4_IN1);
84  
85 break;
86  
87 case 4: output_high(REL1_IN1); //dobijeni Hl. clanku ze sol. clanku
88 output_high(REL2_IN1);
89 output_high(REL3_IN2);
90 output_high(REL4_IN2);
91 delay_ms(100);
92 output_low(REL1_IN1);
93 output_low(REL2_IN1);
94 output_low(REL3_IN2);
95 output_low(REL4_IN2);
96  
97  
98 break;
99 case 5: output_high(REL1_IN2); //vybijeni bat3 do R
100 output_high(REL2_IN2);
101 output_high(REL3_IN1);
102 output_high(REL4_IN2);
103 delay_ms(100);
104 output_low(REL1_IN2);
105 output_low(REL2_IN2);
106 output_low(REL3_IN1);
107 output_low(REL4_IN2);
108  
109 break;
110  
111 case 6: output_high(REL1_IN2); //vybijeni bat2 do R
112 output_high(REL2_IN2);
113 output_high(REL3_IN2);
114 output_high(REL4_IN1);
115 delay_ms(100);
116 output_low(REL1_IN2);
117 output_low(REL2_IN2);
118 output_low(REL3_IN2);
119 output_low(REL4_IN1);
120  
121 default:printf("chyba");
122  
123 break; }
124  
125  
126 }
127  
128  
129 void setHUB(int d) //vstupuje 1-8
130 //hodnota udava jaky port bude pripojen na master I2C
131 //zde je nastaveno propojeni jen jedne brany, je mozne propojit i vice bran naraz
132 {
133 switch (d) {
134  
135 case 1: d=0x01;
136  
137 break;
138  
139 case 2: d=0x02;
140  
141 break;
142  
143 case 3: d=0x04;
144  
145 break;
146  
147 case 4: d=0x08;
148  
149 break;
150 case 5: d=0x10;
151  
152 break;
153  
154 case 6: d=0x20;
155  
156 break;
157 case 7: d=0x40;
158  
159 break;
160  
161 case 8: d=0x80;
162  
163 break;
164  
165 default:printf("chyba");
166  
167 break; }
168  
169 i2c_start();
170 I2C_Write(0xE0);
171  
172 I2C_write(d);
173 i2c_stop();
174  
175 }
176  
177 void batdata(void)
178 {
179 setHUB(1);
180 bat1PROUD=batPROUD();
181 bat1STAV=batSTAV();
182 bat1NAP=batNAP();
183 setHUB(2);
184 bat2PROUD=batPROUD();
185 bat2STAV=batSTAV();
186 bat2NAP=batNAP();
187 setHUB(3);
188 bat3PROUD=batPROUD();
189 bat3STAV=batSTAV();
190 bat3NAP=batNAP();
191  
192 bat1P=(bat1PROUD*(((float)bat1NAP/1000)));
193 bat2P=(bat2PROUD*(((float)bat2NAP/1000)));
194 bat3P=(bat3PROUD*(((float)bat3NAP/1000)));
195 }
196  
197  
198 void main()
199 {
200 batMODE(1);
201 float vypocet;
202 delay_ms(100);
203  
204  
205  
206  
207  
208  
209 setup_adc_ports(NO_ANALOGS|VSS_VDD);
210 setup_adc(ADC_CLOCK_DIV_2);
211 setup_spi(SPI_SS_DISABLED);
212 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
213 setup_timer_1(T1_DISABLED);
214 setup_timer_2(T2_DISABLED,0,1);
215 setup_ccp1(CCP_OFF);
216 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
217  
218 batMODE(1); //kvùli vypnutí
219 delay_ms(1);
220  
221 //printf("STAV1 STAV2 STAV3 VYKON1 VYKON2 VYKON3\r\n");
222 while(TRUE)
223 {
224  
225  
226  
227 batdata();
228 while(bat1STAV<20)
229 {
230 //printf("nedostatek energie\r\n");
231 if(bat2STAV>10)
232 {
233 batMODE(3);
234 }
235 else
236 {
237 if(bat3STAV>10)
238 {
239 batMODE(2);
240 }
241 else
242 {
243 batMODE(4);
244 }
245 }
246 batdata();
247 sprintf(baterka, " %3.0Ld %3.0Ld %3.0Ld %5.0Ld %5.0Ld %5.0Ld %5.0Ld", bat1STAV, bat2STAV, bat3STAV, bat1P, bat2P, bat3P, 0);
248 printf(baterka);
249 delay_ms(5000);
250 }
251  
252 while(bat2STAV>96)
253 {
254 //printf("bat2 prebita");
255 if(bat1STAV>98)
256 {
257 batMODE(6);
258 }
259 else
260 {
261 batMODE(3);
262 }
263  
264 batdata();
265 sprintf(baterka, " %3.0Ld %3.0Ld %3.0Ld %5.0Ld %5.0Ld %5.0Ld %5.0Ld", bat1STAV, bat2STAV, bat3STAV, bat1P, bat2P, bat3P, 0);
266 printf(baterka);
267 bat2STAV=bat2STAV-2;
268 delay_ms(5000);
269 }
270  
271 while(bat3STAV>96)
272 {
273 //printf("bat3 prebyta\r\n");
274 if(bat1STAV>98)
275 {
276 batMODE(5);
277 }
278 else
279 {
280 batMODE(2);
281 }
282  
283 batdata();
284 sprintf(baterka, " %3.0Ld %3.0Ld %3.0Ld %5.0Ld %5.0Ld %5.0Ld %5.0Ld", bat1STAV, bat2STAV, bat3STAV, bat1P, bat2P, bat3P, 0);
285 printf(baterka);
286 bat3STAV=bat3STAV-2;
287 delay_ms(5000);
288 }
289  
290 while(input(NOC)==1)
291 {
292 //printf("noc\r\n");
293 batdata();
294  
295 if(bat1STAV<98)
296 {
297  
298 if(bat2STAV>20)
299 {
300 batMODE(3);
301 }
302 else
303 {
304  
305 if(bat3STAV>20)
306 {
307 batMODE(2);
308 }
309 else
310 {
311 batMODE(1);
312 }
313  
314 }
315 batdata();
316 }
317 else
318 {
319 if(bat2STAV>20)
320 {
321 batMODE(6);
322 }
323 else
324 {
325 if(bat3STAV>20)
326 {
327 batMODE(5);
328 }
329 else
330 {
331 batMODE(1);
332 }
333 }
334 }
335  
336 batdata();
337 sprintf(baterka, " %3.0Ld %3.0Ld %3.0Ld %5.0Ld %5.0Ld %5.0Ld %5.0Ld", bat1STAV, bat2STAV, bat3STAV, bat1P, bat2P, bat3P, 0);
338 //sprintf(baterka, "%Ld %Ld %Ld 0 0 0 0" bat1STAV, bat2STAV, bat3STAV);
339 printf(baterka);
340 delay_ms(5000); //pøidat tøeba min 1min
341 }
342  
343  
344  
345 batMODE(1);
346 if(bat2P==0)
347 {
348 rozdil=9999;
349 }
350 else
351 {
352 vypocet=(((float)bat3P-bat2P)/bat2P)*100;
353 rozdil=(signed int)vypocet;
354  
355 }
356 sprintf(baterka, " %3.0Ld %3.0Ld %3.0Ld %5.0Ld %5.0Ld %5.0Ld %5.0Ld", bat1STAV, bat2STAV, bat3STAV, bat1P, bat2P, bat3P, rozdil);
357 printf(baterka);
358 delay_ms (20000);
359  
360  
361  
362  
363  
364 }
365 }
366