Hi all,

few days ago I needed digital potentiometer for one application. A quick look at available pots has returned Microchip's MCP41HV31 Digital Pot. This pot is controlled via SPI, which is nice, because Bascom-AVR can handle SPI with ease. If you're interested how, Read more if interested...

Digital potentiometer is very handy. If you search the web you will see that there are many types available. Volatile/non volatile, different interface, different No. of steps etc. For my application 127 steps way minimum. I would prefer 255 steps but at the time I needed it there was no stock of it, so I took 127 steps pot MCP41HV31 by Microchip.

Data sheet for the MCP41HV31 can be found on this link. Interface to control it is SPI, which is nice, because Bascom-AVR can handle it with ease. 

Pot has very small package so I had to make a sort of adapter for it to connect it to MegaPin (or MiniPin equally). Schematic diagram of adapter can be seen below:

 

MCP41HV31 has 3 SPI lines: SCK, SDI&SDO, also you will need CS, WLAT and SHDN are not needed in simple application (BTW: on the image above there is Farnell code for 7 or 8 bit pot).

I have added to MegaPin 2x16 LCD module, while buttons are already on the MegaPin, I just had to connect them with a flat cable.

Bascom-AVR program is simple, we have to declare SPI, LCD, buttons variables etc. on the start, then we can read the resistance of the potentiometer. Or shall I say, we have to read resistance of wiper to the GND. Why do we need to do that? Well the MCP41HV31 is volative pot, which means that it remembers last set position of wiper. If we want to read correct resistance from the start of the program, we have to read data from the pot.

Then we enter into Do-Loop, where we set resistance by means of two buttons: Up/Down. In datasheet it is described that MCP41HV31 can be controlled with 8 or 16 bits. 8 bit controll has options Increment/Decrement & Reading/Writing, which we are using. See image below.

 

Image was taken from MCP41HV31 data sheet.

From the image above you can see configuration of our variables for commands:

Incr_comm = &B00000111                                      'increment command
Decr_comm = &B00001011                                      'decrement command
Read_comm = &B00001111                                      'read command
Write_comm = &B00000011                                     'write command

So basically what we have to do is to set Cs = 0, send via SPI command Read_comm, after that via SPI read resistance and set Cs = 1. When incrementing resistance we have to do the following:

Up_sub:
While Enk1 = 0
   Cs = 0                                                   'active
   Waitms 100
   Incr resistance
   Spiout Incr_comm , 1
   Spiout Read_comm , 1                                     'command to read set resistance

   Spiin Resistance , 1                                     'read set resistance
   Gosub Calculate
   Wlat = 0                                                 'set to write to SPI register
   Cs = 1                                                   'not active
   Locate 1 , 1
   Lcd "Resist.= " ; Wiper ; "   "

Wend
Return

 

There is a complete program in our Download section that you can download for free. Within this program fully functional are Up/Down procedures which increase/decrease resistance of the pot. The Write procedure is there only for a test to demonstrate that we can also write resistance value into pot.