' MCP42XX_1.Bas
'
' Illustrates an interface with a Microchip MCP42010 Dual 10K Digital Potentiometer.
'
' Uses a bit bang implementation of the SPI interface.  Each bit of a byte is output,
' beginning with the most significant bit on the MOSI output, SCK is brought high, MISO
' is read and SCK is brought low.  The byte is then shifted to the left such that the next bit
' is now in the most significant bit position.  This is repeated for each of the eight bits.
'
' Each command requires a two byte transfer.
'
' The first byte output is a command byte of the form;
'
'
'	X X C1 C0 X X P1 P0
'
' where C1 and C0 are the command bits and P1 and P0 identify the potentiometer.
'
' Valid vales of CC are 01 (write data) and 10 (shut down).
' Valid values of PP are 01 (pot 0), 10 (pot 1) and 11 (both pots)
'
' The second byte is the data to be written.
'
' Routine sets Pot 0 to half scale and Pot 1 to quarter scale.  Voltages are then measured on
' AN0 and AN1 and displayed followed by a multi second delay.
'
' Both pots are then set to 3/4 scale.  Voltages are measured and displayed (Point 2) followed by
' a delay.
'
' Pot 0 is then shut down.  Voltages are measured and displayed (Point 3) followed by a delay.
'
' Note that MOSI, SCK (and unused terminal MISO) may be multipled to other SPI devices such as the 
' TI TLC2543 A/D and Microchip MCP3205.  The active device is selected by bringing the CS associated with 
' the device low.
'
'
' PICAXE-18X				MCP42010
'
'
'  				 SO (term 13)  -------> To SI of Next MCP42XXX
'  MOSI, Pin2 (term 8) --------->SI (term 3)
'  SCK, Pin1 (term 7) ---------->SCK (term 2) --------> To SCK of next MCP42XXX
'  CS, Pin0 (term 6) ----------->/CS (term 1) --------> TO /CS of next MCP42XXX
'
'                       +5VDC -- Vdd (term 14)
'                       GRD ---- Vss (term 4)
'
'             +5VDC ---- 10K --- /RS (term 11)
'             +5VDC ---- 10K --- /SHDN (term 12)
'
'                                                        For Testing
'                                 PA0 (term 8) --------  +5 VDC
'                                 PW0 (term 9) --------  PICAXE AN0 (term 17)
'                                 PB0 (term 10) -------  GRD
'
'                                 PA1 (term 7) --------  +5 VDC
'                                 PW1 (term 6) --------  PICAXE AN1 (term 18)
'                                 PB1 (term 5) --------  GRD
'
'
' copyright, Peter H Anderson, Elmore, VT, Aug, '04

' I/O pins defined

   Symbol CS = Output0
   Symbol SCK = Output1
   Symbol MOSI = Output2
   Symbol MISO = Input0		' Not used for MCP42010
   
   Symbol PotSetting0 = B0
   Symbol PotSetting1 = B1
   
   Symbol PotNum = B2
   Symbol X = B3

   Symbol ADCReading = W2
   
   ' used in SPI_IO
   Symbol N = B6
   
   Pause 3000

Top:   
   PotNum = 0			' Point 1 - Pots set to midscale and quarter scale
   PotSetting0 = 128
   GoSub MCP42WritePot
   
   PotNum = 1
   PotSetting1 = 64
   GoSub MCP42WritePot

   ReadADC10 0, ADCReading
   SerTxD (#ADCReading, "  ")

   ReadADC10 1, ADCReading
   SerTxD (#ADCReading, 13, 10)
   
   For N = 1 to 2 ' multi sec pause to verify wiper voltages
      Pause 1000
   Next
   
   PotNum = 0			' Point 2 - Both pots set 3/4 scale
   PotSetting0 = 192
   GoSub MCP42WritePot
   
   PotNum = 1
   PotSetting1 = 192
   GoSub MCP42WritePot

   ReadADC10 0, ADCReading
   SerTxD (#ADCReading, "  ")

   ReadADC10 1, ADCReading
   SerTxD (#ADCReading, 13, 10)
   
   For N = 1 to 2 ' multi sec pause to verify wiper voltages
      Pause 1000
   Next


   PotNum = 0			' Point 3 - Pot 0 shut down
   Gosub MCP42ShutDown  

   ReadADC10 0, ADCReading
   SerTxD (#ADCReading, "  ")

   ReadADC10 1, ADCReading
   SerTxD (#ADCReading, 13, 10)

    
   For N = 1 to 2	' multi sec pause to verify wiper voltages
      Pause 1000
   Next
   
   Goto Top
     
   
MCP42WritePot:
   High CS
   Low SCK
   Low CS
   Branch PotNum, (WritePot0, WritePot1, WritePotBoth)
   
WritePot0:
   X = %00010001 	' C1 = 0  C0 = 1
   GoSub SPI_IO
   X = PotSetting0
   GoSub SPI_IO
   High CS
   Goto MCP42WritePotDone
   
WritePot1:
   X = %00010010
   GoSub SPI_IO
   X = PotSetting1
   GoSub SPI_IO
   High CS
   Goto MCP42WritePotDone   
   
WritePotBoth:
   X = %00010011
   GoSub SPI_IO
   X = PotSetting0
   GoSub SPI_IO
   X = PotSetting1
   GoSub SPI_IO
   High CS
   Goto MCP42WritePotDone   

MCP42WritePotDone:
   Return

MCP42ShutDown:
   High CS
   Low SCK
   Low CS
   Branch PotNum, (ShutDownPot0, ShutDownPot1, ShutDownPotBoth)
   
ShutDownPot0:
   X = %00100001	' C1 = 1, C0 = 0
   GoSub SPI_IO
   X = 0	
   GoSub SPI_IO
   High CS
   Goto MCP42ShutDownDone
   
ShutDownPot1:
   X = %00100010
   GoSub SPI_IO
   X = 0
   GoSub SPI_IO
   High CS
   Goto MCP42ShutDownDone      
   
ShutDownPotBoth:
   X = %00100011
   GoSub SPI_IO
   X = 0
   GoSub SPI_IO
   X = 0
   GoSub SPI_IO   
   High CS
   Goto MCP42ShutDownDone        

MCP42ShutDownDone:
   Return


SPI_IO:

   For N = 1 to 8
      If X > 127 Then SPI_IO_1	' if most sig bit is a one
      Low MOSI 			' otherwise set MOSI to a zero
      Goto SPI_IO_2

SPI_IO_1:
      High MOSI
SPI_IO_2:
      High SCK 				' clk high
      X = X * 2 + MISO			' read MISO (not used for MCP42010)
      Low SCK 				' clk low
   Next

   Return