; Discharger Program 4 ; http://eleclabyrinth.blog.fc2.com/ ; Memory used = 2023 bytes out of 2048 #picaxe 14m2 #NO_DATA ;I/0 symbol ADC0 = B.3 ; in symbol ADC1 = B.5 ; in symbol ADC2 = C.0 ; in symbol ADC3 = C.4 ; in symbol OUT0 = B.2 ; out symbol OUT1 = B.4 ; out symbol OUT2 = C.1 ; out symbol OUT3 = C.2 ; out symbol VOL_SW = pinC.3 ; 1:0.9V 0:0.8V ;variables symbol Vcutoff09 = 267 ; 0.9V cutoff. (0.9-38.625/1000)/3.3*1024=267.3 symbol Vcutoff08 = 236 ; 0.8V cutoff. (0.8-38.625/1000)/3.3*1024=236.3 symbol Vtoggle_09V = 283; Toggle V. (0.95-38.625/1000)/3.3*1024=282.8 symbol Vtoggle_08V = 252; Toggle V. (0.85-38.625/1000)/3.3*1024=251.8 ;symbol Toggle_time = 250 ; Toggle Time. ms ;Task0 symbols symbol work_b00 = b0 symbol work_w01 = w1 symbol work_w02 = w2 symbol Vbatt0 = b6 ;battery0 voltage ;Task1 symbols symbol work_b10 = b1 symbol work_w11 = w4 symbol work_w12 = w5 symbol Vbatt1 = b7 ;battery1 voltage ;Task2 symbols symbol work_b20 = b14 symbol work_w21 = w8 symbol work_w22 = w9 symbol Vbatt2 = b12;battery2 voltage ;Task3 symbols symbol work_b30 = b15 symbol work_w31 = w11 symbol work_w32 = w12 symbol Vbatt3 = b13;battery3 voltage ;Task4 symbols symbol LI = 5 ; Logging interval 1-60(s) symbol work_b40 = b20 symbol work_b41 = b21 symbol time_w40 = w13 ; Vbatt ; 3.3V ADC10,-> 0-256 ; Max in 1.8V =559 ; ADC10*3300/1024 (mV) ; /10 Max 256 ; ADC10*3300/1024/10 ; = ADC10*0.322265625 = ADC10/3.103 ; = ADC10*10/31.03 = ADC10*10+15.5/31.03 ; = ADC10*20+31/62 = Vbatt ; ********** Task0 ********** start0: ;initialize low OUT0 ;OFF suspend 0 main0: ;Detection of insertion of the battery Dtc_BAT0: ;debug gosub RD_VOL_SW0 if work_b00 = 1 then gosub RDadc0_09V else gosub RDadc0_08V endif if work_b00 != 3 then goto Dtc_BAT0 endif gosub RD_VOL_SW0 if work_b00 = 1 then gosub RDadc0_09V else gosub RDadc0_08V endif if work_b00 != 3 then goto Dtc_BAT0 endif gosub RD_VOL_SW0 if work_b00 = 1 then gosub RDadc0_09V else gosub RDadc0_08V endif if work_b00 != 3 then goto Dtc_BAT0 endif ;Discharge high OUT0 ;ON ;debug Discharge0: gosub RD_VOL_SW0 if work_b00 = 1 then goto Discharge0_09V else goto Discharge0_08V endif Discharge0_09V: gosub RDadc0 if work_w02 < Vtoggle_09V then low OUT0 ;OFF pause 50 gosub RDadc0 pause 275 high OUT0 ;ON pause 40 gosub RDadc0_09V if work_b00 = 0 then goto stop_discg0 endif endif ;debug goto Discharge0 Discharge0_08V: gosub RDadc0 if work_w02 < Vtoggle_08V then low OUT0 ;OFF pause 50 gosub RDadc0 pause 275 high OUT0 ;ON pause 40 gosub RDadc0_08V if work_b00 = 0 then goto stop_discg0 endif endif ;debug goto Discharge0 ;stop discharge stop_discg0: low OUT0 pause 1 ; 100ohom*5000pF*10=5us ;Detection of the removal of the battery ; 125nA*104.7k ohom *1.05 = 0.014V ; --> 0.14V 0.14/3.3*1024=43.4 remove0: ;debug gosub RDadc0 if work_w02 > 44 then goto remove0 endif pause 2750 goto main0 ;Subroutine RD_VOL_SW0: if VOL_SW = 1 then work_b00 = 1 else work_b00 = 0 endif pause 10 if VOL_SW != work_b00 then goto RD_VOL_SW0 endif return RDadc0_09V: gosub RDadc0 if work_w02 > Vcutoff09 then work_b00 = 1 else work_b00 = 0 endif pause 100 gosub RDadc0 if work_w02 > Vcutoff09 then work_b00 = work_b00 + 1 endif pause 100 gosub RDadc0 if work_w02 > Vcutoff09 then work_b00 = work_b00 + 1 endif return RDadc0_08V: gosub RDadc0 if work_w02 > Vcutoff08 then work_b00 = 1 else work_b00 = 0 endif pause 100 gosub RDadc0 if work_w02 > Vcutoff08 then work_b00 = work_b00 + 1 endif pause 100 gosub RDadc0 if work_w02 > Vcutoff08 then work_b00 = work_b00 + 1 endif return RDadc0: readadc10 ADC0,work_w01 work_w02 = work_w01 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 readadc10 ADC0,work_w01 work_w02 = work_w01 + work_w02 + 7 / 14 Vbatt0 = work_w02 * 20 + 31 / 62 return ; ********** Task4 ********** start4: ;initialize pullup %0000111000010100 ;bit15-8=C.7-0 bit7-0=B.7-0 ; pullup off=0 B.1,B.3,B.5,C.0,C.4 ; B.0 pin is output only, so cannot have pullup! ; C.5 pin cannot have pullup! adcconfig %010 ; ADC Vref- = 0V, Vref+ = B.1, 3.3V hsersetup B38400_16, %10010 ;38400 baud, inverted TXD, disable hserin hserout 0,(cr) hserout 0,("\033[108A") ; ESC[24A up 108 line hserout 0,("\033[2J") ; ESC[2J CLS hserout 0,("\033[37m") ; ESC[37m, white time_w40 = time + LI resume 0 resume 1 resume 2 resume 3 main4: ;toggle C.1 ;for debug if time > 65525 then ; 65535-10 timer overflow end endif hserout 0,("\033[?25l") ; ESC[?25l Cursor off work_b41 = Vbatt0 work_b40 = work_b41 / 100 hserout 0,(#work_b40) pause 1 hserout 0,(".") work_b40 = work_b41 /10 % 10 hserout 0,(#work_b40) work_b40 = work_b41 % 10 hserout 0,(#work_b40) pause 1 hserout 0,(" ") work_b41 = Vbatt1 work_b40 = work_b41 / 100 hserout 0,(#work_b40) pause 1 hserout 0,(".") work_b40 = work_b41 /10 % 10 hserout 0,(#work_b40) work_b40 = work_b41 % 10 hserout 0,(#work_b40) pause 1 hserout 0,(" ") work_b41 = Vbatt2 work_b40 = work_b41 / 100 hserout 0,(#work_b40) pause 1 hserout 0,(".") work_b40 = work_b41 /10 % 10 hserout 0,(#work_b40) work_b40 = work_b41 % 10 hserout 0,(#work_b40) pause 1 hserout 0,(" ") work_b41 = Vbatt3 work_b40 = work_b41 / 100 hserout 0,(#work_b40) pause 1 hserout 0,(".") work_b40 = work_b41 /10 % 10 hserout 0,(#work_b40) work_b40 = work_b41 % 10 hserout 0,(#work_b40) pause 1 hserout 0,(cr) pause 1 hserout 0,(lf) pause 1 hserout 0,("\033[?25h") ; ESC[?25l Cursor on Log_interval: if time > time_w40 then time_w40 = time_w40 + LI goto main4 else goto Log_interval endif ; ********** Task1 ********** start1: ;initialize low OUT1 suspend 1 main1: ;Detection of insertion of the battery Dtc_BAT1: ;debug gosub RD_VOL_SW1 if work_b10 = 1 then gosub RDadc1_09V else gosub RDadc1_08V endif if work_b10 != 3 then goto Dtc_BAT1 endif gosub RD_VOL_SW1 if work_b10 = 1 then gosub RDadc1_09V else gosub RDadc1_08V endif if work_b10 != 3 then goto Dtc_BAT1 endif gosub RD_VOL_SW1 if work_b10 = 1 then gosub RDadc1_09V else gosub RDadc1_08V endif if work_b10 != 3 then goto Dtc_BAT1 endif ;Discharge high OUT1 ;debug Discharge1: gosub RD_VOL_SW1 if work_b10 = 1 then goto Discharge1_09V else goto Discharge1_08V endif Discharge1_09V: gosub RDadc1 if work_w12 < Vtoggle_09V then low OUT1 ;OFF pause 50 gosub RDadc1 pause 275 high OUT1 ;ON pause 40 gosub RDadc1_09V if work_b10 = 0 then goto stop_discg1 endif endif ;debug goto Discharge1 Discharge1_08V: gosub RDadc1 if work_w12 < Vtoggle_08V then low OUT1 ;OFF pause 50 gosub RDadc1 pause 275 high OUT1 ;ON pause 40 gosub RDadc1_08V if work_b10 = 0 then goto stop_discg1 endif endif ;debug goto Discharge1 ;stop discharge stop_discg1: low OUT1 pause 1 ; 100ohom*5000pF*10=5us ;Detection of the removal of the battery ; 125nA*104.7k ohom *1.05 = 0.014V ; --> 0.14V 0.14/3.3*1024=43.4 remove1: ;debug gosub RDadc1 if work_w12 > 44 then goto remove1 endif pause 2750 goto main1 ;Subroutine RD_VOL_SW1: if VOL_SW = 1 then work_b10 = 1 else work_b10 = 0 endif pause 10 if VOL_SW != work_b10 then goto RD_VOL_SW1 endif return RDadc1_09V: gosub RDadc1 if work_w12 > Vcutoff09 then work_b10 = 1 else work_b10 = 0 endif pause 100 gosub RDadc1 if work_w12 > Vcutoff09 then work_b10 = work_b10 + 1 endif pause 100 gosub RDadc1 if work_w12 > Vcutoff09 then work_b10 = work_b10 + 1 endif return RDadc1_08V: gosub RDadc1 if work_w12 > Vcutoff08 then work_b10 = 1 else work_b10 = 0 endif pause 100 gosub RDadc1 if work_w12 > Vcutoff08 then work_b10 = work_b10 + 1 endif pause 100 gosub RDadc1 if work_w12 > Vcutoff08 then work_b10 = work_b10 + 1 endif return RDadc1: readadc10 ADC1,work_w11 work_w12 = work_w11 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 readadc10 ADC1,work_w11 work_w12 = work_w11 + work_w12 + 7 / 14 Vbatt1 = work_w12 * 20 + 31 / 62 return ; ********** Task2 ********** start2: ;initialize low OUT2 suspend 2 main2: ;Detection of insertion of the battery Dtc_BAT2: ;debug gosub RD_VOL_SW2 if work_b20 = 1 then gosub RDadc2_09V else gosub RDadc2_08V endif if work_b20 != 3 then goto Dtc_BAT2 endif gosub RD_VOL_SW2 if work_b20 = 1 then gosub RDadc2_09V else gosub RDadc2_08V endif if work_b20 != 3 then goto Dtc_BAT2 endif gosub RD_VOL_SW2 if work_b20 = 1 then gosub RDadc2_09V else gosub RDadc2_08V endif if work_b20 != 3 then goto Dtc_BAT2 endif ;Discharge high OUT2 ;debug Discharge2: gosub RD_VOL_SW2 if work_b20 = 1 then goto Discharge2_09V else goto Discharge2_08V endif Discharge2_09V: gosub RDadc2 if work_w22 < Vtoggle_09V then low OUT2 ;OFF pause 50 gosub RDadc2 pause 275 high OUT2 ;ON pause 40 gosub RDadc2_09V if work_b20 = 0 then goto stop_discg2 endif endif ;debug goto Discharge2 Discharge2_08V: gosub RDadc2 if work_w22 < Vtoggle_08V then low OUT2 ;OFF pause 50 gosub RDadc2 pause 275 high OUT2 ;ON pause 40 gosub RDadc2_08V if work_b20 = 0 then goto stop_discg2 endif endif ;debug goto Discharge2 ;stop discharge stop_discg2: low OUT2 pause 1 ; 100ohom*5000pF*10=5us ;Detection of the removal of the battery ; 125nA*104.7k ohom *1.05 = 0.014V ; --> 0.14V 0.14/3.3*1024=43.4 remove2: gosub RDadc2 if work_w22 > 44 then goto remove2 endif pause 2750 goto main2 ;Subroutine RD_VOL_SW2: if VOL_SW = 1 then work_b20 = 1 else work_b20 = 0 endif pause 10 if VOL_SW != work_b20 then goto RD_VOL_SW2 endif return RDadc2_09V: gosub RDadc2 if work_w22 > Vcutoff09 then work_b20 = 1 else work_b20 = 0 endif pause 100 gosub RDadc2 if work_w22 > Vcutoff09 then work_b20 = work_b20 + 1 endif pause 100 gosub RDadc2 if work_w22 > Vcutoff09 then work_b20 = work_b20 + 1 endif return RDadc2_08V: gosub RDadc2 if work_w22 > Vcutoff08 then work_b20 = 1 else work_b20 = 0 endif pause 100 gosub RDadc2 if work_w22 > Vcutoff08 then work_b20 = work_b20 + 1 endif pause 100 gosub RDadc2 if work_w22 > Vcutoff08 then work_b20 = work_b20 + 1 endif return RDadc2: readadc10 ADC2,work_w21 work_w22 = work_w21 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 readadc10 ADC2,work_w21 work_w22 = work_w21 + work_w22 + 7 / 14 Vbatt2 = work_w22 * 20 + 31 / 62 return ; ********** Task3 ********** start3: ;initialize low OUT3 suspend 3 main3: ;Detection of insertion of the battery Dtc_BAT3: ;debug gosub RD_VOL_SW3 if work_b30 = 1 then gosub RDadc3_09V else gosub RDadc3_08V endif if work_b30 != 3 then goto Dtc_BAT3 endif gosub RD_VOL_SW3 if work_b30 = 1 then gosub RDadc3_09V else gosub RDadc3_08V endif if work_b30 != 3 then goto Dtc_BAT3 endif gosub RD_VOL_SW3 if work_b30 = 1 then gosub RDadc3_09V else gosub RDadc3_08V endif if work_b30 != 3 then goto Dtc_BAT3 endif ;Discharge high OUT3 ;debug Discharge3: gosub RD_VOL_SW3 if work_b30 = 1 then goto Discharge3_09V else goto Discharge3_08V endif Discharge3_09V: gosub RDadc3 if work_w32 < Vtoggle_09V then low OUT3 ;OFF pause 50 gosub RDadc3 pause 275 high OUT3 ;ON pause 40 gosub RDadc3_09V if work_b30 = 0 then goto stop_discg3 endif endif ;debug goto Discharge3 Discharge3_08V: gosub RDadc3 if work_w32 < Vtoggle_08V then low OUT3 ;OFF pause 50 gosub RDadc3 pause 275 high OUT3 ;ON pause 40 gosub RDadc3_08V if work_b30 = 0 then goto stop_discg3 endif endif ;debug goto Discharge3 ;stop discharge stop_discg3: low OUT3 pause 1 ; 100ohom*5000pF*10=5us ;Detection of the removal of the battery ; 125nA*104.7k ohom *1.05 = 0.014V ; --> 0.14V 0.14/3.3*1024=43.4 remove3: gosub RDadc3 if work_w32 > 44 then goto remove3 endif pause 2750 goto main3 ;Subroutine RD_VOL_SW3: if VOL_SW = 1 then work_b30 = 1 else work_b30 = 0 endif pause 10 if VOL_SW != work_b30 then goto RD_VOL_SW3 endif return RDadc3_09V: ;toggle B.0 gosub RDadc3 if work_w32 > Vcutoff09 then work_b30 = 1 else work_b30 = 0 endif pause 100 gosub RDadc3 if work_w32 > Vcutoff09 then work_b30 = work_b30 + 1 endif pause 100 gosub RDadc3 if work_w32 > Vcutoff09 then work_b30 = work_b30 + 1 endif ;toggle B.0 return RDadc3_08V: gosub RDadc3 if work_w32 > Vcutoff08 then work_b30 = 1 else work_b30 = 0 endif pause 100 gosub RDadc3 if work_w32 > Vcutoff08 then work_b30 = work_b30 + 1 endif pause 100 gosub RDadc3 if work_w32 > Vcutoff08 then work_b30 = work_b30 + 1 endif return RDadc3: readadc10 ADC3,work_w31 work_w32 = work_w31 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 readadc10 ADC3,work_w31 work_w32 = work_w31 + work_w32 + 7 / 14 Vbatt3 = work_w32 * 20 + 31 / 62 return