Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

MCP3008 Lib to MCP3208

backflip
2015-12-07
2021-04-01
  • backflip - 2015-12-07

    Hi all

    I'm trying very hard to adapt the MCP3008 lib to read a MCP3208.

    MP3208 is similar so the MCP3008 in terms of connections but it's 12 bit instead of 10. Means way more exact...

    Nevertheless, I have absolut no clue how to change the code to add the highest 2 bit to the output value. Hopefully
    someone can assist me in changing the following lib code.

    Thanks in advance
    Tobias

    IMG: Bildschirmfoto 2015

     
  • Bakkertje - 2015-12-11

    Hi Tobias,

    Now all bits of the first byte and 2 bits of the second byte are used (10 bits in total), now you must use 4 bits of the second byte.
    Change this line of code:

    ui := ui + (BYTE_TO_UINT(aby[1]) AND 2#11) * 256;
    

    to

    ui := ui + (BYTE_TO_UINT(aby[1]) AND 2#1111) * 256;
    

    Bye, Bakkertje

     

    Related

    Talk.ru: 1

  • backflip - 2015-12-12

    Hi Bakkertja

    Many thanks for your help. In the meantime I figured out how to solve it. You're right with changing the mask to 2#1111. But additionally the transmitted date towards the MCP needs also to be changed.

    To help all the other guys struggling with the same problem here's the fix:

    MCP3008 Datasheet:

    MCP3208 Datasheet:

    Due to the difference in the MCU Transmitted Date (3 blocks á 8 bit) we need to shift start bit, SGL DIFF and the 3 bit channel address to an other place. This could be done with the following code:

    //old / original code
    //      aby2[0] := 1;
    //      aby2[1] := 16#80 + SHL(usiChannel AND 7, 4);
    //      aby2[2] := 0;   
    //changed code for MCP3208
          aby2[0] := 2#00000110;
          aby2[1] := 2#00000000;
          aby2[2] := 2#00000000;   
          aby2[0].0 := usiChannel.2;
          aby2[1].7 := usiChannel.1;
          aby2[1].6 := usiChannel.0;
    //end of new code
          
          IF NOT transfer(pabyTxBuffer:=ADR(aby2) , pabyRxBuffer:=ADR(aby) , udiLen:=3 , uiDelayus:=0) THEN
             _iState := 1000;
          END_IF
          ui := aby[2];
          ui := ui + (BYTE_TO_UINT(aby[1]) AND 2#1111) * 256;
          auiValue[usiChannel] := ui;
    

    Just for testing I add the "aby2" array. But it should work with the origin "aby" as well.

    Thanks again for your help and good luck to everyone trying similar stuff.

    IMG: 3008.png

    IMG: 3208.png

     

    Related

    Talk.ru: 1
    Talk.ru: 2

    • laruso - 2021-04-01

      I did my changes for MCP3204. Therefore I coded with shifting (like template), masking out unrelevant values with 2#... and leave some comments ;-)

      FOR usiChannel := 0 TO 3 DO
              aby[0] := SHL(2#1, 2);                                          // Start bit
              aby[0] := aby[0] + SHL(2#1, 1);                                 // 1: Single-ended, 0: differential input mode
              aby[0] := aby[0] + SHL(SHR(usiChannel AND 2#00000111, 2), 0);   // D2 / 2. digit of channel (only digits till D2 allowed)
              aby[1] := SHL(usiChannel AND 2#00000111, 6);                    // D1 and D0 / 1. and 2. digit of channel (only digits till D2 allowed)
              aby[2] := 0;    
      
              IF NOT transfer(pabyTxBuffer:=ADR(aby) , pabyRxBuffer:=ADR(aby) , udiLen:=3 , uiDelayus:=0) THEN
                  _iState := 1000;
              END_IF
      
              ui := SHL(BYTE_TO_UINT(aby[1] AND 2#00001111), 8) + aby[2];     // B11 - B0 / value bits (only digits till B11 allowed)
              auiValue[usiChannel] := ui;
          END_FOR 
      

      And additionally changed the device description. You can find and download both: https://forge.codesys.com/forge/talk/Runtime/thread/da04f8ac8b/#1731

       

      Last edit: laruso 2021-04-01
  • yyannekk - 2016-03-07

    Hi backflip

    Thank you for sharing your code! Helped me alot. I'm now also able to get data from an MCP3208

    Now my problem is I want to communicate with a totally different DAC, the MCP4922 (data sheet http://akizukidenshi.com/download/MCP4922.pdf). It's a write-only device.

    Now I think I just need to apply the datasheets SPI definition into the code (i.e. extend the BeforeWriteOutputs method). However this is not an easy task, as I'm pretty new to SCL programming. The sample SPI_MCP23S17 seems to be interesting, however with my limited knowledge it's also not easy (impossible) to understand.

    Can you give me some hints on how to proceed? Maybe direct me to some other examples, or literature?

     
  • Dergemer - 2016-10-18

    Hello!

    I'm totally new to codesys (and raspberry pi) and am trying to use the 3208 as well.
    unfortunately the piece of code posted does not help me yet.
    Could you please post the complete code or explain what you did? Did you change the library? I tried that, but it did not work. there were variables missing.

    thanks in advance,
    Jens

     
  • Dergemer - 2016-12-11

    Hi backflip,

    I just spent another 6 hours trying to figure out how to get this thing working. unfortunately I didn't. :-/
    what I did manage is, that my modified MCP3008 now counts to 1024 4 times. I just does not add up the counts.
    could you maybe publish your library and device (xml)?
    That would be kind.

    Or can anybody else help me?

    Kind regards,
    Jens

     
  • eschwellinger

    eschwellinger - 2016-12-12

    Hi Jens,

    but isn't this MCP3202 from our public library/devices section:
    l viewforum.php?f=23 l
    MCP3202:
    l viewtopic.php?f=23&t=5872&p=16549#p16549 l
    Is the diference in havin 8 instead of 8 channels (MCP3208) the probem?

    BR
    Edwin

     
  • Dergemer - 2016-12-15

    Hi Edwin,

    I tried this as well. After spending some more time it works now, but yes, having only 2 ports is not enough for me. I need all 8.
    Where and how can I change this?

    Gerne auch in deutsch (per PN oder so).

    Kind regards,
    Jens

     
  • Dergemer - 2017-03-02

    I tried to contact CODESYS, maybe I'll get some help there.
    If I will, I'll let you all know.

     
  • juancho216 - 2018-07-17

    Hi backflip

    thanks for your contribution. It served me a lot.

    I am also trying to do the same for the MCP4922. Hello
    yyannekk, have you been able to do it or can you help me?

     
  • Anonymous - 2019-02-22

    Originally created by: Joseph Sen Krobin

    juancho216 hat geschrieben:
    Hi backflip
    thanks for your contribution. It served me a lot.
    I am also trying to do the same for the MCP4922. Hello
    yyannekk, have you been able to do it or can you help me?

     
  • Anonymous - 2019-02-22

    Originally created by: Joseph Sen Krobin

    yyannekk hat geschrieben:
    Hi backflip
    Thank you for sharing your code! Helped me alot. I'm now also able to get data from an MCP3208
    Now my problem is I want to communicate with a totally different DAC, the MCP4922 (data sheet http://akizukidenshi.com/download/MCP4922.pdf). It's a write-only device.
    Now I think I just need to apply the datasheets SPI definition into the code (i.e. extend the BeforeWriteOutputs method). However this is not an easy task, as I'm pretty new to SCL programming. The sample SPI_MCP23S17 seems to be interesting, however with my limited knowledge it's also not easy (impossible) to understand.
    Can you give me some hints on how to proceed? Maybe direct me to some other examples, or literature?

     
  • Anonymous - 2019-12-21

    Originally created by: phenixrb

    Hello everyone ,
    can you explain to me how to integrate the MCP3208 library in a project.
    or how to modify the existing MCP3008 library from codesys to MCP3208?

    I can use the examples explained in this post, but impossible for me to integrate it into a complete project.

    I wish to be able to use the 8 inputs.

    Thanks for your help.

     
  • Anonymous - 2019-12-31

    Originally created by: phenixrb

    Good morning all ,

    I want to try to modify, the device MCP3008 to MCP3208, using the information described in this post.
    I find two DEVICES for MCP3008, in "CODESYS Control for Raspberry PI" "3.5.15.20":
    MCP3008.devdesc
    MCP3008_IO.devdesc

    should both be changed?
    thank you

     
  • eschwellinger

    eschwellinger - 2020-01-01

    Hi,
    the difference on these two device descriptions,
    _IO offers additional the mapping page, to map variables.
    So use either the one or the other.
    ( without _io you get the values from MCP3008.auiValue[0] see the example project)

    BR
    Edwin

    IMG: IncludeIIOMapping.png

     
  • Anonymous - 2020-01-01

    Originally created by: phenixrb

    hello Edwin,

    thank you for that clarification ,
    is the information described in this post sufficient to adapt the MCP3008 device to MCP3208?

    and best wishes for this new year.

     
  • Anonymous - 2020-01-02

    Originally created by: phenixrb

    Hello everyone ,

    can you tell me if the procedure is good regarding:
    the modification of the MCP3008 library to MCP3208 ?

    I used the MCP3008 files as a base.

    I modified "AfterReadInputs" as indicated in the post,
    replaced "MCP3008" by "MCP3208" in all the other files in the library.
    save as a "compiled library" named MCP3208 and installed in the "library repository".

    then I modified the "xml" file from MCP3008 to MCP3208.
    following the indications of FORGE.

    are there any errors?

    is it possible to have an open source driver in the device list?
    because, for the moment it does not appear in the list.
    if so, what is the procedure?

    thank you

    IMG: device mcp3208 xml.jpg

    IMG: enregistrement bibliothèque.jpg

    IMG: LISTE CHOIX APPAREILS.jpg

     
  • i-campbell

    i-campbell - 2020-01-22

    Hi, phenixrb.
    Did you install the device description to CODESYS using Tools > Device Repository? I copied your device description image out to file, then added it to my device repository, then it came up when I went to add a device to my master.

    It looks OK.

    Will you upload to forge?
    I have already registered your device ID here:
    https://forge.codesys.com/drv/io-drivers/database/Home/

    Next step for you would be to :
    1. register, then make a new io driver project here:
    https://forge.codesys.com/drv/add_project
    By convention, Either MCP3208 or SPI MCP3208 as the name, and mcp3208 as the URL
    2. update the diver ID register (my first link of this post) to this newly created homepage url
    3. Perhaps duplicate and reword the wiki from the MCP3008 project. I notice you handle your IO differently though, so proofread and make sure it make sense for your implementation. (You can get the source by clicking History > select r19 as v1 and r20 as V2 then hitting compare)
    4. Perhaps use cForge tool to create a package of your two files, .library and .xml
    5. select a license and add it's file to your project
    6. check it into your project's code repository
    7. (Optional) invite others to critque

    let me know how I can help. I look forward to seeing you get your first driver up on the forge.

    IMG: Screen Shot 2020

     
  • Anonymous - 2020-01-23

    Originally created by: phenixrb

    "[quote="I-Campbell"]Hi, phenixrb.
    Did you install the device description to CODESYS using Tools > Device Repository? I copied your device description image out to file, then added it to my device repository, then it came up when I went to add a device to my master.

    It looks OK."

    HI I-Campbell,
    thank you, i hadn't done that!
    it works better now ....

    I bought two MCP2308 chips, and now I have to test this in real life.
    with a second activated SPI: SPI-1 with 2 CS, cs1.0 and cs.1.1
    I keep SPI-0 for MCP23S17, with 1 CS,
    since chips have physical addresses, 1 CS is sufficient.

    Regarding FORGE, I'm going to look at how to make the driver available to others, it's new to me.
    and above all I do not claim to have created the driver.
    I took the explanations of the other participants on this post,
    and I just did a program test on the raspberry.
    also, comments are welcome.

     
  • phenixrb - 2020-05-18

    hello everyone, hope you are well.

    I come back to you concerning the MCP3208,
    after making the changes to go from MCP3008 to MCP3208, seen earlier in the post.
    first, the modifications work well with the MCP3208, I receive many values of the chips.

    my problem: I would like to use two MCP3208 chips.
    I noticed that in the lib for MCP3008 or now MCP3208,
    it is not possible to choose the hardware address, as it is possible to do for MCP23S17 chips.
    and so I can only use one MCP3208 chip.

    thanks for any help.

     

Log in to post a comment.