CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

MCP3008 Lib to MCP3208
https://forum.codesys.com/viewtopic.php?f=21&t=6574
Page 1 of 1

Author:  backflip [ Mon Dec 07, 2015 9:17 pm ]
Post subject:  MCP3008 Lib to MCP3208

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.

Attachment:
Bildschirmfoto 2015-12-07 um 21.16.05.png


Thanks in advance
Tobias

Author:  Bakkertje [ Fri Dec 11, 2015 8:30 am ]
Post subject:  Re: MCP3008 Lib to MCP3208

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:
Code:
ui := ui + (BYTE_TO_UINT(aby[1]) AND 2#11) * 256;

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


Bye, Bakkertje

Author:  backflip [ Sat Dec 12, 2015 12:13 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

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:
Attachment:
3008.png


MCP3208 Datasheet:
Attachment:
3208.png


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:

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.

Author:  yyannekk [ Mon Mar 07, 2016 12:01 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

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?

Author:  Dergemer [ Tue Oct 18, 2016 4:32 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

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

Author:  Dergemer [ Sun Dec 11, 2016 6:10 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

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

Author:  Edwin Schwellinger [ Mon Dec 12, 2016 5:37 am ]
Post subject:  Re: MCP3008 Lib to MCP3208

Hi Jens,

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

BR
Edwin

Author:  Dergemer [ Thu Dec 15, 2016 10:33 am ]
Post subject:  Re: MCP3008 Lib to MCP3208

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

Author:  Dergemer [ Thu Mar 02, 2017 10:18 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

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

Author:  juancho216 [ Tue Jul 17, 2018 3:19 am ]
Post subject:  Re: MCP3008 Lib to MCP3208

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?

Author:  Joseph Sen Krobin [ Fri Feb 22, 2019 9:33 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

:D :D :D
juancho216 wrote:
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?

Author:  Joseph Sen Krobin [ Fri Feb 22, 2019 9:35 pm ]
Post subject:  Re: MCP3008 Lib to MCP3208

yyannekk wrote:
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?

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/