Dear friends
In this below program i try to receive data for DS1307. during data write code execute correctly. but i case of receiving the 0xff in sspbuf.. plaese help me... i'm a new leaner..
#include<htc.h>
//#include "lcd4.h"
#pragma config OSC=HS,OSCS=OFF
#pragma config BORV=45,PWRT=ON,BOR=ON
#pragma config WDT=OFF
#pragma config DEBUG=ON,LVP=OFF,STVR=OFF
#define _XTAL_FREQ 8000000
#define _LEGACY_HEADERS
//#define PORTBIT(addr,bit)((unsigned)(&addr)*8+(bit))
//static bit scl @ PORTBIT(PORTC,3);
//static bit sda @ PORTBIT(PORTC,3);
#define ldata PORTD
#define devadd 0x68
#define memadd 0xD0
#define data0 50
#define data1 10
unsigned char REC_DATA[20];
unsigned char value,i;
void i2c_init();
void i2c_Bwrite();
void i2c_Bread();
void startcond();
void stopcond();
void restartcond();
void poll();
//void Tdispay();
void tx_byte(char);
void rx_byte();
void main()
{
while(1)
{
i2c_init();
i2c_Bwrite();
i2c_Bread();
//Tdisplay();
}
}
void i2c_init(){
TRISC=0Xff;
SSPSTAT=0;
SMP=1; //slew rate control disable
SSPADD=0X18; // for 8mhz and 100Khz bit rate
SSPCON1=0X28;
SSPCON2=0X00;
SSPIF=0;
BCLIF=0;
}
void i2c_Bwrite()
{
startcond();
//tx_byte(devadd);
//tx_byte(memadd);
tx_byte(0xd0);// device address
tx_byte(0x01);// sec
tx_byte(0x30);//min
tx_byte(0x09);//hours
tx_byte(0x02);date
tx_byte(0x07);
tx_byte(0x09);
tx_byte(0x09);
stopcond();
}
void i2c_Bread()
{
startcond();
tx_byte(0xD0);// device address
restartcond();//restart
tx_byte(0xD1);// address which location want to read
rx_byte();
//ACKDT=1;
tx_byte(0xD3);
rx_byte();
ACKDT=1;
for (i=0;i<4;i++)
{
rx_byte();
ACKDT=0;
}
stopcond();
}
void tx_byte(char tx_value)
{
SSPIF=0;
SSPBUF=tx_value;
while(SSPIF==0);
while(ACKSTAT==1);
return;
}
void rx_byte()
{
static unsigned char RXI;
SSPIF=0;
RCEN=1;
while(SSPIF==0);
value=SSPBUF;
REC_DATA[RXI++]=value;
SSPIF=0;
ACKEN=1;
return;
}
void startcond()
{
SSPIF=0;
SEN=1;
while(SSPIF==0);
return;
}
void restartcond()
{
SSPIF=0;
RSEN=1;
while(SSPIF==0);
return;
}
void stopcond()
{
SSPIF=0;
PEN=1;
while(SSPIF==0);
return;
}