scanan
Joined: 13 Aug 2004 Posts: 58 Location: Turkey
|
PCF8574 |
Posted: Wed Apr 09, 2008 9:22 am |
|
|
Here a code which run perfectly
I used 8 pcf8574 32 input and 32 output
Code: | #include <18F452.h>
#device adc=10
//#FUSES XT, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV45, NOPUT, STVREN, NODEBUG,NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#FUSES H4, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV45, NOPUT, STVREN, NODEBUG,NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
//#FUSES XT,NOPROTECT, BROWNOUT, NOPUT, NODEBUG,NOLVP, NOWRT, NOCPD
//#use delay(clock=10000000)
#use delay(clock=40M, oscillator=10M)
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,PARITY=N, bITS=8,STOP=1)
//use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)
#define ON 1
#define OFF 0
#define PCF8574_BASE_ADDRESS 0x40;
#define PCF8574_OUT0_WRITE_ADDRESS 0x48
#define PCF8574_OUT1_WRITE_ADDRESS 0x4A
#define PCF8574_OUT2_WRITE_ADDRESS 0x4C
#define PCF8574_OUT3_WRITE_ADDRESS 0x4E
#define PCF8574_OUT0_READ_ADDRESS 0x49
#define PCF8574_OUT1_READ_ADDRESS 0x4B
#define PCF8574_OUT2_READ_ADDRESS 0x4D
#define PCF8574_OUT3_READ_ADDRESS 0x4F
#define PCF8574_IN0_READ_ADDRESS 0x41
#define PCF8574_IN1_READ_ADDRESS 0x43
#define PCF8574_IN2_READ_ADDRESS 0x45
#define PCF8574_IN3_READ_ADDRESS 0x47
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
void PCF8574_SendData(char writeaddress,char data)
{
//char writeadress,data;
i2c_start();
i2c_write(writeaddress);
i2c_write(data);
i2c_stop();
}
unsigned char PCF8574_GetData(char readaddress)
{
char data;
i2c_start();
i2c_write(readaddress);
data=i2c_read(0);
i2c_stop();
return data;
}
void main()
{
char data,i,addr;
do{
for(i=0;i<4;i++)
{
addr=0x40+2*i+1;
data=PCF8574_GetData(addr);
PCF8574_SendData(PCF8574_OUT0_WRITE_ADDRESS,0);
printf("INPUT NUMBER %d DATA %u\r",i,data);
}
}while(1);
}
|
|
|