Library - MCP4131 Digital SPI Potentiometer

[title]MCP4131 Digital SPI Potentiometer[/title]
http://code.tinyclr.com/project/406/mcp4131-digital-spi-potentiometer/
[line]
This library is for the Digital Potentiometer found at Sparkfun Electronics: http://www.sparkfun.com/products/10613

Using this chip is not easy. For me it worked fine at 5Volt but I couldn’t get a solid read on the (multiplexed) SDI/SDO pin when running at 3.3V. Due to the limited number of pins available on the chip (why couldn’t they just add two more pins???) both MISO and MOSI data lines are on the same pin. Using a 4.7k resistor to the MISO port worked in 5V mode but not in 3.3V mode. I tried several other resistors (including the full range of the pot itself which is ~1k-10k) but didn’t get it to work. It’s ok to be used on the FEZ boards as they are 5V tolerant but on the ChipWorkX using 5V can kill the SPI interface unless you use voltage regulators. I had to use two Logic Level Converter (http://www.sparkfun.com/products/8745) because only two of the 4 ports on each of those is bi-directional. For my next order I will be getting these instead: http://www.sparkfun.com/products/10403

The orginal version was developed by declanshanaghy and rewritten by Dennis Liang (11/19/2011).
The code was ported to C# / netmf by Christian Ehlers (12/2011).
All versions are released into the public domain.

Please check the attached Visual Studio 2010 project for the full source code as well as the necessary using statements and assemblies. For any further questions or comments please check this thread: http://tinyurl.com/btzrwns starting on page 4.

The usage example will start at the highest resistance and will go all the way down to the lowest resistance and all the way back up. You can either use the setTap method to any value between 0 and 128 or you can use the increment and decrement methods which are commented in the example. Simply uncomment these two methods (//myMCP4131.increment(); and //myMCP4131.decrement():wink: and comment the myMCP4131.setTap(i); call below each of them.
For each step all the register values are shown on the Debug channel.

P.S. Technically you can use this chip as a DAC and can program a Signal Generator. I’ve been thinkin about that and will get to it when I have some time. I’m not sure of the speed that thing supports though so it’ll be fun to try out. If you do have any plans in that direction let me know in the support thread.

Hi Xykon, I use the Hydra mainboard and i’m getting a strange error:

Cannot deploy the base assembly ‘mscorlib’, or any of his satellite assemblies, to device - USB:Gadgeteer twice. Assembly ‘mscorlib’ on the device has version 4.2.0.0, while the program is trying to deploy version 4.1.2821.0

Any, Idea?

You need to change the framework to 4.2

@ Justin - That did the trick but the program output is the same for all i in the loop:

i=126
Wiper REG: 255
Status REG: 255
TCON: 255
i=125
Wiper REG: 255
Status REG: 255
TCON: 255

And the resistance doesn’t change =
Maybe, you’d be able to provide us with the system scheme or something…

Can you show your code please?

Hi, well the code is the same as this guy posted, I took it as is but I’ve one thing, I call the constructor with the following:
MCP4131 myMCP4131 = new MCP4131(Cpu.Pin.GPIO_Pin3, SPI.SPI_module.SPI1);

and this is the scheme of the system…
I’m really disappointed !

You can’t use Cpu.Pin.GPIO_Pin3 for chip select pin, you need actual pin number on that specific socket. You can find out that information from the Hydra schematics.

What Gadgeteer socket are you using on Hydra?

Number 3 , it is S type, so as number 4.

Here is a snippet from Hydra Mainboard class implementation it shows actual CPU pins for each socket pin.


 #region Socket 3
socket = GT.Socket.SocketInterfaces.CreateNumberedSocket(3);
socket.SupportedTypes = new char[] { 'S', 'Y' };
socket.CpuPins[3] = (Cpu.Pin)FEZHydra_Pins.PB8; //PWM0
socket.CpuPins[4] = (Cpu.Pin)FEZHydra_Pins.PB9; //PWM1
socket.CpuPins[5] = (Cpu.Pin)FEZHydra_Pins.PB12;
socket.CpuPins[6] = (Cpu.Pin)FEZHydra_Pins.PB13;
socket.CpuPins[7] = (Cpu.Pin)FEZHydra_Pins.PA26; //MOSI
socket.CpuPins[8] = (Cpu.Pin)FEZHydra_Pins.PA25; //MISO
socket.CpuPins[9] = (Cpu.Pin)FEZHydra_Pins.PA27; //SCK

// S
socket.SPIModule = SPI.SPI_module.SPI1;

// Y
socket.NativeI2CWriteRead = nativeI2C;

GT.Socket.SocketInterfaces.RegisterSocket(socket);
 #endregion Socket 3

 #region Socket 4
socket = GT.Socket.SocketInterfaces.CreateNumberedSocket(4);
socket.SupportedTypes = new char[] { 'S', 'U', 'Y' };
socket.CpuPins[3] = (Cpu.Pin)FEZHydra_Pins.PB2;
socket.CpuPins[4] = (Cpu.Pin)FEZHydra_Pins.PA11; //TXD1
socket.CpuPins[5] = (Cpu.Pin)FEZHydra_Pins.PA12; //RXD1
socket.CpuPins[6] = (Cpu.Pin)FEZHydra_Pins.PB14;
socket.CpuPins[7] = (Cpu.Pin)FEZHydra_Pins.PA26; //MOSI
socket.CpuPins[8] = (Cpu.Pin)FEZHydra_Pins.PA25; //MISO
socket.CpuPins[9] = (Cpu.Pin)FEZHydra_Pins.PA27; //SCK

// S
socket.SPIModule = SPI.SPI_module.SPI1;

// U
socket.SerialPortName = "COM3";

// Y
socket.NativeI2CWriteRead = nativeI2C;

GT.Socket.SocketInterfaces.RegisterSocket(socket);
 #endregion Socket 4


How do i integrate this part of the code to my project?

Just use this :

(Cpu.Pin)FEZHydra_Pins.PB8

i.e.

MCP4131 myMCP4131 = new MCP4131((Cpu.Pin)FEZHydra_Pins.PB8
, SPI.SPI_module.SPI1);

Ok, I’ll check it, the physical connections are fine?

Looks about right. I will try to power it with 3V3 though.

I know. Still wouldn’t hurt to double check.

@ Architect - It won’t compile.

I changed this:

MCP4131 myMCP4131 = new MCP4131((Cpu.Pin)FEZHydra_Pins.PB8, SPI.SPI_module.SPI1);

into this:

MCP4131 myMCP4131 = new MCP4131((Cpu.Pin)GHI.Hardware.FEZHydra.Pin.PB8, SPI.SPI_module.SPI1);

and now it compiles (Is it OK?) but the output is bad

when connected to socket #3:

i=81
Wiper REG: 0
Status REG: 0
TCON: 0
i=80
Wiper REG: 0
Status REG: 0
TCON: 0

And if I replace it to socket #4 output goes:

i=81
Wiper REG: 255
Status REG: 255
TCON: 255
i=80
Wiper REG: 255
Status REG: 255
TCON: 255

and the resistance isn’t changing…
I think i’m about to lose my job =\

@ andre.m - Thanks but still one problem to go,
The name ‘FEZHydra_Pins’ does not exist in the current context’
What should i do in order to fix this?

What about the bad output of the program?

Andre has asked about Spider. Let me be real clear here. If you are using a Spider, you’re using the wrong pin enumerations (the Hydra enums) and therefore you have to fix that before you can assume you’re correctly talking to the device.

If you would like to post clear pictures of wiring, post clear pictures of the Resources you have included in your test program, and post complete code, someone might have a better chance of assisting.

Ok I will, thanks.