CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Tue Jun 25, 2019 11:29 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 12 posts ] 
Author Message
 Post subject: MCP3008 Lib to MCP3208
PostPosted: Mon Dec 07, 2015 9:17 pm 
Offline

Joined: Tue Dec 01, 2015 7:32 pm
Posts: 2
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


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Fri Dec 11, 2015 8:30 am 
Offline

Joined: Fri Nov 06, 2015 12:10 pm
Posts: 34
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


Top
   
PostPosted: Sat Dec 12, 2015 12:13 pm 
Offline

Joined: Tue Dec 01, 2015 7:32 pm
Posts: 2
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.


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Mon Mar 07, 2016 12:01 pm 
Offline

Joined: Thu Mar 03, 2016 8:12 am
Posts: 5
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?


Top
   
PostPosted: Tue Oct 18, 2016 4:32 pm 
Offline

Joined: Thu Sep 08, 2016 10:07 am
Posts: 6
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


Top
   
PostPosted: Sun Dec 11, 2016 6:10 pm 
Offline

Joined: Thu Sep 08, 2016 10:07 am
Posts: 6
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


Top
   
PostPosted: Mon Dec 12, 2016 5:37 am 
Offline
Site Admin

Joined: Mon Sep 05, 2005 9:42 am
Posts: 3324
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


Top
   
PostPosted: Thu Dec 15, 2016 10:33 am 
Offline

Joined: Thu Sep 08, 2016 10:07 am
Posts: 6
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


Top
   
PostPosted: Thu Mar 02, 2017 10:18 pm 
Offline

Joined: Thu Sep 08, 2016 10:07 am
Posts: 6
I tried to contact CODESYS, maybe I'll get some help there.
If I will, I'll let you all know.


Top
   
PostPosted: Tue Jul 17, 2018 3:19 am 
Offline

Joined: Wed Jun 27, 2018 10:13 pm
Posts: 1
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?


Top
   
PostPosted: Fri Feb 22, 2019 9:33 pm 
Offline

Joined: Thu Feb 08, 2018 1:03 pm
Posts: 3
: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?


Top
   
PostPosted: Fri Feb 22, 2019 9:35 pm 
Offline

Joined: Thu Feb 08, 2018 1:03 pm
Posts: 3
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?


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 12 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited