4.3.4_プログラムリスト(BATAUTSW_AD_User.c)

1.プログラム処理について

   1.初期処理(UserInitialise())
     ・基準電圧15VのAD変換値(BV_15v)の設定(固定値)
       バッテリ電圧(BV_PV)を求める為の15VのAD変換値で、安定化電源を正確に15Vに設定し、
      実際に装置に接続して値を決めます。マイコンのA/D変換の基準電圧(安定化電源要)により値が
      変わる為、装置を作成する場合には調整が必要です。
      BV_15vの値はBV_PV_Work変数に入ります。LCDに表示させて値を決めます。

     ・内部参照電圧(BV_MV)の設定(固定値)
       バッテリ電圧監視用の参照値です。12.6Vに設定しています。

     ・JP設定電圧(BV_UV, BV_LV,BVChON_v, BVChOFF_v)の設定
       装置のJP設定の電圧をそれぞれの変数に設定します。

     ・INITIAL LCD表示(JP設定電圧)
        JP設定電圧をそれぞれ3秒間表示します。


   2.常駐(UserLoop())
      ・バッテリ電圧監視LED表示処理
        バッテリ電圧(BV_PV)とJP設定電圧(BV_UV, BV_LV)を参照してLED(D6(赤),D7(緑))を
       点灯,点滅します。リレー信号(D5)がONの場合は赤,緑のLEDが激しく(1/8秒間隔)点滅します。

      ・バッテリ電源自動電源,切断処理
        バッテリ電圧(BV_PV)とJP設定電圧(BVChON_v, BVChOFF_v)を参照してリレー信号(D5)を
       ON/OFFします。

      ・タイマーSW処理
        バッテリ充電器タイマ1〜4SW(D0,D1,D2,D3)の状態とタイマカウントとバッテリ電圧(BV_PV)を
       参照してリレー信号(D5)をON/OFFします。

      ※常駐は常に、ユーザ定義のタイマ(LED表示)関数を実行して0.5秒周期で動作します。


   3.割り込み(ad_data_v())
     ADコンバータエレメントで設定した割り込みで、AD変換完了時に実行されます。
     BV_PV変数に小数点なし3桁のバッテリ電圧値が常に入ります。


   4.ユーザ定義関数
     ・TimerCount()
       0.5秒周期で、カウントするAllCount変数を時(HourCount),分(MinCount),秒(SecCount)に変換する処理
     ・Timer_GB()
       LEDのGB表示(1秒点滅)の0.5秒タイマ処理
     ・Timer_G()
       LEDのG表示の0.5秒タイマ処理
     ・Timer_RB()
       LEDのRB表示(1秒点滅)の0.5秒タイマ処理
     ・Timer_R()
       LEDのR表示の0.5秒タイマ処理
     ・Timer_GBRB()
       LEDのGB&RB表示(1/8秒点滅)の0.5秒タイマ処理


2.プログラムリスト(BATAUTSW_AD_User.c)
修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include"D:\\WIZ_C\\Projects\\BATAUTSW_AD\\BATAUTSW_AD_Auto.h"
#include <Strings.h>
#include <Delays.h>
#__config 0x3f72

unsigned char    out_str1[17], out_str2[17], out_str3[5];
unsigned long    BV_15v, BV_MV, BV_PV, BV_UV, BV_LV, BV_ChargeON_V, BV_ChargeOFF_V;
unsigned long    BV_Work1, BV_Work2, BV_PV_Work, BV_PV_Work1, BV_PV_Work2;
unsigned long    AllCount, AllSecCount, SecCount, MinCount, HourCount;
bit    BV_Charge;
bit    SW_Timer,SW_Timer1,SW_Timer2,SW_Timer3,SW_Timer4;
bit    Timer_Reset1,Timer_Reset2;
bit    LED_Work1,LED_Work2;

void TimerCount();
void Timer_GB();
void Timer_G();
void Timer_RB();
void Timer_R();
void Timer_GBRB();

//
// This file includes all user definable routines. It may be changed at will as
// it will not be regenerated once the application has been generated for the
// first time.
//

//*******************************************************************************
//
// Insert your interrupt handling code if required here.
// Note quick interrupts are used so code must be simple
// See the manual for details of quick interrupts.
//

void UserInterrupt()
{
    // Insert your code here

#asmline SETPCLATH UserIntReturn,-1 ; SETPCLATH for interrupt routine
#asmline goto UserIntReturn     ; Assembler - go back to interrupt routine
}


//*******************************************************************************
//
// Insert your initialisation code if required here.
// Note that when this routine is called Interrupts will not be enabled - the
// Application Designer will enable them before the main loop
//

void UserInitialise()
{
    BV_15v = 753;        //15V standard a/d value
    BV_MV = 126;        //compare voltage(12.6V)
    if(PC.B0)
    {
        if(PC.B1)
            BV_UV = 139;        //13.9V
        else
            BV_UV = 148;        //14.8V
    }
    else
    {
        if(PC.B1)
            BV_UV = 144;        //14.4V
        else
            BV_UV = 155;        //15.5V
    }    
    if(PC.B2)
    {
        if(PC.B3)
            BV_LV = 112;        //11.2V
        else
            BV_LV = 116;        //11.6V
    }
    else
    {
        if(PC.B3)
            BV_LV = 114;        //11.4V
        else
            BV_LV = 118;        //11.8V
    }    
    if(PC.B4)
    {
        if(PC.B5)
            BV_ChargeON_V = 110;    //11.0V
        else
            BV_ChargeON_V = 114;    //11.4V
    }
    else
    {
        if(PC.B5)
            BV_ChargeON_V = 112;    //11.2V
        else
            BV_ChargeON_V = 116;    //11.6V
    }    
    if(PC.B6)
    {
        if(PC.B7)
            BV_ChargeOFF_V = 120;    //12.0V
        else
            BV_ChargeOFF_V = 124;    //12.4V
    }
    else
    {
        if(PC.B7)
            BV_ChargeOFF_V = 122;    //12.2V
        else
            BV_ChargeOFF_V = 126;    //12.6V
    }    
    
    LCDClear();
    sprintf(out_str1,"INITIALIZE ");
    BV_Work1 = BV_UV / 10;
    BV_Work2 = BV_UV % 10;
    sprintf(out_str2,"BV_UV:%ld.%ldV ",BV_Work1,BV_Work2);    
    LCDPrintAt(0,0);
    LCDString(out_str1);
    LCDPrintAt(0,1);
    LCDString(out_str2);
    Wait(3000);
    BV_Work1 = BV_LV / 10;
    BV_Work2 = BV_LV % 10;
    sprintf(out_str2,"BV_LV:%ld.%ldV ",BV_Work1,BV_Work2);    
    LCDPrintAt(0,1);
    LCDString(out_str2);
    Wait(3000);
    BV_Work1 = BV_ChargeON_V / 10;
    BV_Work2 = BV_ChargeON_V % 10;
    sprintf(out_str2,"ChON_V:%ld.%ldV ",BV_Work1,BV_Work2);    
    LCDPrintAt(0,1);
    LCDString(out_str2);
    Wait(3000);
    BV_Work1 = BV_ChargeOFF_V / 10;
    BV_Work2 = BV_ChargeOFF_V % 10;
    sprintf(out_str2,"ChOFF_V:%ld.%ldV ",BV_Work1,BV_Work2);    
    LCDPrintAt(0,1);
    LCDString(out_str2);
    Wait(3000);
    LCDClear();
    ADCON0 |= (1<<GO);

}

//*******************************************************************************
//
// Insert your main loop code if required here. This routine will be called
// as part of the main loop code
//

void UserLoop()
{
    if(!PD.B0)
    {
        strcpy(out_str3,"(1H)");
        SW_Timer = 1;
        SW_Timer1 = 1;
        SW_Timer2 = 0;
        SW_Timer3 = 0;
        SW_Timer4 = 0;
        Timer_Reset1 = 1;
    }    
    if(!PD.B1)
    {
        strcpy(out_str3,"(2H)");
        SW_Timer = 1;
        SW_Timer1 = 0;
        SW_Timer2 = 1;
        SW_Timer3 = 0;
        SW_Timer4 = 0;
        Timer_Reset1 = 1;
    }    
    if(!PD.B2)
    {
        strcpy(out_str3,"(3H)");
        SW_Timer = 1;
        SW_Timer1 = 0;
        SW_Timer2 = 0;
        SW_Timer3 = 1;
        SW_Timer4 = 0;
        Timer_Reset1 = 1;
    }    
    if(!PD.B3)
    {
        strcpy(out_str3,"(4H)");
        SW_Timer = 1;
        SW_Timer1 = 0;
        SW_Timer2 = 0;
        SW_Timer3 = 0;
        SW_Timer4 = 1;
        Timer_Reset1 = 1;
    }
    if(Timer_Reset1)
    {
        Timer_Reset1 = 0;
        AllCount = 0;
        AllSecCount = 0;
        SecCount = 0;
        MinCount = 0;
        HourCount = 0;
    }

    if(!PD.B4)
        Timer_Reset2 = 1;
    if(SW_Timer1)
        if(HourCount == 1)
            Timer_Reset2 = 1;
    if(SW_Timer2)
        if(HourCount == 2)
            Timer_Reset2 = 1;
    if(SW_Timer3)
        if(HourCount == 3)
            Timer_Reset2 = 1;
    if(SW_Timer4)
        if(HourCount == 4)
            Timer_Reset2 = 1;
    if(BV_PV > BV_UV)
        Timer_Reset2 = 1;
    if(Timer_Reset2)
    {
        Timer_Reset2 = 0;
        SW_Timer = 0;
        SW_Timer1 = 0;    
        SW_Timer2 = 0;    
        SW_Timer3 = 0;
        SW_Timer4 = 0;
        BV_Charge = 0;
    }

    BV_PV_Work1 = BV_PV / 10;
    BV_PV_Work2 = BV_PV % 10;
    sprintf(out_str1,"BATT VOLT:%ld.%ldV ",BV_PV_Work1,BV_PV_Work2);
    if(SW_Timer)
        sprintf(out_str2,"TIMER%s:%02ld:%02ld",out_str3,HourCount,MinCount);
    else
    {
        sprintf(out_str2," ");
        AllCount = 0;
        AllSecCount = 0;
        SecCount = 0;
        MinCount = 0;
        HourCount = 0;
    }

    LCDPrintAt(0,0);
    LCDString(out_str1);
    LCDPrintAt(0,1);
    LCDString(out_str2);

    if(SW_Timer)
        BV_Charge = 1;
    else
    {
        if(BV_PV >= BV_ChargeOFF_V)
            BV_Charge = 0;
        if(BV_PV > 0)
            if(!BV_Charge)
                if(BV_PV <= BV_ChargeON_V)
                    BV_Charge = 1;
    }

    if(BV_Charge)
    {
        PD.B5 = 1;
        Timer_GBRB();
    }
    else
    {
        PD.B5 = 0;
        if(BV_PV > BV_UV)
            Timer_GB();
        if(BV_PV <= BV_UV)
            if(BV_PV >= BV_MV)    
                Timer_G();
        if(BV_PV < BV_MV)
            if(BV_PV >= BV_LV)
                Timer_RB();    
        if(BV_PV < BV_LV)
            Timer_R();
    }                                                        
    TimerCount();

}

//
// User occurrence code
//

//
// Occurrence - A/D Conversion
//

void ad_data_v()
{
    BV_PV_Work = (unsigned long)ADRESH << 8;
    BV_PV_Work = BV_PV_Work | ADRESL;
    BV_PV = BV_PV_Work * 150 / BV_15v;
    ADCON0 |= (1<<GO);
}

void TimerCount()
{
    AllCount = AllCount + 1;
    AllSecCount = AllCount / 2;
    HourCount = AllSecCount / 3600;
    MinCount = AllSecCount / 60;
    MinCount = MinCount % 60;
    SecCount = AllSecCount % 60;
}

void Timer_GB()
{
    PD.B6 = 0;
    if(LED_Work1)
    {
        PD.B7 = 1;
        LED_Work1 = 0;
    }
    else
    {
        PD.B7 = 0;    
        LED_Work1 = 1;
    }
    Wait(500);
}

void Timer_G()
{
    PD.B6 = 0;
    PD.B7 = 1;
    Wait(500);
}

void Timer_RB()
{
    PD.B7 = 0;
    if(LED_Work2)
    {
        PD.B6 = 1;
        LED_Work2 = 0;
    }
    else
    {
        PD.B6 = 0;    
        LED_Work2 = 1;
    }
    Wait(500);
}

void Timer_R()
{
    PD.B6 = 1;
    PD.B7 = 0;
    Wait(500);
}

void Timer_GBRB()
{
    PD.B6 = 1;
    PD.B7 = 1;
    Wait(63);
    PD.B6 = 0;
    PD.B7 = 0;
    Wait(62);
    PD.B6 = 1;
    PD.B7 = 1;
    Wait(63);
    PD.B6 = 0;
    PD.B7 = 0;
    Wait(62);
    PD.B6 = 1;
    PD.B7 = 1;
    Wait(63);
    PD.B6 = 0;
    PD.B7 = 0;
    Wait(62);
    PD.B6 = 1;
    PD.B7 = 1;
    Wait(63);
    PD.B6 = 0;
    PD.B7 = 0;
    Wait(62);

}




トップページヘもどる