digital clock ds1307 using pic

Status
Not open for further replies.

laxmid

New Member
#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.
 
i2c.h
#include<pic.h>
#define SDA RC4 /* Set P2.7 = SDA */
#define SCL RC3 /* Set P2.6 = SCL */
#define I2C_DELAY 0x0F /* For delay i2c bus */

void I2C_delay(void)
{
unsigned char i;

for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
I2C_delay();

SCL = 1; /* Start clock */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */

I2C_delay();

SDA = 0; /* Clear SDA */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 0; /* Clear SDA */
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

SDA = 1; /* Set SDA */
}

bit I2C_write(unsigned char dat)
{
static bit data_bit;
unsigned char i;

for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */

I2C_clock(); /* Call for send data to i2c bus */

dat = dat<<1;
}

SDA = 1; /* Set SDA */

I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */

I2C_delay();

return data_bit; /* If send_bit = 0 i2c is valid */
}

unsigned char I2C_read(void)
{
static bit rd_bit;
unsigned char i, dat;

dat = 0x00;

for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */

SCL = 0; /* Clear SCL */
}

return dat;
}

void I2C_ack()
{
SDA = 0; /* Clear SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SDA = 1; /* Set SDA */
}

void I2C_noack()
{
SDA = 1; /* Set SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SCL = 1; /* Set SCL */
}
ds1307
/*
* Filename : ds1307.h
* Hardware : Controller -> P89V51RD2
* XTAL -> 18.432 MHz
* Mode -> 6 Clock/MC
* I/O : SDA -> P2.7
* SCL -> P2.6
* Compiler : SDCC
* Author : sci-3d@hotmail.com
* Date : 23/08/06
*/

#include "i2c.h" /* Need i2c bus */
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02


unsigned char DS1307_get(unsigned char addr)
{
unsigned char ret;
TRISC3=0;
TRISC4=0;
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID+1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop();
return (ret&addr); /* Stop i2c bus */
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
TRISC3=0;
TRISC4=1;
I2C_start();

I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0x00); /* Request RAM address at 00H */
I2C_write(ss); /* Write sec on RAM address 00H */
I2C_write(mm); /* Write min on RAM address 01H */
I2C_write(hh); /* Write hour on RAM address 02H */
I2C_stop(); /* Stop i2c bus */
}


void DS1307_inti(unsigned char rs, unsigned char sqwe, unsigned char out)
{ rs&=3;
if (sqwe) rs|=0x10;
if (out) rs|=0x80;
I2C_start();
I2C_write(0x07);
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(rs); /* Request RAM address at 00H */
I2C_stop(); /* Stop i2c bus */
}
main.c
#include <pic.h>
#include "ds1307.h"
#include "usart.h"

void send2uart(unsigned char value);

unsigned char ss, mm, hh;

/***************************** Main function *************************************/
void main(void)
{


init_comms();

DS1307_inti(0,0,0);

DS1307_settime(0x05,0x27,0x10); /* Set Time (hh:mm:ss) */


while(1)
{
/* Get Time */
ss = DS1307_get(SEC);
mm = DS1307_get(MIN);
hh = DS1307_get(HOUR);

send2uart(ss);
putch(':');

send2uart(mm);
putch(':');

send2uart(hh);
putch(':');


}
}

void send2uart(unsigned char value)
{
unsigned char buf = 0;

buf = value & 0xF0; /* Filter for high byte */
buf = (buf>>4)|(0x30); /* Convert to ascii code */

putch(buf); /* Show on LCD */

buf = value & 0x0F; /* Filter for low byte */
buf = buf | 0x30; /* Convert to ascii code */

putch(buf); /* Show on LCD */
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…