3.4.16_プログラムリストNo.1(VOICERECORDER_User.c)

修正箇所(自動生成ファイルに記述追加)は、以下の青色の箇所です。
#include "C:\\WIZ_C\\Ver12\\Projects\\VOICERECORDER\\VOICERECORDER_Auto.h"
#include "C:\\WIZ_C\\Ver12\\Projects\\VOICERECORDER\\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, 0x80

#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 DI_TMR2 (PIE1&=~(1<<TMR2IE))    // TMR2割込禁止
#define EI_TMR2 (PIE1|=(1<<TMR2IE))        // TMR2割込許可

WORD    VR_PV, VR_PV_Work;
BYTE    out_str1[17], out_str2[17], data_buf[17], Transmit_Buf[32], Receive_Buf[32];
BYTE    VR_PV_H, VR_PV_L, uadrs, StrCount, EEBufCount;
bit    No1Key, No2Key, No3Key, No4Key, UpKey, DownKey, LeftKey, RightKey, ModeKey, SetKey, Hosyu1Key, Hosyu2Key, SetMode;

void IntTMR2();
void I2C_ROM_AdrsSet_1(BYTE uadrs, WORD adrs);
void I2C_ROM_Write_1(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void I2C_ROM_Read_1(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void I2C_ROM_AdrsSet_2(BYTE uadrs, WORD adrs);
void I2C_ROM_Write_2(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void I2C_ROM_Read_2(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void I2C_ROM_AdrsSet_3(BYTE uadrs, WORD adrs);
void I2C_ROM_Write_3(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void I2C_ROM_Read_3(BYTE uadrs, WORD adrs, BYTE *data, WORD size);
void TxStr(BYTE *Transmit_Buf);
void CmdProc();

//
// 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
    // 100us周期
    if(PIE1&(1<<TMR2IE))
    {
        if(TST_TMR2IF)
        {            // TIMER2
            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()
{
    ADCON0 |= (1<<GO);
    
    TIMER2_OFF;
    EI_TMR2;
    SetPWM2Volts(0);
    sprintf(out_str1,"VOICE OFF ");
    LCDClear();
    LCDPrintAt(0,0);
    LCDString(out_str1);
}

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

void UserLoop()
{
    if(ModeKey)
    {
        ModeKey = 0;
        if(SetMode)
            SetMode = 0;
        else
            SetMode = 1;
        if(SetMode)
        {
            TIMER2_ON;
            sprintf(out_str1,"VOICE ON ");
        }
        else
        {
            TIMER2_OFF;
            SetPWM2Volts(0);
            sprintf(out_str1,"VOICE OFF ");
        }
        LCDClear();
        LCDPrintAt(0,0);
        LCDString(out_str1);
    }

    if(No1Key)
    {
        No1Key = 0;
        sprintf(data_buf,"No.1 EEPROM TEST");
        I2C_ROM_Write_1(0, 0, data_buf, 16);
        sprintf(out_str2," ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
    }
    
    if(No2Key)
    {
        No2Key = 0;
         I2C_ROM_Read_1(0, 0, data_buf, 16);
         data_buf[16] = 0;
        LCDPrintAt(0,1);
        LCDString(data_buf);
    }
    
    if(No3Key)
    {
        No3Key = 0;
        sprintf(data_buf,"No.2 EEPROM TEST");
        I2C_ROM_Write_2(0, 0, data_buf, 16);
        sprintf(out_str2," ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
    }
    
    if(No4Key)
    {
        No4Key = 0;
         I2C_ROM_Read_2(0, 0, data_buf, 16);
         data_buf[16] = 0;
        LCDPrintAt(0,1);
        LCDString(data_buf);
    }

    if(Hosyu1Key)
    {
        Hosyu1Key = 0;
        sprintf(data_buf,"No.3 EEPROM TEST");
        I2C_ROM_Write_3(0, 0, data_buf, 16);
        sprintf(out_str2," ");
        LCDPrintAt(0,1);
        LCDString(out_str2);
    }

    if(Hosyu2Key)
    {
        Hosyu2Key = 0;
         I2C_ROM_Read_3(0, 0, data_buf, 16);
         data_buf[16] = 0;
        LCDPrintAt(0,1);
        LCDString(data_buf);
    }
}

//
// User occurrence code
//

void IntTMR2()
{
        ADCON0 |= (1<<GO);
        if(SetMode)
        {
            VR_PV = VR_PV_Work;
            SetPWM2Volts(VR_PV);
        }
}

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

void KeyPress()
{
    switch(KP4Value)
    {
        case 0:No1Key = 1;break;
        case 1:No2Key = 1;break;
        case 2:No3Key = 1;break;
        case 3:No4Key = 1;break;
        case 4:UpKey = 1;break;
        case 5:DownKey = 1;break;
        case 6:LeftKey = 1;break;
        case 7:RightKey = 1;break;
        case 8:ModeKey = 1;break;
        case 9:SetKey = 1;break;
        case 10:Hosyu1Key = 1;break;
        case 11:Hosyu2Key = 1;break;
    }
}

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

void GetRX()
{
    BYTE ch;
    if(GetRxSize() > 0)
    {
        ch = WaitRx();
        if((ch == '\r') || (ch == '\n'))                //'\r'(0x0D)
        {
            Receive_Buf[StrCount] = 0;    //NULL
            StrCount = 0;
            CmdProc();
        }
        else
        {
            Receive_Buf[StrCount] = ch;
            StrCount = StrCount + 1;
            if(StrCount > 7)
                StrCount = 7;
        }
    }

}

void CmdProc()
{
    if(strcmp(Receive_Buf,"SAR1") == 0)
    {
        TxStr("123456\r\n");
    }

    if(strcmp(Receive_Buf,"SAR2") == 0)
    {
        TxStr("ABCDEF\r\n");
    }

    strcpy(Receive_Buf,"");
    StrCount = 0;
}

void TxStr(BYTE *Transmit_Buf)
{
    BYTE i;
    for(i = 0; i < 8; i++)            //8byte送信
        AddTx(Transmit_Buf[i]);
}

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

void ad_data_t()
{
    VR_PV_L = ADRESL;
    VR_PV_H = ADRESH;
    VR_PV_Work = (WORD)ADRESH << 8;
    VR_PV_Work = VR_PV_Work | ADRESL;
}

//**************************************************
// No.1 I2Cbus EEPROM
// SDA:RC0
// SCL:RA4
//**************************************************

//
// I2C-EEPROM DATA WRITE
//
void I2C_ROM_Write_1(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_1(uadrs, adrs);
    for(i = 0; i < size; i++)
    {
        I2CWrite_1(*data);
        data++;
    }
    I2CStop_1();
}

//
// I2C-EEPROM DATA READ
//
void I2C_ROM_Read_1(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_1(uadrs, adrs);
    I2CStart_1(I2C_START_CONT);
    if(uadrs == 0)
        I2CWrite_1(0xA1);            // control byte (read)
    else
        I2CWrite_1(0xA3);            // control byte (read)
    for(i = 0; i < size; i++)
    {
        if(i != (size - 1))
            *data = I2CRead_1(I2C_READ_ACK);
        else
            *data = I2CRead_1(I2C_READ_NOACK);
        data++;
    }
    I2CStop_1();
}

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

//**************************************************
// No.2 I2Cbus EEPROM
// SDA:RD1
// SCL:RD0
//**************************************************

//
// I2C-EEPROM DATA WRITE
//
void I2C_ROM_Write_2(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_2(uadrs, adrs);
    for(i = 0; i < size; i++)
    {
        I2CWrite_2(*data);
        data++;
    }
    I2CStop_2();
}

//
// I2C-EEPROM DATA READ
//
void I2C_ROM_Read_2(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_2(uadrs, adrs);
    I2CStart_2(I2C_START_CONT);
    if(uadrs == 0)
        I2CWrite_2(0xA1);            // control byte (read)
    else
        I2CWrite_2(0xA3);            // control byte (read)
    for(i = 0; i < size; i++)
    {
        if(i != (size - 1))
            *data = I2CRead_2(I2C_READ_ACK);
        else
            *data = I2CRead_2(I2C_READ_NOACK);
        data++;
    }
    I2CStop_2();
}

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

//**************************************************
// No.3 I2Cbus EEPROM
// SDA:RD3
// SCL:RD2
//**************************************************

//
// I2C-EEPROM DATA WRITE
//
void I2C_ROM_Write_3(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_3(uadrs, adrs);
    for(i = 0; i < size; i++)
    {
        I2CWrite_3(*data);
        data++;
    }
    I2CStop_3();
}

//
// I2C-EEPROM DATA READ
//
void I2C_ROM_Read_3(BYTE uadrs, WORD adrs, BYTE *data, WORD size)
{
    WORD i;
    I2C_ROM_AdrsSet_3(uadrs, adrs);
    I2CStart_3(I2C_START_CONT);
    if(uadrs == 0)
        I2CWrite_3(0xA1);            // control byte (read)
    else
        I2CWrite_3(0xA3);            // control byte (read)
    for(i = 0; i < size; i++)
    {
        if(i != (size - 1))
            *data = I2CRead_3(I2C_READ_ACK);
        else
            *data = I2CRead_3(I2C_READ_NOACK);
        data++;
    }
    I2CStop_3();
}

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

//**************************************************





トップページヘもどる