View previous topic :: View next topic |
Author |
Message |
SveinR
Joined: 04 Aug 2005 Posts: 9
|
I2C i/o expander |
Posted: Thu Aug 19, 2010 6:18 am |
|
|
Hi,
I'm looking for an i/o expander that is controlled by i2c and has the option to invidually set the pins to in or out.
Is there any example files out there?
I'm in need of 8-10 i/o's
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Thu Aug 19, 2010 6:34 am |
|
|
2 questions :-
1. what has this got to do with the CCS C compiler
2. are you incapable of googling "I2C i/o expander"
try PCF8574 |
|
![](templates/subSilver/images/spacer.gif) |
SveinR
Joined: 04 Aug 2005 Posts: 9
|
|
Posted: Thu Aug 19, 2010 6:53 am |
|
|
MikeW wrote: | 2 questions :-
1. what has this got to do with the CCS C compiler
2. are you incapable of googling "I2C i/o expander"
try PCF8574 |
I need and example file showing how to setup this i/o expander,
and as I'm aware of there is no include file for the PCF8574 included in the CCS package that I got. |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
![](templates/subSilver/images/spacer.gif) |
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Thu Aug 19, 2010 7:11 am |
|
|
PCF8575 is a 16 i/o device
Code: |
#define PCF8575_device_type 0x40
// pcf8575 device numbers go from0 -number of devices
void Test_IO_Module()
{
int16 PCF8575_Data_Word;
Digital_Inputs_from_PCF8575=read_PCF8575(0); // get the digital input states
Display_Digital_Input_Values(~Digital_Inputs_from_PCF8575);
} // end of function
// functions
// this is a handy function for taking the i2c physical address
// and shifting the bits left so that the address can simply be
// OR'd with an i2c write command...
int i2c_addr_mask(int device_addr)
{
/* xxxxxIJK --> 00000IJK, then 00000IJK --> 0000IJK0 */
return((device_addr & 0x07)<<1);
} // end of function
#define I2CWRITE 0b00000000
#define I2CREAD 0b00000001
void write_PCF8575(BYTE device_address, WORD data)
{
BYTE Upper_Data_Byte;
BYTE Lower_Data_Byte;
BYTE addr_mask;
BYTE address;
addr_mask=i2c_addr_mask(device_address);
i2c_start();
Upper_Data_Byte=make8(data,1);
Lower_Data_Byte=make8(data,0);
i2c_start();
address=(PCF8575_device_type | addr_mask | I2CWRITE);
i2c_write(address);
i2c_write(lower_Data_Byte); // LSB 1 is input mode
i2c_write(~Upper_Data_Byte); // MSB 1 is OFF , inverted 'cos of the HC240
i2c_stop();
} // end of function
int16 read_PCF8575(BYTE device_address)
{
BYTE Upper_Data_Byte;
BYTE Lower_Data_Byte;
BYTE addr_mask;
BYTE address;
addr_mask=i2c_addr_mask(device_address);
i2c_start();
address=(PCF8575_device_type | addr_mask | I2CREAD);
i2c_write(address);
Lower_Data_Byte=i2c_read(); // LSB get the byte
Upper_Data_Byte=i2c_read(0); // MSB get the byte
i2c_stop();
return (make16(Upper_Data_Byte,Lower_Data_Byte));
} // end of function
|
|
|
![](templates/subSilver/images/spacer.gif) |
SveinR
Joined: 04 Aug 2005 Posts: 9
|
|
Posted: Thu Aug 19, 2010 7:24 am |
|
|
Thanks all ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|