' -----[ Title ]---------------------------------------------------------------- ' ' BS2p Plus Pack ' ' File...... PP_PCF8591.BSP ' Purpose... PCF8591 A2D/D2A Demo ' Author.... (c) Parallax Inc. -- All Rights Reserved ' E-mail.... support@parallax.com ' Started... 26 SEP 2001 ' Updated... 11 APR 2005 ' {$STAMP BS2px} ' {$PBASIC 2.5} ' -----[ Program Description ]-------------------------------------------------- ' ' This program demonstates the Philips PCF8591 4-channel A2D plus 1-channel ' D2A. Channel 0 input is tied to the output of the D2A pin. Channel 1 input ' is tied to the wiper of a pot. Channels 2 and 3 are tied to Vss. ' ' The PCF8591 uses a control byte after the Slave Address. The control byte ' data (see details in PCF8591 documentation) is used to enable the analog ' output bit and set the kind of analog inputs. In this demo, the analog output ' bit is enabled and four single-ended analog inputs are used. ' ' Note that the first byte transmitted in a read cycle contains the conversion ' 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 ]------------------------------------------------------ ' SDA PIN 0 ' SDA on 0; SCL on 1 ' -----[ Constants ]------------------------------------------------------------ ' DevType CON %1001 << 4 ' device type DevAddr CON %000 << 1 ' address = %000 -> %111 PCF8591 CON DevType | DevAddr 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, "PCF8591 Demo" ' -----[ Main Code ]------------------------------------------------------------ ' Main: DO DEBUG CRSRXY, 0, 2, "D2A Out........ ", DEC aOut, " ", CR ' write to D2A I2COUT SDA, PCF8591, [D2A_Enable + Auto_Inc, aOut] ' read A2D channels I2CIN SDA, PCF8591, [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 NEXT PAUSE 500 aOut = aOut + 1 ' increment analog output LOOP END ' -----[ Subroutines ]---------------------------------------------------------- '