////////////////////////////////////////////////////////

//

//   TR_con01 サンプルソフト

//        PIC12F675 HitschC

//

//   2011.01.20.

//        HARD:TR_CON01

//        FILE = TR_CON01_test01.c

//        PIC  = PIC12F675

//        GP0:(IN) 入力電圧 300K-100Kで分圧

//        GP1:(OUT) LOAD

//        GP2:(OUT) Charge H=OFF

//        GP3:(IN) ResetSW Normaly=H

//        GP4:(IN) Day/Night H=Day

//        GP5:(OUT)Monitor L=ON

//

////////////////////////////////////////////////////////

 

#include <htc.h>

 

 

__CONFIG(

UNPROTECT & BORDIS & MCLREN & PWRTEN & WDTDIS & INTIO);

 

 

 

#define _XTAL_FREQ 4000000

 

#define MONITOR_ON     GPIO5=0;          //モニタ点灯

#define MONITOR_OFF    GPIO5=1;          //モニタ消灯

#define CHARGE_ON       GPIO2=0;          //充電

#define CHARGE_OFF      GPIO2=1;          //充電停止

#define LOAD_ON                      GPIO1=1;          //負荷接続

#define LOAD_OFF         GPIO1=0;          //負荷遮断

 

/*旧型切削加工基板の場合

#define CHARGE_ON       GPIO1=0;          //充電

#define CHARGE_OFF      GPIO1=1;          //充電停止

#define LOAD_ON                      GPIO2=1;          //負荷接続

#define LOAD_OFF         GPIO2=0;          //負荷遮断

*/

 

void adinit(void){             //ADCON0 register

           ADON=1;           //AD ON

           ADFM=1;           //right

           VCFG=0;           //Vref =Vdd

           CHS1=0;           //

           CHS0=0;           //とりあえずCH0

           ANSEL=0b00000001;       //AN0のみ使用

}

 

 

unsigned int adconv(unsigned char adselect){          //AD変換関数 chを指定

           unsigned int ad_data;

           ADCON0 = (ADCON0 & 0b11110011) |(( adselect <<2) & 0b00001100);//ADチャンネルをセット

           __delay_us(20);//20us wait

           GODONE=1;       //start AD

           while(GODONE);  //Wait AD finish

           ad_data =ADRESH;

           ad_data =( ad_data<<8 )| ADRESL;

           return ad_data;

}

 

 

unsigned char v_stat( unsigned int v )        //電圧状態設定 各電圧の設定はこの関数で行う

{

           unsigned char stat;

 

           if( v < 553 )

                      stat=0;                                                                      //10.8V(10.8/20*1024)未満

                      else if(( v >= 553) && (v <614 ))

                      stat=1;                                                                      //11V以上12V(12/20*1024)未満

                                  else if(( v >= 614) && (v <666 ))

                                             stat=2;                                               //12V以上13(13/20*1024)未満

                                             else if(( v >= 666) && (v < 717))

                                                        stat=3;                                    //13V以上14(14/20*1024)未満

                                                        else if( v >= 717 )

                                                                   stat=4;                         //14以上

           else{}

           return( stat );

}

 

void main()

{

           unsigned int v_bat;                      //バッテリ電圧

           unsigned char bat_stat;    //バッテリー電圧ステータス

           unsigned char kajyuuden_flg;                    //1=過充電

           unsigned char kahouden_flg;                    //1=過放電

 

           CMCON = 0b00000111;   //コンパレータは使用しない

           adinit();             //ADコンバータ初期設定

           TRISIO = 0b00011001;

           GPIO = 0b11111111;

 

           kajyuuden_flg=0;

           kahouden_flg=0;

           CHARGE_ON LOAD_ON

 

           while(1)

           {

                      v_bat = adconv(0);                                 //蓄電池電圧→v_bat

                      bat_stat=v_stat( v_bat );                //蓄電池状態→bat_stat

 

           //充放電制御 & モニタ制御(処理中は消灯)

                      switch( bat_stat ){

                                  case 0:                        //過放電

                                             kahouden_flg=1;

                                             LOAD_OFF

                                             CHARGE_ON

                                             MONITOR_ON    __delay_ms(100);  //過放電

                                             MONITOR_OFF   __delay_ms(2000);

                                             break;

                                  case 1:

                                             if( kahouden_flg==1 ){      //過放電

                                                        LOAD_OFF CHARGE_ON

                                             }

                                             else{                            //過放電から復帰していた場合

                                                        LOAD_ON CHARGE_ON

                                             }

                                             MONITOR_ON    __delay_ms(100);  //電圧低下

                                             MONITOR_OFF   __delay_ms(1000);

 

                                             break;

                                  case 2:                                    //正常

                                             kahouden_flg=0;

                                             LOAD_ON

                                             kajyuuden_flg=0;

                                             CHARGE_ON

                                             MONITOR_ON    __delay_ms(300);  //正常

                                             MONITOR_OFF   __delay_ms(1000);

                                             break;

                                  case 3:

                                             if( kajyuuden_flg==1 ){      //過充電

                                                        LOAD_ON CHARGE_OFF

                                             }

                                             else{                            //過充電から復帰していた場合

                                                        LOAD_ON CHARGE_ON

                                             }

                                             MONITOR_ON    __delay_ms(500);  //ほぼ充電完了

                                             MONITOR_OFF   __delay_ms(1000);

                                             break;

                                  case 4:

                                             kajyuuden_flg=1;             //過充電

                                             LOAD_ON

                                             CHARGE_OFF

                                             MONITOR_ON    __delay_ms(800);  //過充電

                                             MONITOR_OFF   __delay_ms(1000);

                                             break;

                                  default:

                                             break;

                      }

 

           }

}