Helpful ReplyHot!Newbie: How to read multiple registers at one time using MCC generated I2C functions

Author
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
2018/06/20 06:36:41 (permalink)
5 (1)

Newbie: How to read multiple registers at one time using MCC generated I2C functions

I have a sensor (TLV493D-A1B6) that requires multiple registers to be read at one time. I am unsure how to implement this using the MCC generated code and the Transaction Request Blocks. I am able to read and write to single registers on other types of sensors, but this one is a bigger challenge. Can someone point me in the right direction on how to get this done? I am using the PIC16F1619, MPLAB-X and XC8 v1.45 compiler.
 
Thanks in advance!
 
 
#1
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/20 15:27:12 (permalink)
0
I don't use MCC, so this is just guesswork.
If you are able to read a single register, can't you tell it to just read more than one byte?
The chip will just keep outputting sequential register values if you keep reading.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
MBedder
Circuit breaker
  • Total Posts : 6471
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/20 15:37:52 (permalink)
0
cgaufinI have a sensor (TLV493D-A1B6) that requires multiple registers to be read at one time.
Who told you that bullsh¡t?


#3
Mysil
Super Member
  • Total Posts : 2885
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/20 16:08:05 (permalink)
+1 (1)
Hi,
Sensor may have registers that are larger than one byte.
I2C transfer deal with bytes only, you must count up the number of bytes needed to transfer contents of actual register, according to datasheet for the actual device type.
 
Many device types have possibility to read multiple registers in sequence, in the same transfer.
See datasheet for the sensor.
 
MCC have 2 or 3 different ways of doing I2C transfers, that have different application interface,
either Foundation Services library, or MCC device library.
Anyway, there is a length argument to specify the number of Register Bytes that you want to read:
example:
static uint8_t data[8];          /* Data array to hold the largest number of register bytes wanted. */
uint8_t length;
static I2C1_MESSAGE_STATUS   I2C1status;    /* Static or global variable to receive status from I2C transfer. */
 
    length = 4;        /* Set number of register bytes wanted in this transfer. */
    I2C1_MasterRead( data, length,  address,   &I2C1status);
...

  
   Mysil
post edited by Mysil - 2018/06/21 02:45:39
#4
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/21 07:17:24 (permalink)
-1 (1)
All thank you for your feedback so far.
 
I have seen the length variable and have changed that, but that doesn't seem to work either. According to the spec sheet I need to somehow read the data from registers 0x00 to 0x09 in one I2C read command. I have tried doing one address sequentially right after another and storing the results in parts of an array, but I don't think that's what it wants as the sensor doesn't function still. I think it's important to not get hung up on the specific sensor though, as my question is more basic. I really want to learn how to initiate a read command and then get the data from more than one sequential register during that same read before the stop condition is sent? Is it found in the structure of the TRB?
 
#5
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/21 13:17:42 (permalink)
+1 (1)
cgaufin
...
I think it's important to not get hung up on the specific sensor though, as my question is more basic. I really want to learn how to initiate a read command and then get the data from more than one sequential register during that same read before the stop condition is sent? Is it found in the structure of the TRB?

You MUST allow for which specific sensor you are using.
What happens when you read multiple bytes is a feature of the device, not of the I2C standard.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#6
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/22 06:02:45 (permalink)
-1 (1)
Agreed. The sensor does matter in the end, as the code written will need to meet it's requirements, but I didn't want to get too far lost in the sensor. I am still just trying to see how to properly use the MCC generated code and transaction request blocks to read multiple sequential registers before sending a Stop command for one I2C read function.
#7
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2668
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: online
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/22 06:24:52 (permalink)
0
It may be better to program the i2c directly, at least you will get a better grasp as to how it works.
I also don't use MCC.
 
Yes you can read multiple registers using i2c.
 
You need a prototype like this:
void i2c_gets(int Device, int Address, void *Dest, int Len);
 
That will read len bytes into destination starting at address.
Device is the address of the i2c device.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#8
Mysil
Super Member
  • Total Posts : 2885
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/23 03:08:03 (permalink)
0
Hi,
When using TRB interface,  transfer length is in the Transfer Request Block.
When transfer length is more than 1 byte, then make sure that buffer is an array with suitable size.
Are you sure that the problem is transfer length, and not something else?
Show the code you are actually using, all of it.
 
 

static  I2C1_TRANSACTION_REQUEST_BLOCK TRB;
static  I2C1_MESSAGE_STATUS status;
uint16_t address = 0x5E;                   /* 7 Bit value = 0x5E, 8 bit value 0xBC  */
static  uint8_t      read_data[10];        /* adjust array size as needed. */
uint8_t   length = 10;                     /* Adjust length argument as needed. */
   
    I2C1_MasterReadTRBBuild( &TRB,
                                read_data,  length,  address);
 
    I2C1_MasterTRBInsert(1, &TRB, &status);
                                   /* Check value of status variable while waiting for transfer to complete. */

 
   Mysil
 
#9
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/25 05:58:34 (permalink)
-1 (1)
I am not sure that transfer "length" is the issue (unless I'm not seeing something). The issue that I am trying to resolve is how to read from sequential registers each having a size of 8bits per register in one read command.
#10
Mysil
Super Member
  • Total Posts : 2885
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/25 06:41:20 (permalink)
+1 (1)
Hi,
You must show the code you are actually trying to use, all of it.
  
You may show program code in forum messages, using "BBCode",
see: https://en.wikipedia.org/wiki/BBCode
use tags for "monospaced text"
 
There is a facility in MPLAB X to 'Package' the files needed to restore a project into a .zip  file,
but you may not yet be allowed to make an attachment with a forum message.
There may be a minimum number of mesages, and some stars needed.
 
   Mysil
post edited by Mysil - 2018/06/25 06:46:34
#11
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/25 13:12:03 (permalink)
0
cgaufin
I am not sure that transfer "length" is the issue (unless I'm not seeing something).

Why?
As I already said once, with THIS device, all you need do is keep reading to get sequential registers.
 

The issue that I am trying to resolve is how to read from sequential registers each having a size of 8bits per register in one read command.

That is something that THIS DEVICE can do, and it is documented in the device's data sheet.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#12
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2668
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: online
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/06/25 13:43:02 (permalink)
0
Most i2c devices increment the address and then wrap round to the start address.
[START] [wr ID | 0]  [wr addr] [RESTART] [wr ID | 1]
loop 1 TO 10
    loop equals 10 ? [NACK..rd n to *Buffer++] else [ACK...rd n to *Buffer++]
[STOP]
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#13
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/07/12 09:19:41 (permalink)
-1 (1)
All, thank you again for the feedback. I will begin to try and see if I can use the non-MCC method, but I would really like to see how the MCC code can be applied to this problem. I can read the sensor using an Arduino and the codes/library at "https://github.com/IRNAS/TLV493D-3D-Magnetic-Sensor-Arduino-Library" .  I will work through the snippet of code from the github .cpp file and see what can be derived.
 

uint8_t TLV493D::init(int pwrPinLevel)
{
  /* Read all registers, although only interested in configuration data
     stored in rbuffers 7,8,9, as 0-6 might be empty or invalid at the moment.
  */

  m_bAddr = (pwrPinLevel == HIGH) ? m_bAddr1 : m_bAddr2;

  uint8_t n_bytes_returned = Wire.requestFrom(m_bAddr, sizeof(m_rbuffer));
//  Serial.print("Bytes returned: ");
//  Serial.println(n_bytes_returned);
  if (n_bytes_returned != sizeof(m_rbuffer)) return 0x05;

  for (int i = 0; i < sizeof(m_rbuffer); i++)
  {
    m_rbuffer = Wire.read();
  }

  //dump_registers();

  for(int i = 0; i < 5; i++)
  {
    if(m_rbuffer != 0x00) return 0x07;
  }

  // Write Register 0H is non configurable.  Set all bits to 0.
  m_wbuffer[0] = B00000000;

  // Read Register 7H 6:3 -> Write Register 1H 6:3.
  m_wbuffer[1] = m_rbuffer[7] & B01111000;

  // Read Register 8H 7:0 -> Write Register 2H 7:0.
  m_wbuffer[2] = m_rbuffer[8];

  // Read Register 9H 4:0 -> Write Register 3H 4:0 (Mod2).
  m_wbuffer[3] = m_rbuffer[9] & B00001111;

  // Set Power Mode (ulpm, lpm, fm, pd).
  for (int i = 0; i < sizeof(m_wbuffer); i++)
  {
    m_wbuffer |= m_lpm;
  }

  // Write to registers.
  Wire.beginTransmission(m_bAddr);

  uint8_t n_bytes_written;
  for (int i = 0; i < sizeof(m_wbuffer); i++)
  {
    n_bytes_written = Wire.write(m_wbuffer);
//    Serial.print("Bytes written: ");
//    Serial.println(n_bytes_written);
    if (n_bytes_written != 1) return 0x06;
  }

  uint8_t write_status = Wire.endTransmission();
//  Serial.print("Write status: 0x");
//  Serial.println(write_status, HEX);

  return write_status;
}

 
post edited by cgaufin - 2018/07/12 13:19:40
#14
jack@kksound
code tags!
  • Total Posts : 2762
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/07/12 09:28:41 (permalink) ☄ Helpfulby cgaufin 2018/07/12 09:45:40
+3 (3)
FYI code tags use the [ ] symbols not the < > symbols.
#15
cgaufin
New Users
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/07/12 09:38:16 (permalink)
-1 (1)
Thanks for the advice. I tried to go back and edit the post, but it says I don't have permission.
 
I will try to get those lines of code to work with the MCC generated code and replace the Wire references as applicable.
 
 
#16
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: How to read multiple registers at one time using MCC generated I2C functions 2018/07/12 13:14:06 (permalink)
+3 (3)
cgaufin
Thanks for the advice. I tried to go back and edit the post, but it says I don't have permission.

That is because there is a live URL in your post.
You can NOT edit a post that contains a URL due to a clash between this forum software and an independent firewall that is running on this web server.
See: How to avoid errors when editing posts

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#17
Jump to:
© 2018 APG vNext Commercial Version 4.5