3.4.6_プログラムリスト(IRREMO1_User.c)

修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include "C:\\WIZ_C\\Ver12\\Projects\\IRREMO1\\IRREMO1_Auto.h"
#include "C:\\WIZ_C\\Ver12\\Projects\\IRREMO1\\i2c.h"

#include <stdlib.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

#define TST_TMR2IF (PIR1&(1<<TMR2IF))        // TMR2IFフラグチェック
#define CLR_TMR2IF (PIR1&=~(1<<TMR2IF))        // TMR2IFフラグクリア
#define TIMER2_ON T2CON|=(1<<TMR2ON)        // TIMER2 START
#define TIMER2_OFF T2CON&=~(1<<TMR2ON)        // TIMER2 STOP
#define TST_IrIN BitTest(PORTA, 0)    // 赤外線パルス入力
#define BitTest(p, b) (p&(1<<b))
#define DI_TMR2 (PIE1&=~(1<<TMR2IE))    // TMR2割込禁止
#define EI_TMR2 (PIE1|=(1<<TMR2IE))        // TMR2割込許可

unsigned char    out_str1[17], out_str2[17], Receive_str[32], Transmit_str[32], rom_write[16], rom_read[16];
signed char    BitPtr;
WORD    EEadrs;
BYTE PdBuf[256],PdBuf_work[64],RemoKeyBuf[12];
BYTE BitPatn[8];// = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
BYTE SplB,SmpState,BmpData,CaptNo,PlsOutNo,RemoCapSetNo,RemoKeySetNo,RemoKeyPush;
int    PInByte,POutByte,StrCount,MenuNo;
bit    CR_exist,LF_exist;
bit    ModeKey_Work, SetKey_Work, Hosyu1_Work,Hosyu2_Work;
bit    SetMode, SetKey, Hosyu1Key, Hosyu2Key, LeftKey, RightKey, UpKey, DownKey;
bit    IrOutSw,NormalModeInit,MenuInit,RemoCapSet,RemoKeySet;

void TxStr(char *Transmit_str);
void CmdProc();
void I2C_AdrsSet(BYTE uadrs, WORD adrs);
void I2C_ROM_Write(BYTE uadrs, WORD adrs, BYTE *data, BYTE size);
void I2C_ROM_Read(BYTE uadrs, WORD adrs, BYTE *data, int size);
void IrPlsOut(BYTE remcode);
void IrCap(BYTE remcode);
void IntTMR2();

//
// 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

    // 104.8us周期
    if(PIE1&(1<<TMR2IE))
    {
        if(TST_TMR2IF)
        {            // TIMER2 OverFlow T=104.8us
            CLR_TMR2IF;
            IntTMR2();
        }
    }
#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()
{

    PR2 = 130;
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    SetPWM1Volts(0);            // Ir Pulse off:0 / on:260
    TIMER2_OFF;
    DI_TMR2;
    BitPatn[0] = 0x01;
    BitPatn[1] = 0x02;
    BitPatn[2] = 0x04;
    BitPatn[3] = 0x08;
    BitPatn[4] = 0x10;
    BitPatn[5] = 0x20;
    BitPatn[6] = 0x40;
    BitPatn[7] = 0x80;
    NormalModeInit = 1;
}

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

void UserLoop()
{

    if(RemoKeyPush != 0)
    {
        I2C_ROM_Read(0x00,0x6400,RemoKeyBuf,12);
        PlsOutNo = RemoKeyBuf[RemoKeyPush - 1];
        if(PlsOutNo >= 1)
            if(PlsOutNo <= 100)
                IrPlsOut(PlsOutNo);
        RemoKeyPush = 0;
    }

    if(!PA.B2 && !ModeKey_Work)        //Mode key
    {
        PA.B2 = 1;
        ModeKey_Work = 1;
        SetMode = !SetMode;
        if(!SetMode)
            NormalModeInit = 1;
        MenuInit = SetMode;
        MenuNo = 1;
        UpKey = 0;
        DownKey = 0;
        LeftKey = 0;
        RightKey = 0;
        SetKey = 0;
    }
    if(PA.B2)
        ModeKey_Work = 0;

    if(!PA.B3 && !SetKey_Work)        //Set key
    {
        PA.B3 = 1;
        SetKey_Work = 1;
        SetKey = 1;
    }
    if(PA.B3)
        SetKey_Work = 0;

    if(!PA.B4 && !Hosyu1_Work)        //Hosyu1key
    {
        PA.B4 = 1;
        Hosyu1_Work = 1;
        Hosyu1Key = 1;
    }
    if(PA.B4)
        Hosyu1_Work = 0;
    
    if(!PA.B5 && !Hosyu2_Work)        //Hosyu2key
    {
        PA.B5 = 1;
        Hosyu2_Work = 1;
        Hosyu2Key = 1;
    }
    if(PA.B5)
        Hosyu2_Work = 0;

    if(SetMode)
    {
        if(RightKey)
        {
            RightKey = 0;
            LeftKey = 0;
            MenuInit = 1;
            MenuNo = MenuNo + 1;
            if(MenuNo > 2)
                MenuNo = 1;
        }    
        if(LeftKey)
        {
            RightKey = 0;
            LeftKey = 0;
            MenuInit = 1;
            MenuNo = MenuNo - 1;
            if(MenuNo == 0)
                MenuNo = 2;
        }
        
        if(MenuNo == 1)
        {    
            if(MenuInit)
            {
                MenuInit = 0;
                CaptNo = 1;
                sprintf(out_str1,"M1 Capture Pulse");
                sprintf(out_str2,"Capture No. %03d ",CaptNo);
                LCDClear();
                LCDPrintAt(0,0);
                LCDString(out_str1);
                LCDPrintAt(0,1);
                LCDString(out_str2);
            }
            if(UpKey)
            {
                UpKey = 0;
                DownKey = 0;
                CaptNo = CaptNo + 1;
                if(CaptNo > 100)
                    CaptNo = 1;
                sprintf(out_str2,"Capture No. %03d ",CaptNo);
                LCDPrintAt(0,1);
                LCDString(out_str2);
            }
            if(DownKey)
            {
                UpKey = 0;
                DownKey = 0;
                CaptNo = CaptNo - 1;
                if(CaptNo == 0)
                    CaptNo = 100;
                sprintf(out_str2,"Capture No. %03d ",CaptNo);
                LCDPrintAt(0,1);
                LCDString(out_str2);
            }

            if(SetKey)
            {
                SetKey = 0;
                sprintf(out_str1,"M1 Capture Pulse");
                sprintf(out_str2,"Capture Waiting!");
                LCDClear();
                LCDPrintAt(0,0);
                LCDString(out_str1);
                LCDPrintAt(0,1);
                LCDString(out_str2);
                IrCap(CaptNo);
            }
        }

        if(MenuNo == 2)
        {    
            if(MenuInit)
            {
                MenuInit = 0;
                RemoKeySet = 1;
                RemoCapSet = 0;
                RemoKeySetNo = 1;
                RemoCapSetNo = 1;
                sprintf(out_str1,"M2 SetRemoconKey");
                sprintf(out_str2,"RemoconKeyNo.%03d",RemoKeySetNo);
                LCDClear();
                LCDPrintAt(0,0);
                LCDString(out_str1);
                LCDPrintAt(0,1);
                LCDString(out_str2);
            }
            if(RemoKeySet)
            {
                if(UpKey)
                {
                    UpKey = 0;
                    DownKey = 0;
                    RemoKeySetNo = RemoKeySetNo + 1;
                    if(RemoKeySetNo > 12)
                        RemoKeySetNo = 1;
                    sprintf(out_str2,"RemoconKeyNo.%03d",RemoKeySetNo);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                }
                if(DownKey)
                {
                    UpKey = 0;
                    DownKey = 0;
                    RemoKeySetNo = RemoKeySetNo - 1;
                    if(RemoKeySetNo == 0)
                        RemoKeySetNo = 12;
                    sprintf(out_str2,"RemoconKeyNo.%03d",RemoKeySetNo);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                }
                if(SetKey)
                {
                    SetKey = 0;
                    sprintf(out_str1,"M2 SetRemoconKey");
                    sprintf(out_str2,"Capture No.%03d ",RemoCapSetNo);
                    LCDClear();
                    LCDPrintAt(0,0);
                    LCDString(out_str1);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                    RemoKeySet = 0;
                    RemoCapSet = 1;
                }
            }
            if(RemoCapSet)
            {
                if(UpKey)
                {
                    UpKey = 0;
                    DownKey = 0;
                    RemoCapSetNo = RemoCapSetNo + 1;
                    if(RemoCapSetNo > 100)
                        RemoCapSetNo = 1;
                    sprintf(out_str2,"Capture No.%03d ",RemoCapSetNo);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                }
                if(DownKey)
                {
                    UpKey = 0;
                    DownKey = 0;
                    RemoCapSetNo = RemoCapSetNo - 1;
                    if(RemoCapSetNo == 0)
                        RemoCapSetNo = 100;
                    sprintf(out_str2,"Capture No.%03d ",RemoCapSetNo);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                }
                if(SetKey)
                {
                    SetKey = 0;
                    I2C_ROM_Read(0x00,0x6400,RemoKeyBuf,12);
                    RemoKeyBuf[RemoKeySetNo - 1] = RemoCapSetNo;
                    I2C_ROM_Write(0x00,0x6400,RemoKeyBuf,12);
                    Wait(10);
                    sprintf(out_str1,"M2 SetRemoconKey");
                    sprintf(out_str2,"RemoconKeyNo.%03d",RemoKeySetNo);
                    LCDClear();
                    LCDPrintAt(0,0);
                    LCDString(out_str1);
                    LCDPrintAt(0,1);
                    LCDString(out_str2);
                    RemoKeySet = 1;
                    RemoCapSet = 0;
                }
            }
        }
    }
    if(NormalModeInit)
    {
        NormalModeInit = 0;
        sprintf(out_str1,"IR STUDY REMOCON");
        LCDClear();
        LCDPrintAt(0,0);
        LCDString(out_str1);
    }
}

//
// User occurrence code
//

void IrPlsOut(BYTE remcode)
{
    IrOutSw = 1;
    EEadrs = ((WORD)remcode - 1) * 256;
    I2C_ROM_Read(0x00,EEadrs,PdBuf,256);
    BmpData = PdBuf[0];
    BitPtr = 7;
    POutByte = 0;
    TIMER2_ON;
    EI_TMR2;
}

void IrCap(BYTE remcode)
{
    sprintf(out_str1,"M1 Capture Pulse");
    sprintf(out_str2,"No.%03d Capturng ",CaptNo);
    LCDClear();
    LCDPrintAt(0,0);
    LCDString(out_str1);
    LCDPrintAt(0,1);
    LCDString(out_str2);
    IrOutSw = 0;
    EEadrs = ((WORD)remcode - 1) * 256;
    SmpState = 0;
    SplB = 0xE0;            // 3サイクル分をセット済み
    BitPtr = 4;            // 3サイクル分をカウント済み
    PInByte = 0;
    TIMER2_ON;
    EI_TMR2;
}

void IntTMR2()
{
    if(IrOutSw)
    {
        // ------------ リモコンパルス出力 --------------------------
        if(BitPtr < 0)
        {
            POutByte++;
            if(POutByte < 256)
            {
                BmpData = PdBuf[POutByte];
                BitPtr = 7;                                // 次の8ビットのための準備
            }
            else
            {
                // 出力終了
                SetPWM1Volts(0);
                TIMER2_OFF;
                DI_TMR2;
                return;
            }        

        }
        if(BitPatn[BitPtr] & BmpData)
        {
            // ccp1(PWM) duty 50%
            SetPWM1Volts(260);
        }
        else
        {
            // ccp1(PWM) duty 0%
            SetPWM1Volts(0);
        }
        BitPtr--;                // next bit
    }
    else
    {
        // -------------- リモコンパルス・サンプリング --------------
        if(SmpState == 3)
        {
            // サンプリング中
            if(!TST_IrIN) {                // input Ir bit
                // L
                SplB |= BitPatn[BitPtr];
            }
            BitPtr--;
            if(BitPtr < 0)
            {
                // 8bitサンプリング完了
                    PdBuf[PInByte] = SplB;            // 8bitを1byteとして保存
                    PInByte++;
                    if(PInByte >= 256)
                    {
                        // サンプリング終了
                        TIMER2_OFF;
                        DI_TMR2;
                        int i,j,k;
                        for(i=0;i<4;i++)
                        {
                            EEadrs = EEadrs + (WORD)i*0x0040;
                            k = i*64;
                            for(j=0;j<64;j++)
                                PdBuf_work[j] = PdBuf[j+k];
                            I2C_ROM_Write(0x00,EEadrs,PdBuf_work,64);    
                            Wait(10);
                        }
                        sprintf(out_str1,"M1 Capture Pulse");
                        sprintf(out_str2,"CaptureNo. %03d ",CaptNo);
                        LCDClear();
                        LCDPrintAt(0,0);
                        LCDString(out_str1);
                        LCDPrintAt(0,1);
                        LCDString(out_str2);
                        return;
                    }
                    else
                    {
                        BitPtr = 7;
                        SplB = 0;                        // next
                    }
            }
        }
        else
        {
            // スタート待ち,リーダ継続チェック
            if(TST_IrIN)
            {
                // Hレベル(負論理) リーダ入力なし
                SmpState = 0;    // reset
            }
            else
            {
                // Lレベル(負論理) リーダ入力あり
                SmpState++;        // 次のサイクルを待つ
            }
        }
    }
}

//
// Occurrence - A key has been pressed (or is repeating)
//

void KeyPress()
{

    if(KP4Value <= 11)
        RemoKeyPush = KP4Value + 1;
    switch(KP4Value)
    {
        case 12:UpKey = 1;break;
        case 13:DownKey = 1;break;
        case 14:LeftKey = 1;break;
        case 15:RightKey = 1;break;
    }
}

//
// Occurrence - Byte received on serial interface
//

void GetRX()
{

    BYTE ch;
    if(GetRxSize() > 0)
    {
        ch = WaitRx();
        if(ch == '\r')        //'\r'(0x0D)
            CR_exist = 1;
        if(ch == '\n')        //'\n'(0x0A)
            LF_exist = 1;
        if(!(CR_exist | LF_exist))
        {
            Receive_str[StrCount] = ch;
            StrCount = StrCount + 1;
        }
        if(CR_exist & LF_exist)
        {
            Receive_str[StrCount] = 0;    //NULL
            StrCount = 0;
            CR_exist = 0;
            LF_exist = 0;
            CmdProc();
        }
    }
}

void CmdProc()
{
    ///////////////////
    strcpy(Receive_str,"");
}

void TxStr(char *Transmit_str)
{
    int i;
    for(i = 0; i < 15; i++)
    {
        if(Transmit_str[i] !=0)
            AddTx(Transmit_str[i]);
        else
            break;
    }
}

//
// I2C-EEPROM DATA WRITE
//
void I2C_ROM_Write(BYTE uadrs, WORD adrs, BYTE *data, BYTE size)
{
    int i;
    I2C_AdrsSet(uadrs, adrs);
    for(i = 0; i < size; i++)
    {
        I2CWrite(*data);
        data++;
    }
    I2CStop();
}

//
// I2C-EEPROM DATA READ
//
void I2C_ROM_Read(BYTE uadrs, WORD adrs, BYTE *data, int size)
{
    int i;
    I2C_AdrsSet(uadrs, adrs);
    I2CStart(I2C_START_CONT);
    if(uadrs == 0)
        I2CWrite(0xA1);            // control byte (write)
    else
        I2CWrite(0xA3);            // control byte (write)

    for(i = 0; i < size; i++)
    {
        if(i != (size - 1))
            *data = I2CRead(I2C_READ_ACK);
        else
            *data = I2CRead(I2C_READ_NOACK);
        data++;
    }
    I2CStop();
}

//
// I2C-EEPROM DATA READ/WRITE ADDRES SET
//
void I2C_AdrsSet(BYTE uadrs, WORD adrs)
{
    I2CStart(I2C_START_NORM);
    if(uadrs == 0)
        I2CWrite(0xA0);            // control byte (write)
    else
        I2CWrite(0xA2);            // control byte (write)
    I2CWrite(adrs>>8);            // EEPROM Address(H)
    I2CWrite(adrs);                // EEPROM Address(L)
}





トップページヘもどる