I'm working on a project using the Microchip MCP4261 Digital Pots. Cute little parts...
Here's a code snippet and a schematic as example. The code is an example of reading a "real" pot via A/D and updating a "digital pot" with the value. I'm using the LAB-X2 board from meLabs for testing. The code uses the on-board POT1 for one channel (RA0) and a second off-board pot for channel two (RA1). Put a voltmeter on the wiper of the digital pot to see the values update. There are a few other features of the digital pot that can be used simply by changing the "address" value. There's code in there for writing the A/D value to a serial LCD display for testing - ignore it. Simple but maybe useful to someone...
Have fun.
MT
MCP4261.pdf
Code:'****************************************************************************** '* Name : MCP4261POT_000.bas * '* Compiler: PICBASIC PRO Compiler microEngineering Labs * '* Author : Mike Tripoli * '* Notes: * '* Read an analog voltage on RA.0 and and RA.1 * '* copy the 8 bit value to a digital pot value register * '* Display value on serial LCD display * '* 8-bit conversion yields 0-255 result for 0-5 volt input. * '* Uses meLabs LAB-X2 demo board * '* * '* * '* Date : 03/03/2011 * '****************************************************************************** 'Setup some defaults for programming the device. 'High-speed OSC, Watch dog timer-OFF, Brown-out detect OFF, Low Voltage Program OFF, Protect code OFF @ DEVICE HS_OSC,WDT_OFF,BOD_OFF,LVP_OFF,PROTECT_OFF include "modedefs.bas" 'Shiftout mode definitions ' Define ADCIN parameters Define ADC_BITS 8 ' Set number of bits in result Define ADC_CLOCK 3 ' Set clock source (3=rc) Define ADC_SAMPLEUS 50 ' Set sampling time in uS ADC_VAL_0 Var byte ' Only using 8 bits of ADC Channel 0 ADC_VAL_1 Var byte ' Only using 8 bits of ADC Channel 1 POT0 var word ' WORD variable to for address (high byte) and pot value (low byte) POT0 POT1 var word ' WORD variable to for address (high byte) and pot value (low byte) POT1 LED1 Var PORTB.0 ' Alias PORTB.0 to LED1 on LAB-X2 board from meLabs LED2 VAR PORTB.1 ' Alias PORTB.1 to LED2 LED3 VAR PORTB.2 ' Alias PORTB.2 to LED3 SCK var PORTC.3 ' Alias PORTC.3 to SCK (serial data clock) SDO var PORTC.5 ' Alias PORTC.5 to SDO (serial data out) CS var PORTC.2 ' Alias PORTC.2 to C_EN (chip enable) LCD_DISP var PORTB.3 ' LCD connected to PORTB.3 - LAB-X2 TRISA = %11111111 ' Set PORTA to all input TRISB = %00000000 ' Set PORTB to all output TRISC = %00000000 ' Set PORTC to all output ADCON1 = %00000010 ' Set PORTA analog 'Initialize registers and variables LOW LED1 'Initialize LEDs OFF LOW LED2 ' " LOW LED3 ' " HIGH CS 'Chip enable high to start high sck 'Mode 1,1 SCK idles high ; Serout2 LCD_DISP,396,[12] ' Clear the LCD display ;Initialize POT WORD pot0.Highbyte = %00000000 'POT0 address in hex - easier for me to see which pot selected pot0.lowbyte = 0 'POT0 value in DEC - initialize pot - can be any value pot1.Highbyte = %00010000 'POT1 address in hex pot1.lowbyte = 0 'POT1 value in DEC mainloop: ADCIN 0, adc_val_0 ' Read channel RA.0 to ADC_VAL_0 - Uses onboard "POT1" on LAB - X2 pot0.lowbyte = adc_val_0 'copy input voltage from pot to digital pot pause 10 ADCIN 1, adc_val_1 ' Read channel RA.1 to ADC_VAL_1 pot1.lowbyte = adc_val_1 'copy input voltage from pot to digital pot ;***** DELETE THIS SECTION - ONLY FOR TESTING *************** ; Serout2 PORTB.3,396,[12] 'Set to beginning of LCD display ; Serout2 PORTB.3,396,["ADC_VAL_0=",DEC3 adc_val_0,148] ' Display adc channel 0 ; Serout2 PORTB.3,396,["ADC_VAL_1=",DEC3 adc_val_1,13] ' Display adc channel 1 ; PAUSE 10 ;************************************************************ GOSUB write_pots goto mainloop WRITE_POTS: ; Routine writes value to pots WR_POT_0: low CS ' Make chip active pause 1 ' Delay after making chip active per data sheet - only need 60nS shiftout sdo,sck,5,[pot0\16] ' Mode 1,1 in MCP4261 DS / Mode 5 PBP - clock idles high, MSB shift out first high CS ' MCP4261 DS recommends toggling /CS between commands pause 1 ' Delay keeps from banging CS pin WR_POT_1: low CS 'Make chip active pause 1 'Delay after making chip active shiftout sdo,sck,5,[pot1\16] high CS RETURN END 'ALL PROGRAMS NEED AN END STATEMENT!
Hi, we are pleased to see that you are using our forum.
We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!
Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.


Reply With Quote


Bookmarks