#include <pic.h>
#include "usart.h"
#include<stdio.h>
#include<stdlib.h>
#define SDA RC3
#define SCL RC4
//declare function
void delay(void);
void SCL_high(void);
void SCL_low(void);
void I2C_Start(void);
void I2C_Stop(void);
bit I2C_Write(unsigned char dat);
unsigned char I2C_Read(char ack);
//RTC DS1307
void rtc_write(unsigned char add , unsigned char dataa);
unsigned char rtc_read(unsigned char add);
unsigned char hour, minute, second,ss,mm,hh,yy,ff;
unsigned char dataa ;
bit inbit,outbit;
unsigned char buf[2];
void main(void)
{
//TRISC3=0;
TRISC4=0;
PORTC=0x00;
init_comms();
rtc_write(0x07, 10); // set control register 00010000; 1Hz
ss=50;
mm=0x26;
hh=0x01;
rtc_write(0x00, ss);
rtc_write(0x01, mm);
rtc_write(0x02, hh);
hour=0;
minute=0;
second=0;
while(1)
{
second = rtc_read(0x00)|0x00;
minute = rtc_read(0x01)|0x01;
hour = rtc_read(0x02);
}
}
//===============delay=======================
void delay(void)
{
unsigned char i;
for (i = 0; i < 20; i++)
{
;
}
}
//---------------SCL high--------------------------
void SCL_high(void)
{
SCL = 1;
delay();
}
//---------------SCL low---------------------------
void SCL_low(void)
{
SCL = 0;
delay();
}
//-----------------START Condition-----------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
SDA = 0;
delay();
SCL = 0;
SDA = 1;
}
//------------------STOP Condition--------------------------
void I2C_Stop(void)
{
SDA = 0;
SCL_high();
SDA = 1;
}
//-------------------I2C Write---------------------------
bit I2C_Write(unsigned char dat)
{
unsigned char i;
//static bit outbit;
for (i = 1; i <= 8; i++)
{
outbit=dat&0x80;
SDA = outbit;
dat = dat << 1;
SCL_high();
SCL_low();
}
SDA = 1; // set SDA to receive Acknowledge
SCL_high();
outbit = SDA; // check busy or not
SCL_low();
return(outbit); // if outbit=1 ,A=1: error // if outbit=0 ,A=0: ok
}
//------------------I2C Read----------------------------------
unsigned char I2C_Read(char ack)
{
unsigned char i, dat;
//static bit inbit;
//static bit ack=0;
dat = 0;
for(i=1;i<=8;i++)
{
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack)
{
SDA = 0; // set SDA = 0 (ACK)
}
else
{
SDA = 1; // set Non ACK
}
SCL_high();
SCL = 0;
SDA = 1; // Set SDA = 1 for next read
delay();
return(dat);
}
//-------------------------------------------------------------
//Rtc write function
//---------------------RTC wtie----------------------------------------
void rtc_write(unsigned char add , unsigned char data)
{
TRISC3=1;
TRISC4=0;
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
//I2C_Write(data);
I2C_Write((((data/10)<<4)|(data%10)));
I2C_Stop();
}
//---------------------------------------------------------------
// rtc read
//---------------------------------------------------------------
unsigned char rtc_read(unsigned char add)
{
TRISC3=0;
TRISC4=0;
//unsigned char dataa ;
I2C_Start();
I2C_Write(0xd0); //for write
I2C_Write(add);
I2C_Start();
I2C_Write(0xd1); //for reading
dataa = I2C_Read(0);
I2C_Stop();
dataa = ((dataa & 0x0f) + ((dataa<<4)*10));
return (dataa);
}
I am not getting required please help. where i had gone wrong.
#include "usart.h"
#include<stdio.h>
#include<stdlib.h>
#define SDA RC3
#define SCL RC4
//declare function
void delay(void);
void SCL_high(void);
void SCL_low(void);
void I2C_Start(void);
void I2C_Stop(void);
bit I2C_Write(unsigned char dat);
unsigned char I2C_Read(char ack);
//RTC DS1307
void rtc_write(unsigned char add , unsigned char dataa);
unsigned char rtc_read(unsigned char add);
unsigned char hour, minute, second,ss,mm,hh,yy,ff;
unsigned char dataa ;
bit inbit,outbit;
unsigned char buf[2];
void main(void)
{
//TRISC3=0;
TRISC4=0;
PORTC=0x00;
init_comms();
rtc_write(0x07, 10); // set control register 00010000; 1Hz
ss=50;
mm=0x26;
hh=0x01;
rtc_write(0x00, ss);
rtc_write(0x01, mm);
rtc_write(0x02, hh);
hour=0;
minute=0;
second=0;
while(1)
{
second = rtc_read(0x00)|0x00;
minute = rtc_read(0x01)|0x01;
hour = rtc_read(0x02);
}
}
//===============delay=======================
void delay(void)
{
unsigned char i;
for (i = 0; i < 20; i++)
{
;
}
}
//---------------SCL high--------------------------
void SCL_high(void)
{
SCL = 1;
delay();
}
//---------------SCL low---------------------------
void SCL_low(void)
{
SCL = 0;
delay();
}
//-----------------START Condition-----------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
SDA = 0;
delay();
SCL = 0;
SDA = 1;
}
//------------------STOP Condition--------------------------
void I2C_Stop(void)
{
SDA = 0;
SCL_high();
SDA = 1;
}
//-------------------I2C Write---------------------------
bit I2C_Write(unsigned char dat)
{
unsigned char i;
//static bit outbit;
for (i = 1; i <= 8; i++)
{
outbit=dat&0x80;
SDA = outbit;
dat = dat << 1;
SCL_high();
SCL_low();
}
SDA = 1; // set SDA to receive Acknowledge
SCL_high();
outbit = SDA; // check busy or not
SCL_low();
return(outbit); // if outbit=1 ,A=1: error // if outbit=0 ,A=0: ok
}
//------------------I2C Read----------------------------------
unsigned char I2C_Read(char ack)
{
unsigned char i, dat;
//static bit inbit;
//static bit ack=0;
dat = 0;
for(i=1;i<=8;i++)
{
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack)
{
SDA = 0; // set SDA = 0 (ACK)
}
else
{
SDA = 1; // set Non ACK
}
SCL_high();
SCL = 0;
SDA = 1; // Set SDA = 1 for next read
delay();
return(dat);
}
//-------------------------------------------------------------
//Rtc write function
//---------------------RTC wtie----------------------------------------
void rtc_write(unsigned char add , unsigned char data)
{
TRISC3=1;
TRISC4=0;
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
//I2C_Write(data);
I2C_Write((((data/10)<<4)|(data%10)));
I2C_Stop();
}
//---------------------------------------------------------------
// rtc read
//---------------------------------------------------------------
unsigned char rtc_read(unsigned char add)
{
TRISC3=0;
TRISC4=0;
//unsigned char dataa ;
I2C_Start();
I2C_Write(0xd0); //for write
I2C_Write(add);
I2C_Start();
I2C_Write(0xd1); //for reading
dataa = I2C_Read(0);
I2C_Stop();
dataa = ((dataa & 0x0f) + ((dataa<<4)*10));
return (dataa);
}
I am not getting required please help. where i had gone wrong.