Hot!How to implement I2C communication for PIC32MX220F by MPLAB Harmony

Author
Tank2005
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2018/10/09 01:45:37
  • Location: 0
  • Status: offline
2018/10/11 04:50:16 (permalink)
0

How to implement I2C communication for PIC32MX220F by MPLAB Harmony

My environment is MPLAB X IDE 5.0 and XC32 2.1(Lite) on Windows 10.
 
I want to drive MCP23017 chip(Microchip's I/O expander) on PIC32MX200 series.
 
I wrote a test code for Arduino and it works on Arduino UNO.
 
#include <Wire.h>
void setup( void )
{
  // Set all a ports as output
  Wire.begin();
    Wire.beginTransmission(0x20);
    Wire.write(0x00);
    Wire.write(0x00);
  Wire.endTransmission();
 
  // GPIOA(GPA0~GPA7)
  Wire.beginTransmission(0x20);
    Wire.write(0x12);
    Wire.write(0xff);
  Wire.endTransmission();
}

 
byte b = 0xFF;

 
void loop( void )
{
  byte buf[2];
  buf[0] = 0x12, buf[1] = b; b = ~b;
  delay(500);
  Wire.beginTransmission(0x20);
  Wire.write(buf, 2);
  Wire.endTransmission();
}

I'm trying to write a code that works the same with MPLAB Harmony. However, I2C signal is not transmitted because of incorrect initialization probably(Harmony API has "DRV_I2C0_Initialize" and it is called from SYS_Initialize).
 

Blue colored Harmony configuration on I2C.
Use I2C Driver? : checked
Driver Implementation : STATIC
I2C Driver INstance 0 : checked
12C Module : ID I2C_ID_2
I2C Interrupt Priority : INT_PRIORITY_LEVEL4
 
PIN Settins
Pin6 / RB2 / SDA2
Pin7 / RB3 / SCL2
 
 
 
#include "app.h"

 
typedef unsigned char byte;
#define MCP23017_ADDRESS (0x20 << 1)

 
byte toggle = 0xFF;

 
void Send(byte reg, byte value)
{
    byte buf[2];
    buf[0] = reg;
    buf[1] = value;
   
    DRV_I2C0_Transmit(MCP23017_ADDRESS, buf, 2, NULL);
}

 
void APP_Initialize ( void )
{
    Send(0x00, 0x00);
    Send(0x12, toggle);
}

 
void OnI2C0()
{
}

 
void OnTimer0()
{
    toggle = ~toggle;
    Send(0x12, toggle);
}

 
void APP_Tasks ( void )
{
}

 
I have confirmed the I2C signal by putting to I2C2CON/I2C2TRN register directly, so there is seems to be no problem in the hardware. How can I deal with this problem?
post edited by Tank2005 - 2018/10/11 04:51:41

Attached Image(s)

#1

3 Replies Related Threads

    Paul PortSol
    Super Member
    • Total Posts : 331
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: 0
    • Status: offline
    Re: How to implement I2C communication for PIC32MX220F by MPLAB Harmony 2018/10/11 04:55:40 (permalink)
    0
    Check if the "bit bang" option is available under MHC Drivers, that works well with some other PIC32 in harmony.
     
    #2
    Tank2005
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2018/10/09 01:45:37
    • Location: 0
    • Status: offline
    Re: How to implement I2C communication for PIC32MX220F by MPLAB Harmony 2018/10/11 22:26:58 (permalink)
    0
    Thank you for your replying.
     
    When I activated Bit Bang I2C, at least a periodic signal occurred. I'm going to continue about testing so that it generates a correct signal.
     
    [code]--------------------------------------------------------------#include "app.h"
    #include <p32xxxx.h>
    typedef unsigned char byte;
    #define MCP23017_ADDRESS (0x20 << 1)
    const DRV_I2C_INIT drvI2C0InitData =
    {
        .i2cId = DRV_I2C_PERIPHERAL_ID_IDX0,
        .i2cPerph = true,
        .i2cMode = DRV_I2C_MODE_MASTER,
        .portSCL = PORT_CHANNEL_B,
     .pinSCL  = PORTS_BIT_POS_3,
     .portSDA = PORT_CHANNEL_B,
     .pinSDA  = PORTS_BIT_POS_2,
        .baudRate = 100000,
     .tmrSource = TMR_ID_5,
     .tmrInterruptSource = INT_SOURCE_TIMER_5,
        .busspeed = false,
        .buslevel = false,
    };
    SYS_MODULE_OBJ i2cMaster;
    DRV_HANDLE i2cHandle, i2cHandle2;
    DRV_I2C_BUFFER_HANDLE i2cBuffer, i2cBuffer2;
    byte toggle = 0xFF;
    int count = 0;
    void OnI2C(DRV_I2C_BUFFER_EVENT event, DRV_I2C_BUFFER_HANDLE bufferHandle, uintptr_t context)
    {
        switch(event)
        {
            case DRV_I2C_BUFFER_EVENT_COMPLETE:
                break;
            case DRV_I2C_BUFFER_EVENT_ERROR:
            default:
                break;
        }
    }
    void Send(byte reg, byte val)
    {
        byte buf[2];
        buf[0] = reg;
        buf[1] = val;
       
        i2cBuffer = DRV_I2C_Transmit(i2cHandle, MCP23017_ADDRESS, buf, 2, NULL);
        while(DRV_I2C_TransferStatusGet(i2cHandle, i2cBuffer) != DRV_I2C_BUFFER_EVENT_COMPLETE) ;
    }
    void APP_Initialize(void)
    {
        ANSELB = 0x00;
        TRISB = 0xFF;
       
        //i2cMaster = DRV_I2C_Initialize(DRV_I2C_INDEX_0, (SYS_MODULE_INIT*)&drvI2C0InitData);
                   
        i2cHandle = DRV_I2C_Open(DRV_I2C_INDEX_0, DRV_IO_INTENT_WRITE);
        DRV_I2C_BufferEventHandlerSet(i2cHandle, OnI2C, NULL);
     
        Send(0x00, 0x00);
    }
    void APP_Tasks ( void )
    {
        count++;
        if(count >= 100000){
            count = 0;
            toggle = ~toggle;
            Send(0x12, toggle);
        }
    }
    [code]--------------------------------------------------------------

     

     
    #3
    qhb
    Superb Member
    • Total Posts : 7534
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: How to implement I2C communication for PIC32MX220F by MPLAB Harmony 2018/10/11 22:38:49 (permalink)
    0
    Note your code tags didn't work because the closing one is missing the slash.
    [/code]
     
    #4
    Jump to:
    © 2018 APG vNext Commercial Version 4.5