I have scoured all of my resources and have come up with the following code but I seem to be missing something. Can anyone provide some insight?
' Set compiler to use HS (20 mHz)
@ DEVICE HS_OSC
DEFINE OSC 20 'resonator frequency at 20 Mhz
' Define the LCD Hookup parameters
' data pins D4-D7 are hooked to pins C.0-C.3, respectively
define LCD_DREG PORTC 'Data Port = PORTC
define LCD_DBIT 0 'Data starting bit = 0
define LCD_RSREG PORTC 'Register Select Port = PORTC
define LCD_RSBIT 4 'RS bit = PortC.4
define LCD_EREG PORTC 'Enable Port = PORTC
Define LCD_EBIT 5 'Enable Bit = PortC.5
define LCD_BITS 4 'LCD Bus Size = 4
define LCD_LINES 2 'Number of lines on LCD
define SHIFT_PAUSEUS 1000 '
Pause 1000 ' Wait for LCD to startup
Lcdout $FE, $80, "MAX1247"
lcdout $FE, $C0, "Example"
pause 1000
'Variables for ADC
CB var byte
CB0 var Byte
CB1 var Byte
CB2 var Byte
CB3 var Byte
ADResult var word ' contains the result of the read
X var byte ' counter
'Control Byte Setup:
' Bit 7: Start Bit = 1
' Bit 6: Channel Select Bit 2 (SEL2) -See Channel Selection Below
' Bit 5: SEL1
' Bit 4: SEL0
' Bit 3: UNI/BIP - 1=Unipolar 0=Bipolar
' Bit 2: SNGL/DIF - 1=Single Ended 0=Differential
' Bit 1: PD1 - Sets clock and power down mode
' Bit 0: PD0 - Sets clock and power down mode
'Channel Selection:
' SEL2 SEL1 SEL0
'CH0 0 0 1
'CH1 1 0 1
'CH2 0 1 0
'CH3 1 1 0
' To read channel 0 in Unipolar, single ended, external clock mode,
' Control Byte (CB) = 10011111
CB0 = %10011111 'To Read Channel 0
CB1 = %11011111 'To read channel 1
CB2 = %10101111 'To read Channel 2
CB3 = %11101111 'To read Channel 3
' Set up MAX1247 4-channel 12-bit ADC
CLKpin var PORTA.0 'SPI clock pin
CSpin var PORTA.1 'DAC chip select
DINpin var PORTA.2 'Data in pin
DOUTpin var PORTA.3 'Data Out Pin
'set up Max1247 pins
TRISA.0 = 0 'CLKpin as output
TRISA.1 = 0 'CSpin as output
TRISA.2 = 0 'DINpin as output
TRISA.3 = 1 'DOUTpin as input
'Take Reading
CB = CB0 'Read Channel 0
Loop:
CSpin = 1 'keep max1247 off until called
clkpin = 0 'keep clock low until ready
gosub ReadADC
Lcdout $FE, 1 ' Clear LCD screen
Lcdout $FE, $80, #ADresult
pause 330
goto loop
ReadADC:
'Send Control Byte
low CSpin 'cs going low starts the conversion process
shiftout doutpin, clkpin, 1, [cb\8]
high cspin
pause 10
'Read Result
adresult = 0
low cspin
shiftin dinpin, clkpin, 0, [adresult\12]
high cspin
return
end
Bookmarks