'18F2550 SPI dev1 17/10/10
Define CONFIG1L = 0x24
Define CONFIG1H = 0x0c
Define CONFIG2L = 0x38
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x83
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
AllDigital
Define CLOCK_FREQUENCY = 20
Define SIMULATION_WAITMS_VALUE = 1
'''''''''''''''''''''''''''''''''''''''
Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 6
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7 ''5
Define SPI_SDI_REG = PORTB
Define SPI_SDI_BIT = 0
Define SPI_SCK_REG = PORTB
Define SPI_SCK_BIT = 1
''''''''''''''''''''''''''''''''''''''''
Define LCD_LINES = 2
Define LCD_CHARS = 16
''
Define LCD_BITS = 4 'allowed are 4 and 8 - number of data interface lines
Define LCD_DREG = PORTB
Define LCD_DBIT = 4 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG = PORTC
Define LCD_RSBIT = 0
Define LCD_EREG = PORTC
Define LCD_EBIT = 1
Define LCD_RWREG = PORTC
Define LCD_RWBIT = 2
Dim ch0lo As Byte
Dim ch0hi As Byte
Dim temp0 As Word
Dim ch1lo As Byte
Dim ch1hi As Byte
Dim temp1 As Word
ADCON0 = 0x03
ADCON1 = 0x0e
TRISA = 0
TRISB = 0
TRISC = 0
Lcdinit
SPIPrepare
Lcdcmdout LcdClear
Lcdout "Enable read MCP"
'MCP3202 control strings
'CmdStr0 = Trim("00000001 10000000 00000000") ' chn0
'CmdStr1 = Trim("00000001 11000000 00000000") ' chn1
loop:
PORTC.6 = 1
'channel0
SPICSOn
PORTC.6 = 0
SPISendBits 2, %11
SPIReceive ch0hi
SPIReceive ch0lo 'this will be discarded
PORTC.6 = 1
PORTC.6 = 0
SPISend 0x01
SPISend 0x80
SPIReceive ch0lo
PORTC.6 = 1
SPICSOff
'channel1
SPICSOn
PORTC.6 = 0
SPISendBits 3, %111
SPIReceive ch1hi
SPIReceive ch1lo 'this will be discarded
PORTC.6 = 1
PORTC.6 = 0
SPISend 0x01
SPISend 0xc0
SPIReceive ch1lo
PORTC.6 = 1
SPICSOff
ch0hi = ShiftRight(ch0hi, 1)
ch0hi = ch0hi And 0x0f
temp0 = (ch0hi * 256) + ch0lo '0 thru 4095 counts
ch1hi = ShiftRight(ch1hi, 2)
ch1hi = ch1hi And 0x0f
temp1 = (ch1hi * 256) + ch1lo '0 thru 4095 counts
Lcdcmdout LcdLine1Home
Lcdout #temp0, " ", #ch0hi, " ", #ch0lo, " "
Lcdcmdout LcdLine2Home
Lcdout #temp1, " ", #ch1hi, " ", #ch1lo, " "
WaitMs 200
Goto loop