• AVR Freaks

AnsweredHot!I2C issues

Author
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
2019/08/02 05:00:29 (permalink)
0

I2C issues

Hello,
 
i am currently having issues with my implementation of the I2C protrocol.
The issues are:
1. My code hangs up at "while (!I2C1STATbits.RBF);" , this means, it is waiting for the payload from the slave.
2. The behaviour of the code doesn't change when i input wrong slave addresses, this means that i get acknowlegment from a slave that doesn't even exists. 
My implementation is the following:
 
// Check dsPIC33/PIC24 Family Ref. Manual, Inter-Integrated Circuit (I2C)
    // Page 24
    uint8_t I2C1_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t* value)
    {
        I2C1CONbits.SEN = 1;
        while (I2C1CONbits.SEN == 1);
        I2C1TRN = (slave_addr << 1);
        while (I2C1STATbits.TRSTAT);
        if (I2C1STATbits.ACKSTAT)
            goto fail;
        I2C1TRN = reg_addr;
        while(I2C1STATbits.TRSTAT);
        if (I2C1STATbits.ACKSTAT)
            goto fail;
        // Send restart condition
        I2C1CONbits.RSEN = 1;
        while (I2C1CONbits.RSEN == 1);
        // Send I2C device address on the bus for read operation
        I2C1TRN = ((slave_addr << 1) | 0x01);
        while (I2C1STATbits.TRSTAT);
        if (I2C1STATbits.ACKSTAT)
            goto fail;
        // Enable I2C for read
        I2C1CONbits.RCEN = 1;
        // Retrieve value from I2C register
        while (!I2C1STATbits.RBF);
        *value = I2C1RCV;
        I2C1CONbits.ACKDT = 1;
        I2C1CONbits.ACKEN = 1;
        I2C1CONbits.PEN = 1;
        while (I2C1CONbits.PEN);
        return I2C_OK;
    fail:
        I2C1CONbits.PEN = 1;
        while (I2C1CONbits.PEN);
        return I2C_ERROR;
    }

I am using the dsPIC33EP512GM706. 
The configuration is
oid I2C1_init(uint32_t baudRate)
    {
        // Disable I2C1 Module
        I2C1CONbits.I2CEN = 0;
        // Continue the module to run in Idle Mode
        I2C1CONbits.I2CSIDL = 0;
        // Disable IPMI Support Mode
        I2C1CONbits.IPMIEN = 0;
        // Using 7 bit slave address
        I2C1CONbits.A10M = 0;
        // Slew rate control is disabled because we run at 100kHz
        I2C1CONbits.DISSLW = 1;
        // SMBus Input Levels bit disabled
        I2C1CONbits.SMEN = 0;
        // Set Baund Rate Generator
        I2C1BRG = baudRate;
        // Set address of the Slave
        I2C1ADD = 0x68;
        // Reenable I2C1
        I2C1CONbits.I2CEN = 1;
    }

 
Any help would be appreciated. Thanks.
#1
ric
Super Member
  • Total Posts : 24180
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C issues 2019/08/02 05:16:30 (permalink)
0
Are the SDA and SCL pins analog capable?
If yes, have you switched them to digital mode?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/02 05:54:58 (permalink)
0
ric,
 
it doesn't seem that the SDA and the SCL pins are analog capable.
This issue has been driven me crazy.
Thanks.
#3
ric
Super Member
  • Total Posts : 24180
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C issues 2019/08/02 06:03:36 (permalink)
4 (1)
Are these the pins you are using?
AN31/SCL1/RPI53/RC5 (edit added)
AN30/SDA1/RPI52/RC4
AN29/SCK1/RPI51/RC3 (edit removed. I copied the wrong line from the datasheet, This is the SPI clock)
 
Those ones most certainly are "analog capable".
 
post edited by ric - 2019/08/02 13:27:30

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/02 06:11:15 (permalink)
0
ricm 
 
on the documentation, page 5, is the pinout of the controller.
I am using :
AN31/SCL1/RPI53/RC5
AN30/SDA1/RPI52/RC4
 
Thanks.
#5
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/02 06:35:10 (permalink)
0
Hello ric,
 
i suppose AN, means ANalog. I haven't used them yet and didn't knowuntil now.
Wouldn't the fact that i set 'I2CEN = 1' force the controller to use digital inputs and outputs?
 
Thanks.
#6
du00000001
Just Some Member
  • Total Posts : 3144
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: I2C issues 2019/08/02 07:23:42 (permalink)
5 (2)
AKJ
Wouldn't the fact ...

 
I wouldn't bet on that, taking absolutely no risks. (I personally do not even scan the documentation for this, but just set them to digital (ANSEL = 0)). TRIS is different for I2C, as both signals (SCL,SDA) may be inputs as well as outputs.
 
Thus: Set the ANSEL bits for RC4/RC5 to 0.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#7
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/02 07:46:05 (permalink)
0
du00000001,
i tried placing ANSELC = 0;  both at the top of the i2c1_init() and at the bottom but nothing changed. 
According to the the debugger I/O view, every ANSC stays high no matter what i do.
By the way, these are my configuration bits:

// DSPIC33EP512GM706 Configuration Bit Settings
// 'C' source line config statements
// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = ON // JTAG Enable bit (JTAG is enabled)
// FPOR
#pragma config BOREN = ON // (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25 // Watchdog Window Select bits (WDT Window is 25% of WDT period)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer always enabled)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = ON // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
// FOSCSEL
#pragma config FNOSC = FRCDIVN // Oscillator Source Selection (Internal Fast RC (FRC) Oscillator with postscaler)
#pragma config PWMLOCK = ON // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF // General Segment Code-Protect bit (General Segment Code protect is Disabled)

 
Thanks for your reply.
#8
du00000001
Just Some Member
  • Total Posts : 3144
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: I2C issues 2019/08/02 08:30:43 (permalink) ☼ Best Answerby AKJ 2019/08/07 07:36:48
5 (2)
Hi,
do you use an MCC-generated framework?
 
Might be you'll have to find "the other code" that's setting ANSC - currently overwriting the settings you imple,emted in i2c1_init().
(I checked the documentation: for the '706 devices, ANSC[5:0] exist.)

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#9
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/02 09:00:39 (permalink)
0
du00000001,
 
i am not using any sort of library.
 
I will try on Monday to debug to try to see, where and why the pins are set to analog.
 
Thanks.
#10
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/05 02:17:00 (permalink)
0
Hello,
 
i managed to change every pin  from port C to digital with ANSELC = 0 and even tried to make them as output pin, TRISC = 0, but my issue still remains.
 
Does anyone have a clue, what i might be doing wrong here?
 
Thanks.
#11
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
Re: I2C issues 2019/08/05 04:42:22 (permalink)
4.5 (2)
hello,
i solved it, or should i say we solved it!  The isssue was really the analog pins. 
However, last friday, in my crazyness, i commented the reenabling of the I2C in the 'void I2C1_init' from above (the last line of the code). This made every tests i did today worthless. 
After the removed the comment and made the pins digital, i am able to read the sensors values through the I2C protocol flawlessly.
 
Thanks guys.


#12
Jump to:
© 2019 APG vNext Commercial Version 4.5