Parallax, Inc. • BS2p “Plus Pack” (#45184) • 10/2001
Page 27
' result code of the previous read cycle, so a dummy byte is placed ahead of
' the analog input array in the I2CIN command.
' -----[ Revision History ]-----------------------------------------------------
'
' -----[ I/O Definitions ] ------------------------------------------------------
'
I2Cpin CON 8 ' SDA on 8; SCL on 9
' -----[ Constants ]------------------------------------------------------------
'
DevType CON %1001 << 4 ' device type
DevAddr CON %000 << 1 ' address = %000 -> %111
Wr8591 CON DevType | DevAddr ' write to PCF8591
Rd8591 CON Wr8591 | 1 ' read from PCF8591
D2A_Enable CON %01000000 ' enable analog output
Auto_Inc CON %00000100 ' auto inc a2d channels
MVPB CON $139C ' millivolts per bit factor
' -----[ Variables ]------------------------------------------------------------
'
aOut VAR Byte ' analog out value
aIn VAR Byte(4) ' analog input channels
mVolts VAR Word ' convert to millivolts
dummy VAR mVolts.LowByte
chan VAR Nib ' channel
' -----[ EEPROM Data ]----------------------------------------------------------
'
' -----[ Initialization ]-------------------------------------------------------
'
Initialize:
DEBUG CLS ' call DEBUG window
PAUSE 250 ' let it open
' -----[ Main Code ]------------------------------------------------------------
'
Main:
DEBUG Home, "PCF8591 Demo"
Set_D2A:
DEBUG Home, CR, CR, "D2A Out........ ", DEC aOut, " ", CR
I2COUT I2Cpin, Wr8591, D2A_Enable, [aOut]
Get_A2D:
I2CIN I2Cpin, Rd8591, (D2A_Enable | Auto_Inc), [dummy, STR aIn\4]
FOR chan = 0 TO 3
DEBUG "Channel ", DEC1 chan, " In... ", DEC aIn(chan), " ", Tab
mVolts = aIn(chan) */ MVPB
DEBUG "(", DEC mVolts DIG 3, ".", DEC3 mVolts, " volts)", CR