Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

i2C Question (retrieving value)


Best Answer iced98lx, 20 May 2013 - 01:13 PM

Thanks for the insights guys, I see what they're doing now, and it's frankly a lot less confusing to just store an array with the values before I set them for easy retrieval and assume the i2c set goes properly.

Go to the full post


  • Please log in to reply
7 replies to this topic

#1 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 18 May 2013 - 08:37 PM

So, I'm using someone else's great porting of a driver for the ADAFruit 16 channel PWM Driver, and I can set PWM output fine but now I'm looking to GET PWM's out of it, so I can check status without re-saving it in another variable (save ram, folks)

 

so, here is the code that saves the values:

 

 

public void setPWM(byte num, UInt16 on, UInt16 off)
        {
            write8((byte)(LED0_ON_L + 4 * num), (byte)on);
            write8((byte)(LED0_ON_H + 4 * num), (byte)(on >> 8));
            write8((byte)(LED0_OFF_L + 4 * num), (byte)off);
            write8((byte)(LED0_OFF_H + 4 * num), (byte)(off >> 8));
        }

 

write8 takes two arguments, the Address and the value. 

 

I pass in the number of the PWM output (0->15) the value on (0->~4000) and value off (0->~4000) and it bitshifts and sets the the PWM timings (On low, on high, off low and off high). For the most part since i'm driving a light I just pass 0 as the on and something > 0 for off.

 

I'd like to be able to retrieve either on and off or just the off value I've passed in.

 

I've tried this:

 

int power= read8((byte)(LED0_OFF_L + 4 * num)); and it's accurate until it gets above 256. 

 

I've tried this:

 

 

int power= read8((byte)(LED0_OFF_H + 4 * num));
power = power << 8;

 

and it's not accurate. 

 

 

Any suggestions?

 

Thanks!

 



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 May 2013 - 09:20 PM

Hi iced98lx, What do the write8 and read8 functions looks like? Chris

#3 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 19 May 2013 - 12:00 AM

Call me Chris, Chris, they look like this:

   private byte read8(byte addr)        {            byte[] ReadBuffer = new byte[1];            this._Device.ReadRegister(addr, ReadBuffer);            return ReadBuffer[0];        }        private void write8(byte addr, byte d)        {            this._Device.WriteRegister(addr, d);        }

ReadRegister and WriteRegister are coming from NooM's i2c library



#4 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 19 May 2013 - 06:42 AM

Hi Chris,

 

Can you give us some examples of what you wrote to the device and what you got back for the high and low parts.

(Before you shift to the left)

 

This may help us understand the issue.

 

I notice you say the range is 0-4000, this gives 0x0000 to 0x0FA0 0x0FFF. It might be worth masking the high byte before shifting.

int power =  read8((byte)(LED0_OFF_H + 4 * num)) & 0x0F;    power =  power << 8;    power += read8((byte)(LED0_OFF_L + 4 * num));

Paul

 

EDIT:

Just read some of the data sheet.

The on/off values are 0-4095 so actually 0x0000 to 0x0FFF. Not 0xFAO as I wrote above.

 

Looking at everything I could find, I can't see any example of reading the current servo setting back from the driver.

(There is an example of reading back a mode in the example driver code on Github.)

 

It may be that what you are trying to do is just not possible.

I don't seem to have found any data showing the I2C interface registers. A data sheet of I2C would clear things up.

 

Paul



#5 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 19 May 2013 - 10:08 AM

 (0->~4000)

doesent fit in a byte, so you have to send/read 2, and shift it right before/after.

 

like

 

byte[] ReadBuffer = new byte[2];

this._Device.ReadRegister(addr, ReadBuffer);

int value = shift the byte correct here;

return value;



#6 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 20 May 2013 - 01:13 PM   Best Answer

Thanks for the insights guys, I see what they're doing now, and it's frankly a lot less confusing to just store an array with the values before I set them for easy retrieval and assume the i2c set goes properly.



#7 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 20 May 2013 - 04:42 PM

Glad your happy!



#8 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 20 May 2013 - 05:54 PM

Glad your happy!

 

in the end it cuts down on i2c traffic to store it locally, no downsides really here besides slight memory use.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.