AnsweredHot!MCC I2C Restart Condition PIC16F1615

Author
tgor152
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2016/02/18 17:42:45
  • Location: 0
  • Status: offline
2016/02/29 14:15:50 (permalink)
0

MCC I2C Restart Condition PIC16F1615

Hello I have used Mplab code configurator v3 to get I2C working to send data to the slave I'm interfacing with however I can't get a restart condition going to receive data from the slave. In my understanding the master must write to the correct location, send a restart condition, and read from the slave. This is what I have tried but I get a stop function and a start between the two. Is there something I'm not understanding here?
 
    I2C_MESSAGE_STATUS MSG_STATUS;
    I2C_MESSAGE_STATUS* PT_MSG_STATUS;
    PT_MSG_STATUS = &MSG_STATUS;
    
    uint8_t write_value;
    uint8_t* pt_write_value;
    pt_write_value = &write_value;

    while (1)
    {
        uint8_t read_values;
        uint8_t write_values[2]; //create a two-byte array
        write_values[0] = 0x88; // 0x80
        write_values[1] = 0x89; // 0x01
        
        I2C_MasterWrite(write_values,1,SLAVE_ADDRESS,PT_MSG_STATUS);
        I2C_MasterRead(read_values,1,SLAVE_ADDRESS,PT_MSG_STATUS);
    }

#1
ric
Super Member
  • Total Posts : 22098
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 14:23:38 (permalink) ☄ Helpfulby tgor152 2016/02/29 14:56:39
+2 (2)
If you are not in a multi-master environment, a stop followed by a start is functionally equivalent to doing a restart.
 
I've not used MCC. Does the  I2C_MasterRead() function handle setting the read bit in the slave address for you?
 
 

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
tgor152
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2016/02/18 17:42:45
  • Location: 0
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 14:43:13 (permalink)
0
I have only one master and one slave
 
If I'm understanding correctly I shouldn't be worried about the stop and start conditions in between?
 
Yes the I2C_MasterRead Handles setting up the read bit
 
I have a picture of the signal.
 

Attached Image(s)

#3
ric
Super Member
  • Total Posts : 22098
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 14:46:58 (permalink) ☼ Best Answerby tgor152 2016/02/29 14:56:42
+2 (2)
tgor152
If I'm understanding correctly I shouldn't be worried about the stop and start conditions in between?

That is correct. Don't worry about it.
 

Yes the I2C_MasterRead Handles setting up the read bit
I have a picture of the signal.

That signal looks fine. The read address had bit 0 set, the slave ACKed it, and the slave responded with a value.
 
The only funny I can see is that your code sets up a two byte array of data to send, but you only told it to send the first byte.
post edited by ric - 2016/02/29 14:48:28

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
tgor152
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2016/02/18 17:42:45
  • Location: 0
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 14:56:28 (permalink)
0
Ya two bytes were used to test other function just didn't change it for testing the read procedure. Thank you for helping out.
#5
NKurzman
A Guy on the Net
  • Total Posts : 14835
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 15:58:39 (permalink)
+1 (1)
typically:
[Start][address w/write][Command][Restart][address w/read][read,ACK][read,ACK]...[read,NACK][STOP]
#6
Mysil
Super Member
  • Total Posts : 2386
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2016/02/29 16:17:58 (permalink)
0
Hi,
Yes, what I have used the I2C driver generated by MCC,
the R/W bit is handled by the API functions I2C_MasterWrite(...) and I2C_MasterRead()
Be aware that the SlaveAddress argument to both these functions are strict 7 bit address without R/W bit in pos<0>,
that make the valid range of addresses are 0 to 127, or more strictly 8 thru 119 considering reserved addresses.
( unless the generated code have been altered.)
 
What is the contents of  MSG_STATUS after each function have returned?
The code generated by MCC may be is a interrupt driver with a queue of tasks,
depending on what settings was selected in MCC when the code was generated.
 
I2C is a slow protocol, so calling these functions in a fast loop will soon fill the queue to max. 
 
At least if you want to use the values received by I2C_MasterRead(),
you are supposed to test the value placed in the variable MSG_STATUS,
and wait until (MSG_STATUS == I2C_MESSAGE_COMPLETE).
 
Besides, I think there is a weakness in the API that make it possible to overwrite transfer request packets,
if you call the same I2C_MasterWrite() function before the previous transfer started by the same call was completed.
 
The interrupt transfer machine in the MCC generated driver is intended to make combined/chained
I2C operations possible, like Start-Write-Restart-Read-Stop, but the API to use that, is kind of awkward in my opinion. And as ric explained, usually not nessesary.
 
Regards,
   Mysil
#7
embedded Engg
Starting Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/12/17 11:11:58
  • Location: 0
  • Status: offline
Re: MCC I2C Restart Condition PIC16F1615 2017/10/12 07:46:53 (permalink)
0
Hi,
I am facing problem while using repeated start condition. only address and DATA is being write and it is getting stuck in that sequence only. here is my code for reading data.

 
uint16_t readObjectTemp(float *dat)
{
uint16_t raw;
status = I2C1_MESSAGE_PENDING;
 
// build the write buffer first
writeBuffer = 0x07;
 
// we need to create the TRBs for a random read sequence to the EEPROM
// Build TRB for sending address
I2C1_MasterWriteTRBBuild(&readTRB[0],&writeBuffer,1,address);
// Build TRB for receiving data
I2C1_MasterReadTRBBuild(&readTRB[1],readBuff,3,address);
 
timeOut = 0;
 
while(status != I2C1_MESSAGE_FAIL)
{
// now send the transactions
I2C1_MasterTRBInsert(2, readTRB, &status);
while(status == I2C1_MESSAGE_PENDING);
 
if (status == I2C1_MESSAGE_COMPLETE)
break;
 
// check for max retry and skip this byte
if (timeOut == 750)
break;
else
timeOut++;
}
raw = (readBuff[1]<<8) | (readBuff[0]);
*dat = ((float)raw *0.02 -0.01 ) - 273.15; // convert Absolute temperature to Degrees Celsius of temperature
return 0;
}
 

 
Also i changed I2C1_CONFIG_TR_QUEUE_LENGTH to 2
post edited by embedded Engg - 2017/10/12 08:04:54
#8
Jump to:
© 2017 APG vNext Commercial Version 4.5