/* Power Shutdown circuit for LiPo and LiFe PIC12F675 GP5 NC GP4 NC GP3 NC GP2 LED GP1 FET-OFF GP0 AN-INPUT LiPo 1S 3-4.2V 2S 6-8.4V 3S 9-12.6V 4S 12-16.8V 128*1.4=179.2 179-128=51 */ #define stop_count 100 #define LED GPIO.GPIO2 #define OUT GPIO.GPIO1 unsigned short voltage; unsigned short count; unsigned short time1; unsigned short time2; unsigned short flag; unsigned short pres; void interrupt(){ if(INTCON.T0IF){ INTCON.T0IF=0; flag|=0x01; if(!ADCON0.GO_DONE){ ADCON0.GO_DONE=1; } } return; } void init(){ TRISIO=0x39; GPIO=0x00; ANSEL=0x61; ADCON0=0x01; OPTION_REG=0xD7; INTCON=0xE0; return; } main(){ init(); OUT=0; LED=1; count=0; pres=0; while(1){ if(PIR1.ADIF){ PIR1.ADIF=0; voltage=ADRESH; if(voltage<=127){ time2=0; if(count>=stop_count){ OUT=1; while(1); }else{ count++; } }else{ count=0; time2=voltage-127; } } if(flag&0x01){ pres++; if(time2>=51){ LED=1; time1=0; }else if(time2==0){ LED=0; }else if(time1>=time2){ time1=0; LED=!LED; }else if(pres==0){ time1++; } } } }