View previous topic :: View next topic |
Author |
Message |
chirag
Joined: 05 May 2004 Posts: 2
|
need code to interface PCF8574 with PIC16F877 |
Posted: Wed May 05, 2004 7:37 am |
|
|
Hi,
I am interfacing PCF8574 with PIC16F877 using I2C protocol.
But I am facing one problem. Though I am giving wrong address of PCF8574, it is giving acknowledgement. I think there is some problem with software itself. Can some one provide the code? it would be great help.
chirag |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
PCF8574 |
Posted: Wed May 05, 2004 8:48 am |
|
|
Obviously you didn''t search this board for the PCF8574. I did and got a bunch of stuff including code. There is one particularly long thead with a lot of good info!
I would recommend you try searching the board it before you post asking for help... That can save you a lot of time and and us repetitive questions. |
|
|
chirag
Joined: 05 May 2004 Posts: 2
|
problem with PIC16F877 while interfacing with PCF8574 |
Posted: Wed May 05, 2004 11:45 pm |
|
|
Hi, the problem is slightly different and I couldn't get any from the earlier posts. Let me describe it with code.
I am making use of PIC16F877 as a master device interfaced with PHILIPS PCF8574 as a slave device under i2c master mode with 100kbps and 12MHz crystal. Now, my problem is that my slave gives me acknowledgement even i give wrong slave address in sspstat register in first byte. I have successfully implemented the i2c operation on 24c01 eeprom device as a slave with pic16f877 and i got data from the desired location also.
But i am getting failure on pcf8574 device which comes in picture for my real application. Is there anybody to help me out to solve the problem?
/*this program tries to make the LED connected on port P0 of slave pcf8574 on.
ie. the write operation to pcf by master pic16f877*/
#include <pic.h>
#include "c:\ht-pic\samples\delay\delay.h"
#include "c:\ht-pic\samples\delay\delay.c"
#define FOSC 12000 //crystal frequency of 12MHz
#define BAUD 100 //standard baud rate at 100kbps
static bit SMP @ (unsigned) &SSPSTAT*8+7;
static bit CKE @ (unsigned) &SSPSTAT*8+6;
static bit DOA @ (unsigned) &SSPSTAT*8+5; //data/address
static bit P @ (unsigned) &SSPSTAT*8+4;
static bit S @ (unsigned) &SSPSTAT*8+3;
static bit RW @ (unsigned) &SSPSTAT*8+2; //read/write mode
static bit BF @ (unsigned) &SSPSTAT*8+0;
static volatile bit SCL @ (unsigned)&PORTC*8+3;
static volatile bit SDA @ (unsigned)&PORTC*8+4;
static volatile bit LED @ (unsigned)&PORTD*8+3;
void Init_pic(void);
void Init_I2C(void);
void TxI2c(void);
//void RecI2c(void);
void i2c_fail(void);
void i2c_wait(void);
void main()
{
Init_pic();
DelayMs(250);
Init_I2C();
while(1)
{
TxI2c();
}
}
void TxI2c()
{
SEN = 1; //start enable
while(SEN)
{
#asm
nop;
#endasm
}
SSPBUF = 0X40 //slave address having 000 address
i2c_wait();
if(ACKSTAT)
i2c_fail();
else{
LED=1; //to check the acknowledment of slave address
SSPBUF=0XFE; //to turn the led ON on port P0
i2c_wait();
}
PEN=1;
i2c_wait();
}
void i2c_fail()
{
PEN=1;
i2c_wait();
}
void i2c_wait()
{
while(!SSPIF);
SSPIF=0;
}
void Init_pic()
{
ADCON1=0X86; //AD0 as analog channel and other digital
TRISA=0XCF; //i/o ports are initialized
TRISB=0XBD;
TRISD=0X00;
TRISE=0X00;
PORTE=0X00;
PORTA=0XCF;
PORTB=0X00;
TRISC=0X18; //RC3 & RC4 BEING SCL AND SDA before use of any i2c mode
PORTC=0X07; //to make three segments OFF
STATUS=0X18;
OPTION=0X87; //internal pull ups are enabled and 256 prescaler
INTCON=0XC0; //GIE=1 & PEIE=1
LED=0;
}
void Init_I2C(void)
{
SSPCON = 0X28; // Set I2C to Master Mode and SSPEN=1
SSPADD = (FOSC/(4*BAUD))-1; // To set Baud rate to 100 kHz based on formula Baud Rate=FOSC/(4*(SSPADD + 1))
SSPSTAT = 0X80; //SMP=1 for standard mode
}
Here is my code. Please refer it and suggest me. I have configured my hardware with 2.2k resistors on both buses.
regards,
Chirag |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Thu May 06, 2004 12:28 am |
|
|
The CCS functions make it easy - no need to test register bits.
Code: |
#include <16F877.h>
#use delay(clock=12000000)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)
#fuses HS,NOWDT,PUT,NOLVP
#define PCF8574_WRITE_ADDRESS 0x40
void main(void)
{
i2c_start();
i2c_write(PCF8574_WRITE_ADDRESS);
i2c_write(0xfe); // Turn on led on bit 0, sinking current
i2c_stop();
while(1);
} |
|
|
|
|