4.4.1_プログラムリスト(BATCONTROL_User.c)

修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include "D:\\WIZ_C\\Ver11\\Projects\\BATTCONTROL\\BATTCONTROL_Auto.h"
#include <Strings.h>
#include <Delays.h>

#__config _CONFIG1H, 0x02
#__config _CONFIG1L, 0x00
#__config _CONFIG2H, 0x0E
#__config _CONFIG2L, 0x0E
#__config _CONFIG3H, 0x01
#__config _CONFIG3L, 0x00
#__config _CONFIG4H, 0x00
#__config _CONFIG4L, 0x81

unsigned char    out_str1[17], out_str2[17], out_str3[5];
unsigned long    BV_15v, BV_PV;
unsigned long    BV_PV_Work, BV_PV_Work1, BV_PV_Work2;
unsigned long    AllCount, AllSecCount, SecCount, MinCount, HourCount;

WORD    BV_MV, BV_UV, BV_LV, BV_ChargeON_V, BV_ChargeOFF_V;
WORD    BV_Work1, BV_Work2, EE_Work1;

BYTE    dt1, dt2;

bit    EE_SetMode, EE_Check;
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 goto UserIntReturn    ; PIC 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 = 727;        //15V standard a/d value
    BV_MV = 126;        //compare voltage(12.6V)
    
    dt1 = ReadEEData(0);
    dt2 = ReadEEData(1);
    EE_Work1 = (WORD)dt2 << 8;
    BV_UV = EE_Work1 | dt1;
    dt1 = ReadEEData(2);
    dt2 = ReadEEData(3);
    EE_Work1 = (WORD)dt2 << 8;
    BV_LV = EE_Work1 | dt1;
    dt1 = ReadEEData(4);
    dt2 = ReadEEData(5);
    EE_Work1 = (WORD)dt2 << 8;
    BV_ChargeON_V = EE_Work1 | dt1;
    dt1 = ReadEEData(6);
    dt2 = ReadEEData(7);
    EE_Work1 = (WORD)dt2 << 8;
    BV_ChargeOFF_V = EE_Work1 | dt1;
            
    if(BV_UV < 105)
        EE_Check = 1;
    if(BV_UV > 155)
        EE_Check = 1;
    if(BV_LV < 105)
        EE_Check = 1;
    if(BV_LV > 155)
        EE_Check = 1;
    if(BV_LV >= BV_UV)
        EE_Check = 1;        
    if(BV_ChargeON_V < 105)
        EE_Check = 1;
    if(BV_ChargeON_V > 155)
        EE_Check = 1;
    if(BV_ChargeOFF_V < 105)
        EE_Check = 1;
    if(BV_ChargeOFF_V > 155)
        EE_Check = 1;
    if(BV_ChargeOFF_V <= BV_ChargeON_V)
        EE_Check = 1;

    if(EE_Check)                        
    {
        //INITIAL SET VALUE
        BV_UV = 144;        //14.4V
        BV_LV = 114;        //11.4V
        BV_ChargeON_V = 112;    //11.2V
        BV_ChargeOFF_V = 122;    //12.2V
    }    
    
    LCDClear();
    if(EE_Check)
        sprintf(out_str1,"SET VALUE FALSE ");
    else
        sprintf(out_str1,"INITIALIZE ");
    BV_Work1 = BV_UV / 10;
    BV_Work2 = BV_UV % 10;
    sprintf(out_str2,"BV_UV:%d.%dV ",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:%d.%dV ",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:%d.%dV ",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:%d.%dV ",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(!PC.B0)
        EE_SetMode = 1;
    if(EE_SetMode)
    {
        PD.B6 = 0;
        PD.B7 = 0;
        LCDClear();
        sprintf(out_str1,"SETTING MODE ");
        LCDPrintAt(0,0);
        LCDString(out_str1);
        Wait(2000);
//BV_UV SETTING
        BV_Work1 = BV_UV / 10;
        BV_Work2 = BV_UV % 10;
        sprintf(out_str2,"BV_UV:%d.%dV ",BV_Work1,BV_Work2);    
        LCDPrintAt(0,1);
        LCDString(out_str2);
        while(PC.B0)
        {
            if(!PC.B1)
            {
                BV_UV = BV_UV + 1;
                if(BV_UV > 155)
                    BV_UV = 155;
                BV_Work1 = BV_UV / 10;
                BV_Work2 = BV_UV % 10;
                sprintf(out_str2,"BV_UV:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
            if(!PC.B2)
            {
                BV_UV = BV_UV - 1;
                if(BV_UV < 105)
                    BV_UV = 105;
                BV_Work1 = BV_UV / 10;
                BV_Work2 = BV_UV % 10;
                sprintf(out_str2,"BV_UV:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
        }
        sprintf(out_str2,"BV_UV:WRITING. ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
        dt1 = BV_UV & 0x00FF;
        dt2 = BV_UV >> 8;
        WriteEEData(0,dt1);
        Wait(500);
        WriteEEData(1,dt2);
        Wait(1000);
//BV_LV SETTING
        BV_Work1 = BV_LV / 10;
        BV_Work2 = BV_LV % 10;
        sprintf(out_str2,"BV_LV:%d.%dV ",BV_Work1,BV_Work2);    
        LCDPrintAt(0,1);
        LCDString(out_str2);
        while(PC.B0)
        {
            if(!PC.B1)
            {
                BV_LV = BV_LV + 1;
                if(BV_LV > 155)
                    BV_LV = 155;
                BV_Work1 = BV_LV / 10;
                BV_Work2 = BV_LV % 10;
                sprintf(out_str2,"BV_LV:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
            if(!PC.B2)
            {
                BV_LV = BV_LV - 1;
                if(BV_LV < 105)
                    BV_LV = 105;
                BV_Work1 = BV_LV / 10;
                BV_Work2 = BV_LV % 10;
                sprintf(out_str2,"BV_LV:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
        }
        sprintf(out_str2,"BV_LV:WRITING. ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
        dt1 = BV_LV & 0x00FF;
        dt2 = BV_LV >> 8;
        WriteEEData(2,dt1);
        Wait(500);
        WriteEEData(3,dt2);
        Wait(1000);
//ChON_V SETTING
        BV_Work1 = BV_ChargeON_V / 10;
        BV_Work2 = BV_ChargeON_V % 10;
        sprintf(out_str2,"ChON_V:%d.%dV ",BV_Work1,BV_Work2);    
        LCDPrintAt(0,1);
        LCDString(out_str2);
        while(PC.B0)
        {
            if(!PC.B1)
            {
                BV_ChargeON_V = BV_ChargeON_V + 1;
                if(BV_ChargeON_V > 155)
                    BV_ChargeON_V = 155;
                BV_Work1 = BV_ChargeON_V / 10;
                BV_Work2 = BV_ChargeON_V % 10;
                sprintf(out_str2,"ChON_V:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
            if(!PC.B2)
            {
                BV_ChargeON_V = BV_ChargeON_V - 1;
                if(BV_ChargeON_V < 105)
                    BV_ChargeON_V = 105;
                BV_Work1 = BV_ChargeON_V / 10;
                BV_Work2 = BV_ChargeON_V % 10;
                sprintf(out_str2,"ChON_V:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
        }
        sprintf(out_str2,"ChON_V:WRITING. ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
        dt1 = BV_ChargeON_V & 0x00FF;
        dt2 = BV_ChargeON_V >> 8;
        WriteEEData(4,dt1);
        Wait(500);
        WriteEEData(5,dt2);
        Wait(1000);
//ChOFF_V SETTING
        BV_Work1 = BV_ChargeOFF_V / 10;
        BV_Work2 = BV_ChargeOFF_V % 10;
        sprintf(out_str2,"ChOFF_V:%d.%dV ",BV_Work1,BV_Work2);    
        LCDPrintAt(0,1);
        LCDString(out_str2);
        while(PC.B0)
        {
            if(!PC.B1)
            {
                BV_ChargeOFF_V = BV_ChargeOFF_V + 1;
                if(BV_ChargeOFF_V > 155)
                    BV_ChargeOFF_V = 155;
                BV_Work1 = BV_ChargeOFF_V / 10;
                BV_Work2 = BV_ChargeOFF_V % 10;
                sprintf(out_str2,"ChOFF_V:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
            if(!PC.B2)
            {
                BV_ChargeOFF_V = BV_ChargeOFF_V - 1;
                if(BV_ChargeOFF_V < 105)
                    BV_ChargeOFF_V = 105;
                BV_Work1 = BV_ChargeOFF_V / 10;
                BV_Work2 = BV_ChargeOFF_V % 10;
                sprintf(out_str2,"ChOFF_V:%d.%dV ",BV_Work1,BV_Work2);    
                LCDPrintAt(0,1);
                LCDString(out_str2);
                Wait(1000);
            }    
        }
        sprintf(out_str2,"ChOFF_V:WRITING.");
        LCDPrintAt(0,1);
        LCDString(out_str2);
        dt1 = BV_ChargeOFF_V & 0x00FF;
        dt2 = BV_ChargeOFF_V >> 8;
        WriteEEData(6,dt1);
        Wait(500);
        WriteEEData(7,dt2);
        Wait(1000);
        sprintf(out_str2,"PREASE RESET!! ");
        LCDPrintAt(0,1);
        LCDString(out_str2);

        while(EE_SetMode){}    //process interrupt
    }
    else
    {
        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
//

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);
}

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

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);
}





トップページヘもどる