Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RTC(ds1307).in Hi-Tech

Status
Not open for further replies.

fawadbutt

Member level 3
Joined
Oct 29, 2010
Messages
61
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
Lahore, Pakistan, Pakistan
Activity points
1,754
//Project:RTC_LCD_Basic
plz help me,,, i m trying to read the RTC with I2C protocol ,,,i search the different web link ,,,and write the code...plz read my code and fine the error ..and tell me what can i do,,,,:roll:



Code:
#include<pic.h>
#include<htc.h>
__CONFIG(0x3F32);
#define _XTAL_FREQ 400000
#include "my4lcd.h"

/////////////////// RTC DEFINE //////////////////
//DS1307 7 bit address=0b1101000=0x68
#define SLAVE_ADD 0x68
#define READ		1
#define WRITE		0

////////////////////-IC-//////////////////////////////
void ic_init();
///// DS1307 ////// RTC FUNTIONS /////////////////////
//RTC address 0x68
//send start sequence to slave
void i2c_start();
//send stop sequence to slave
void i2c_stop();
//initialize i2c module
void i2c_init();
unsigned char i2c_read(unsigned char ack);
//ack=1: not acknowledge
//ack=0:Acknowlege
unsigned char i2c_write(unsigned char da);
//d: 8 bit data
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned short read_ds1307(unsigned short address);

void i2c_Repeated_Start();
/////////////////// Display ///////////////////////////////////////////

unsigned char BCD2LowerCh(unsigned char bcd);
unsigned char BCD2UpperCh(unsigned char bcd);

unsigned char d1,d2,d3,d4;
void display(int value);
////////////////////////////////////////////////
unsigned short sec;
unsigned short mint;
unsigned short hour;
unsigned short day;
unsigned short date;
unsigned short month;
unsigned short year;
unsigned short data;

char time[9];
char ddate[11];
char value;
////////////////////////////////////////////////
#define SCL RC3//SCK
#define SDA RC4//SDL
//////////////////////////////////////////////
void main()
{
ic_init();
lcd_init();
i2c_init();

//send start condetion
i2c_start();
//send Slave address+ WRITE command
i2c_write((SLAVE_ADD<<1)|WRITE);
((0x68<<1)|0)==0xD0;
//send register address (register of slave-the first register need to read data)
//lcd_string("DATE");
i2c_read((SLAVE_ADD<<1)|READ);

//Set Time
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x10); //write min 27
write_ds1307(2,0x01); //write hour 14
write_ds1307(3,0x02); //write day of week 2:Monday
write_ds1307(4,0x05); // write date 17
write_ds1307(5,0x01); // write month 6 June
write_ds1307(6,0x09); // write year 8 --> 2008
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator

while(1)
{lcd_cmd(0x80);
lcd_string("TIME");
lcd_cmd(0xC0);
__delay_ms(1);
/*
sec=i2c_read(0);		//read the second
mint-i2c_read(1);		//read the mint
mint-i2c_read(2);		//read the hour
*/
sec=read_ds1307(0);		//read the second
mint=read_ds1307(1);		//read the mint
mint=read_ds1307(2);		//read the hour



time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = BCD2UpperCh(mint);
time[3] = BCD2LowerCh(mint);
time[4] = BCD2UpperCh(sec);
time[5] = BCD2LowerCh(sec);
value=time[0];
lcd_data(value);
value=time[1];
lcd_data(value);
lcd_data('-');
value=time[2];
lcd_data(value);
value=time[3];
lcd_data(value);
lcd_data('-');
value=time[4];
lcd_data(value);
value=time[5];
lcd_data(value);


}




}

////////////////////////RTC///////////////////////////////////////////////
void i2c_init()
{
SCL=1;//SCK
SDA=1;//SDL
TRISC3=1;
TRISC4=1;	//control by MSSP
SSPADD=9;	//Baudrate= Fosc/4/(SSPADDR+1)=(100khz)
SMP=1;	//slew rate control disable for 100KHz baudrate
SSPCON2=0;
SSPCON=0x28;	//i2c MasterHardware mode
}

void i2c_start(){
	SEN = 1;		//Start Condition Enable(bit of SSPCON2 Register)
	while(SEN);
}

void i2c_stop(){
	PEN = 1;		//PEN Stop Condition Enable(bit of SSPCON2 Register)
	while(PEN);
}
unsigned char i2c_write(unsigned char da)
{
	WCOL = 0;		//WCOL stands for Write COLlision and is set
					//when the user tries to write to SSPBUF, but
					//the I2C bus is not ready(use for multiple master mode)
	SSPIF = 0;		//
	SSPBUF = da;		//SSPBUFFER equal to data
	asm("nop");			//delay
	asm("nop");			//	delay
	asm("nop");			//delay for 1 instraction cycle
	if(WCOL) return 2;			// write collision
	else {
		while(!SSPIF);
		if(ACKSTAT) return 1;	// not acknowledge
		return 0;				// everything is ok
	}
}
unsigned char i2c_read(unsigned char ack)
{
	i2c_start();
	RCEN = 1;			//Receive Enable
	while(RCEN);	
	
	ACKDT = ack;	//ACK Transmit Data
	ACKEN = 1;		//ACK Transmit Enable
	while(ACKEN);	
	return SSPBUF;
}


////////////////////////////////////////////////////////////////////////
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
//unsigned char BCD2Dec(unsigned short bcd){
// return ((bcd >> 4)*10+(bcd & 0x0F));
//}

////////////////////////// IC /////////////////////////////////////////////

void ic_init()
{

TRISB=PORTB=0x00;
TRISD=PORTD=0x00;
TRISC=PORTC=0x00;
}
void write_ds1307(unsigned short address,unsigned short w_data)
{

	i2c_start();
    while(SEN==1);
    i2c_write(0xD0);  // Write mode and clock in Ack
    i2c_write(address); // Control reg adress);
    i2c_write(w_data); // sec    [0]
    i2c_stop(); 
}

unsigned short read_ds1307(unsigned short address)
{
i2c_start();
i2c_write(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
i2c_write(address);
//i2c_Repeated_Start();
i2c_write(0xd1); //0x68 followed by 1 --> 0xD1
data=i2c_read(0);
i2c_stop();
return(data);
}
void i2c_Repeated_Start()
{
//i2c_read(0x00);
//i2c_write(0x00);


//ReStart condition
i2c_stop();
i2c_start();

}

Use CODE Tags When Posting Your Code
 
Last edited by a moderator:

What is the exact model of PIC you are utilizing in your design?

Have you implement pull up resistors of the proper value on both the I2C lines?

What value are the resistors?

Just glancing at your code, the following #define may possibly be incorrect:

Code:
#define _XTAL_FREQ 400000

A system clock frequency of 400kHz is a little on the slow side for any PIC?

Also there is no need to include the following line:

Code:
#include <pic.h>

The inclusion of htc.h, takes care of it.

BigDog
 

RTC.JPGhere is my schematic circuit.....i use 16f877A.....in Hi-tech compiler ,
show all detail of my project...i use 4.7K pull up resister ....my lcd code is work well...but some problem in RTC section...
and i correct the crystal frequency 4000000(4MHz).....thanx for replay..
 

here is my schematic circuit.....i use 16f877A.....in Hi-tech compiler ,
show all detail of my project...i use 4.7K pull up resister ....my lcd code is work well...but some problem in RTC section...
and i correct the crystal frequency 4000000(4MHz).....thanx for replay..

I had also found this problem...

But i am able to solve this problem...
By not reading in multibytes format..
Reading Single bytes works well..

i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_restart();
i2c_write(0xd1);
temp = i2c_read()
i2c_stop()


using this format... it will work
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top