• AVR Freaks

Hot!PIC18F26k42 and I2C

Author
VincenzoV
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/02/06 10:41:25
  • Location: Monticello Brianza - LC - Italy
  • Status: offline
2017/11/01 10:51:09 (permalink)
0

PIC18F26k42 and I2C

Hi all
I want to upgrade my application from PIC18F26K22 to PIC18F26k42 but I've two problems with I2C:
* It doesn't work at all (no output, with oscilloscope I see a fixed +3V, from pull-up resistors)
* No interrupt driven code in MCC
I'm using XC8 1.44 and MCC 3.36 (latest). All configuration code from MCC.

 
// Inside pin configuration
 
bool state = (unsigned char)GIE;
GIE = 0;
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
 
I2C2SDAPPSbits.I2C2SDAPPS = 0x0A; //RB2->I2C2:SDA2;
I2C2SCLPPSbits.I2C2SCLPPS = 0x09; //RB1->I2C2:SCL2;
RB1PPS = 0x23; //RB1->I2C2:SCL2;
RB2PPS = 0x24; //RB2->I2C2:SDA2;
 
PPSLOCK = 0x55;
PPSLOCK = 0xAA;
PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
 
GIE = state;
 
 
 
// Inside I2C2 auto-generated code
 
void I2C2_Initialize(void)
{
if(!I2C2CON0bits.EN || lastError != I2C2_GOOD)
{
lastError = I2C2_GOOD;
// TXU 0; CSD Clock Stretching enabled; ACKT 0; RXO 0; ACKDT Acknowledge; ACKSTAT ACK received; ACKCNT Acknowledge;
I2C2CON1 = 0x00;
// ABD enabled; GCEN disabled; ACNT disabled; SDAHT 300 ns hold time; BFRET 8 I2C Clock pulses; FME disabled;
I2C2CON2 = 0x00;
// CLK Fosc/4;
I2C2CLK = 0x00;
// CSTR Enable clocking; S Cleared by hardware after Start; MODE 7-bit address; EN enabled; RSEN disabled;
I2C2CON0 = 0x84;
I2C2PIR = 0;// ;Clear all the error flags
I2C2ERR = 0;
}
}
 
 
 
void i2c2_write1ByteRegister(uint8_t address, uint8_t reg, uint8_t data)
{
I2C2_Initialize();
I2C2ADB1= (uint8_t)(address<<1);
I2C2CNT=2;
I2C2CON0bits.S=1;
sendByte(reg);
sendByte(data);
wait4Stop();
}
 
 
 
// My code
 
 
 
 
 
i2c2_write1ByteRegister(LM75A_ADDR, LM75A_CONF, LM75_NORMAL);
 
// i2c2_getLastError() return time-out

 
Thanks
 
post edited by VincenzoV - 2017/11/01 11:15:04

Attached Image(s)


Vincenzo Villa - https://www.vincenzov.net
#1

6 Replies Related Threads

    VincenzoV
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/02/06 10:41:25
    • Location: Monticello Brianza - LC - Italy
    • Status: offline
    Re: PIC18F26k42 and I2C 2017/11/01 13:06:22 (permalink)
    0
    Partially solved (it work!).
    RTFM! Note 1 for REGISTER 33-1:I2CxCON0: I 2 C CONTROL REGISTER 0
     

    Attached Image(s)


    Vincenzo Villa - https://www.vincenzov.net
    #2
    jack527
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2016/07/21 23:35:44
    • Location: 0
    • Status: offline
    Re: PIC18F26k42 and I2C 2020/11/02 06:35:32 (permalink)
    1 (1)
    extern bool InitPAJ7635U2(void)
    {
    unsigned char i=0;
    unsigned char buf[] =
    {
    0xEF, 0x00, /*** Write register (vertical 1) with value vertical 2 ***/
    0x03, 0x01,
    0x04, 0x00,
    0x07, 0x01,
    0x51, 0x10,
    0x57, 0x0B,
    0x5A, 0x03,
    0x60, 0x30,
    0x69, 0x3C,
    0x6A, 0x28,
    0x6C, 0x00,
    0xF0, 0x01,
    0xEF, 0x01,
    0x04, 0x03,
    0x41, 0x44,
    0x64, 0x06,
    0x77, 0x31,
    0x78, 0x07,
    0x7E, 0x00,
    0xEF, 0x02,
    0x20, 0x01,
    0x27, 0x08,
    0x98, 0x30,
    };

    for (i=0; i<(sizeof(buf)/2); ++i)
    {
    I2C2_Write1ByteRegister(PIXART_I2C_DEV_ADDR << 1, buf[i*2], buf[i*2+1]);
    }
    I use I2C2_Write1ByteRegister function , but IIC not work ,why? who can tell me why not work ?
    #3
    Mysil
    Super Member
    • Total Posts : 4062
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC18F26k42 and I2C 2020/11/02 08:42:53 (permalink)
    0
    Hi, jack527
    Your explaination of what you are trying to do, and what you have done to set it up,
    is somewhat lacking.
     
    Treatment of I2C address is probably wrong:
        I2C2_Write1ByteRegister(PIXART_I2C_DEV_ADDR << 1, ... 

    I2C code generated by MCC, normally expect I2C slave address to be unmodified 7-bit address.
    You may try:
        I2C2_Write1ByteRegister(PIXART_I2C_DEV_ADDR, buf[i*2], buf[i*2+1]);   

    There may be many other problems in other code you have not shown.
     
        Mysil
    #4
    jack527
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2016/07/21 23:35:44
    • Location: 0
    • Status: offline
    Re: PIC18F26k42 and I2C 2020/11/02 22:55:19 (permalink)
    0
    IIC still doesn't work, when stop debug , program run in picture place;

    Attached Image(s)

    #5
    mirkwood
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2020/06/14 05:38:37
    • Location: 0
    • Status: offline
    Re: PIC18F26k42 and I2C 2020/11/30 17:52:12 (permalink)
    0
    Does anything in this post help?
     
    https://www.microchip.com/forums/m1158853.aspx
    #6
    mirkwood
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2020/06/14 05:38:37
    • Location: 0
    • Status: offline
    Re: PIC18F26k42 and I2C 2020/11/30 17:57:21 (permalink)
    4 (1)
    Also you I2C2CLK is set to Fosc/4, that's probably a bit fast for I2C.  Try using MFINTOSC
    #7
    Jump to:
    © 2021 APG vNext Commercial Version 4.5