4.3.6_プログラムリスト(BATAUTSW_AD2_User.c)

修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include "D:\\WIZ_C\\Projects\\BATAUTSW_AD2\\BATAUTSW_AD2_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_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 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 = 737;        //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 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);
}

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





トップページヘもどる