Help with i2c - aka help me learn how to convert a datasheet into code

  1. Not without additional electronics.
  2. Shift it right and use that value as a 7bit address.
1 Like

Unfortunately, those chips come with pre-configured slave addresses and cannot be changed :wall:

Look at the Devantech LCD picture : it shows address 0xC6, which is greater than 0x7F. As Architect stated, you should then use (0xC6 >> 1) as base address for your I2C device in this case.

1 Like

First of all thanks for the help!

:dance: Woohoo. :dance:

Some tweaking still needed, but so far so good. I think I need to add some pull down resistors to the board. There are other options I want to play with - for example I wish the value would be closer to 0 when pulled low (is 240 now).

In the video, it’s showing a 10bit value of 1 one of the ADC channels.

  1. (1009) Initially it’s connected to 5v, which is how the board is powered with - via the Gadgeteer breakout.
  2. (496) Then I disconnect the power, and the value drops.
  3. (752) Next I connect it to the 3v3 pin.
  4. (240) Then I disconnect and plug into ground.

Below is the code, any suggestions would be appreciated (realizing this is just PoC code and hardly the finished product).


using Microsoft.SPOT;
using GT = Gadgeteer;
using Microsoft.SPOT.Hardware;
using Gadgeteer.Modules.GHIElectronics;

namespace gMBN
{
    public partial class Program
    {
        Font fnt;
        I2CDevice MyI2C;
        int address = 53;        
        void ProgramStarted()
        {
            fnt = Resources.GetFont(Resources.FontResources.NinaB);
            I2CDevice.Configuration con = new I2CDevice.Configuration((byte)address, 400);
            MyI2C = new I2CDevice(con);
            setup();

            GT.Timer t = new GT.Timer(1000);
            t.Tick += (o) =>
                {
                    t.Stop();
                    read();
                    t.Start();
                };
            t.Start();
        }

        void setup()
        {
            // start config
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];

            // 1101 0110 -- Table on 14  // 0xD6 or 214
            // 1 = Setup
            // 101 = Internal Ref / An Input / Ref not connected / Internal ref always on -- Table 6 on page 19
            // 0 = internal clock
            // 1 = Unipolar
            // 1 = No reset
            // 0 = "don't care bit"
            
            byte[] RegisterNum = new byte[1] { 0xD6 };
            xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
            if (MyI2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Success: ");
            }
        }

        void read()
        {
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[3];
            
            // reading ADC 4
            // 0110 1001 -- page 15 -- 105 or 0x69
            // 0 = reading, aka confi
            // 1 1 = *Converts channel selected by CS3-CS0. - table 5 on page 18
            // 0 1 0 0 == selection of ADC4
            // 1 == Single ended reading 

            byte[] RegisterNum = new byte[1] { 105 };
            // to specify which channel
            xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
            // value comes back in 2 bytes - datasheet page 16: 
            // The result is transmitted in two bytes; first four bits of
            // the first byte are high, then MSB through LSB are consecutively
            // clocked out.
            byte[] RegisterValue1 = new byte[1];
            xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue1);
            byte[] RegisterValue2 = new byte[1];
            xActions[2] = I2CDevice.CreateReadTransaction(RegisterValue2);

            if (MyI2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
               // trim first 4 bits, and convert to 10bit value
                var z = (RegisterValue1[0] & 15) << 8;
                z+=RegisterValue2[0];

                Debug.Print("Register value1 : " + RegisterValue1[0].ToString());
                Debug.Print("Register value2 : " + RegisterValue2[0].ToString());

                Debug.Print("10 bits: " + z);

                displayN18.SimpleGraphics.Clear();
                displayN18.SimpleGraphics.DisplayText(z.ToString(), fnt, GT.Color.White, 10, 10);
            }
        }
    }
}

1 Like