CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

MCP41xxx / MCP42xxx Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

MCP41xxx / MCP42xxx Driver
PostPosted: Mon Nov 24, 2008 11:00 pm     Reply with quote

Here is a driver for both the MCP41xxx and the MCP42xxx. Theoretically it can be used to drive both devices with the same driver.

It allows you to use more than one device and independently control each pot.

To wire it up, connect CS, DI and SCK of the MCP4xxxx device to the PIC (doesn't have to be the SPI bus pins on the PIC as it's bit-bashed) and then make alterations to the #define's.

For the CS argument on the set_pot command, put in the pin name of where the CS line is hooked up for the MCP4xxxx you want to talk to.

For the MCP42xxx, I left SO floating and tied RS and SHDN to +5v. This makes it almost identical to the MCP41xxx, except it has 2 pots.

Driver code:
Code:

///////////////////////////////////////////////////////////////////////////
////   Driver for mcp41xxx & mcp42xxx digital pots                     ////
////   Adapted from the MCP41010.C driver from CCS                     ////
////     By Peter Murray                                               ////
////   set_pot (int data, byte CS,POT);                                ////
////              data = new value                                     ////
////              CS = Chip Select Pin on digipot chip                 ////
////              POT = POT_A,POT_B,POT_BOTH                           ////
////                                                                   ////
////   shutdown_pot (byte CS);  shutdown pot to save power             ////
////              CS = Chip Select Pin on digipot chip                 ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////

// Define pins here
#define CLK PIN_C3
#define DAT PIN_C5

// internal defines
#define POT_A    1
#define POT_B    2
#define POT_BOTH 3

void set_pot(int data, byte CS,byte POT)
{
   BYTE i;
   BYTE cmd[2];

   cmd[0] = data;
   if (POT==POT_A) cmd[1] = 0b00010001;
   if (POT==POT_B) cmd[1] = 0b00010010;
   if (POT==POT_BOTH) cmd[1] = 0b00010011;

   output_low(CLK);
   output_low(CS);

   for(i=1;i<=16;++i) {
      output_bit(DAT, shift_left(cmd,2,0));

      output_high(CLK);
      output_low(CLK);
   }
   output_high(CS);
}

void shutdown_pot (byte CS) {
   BYTE i;
   BYTE cmd[2];
   cmd[0] = 0;
   cmd[1] = 0x21;
   output_low(CLK);
   output_low(CS);
   for(i=1;i<=16;++i) {
      output_bit(DAT, shift_left(cmd,2,0));
      output_high(CLK);
      output_low(CLK);
   }
   output_high(CS);
}


And here is some example code to use it:
Note: Count is a byte variable initialized to 0.
Code:

   while(true)
   {
     delay_ms(100);
     if (count==255) count=0;
       else
     count++;
     
   set_pot(count,PIN_C1,POT_A);
   delay_ms(1);
   set_pot(255-count,PIN_C1,POT_B);
   delay_ms(1);

   }


I hope this helps someone.

(search keywords: MCP41010 MCP42010 SPI Digital potentiometer )


Last edited by ratgod on Tue Dec 02, 2008 4:35 am; edited 1 time in total
Diego Oliveira



Joined: 10 Aug 2008
Posts: 3

View user's profile Send private message

PostPosted: Thu Nov 27, 2008 11:32 am     Reply with quote

Very good!

But I did not understand the true functioning of the routine of shutdown_pot(cs); Could you give me more information?

Thank you for your attention!
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Tue Dec 02, 2008 4:33 am     Reply with quote

This is a function that was originally in the driver, I haven't tried it myself but the datasheet states :
Quote:
Shutdown feature open circuits of all resistors for maximum power savings

I have to assume that this is what it induces.
If anyone finds out for certain I would be happy to add it here.
I'm not at my lab this week so I cannot try it.

Thanks for the reply

Peter M
asmboy



Joined: 20 Nov 2007
Posts: 2116
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Aug 16, 2012 5:47 pm     Reply with quote

re: MCP 4251

this post was brought to my attention by an intern , who tried to use it, as is, but found it is defective, since 10 data bits are required to send all 257 valid positions - per the data sheet .

as written the code only allows an 8 bit data value and is unable to send codes 255,256 or 257 - thus locking out the 3 steps near the A terminal of the pot.

caveat emptor
ratgod



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

PostPosted: Sun Feb 10, 2013 12:19 pm     Reply with quote

asmboy:
I have not heard of the MCP4251 until you mentioned it, having read the datasheet it looks like a nice digipot but this driver was written for the MCP41xxx and the MCP42xxx range, I'd personally consider the MCP4251 as being in the MCP4xxx range, but that's an easy mistake to make, the part numberings are a little odd at times.

It was a while since I wrote that driver, I'm not sure the MCP4251 was available at that time, but now I know about it, I may adapt this driver to cover it.

Thanks for the response.
asmboy



Joined: 20 Nov 2007
Posts: 2116
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun Feb 10, 2013 1:42 pm     Reply with quote

i think you will find , that this fills the bill, for all tap positions of the pot.

http://www.ccsinfo.com/forum/viewtopic.php?t=48927&highlight=mcp4%2A
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group