Schematic

' MCP3202_2.Bas
'
' Illustrates an interface with the Microchip MCP3202 Dual 12-bit A/D.
'
' Illustrates use of the hardware SPI which is provided on the X parts.
'
' As reading from the A/D involves both outputting and at the same time
' receiving a byte, the SSPBUF SFR FC9 is read using the peeksfr command.
'
' copyright, Peter H Anderson, Baltimore, MD, Sept 15, '11

#picaxe 20x2
#Terminal 9600
#No_Table
#No_Data
#freq m4

Symbol ADVal = W0

Symbol Channel = B2
Symbol X = B3
Symbol Config = B4

Symbol SCK = B.7
Symbol CS = B.0

   HSPISetup spimode01, spimedium
   Low SCK
   High CS
   Pause 500
   
   SerTxD ("Hello")

   Do
   
      For Channel = 0 to 1
         GoSub ADMeas
         SerTxD (#Channel, "  ", #ADVal, CR, LF)
      Next
      Pause 1000
      
   Loop

        
ADMeas:
   
   
   Low CS
   
   If Channel = 0 Then
      Config = $a0
   Else
      Config = $e0
   End If
    
   HSPIOut ($01, Config)
   PeekSFR $c9, X  ' now read SSPBUF
  
   ADVal = X
   
   HSPIOut ($00)
   PeekSFR $c9, X  ' read SSPBUF

   ADVal = ADVal * 256 + X 
   
   High CS      
   
   Return