Shop OBEX P1 Docs P2 Docs Learn Events
Any example code for I2C — Parallax Forums

Any example code for I2C

SawmillerSawmiller Posts: 276
edited 2013-06-04 20:43 in Learn with BlocklyProp
do we have any example code for I2C ? in C for the prop that is.
i am looking at i2c.h in propgcc/propeller-elf/include and see the briefs... but i could use a lil more if someone know of some code.
dan

Comments

  • dgatelydgately Posts: 1,629
    edited 2013-06-02 17:58
    Sawmiller wrote: »
    do we have any example code for I2C ? in C for the prop that is.
    i am looking at i2c.h in propgcc/propeller-elf/include and see the briefs... but i could use a lil more if someone know of some code.
    dan

    Here's a Spin2Cpp-created project for the Wii Nunchuk that uses i2c. Hopefully, you'll be able to mod as needed. This is a Zip of a SimpleIDE project.

    TestWiNunchukC.zip


    dgately
  • SawmillerSawmiller Posts: 276
    edited 2013-06-02 18:06
    thnaks, will look at it.
    dan
  • David BetzDavid Betz Posts: 14,516
    edited 2013-06-02 19:35
    Sawmiller wrote: »
    do we have any example code for I2C ? in C for the prop that is.
    i am looking at i2c.h in propgcc/propeller-elf/include and see the briefs... but i could use a lil more if someone know of some code.
    dan
    Did you try using the functions mentioned in i2c.h? Did you have problems with them that caused you to look for other solutions?
  • SawmillerSawmiller Posts: 276
    edited 2013-06-03 03:36
    hi David,
    i was looking for example code because i have written the functions as i think they need to be, foe example
    i2c_Address = simple_i2cOpen( DS1721_Addr,i2cSCL,i2cSDA); // get a i2c address for the ds1721 chip


    flag = i2cWrite(i2c_Address,DS1721_Addr,AccessConfig,8,False); // tell DS1721 we are going to config

    if (flag == 0)
    printf("Success on config \n");

    if (flag == -1)
    printf("Failure on config\n");
    pause (1000);
    flag = 2;


    flag = i2cWrite(i2c_Address,DS1721_Addr,Cont_09_Bit,8,True); // tell DS1721 9 bit accuracy
    if (flag == 0)
    printf("Success on 9 bit\n");

    pause (1000);
    flag = 2;

    flag = i2cWrite(i2c_Address,DS1721_Addr,StartConvert,8,False);
    if (flag == 0)
    printf("success on starting conversion of temp\n");

    and i am getting success printf's ( flag coming back as zero )
    but i am not getting a valid temp reading, have gone over my wiring several times
    ( using a setup that i put together in 2008 and used spin with.)

    i am excited about being able to use C with the prop, but was hoping for more documentation like the prop manual ( which is a lil more wordy ) or like the way they did the sx when they were programming that.
    coming back to programming after a 5 year hiatus so i am a lil rusty.
    spin2cpp appears to work, but doesn't interact with the library
    sorry, don't mean to be so verbose.
    dan
  • David BetzDavid Betz Posts: 14,516
    edited 2013-06-03 06:51
    Sawmiller wrote: »
    hi David,
    i was looking for example code because i have written the functions as i think they need to be, foe example
    i2c_Address = simple_i2cOpen( DS1721_Addr,i2cSCL,i2cSDA); // get a i2c address for the ds1721 chip


    flag = i2cWrite(i2c_Address,DS1721_Addr,AccessConfig,8,False); // tell DS1721 we are going to config

    if (flag == 0)
    printf("Success on config \n");

    if (flag == -1)
    printf("Failure on config\n");
    pause (1000);
    flag = 2;


    flag = i2cWrite(i2c_Address,DS1721_Addr,Cont_09_Bit,8,True); // tell DS1721 9 bit accuracy
    if (flag == 0)
    printf("Success on 9 bit\n");

    pause (1000);
    flag = 2;

    flag = i2cWrite(i2c_Address,DS1721_Addr,StartConvert,8,False);
    if (flag == 0)
    printf("success on starting conversion of temp\n");

    and i am getting success printf's ( flag coming back as zero )
    but i am not getting a valid temp reading, have gone over my wiring several times
    ( using a setup that i put together in 2008 and used spin with.)

    i am excited about being able to use C with the prop, but was hoping for more documentation like the prop manual ( which is a lil more wordy ) or like the way they did the sx when they were programming that.
    coming back to programming after a 5 year hiatus so i am a lil rusty.
    spin2cpp appears to work, but doesn't interact with the library
    sorry, don't mean to be so verbose.
    dan
    If you could post all of your code I could try running it to see where it is getting into trouble. In the meantime, I'm attaching the source code for the simple i2c driver in the hopes that that might help you.

    i2c_simple.c
  • SawmillerSawmiller Posts: 276
    edited 2013-06-03 08:53
    thanks.
    if you have time, please look.
    this is the latest itteration, was trying simple open, so i didnt have to figure out cog driver yet, but i commented out simple and am playing with the cog driver
  • David BetzDavid Betz Posts: 14,516
    edited 2013-06-03 09:06
    Sawmiller wrote: »
    thanks.
    if you have time, please look.
    this is the latest itteration, was trying simple open, so i didnt have to figure out cog driver yet, but i commented out simple and am playing with the cog driver
    I can see a number of problems right away. First, you should not redefine I2C_COGDRIVER. It is already defined in i2c.h and redefining it is likely to cause problems. Second, you need to pass the address of a I2C_COGDRIVER structure to i2cOpen. You're passing an uninitialized pointer. Try modifying your code like this:
    int main()                                    // Main function
    {
      long freq          = 100000 ;     
      int  i2cSDA        = 5;                     // pin 5
      int  i2cSCL        = 3;                     // pin 3
      int DS1721_Addr      = 146;                 // ds1721 address 10010010
      int DS1721_Addr_Read = 147;                 // ds1721 address +1 for read
    
      int ReadTemp      = 0xAA;
      int AccessTH      = 0xA1;
      int AccessTL      = 0xA2;
      int AccessConfig  = 0xAC;
      int ReadCounter   = 0xA8;
      int ReadScope     = 0xA9;
      int StartConvert  = 0x51;
      int StopConvert   = 0x22;
      int OneShotMode   = 1;
      int Cont_09_Bit   = 0x00; 
      
      //I2C_SIMPLE *  i2c_Address;
      I2C *  i2c_Address;
        int cog = 3;
    
      I2C_COGDRIVER COG;
     
      int flag = 2;
      int tempvar = 0;
      const int  True  = 1;
      const int False = 0;
    
      pause (1000);
    
    
        printf("starting\n");
    
        // simple_i2cOpen( i2c_Address,i2cSCL,i2cSDA);     // get a i2c address for the ds1721 chip
    
          i2c_Address = i2cOpen(&COG, i2cSCL, i2cSDA, freq);
    
  • SawmillerSawmiller Posts: 276
    edited 2013-06-03 09:31
    ok, thanks david. will let this settle for a while
    where did you find i2c_simple.c ?

    am trying to see why I2C_COGDRIVER COG; works
    but if i uncomment I2C_SIMPLE i2c_Address; it doesnt work the same.
    sigh
    thanks for the help, i will work on this some more, and see if i can't dragg my head out of where it is :)
  • David BetzDavid Betz Posts: 14,516
    edited 2013-06-03 09:45
    Sawmiller wrote: »
    ok, thanks david. will let this settle for a while
    where did you find i2c_simple.c ?

    am trying to see why I2C_COGDRIVER COG; works
    but if i uncomment I2C_SIMPLE i2c_Address; it doesnt work the same.
    sigh
    thanks for the help, i will work on this some more, and see if i can't dragg my head out of where it is :)
    i2c_simple.c is part of the PropGCC source code on Google Code:

    http://code.google.com/p/propgcc/

    It's in propgcc/lib/sys/propeller.
  • SawmillerSawmiller Posts: 276
    edited 2013-06-04 18:38
    This works now, will add display,probally beans old hc4 LEDs and control over a 120v outlet later.
    But for now its working.
    Thanks David.
  • David BetzDavid Betz Posts: 14,516
    edited 2013-06-04 20:43
    Sawmiller wrote: »
    This works now, will add display,probally beans old hc4 LEDs and control over a 120v outlet later.
    But for now its working.
    Thanks David.
    Congratulations on getting it working! By the way, someone else has discovered a problem with using the i2c driver in xmm modes. I'm working on a fix but I wanted to warn you in case you decide to switch to xmm at some point.
Sign In or Register to comment.