Difference between revisions of "NU32: Using the MCP42X1 SPI Digital Potentiometer"

From Mech
Jump to navigationJump to search
(Created page with " The MCP42X1 is a 7-bit dual digital potentiometer. It is available in total resistance options of 5, 10, 50 and 100 kOhm. It is controlled using SPI communication and a digit...")
 
 
(18 intermediate revisions by one other user not shown)
Line 1: Line 1:
'''THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT [[NU32|THE NU32 PAGE]].'''



The MCP42X1 is a 7-bit dual digital potentiometer. It is available in total resistance options of 5, 10, 50 and 100 kOhm. It is controlled using SPI communication and a digital output (chip select).
The MCP42X1 is a 7-bit dual digital potentiometer. It is available in total resistance options of 5, 10, 50 and 100 kOhm. It is controlled using SPI communication and a digital output (CS). It has two independent potentiometer channels, each with its own wiper.




== Overview ==
== Overview ==
A potentiometer is useful in a variety of circumstances when a variable resistance is needed. A digital potentiometer allows provides a variable resistor that can be controlled from a microcontroller. Because the MCP42X1 is a 7-bit device, there are 2^7 = 128 possible resistance "steps". By communicating with the IC over SPI, we can choose what step (and thus, resistance) the potentiometer is set at.
A potentiometer is useful in a variety of circumstances when a variable resistance is needed. A digital potentiometer provides a variable resistor that can be controlled from a microcontroller. Because the MCP42X1 is a 7-bit device, there are 2^7 = 128 possible resistance "steps". By communicating with the IC over SPI, we can choose what step (and thus, resistance) the potentiometer is set at.

[[image:mcp42x1.png|thumb|300px|MCP42X1 Pinout|right]]


The resistance across the PxB and PxW (the wiper) is governed by
The resistance across the PxB and PxW (the wiper) is governed by
Line 10: Line 14:
where the 5kOhm comes from the total resistance of the pot, depending on the model. The value N is the setting of the pot between 0 and 127.
where the 5kOhm comes from the total resistance of the pot, depending on the model. The value N is the setting of the pot between 0 and 127.


The pot requires four communication pins from a microcontroller: SPI CLK, SDO, SDI, and one digital output. The pot is powered with 3.3V. This model is a dual pot chip, meaning it has two completely independent potentiometer channels in the same IC, referred to as Pot 0 and Pot 1, respectively.
The pot requires four communication pins from a microcontroller: SPI CLK, SDO, SDI, and one digital output. The pot is powered with 3.3V. This model is a dual pot chip, meaning it has two completely independent potentiometer channels in the same IC, referred to as Pot 0 and Pot 1, respectively.


== Details ==
== Details ==
[[Media:mcp42X1.pdf|Datasheet]]
[[Media:MCP42X1.pdf|Datasheet]]


Example schematic when communicating with the NU32, using SPI channel 1:
Example schematic when communicating with the NU32, using SPI channel 1:
* Pot 1-CS -----> F3
* Pot 1-CS -----> NU32-F3 (this can be any DIO pin you want, change code accordingly)
* Pot 2-SCK -----> D10
* Pot 2-SCK -----> NU32-D10
* Pot 3-SDI -----> D0
* Pot 3-SDI -----> NU32-D0
* Pot 4-VSS -----> GND
* Pot 4-VSS -----> GND
* Pot 5-P1B -----> P1+
* Pot 5-P1B -----> P1+
* Pot 6-P1W -----> P1-
* Pot 6-P1W -----> P1-
* Pot 7-P1A -----> NC
* Pot 9-P0W -----> P0-
* Pot 8-P0A -----> NC
* Pot 10-P0B -----> P0+
* Pot 9-P0W -----> P0-
* Pot 12 - SHDN -----> 3.3v (this pin is inverse-shutdown, so we keep it high)
* Pot 10-P0B -----> P0+
* Pot 13-SDO -----> NU32-C4
* Pot 11 -WP -----> NC
* Pot 12 - SHDN -----> 3.3v
* Pot 13-SDO -----> C4
* Pot 14-VDD -----> 3.3v
* Pot 14-VDD -----> 3.3v


Line 34: Line 35:


== Library Functions ==
== Library Functions ==
Code: [[Media:Nokia5110.h|Nokia5110.h]], [[Media:Nokia5110.c|Nokia5110.c]]
Code: [[Media:MCP42X1.h|MCP42X1.h]], [[Media:MCP42X1.c|MCP42X1.c]]


Full example: [[Media:NU32v2_Nokia5110_example.zip|All example code]]
Full example project: [[Media:MCP42X1_Example.zip|MCP42X1_Example.zip]]


MCP42X1.c contains the following functions:
Nokia5110.h sets up control pins on E0, E1 and E2. These may be changed if desired. The file also contains function prototypes, constants, and a 96 x 5 element lookup array of ASCII characters.


* void SPI_initPots() : sets up SPI channel 1, and CS pin
Nokia5110.c contains the following functions:
* void incPot(int pot_num) : will increment wiper by one step
* LcdCharacter - used to write an individual ASCII character
* void decPot(int pot_num) : will decrement wiper by one step
* LcdClear - clears the entire LCD
* int setPot(int pot_num, int wiper) : sets the given pot to specific wiper value (0 - 127), returns success boolean
* LcdInitialize - Initializes SPI3 (can be changed if desired) and sends the startup commands to the LCD
* int readPot(int pot_num) : reads the current wiper setting from the pot, returns (0 - 127)
** The second command sent (LCD Vop) sets the contrast of the LCD and can be altered for more or less contrast
* void testPots() : runs tests of setting, incrementing, decrementing, and reading both channels to confirm pots are working properly
* LcdString - writes a string of characters to the LCD
* LcdWrite - sends individual commands to the LCD
** Sending commands before the LCD is ready for them will make the LCD miss them, so 1000 Nop()s are called between commands
* gotoXY - places the cursor at an x pixel (0-84) and y character (0-5)
* setPixel - turns on a pixel at x (0-84) and y (0-48)


== Sample Code ==
== Sample Code ==


Initialize and clear the LCD:
Initialize and manipulate the pots:
<code><pre>
<code><pre>
LcdInitialize();
LcdClear();
SPI_initPots();
</pre></code>


//Setting pots to zero...
Go to the leftmost side of the screen on the second line and write 'Hello'
<code><pre>
gotoXY(0,1);
setPot(0,0);
LcdString("Hello");
setPot(1,0);
//both now at 0
</pre></code>


//Setting pots to mid-range (64), or 1/2 the total resistance
Write the value of a variable:
<code><pre>
setPot(0,64);
char str[5];
setPot(1,64);
sprintf(str, "i=%d", i);
// both at 64
LcdString(str);
</pre></code>


//Incrementing both
Turn on a pixel at (20,35):
incPot(0);
<code><pre>
setPixel(20,35,1);
incPot(1);
// (now at 65)
</pre></code>


//Incrementing both
== More Information ==
incPot(0);
The sample code in [[Media:NU32v2_Nokia5110_example.zip|All example code]] will create the image at the top of this page. Sending the NU32v2 an 'a' over serial will clear the LCD.
incPot(1);
//now at 66

//Decrementing both
decPot(0);
decPot(1);
//now 65


//Decrementing both
decPot(0);
decPot(1);
//now 64


</pre></code>

Latest revision as of 06:40, 16 January 2016

THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT THE NU32 PAGE.


The MCP42X1 is a 7-bit dual digital potentiometer. It is available in total resistance options of 5, 10, 50 and 100 kOhm. It is controlled using SPI communication and a digital output (CS). It has two independent potentiometer channels, each with its own wiper.


Overview

A potentiometer is useful in a variety of circumstances when a variable resistance is needed. A digital potentiometer provides a variable resistor that can be controlled from a microcontroller. Because the MCP42X1 is a 7-bit device, there are 2^7 = 128 possible resistance "steps". By communicating with the IC over SPI, we can choose what step (and thus, resistance) the potentiometer is set at.

MCP42X1 Pinout

The resistance across the PxB and PxW (the wiper) is governed by where the 5kOhm comes from the total resistance of the pot, depending on the model. The value N is the setting of the pot between 0 and 127.

The pot requires four communication pins from a microcontroller: SPI CLK, SDO, SDI, and one digital output. The pot is powered with 3.3V. This model is a dual pot chip, meaning it has two completely independent potentiometer channels in the same IC, referred to as Pot 0 and Pot 1, respectively.

Details

Datasheet

Example schematic when communicating with the NU32, using SPI channel 1:

  • Pot 1-CS -----> NU32-F3 (this can be any DIO pin you want, change code accordingly)
  • Pot 2-SCK -----> NU32-D10
  • Pot 3-SDI -----> NU32-D0
  • Pot 4-VSS -----> GND
  • Pot 5-P1B -----> P1+
  • Pot 6-P1W -----> P1-
  • Pot 9-P0W -----> P0-
  • Pot 10-P0B -----> P0+
  • Pot 12 - SHDN -----> 3.3v (this pin is inverse-shutdown, so we keep it high)
  • Pot 13-SDO -----> NU32-C4
  • Pot 14-VDD -----> 3.3v

The resistance across PXB and PXW is what changes, labeled above as Px+ and Px-.

Library Functions

Code: MCP42X1.h, MCP42X1.c

Full example project: MCP42X1_Example.zip

MCP42X1.c contains the following functions:

  • void SPI_initPots() : sets up SPI channel 1, and CS pin
  • void incPot(int pot_num) : will increment wiper by one step
  • void decPot(int pot_num) : will decrement wiper by one step
  • int setPot(int pot_num, int wiper) : sets the given pot to specific wiper value (0 - 127), returns success boolean
  • int readPot(int pot_num) : reads the current wiper setting from the pot, returns (0 - 127)
  • void testPots() : runs tests of setting, incrementing, decrementing, and reading both channels to confirm pots are working properly

Sample Code

Initialize and manipulate the pots:

 
    SPI_initPots();

    //Setting pots to zero...
     
     setPot(0,0);
     setPot(1,0);
   //both now at 0

    //Setting pots to mid-range (64), or 1/2 the total resistance
   
     setPot(0,64);
     setPot(1,64); 
   // both at 64

    //Incrementing both
    incPot(0);
    incPot(1);
   // (now at 65)

    //Incrementing both 
    incPot(0);
    incPot(1);
   //now at 66

    //Decrementing both
    decPot(0);
    decPot(1);
   //now 65 


    //Decrementing both 
    decPot(0);
    decPot(1);
   //now 64