|
|
View previous topic :: View next topic |
Author |
Message |
ratgod
Joined: 27 Jan 2006 Posts: 69 Location: Manchester, England
|
MCP41xxx / MCP42xxx Driver |
Posted: Mon Nov 24, 2008 11:00 pm |
|
|
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
|
|
Posted: Thu Nov 27, 2008 11:32 am |
|
|
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
|
|
Posted: Tue Dec 02, 2008 4:33 am |
|
|
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: 2128 Location: albany ny
|
|
Posted: Thu Aug 16, 2012 5:47 pm |
|
|
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
|
|
Posted: Sun Feb 10, 2013 12:19 pm |
|
|
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: 2128 Location: albany ny
|
|
|
|
|
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
|