Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ask for sample project i2c using pic16f877 and microC

Status
Not open for further replies.
The MikroC help file has example code under I2C hardware library.
 
@ Rmain1972

i know that..the problem is,i need a display output for this project and i don't understand the example in MikroC..

@ be80be

tnx for the link..i'm only required to use a PIC16F877..
 
lgsg89 The code over there is chip independent. That's the beauty of C you may have to change some registers.

Not a real biggie But the writer goes into great details on how it works. Now if your looking for some one to do all the work that's not going to do you any real justice. You'll not learn much that way. Write some code post it and we will help you get it working.
 
unsigned short hh;
unsigned short mm;
unsigned short ss;
unsigned char x[17];

void write_DS1307(unsigned short address, unsigned short data)
{
unsigned short status;
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Wr(data);
I2C_Stop();
}

unsigned short read_DS1307(unsigned short address)
{
unsigned short data;
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Repeated_Start();
I2C_Wr(0xd1);
data=I2C_Rd(0);
while (!I2C_Is_Idle()) asm nop;
I2C_Stop();
return(data);
}


void main(){
PORTB = 0;
TRISB = 0;

PORTD = 0;
TRISD = 0;

hh = 0;
mm = 0;
ss = 0;

Lcd_Init(&PORTD);
Lcd_Out(1,1,"Init...");
I2C_Init(100000); //DS1307 operates at 100Khz only

ss=read_ds1307(0);
write_ds1307(0, ss & 0x7F); // enable oscillator(bit 7 =0)

ss=read_ds1307(2);
write_ds1307(2, ss & 0b10111111); // set 24H mode



while(1)
{
ss=read_ds1307(0); // read second
mm=read_ds1307(1); // read minute
hh=read_ds1307(2); // read hour
/*
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year
*/

Lcd_Chr(1,1, 48+ ((hh & 0b00110000) >> 4));
Lcd_Chr(1,2, 48+ (hh & 0b00001111) );

Lcd_Chr(1,3, ':');

Lcd_Chr(1,4, 48+ ((mm & 0b01110000) >> 4));
Lcd_Chr(1,5, 48+ (mm & 0b00001111) );

Lcd_Chr(1,6, ':');

Lcd_Chr(1,7, 48+ ((ss & 0b01110000) >> 4));
Lcd_Chr(1,8, 48+ (ss & 0b00001111) );


delay_ms(1000);
}
}

tnx be80be,
here..i don't know where the problem is..when i play the isis, it only diplays 00:00:00
 
@ Ian Rogers

tnx Ian Rogers.. it work..

one more thing,,can anyone examine my code,so that i can also display the date..tnx
 
Hi lgsg89. I'm also doing the same project with you. Would you like to implement an alarm into this project and make it like alarm clock? Do you know how to write the coding for the alarm? Or any other coding can I refer to? Thanks for the reply.
 
If I have done it, for sure I will share it for you guys.But now I having the problem in writing the coding for alarm.Hope any of you guys can help me.:)
 
I read the clock into an array,

Code:
//Write a byte to The RTC
char WriteRTC(char Address, char Byte)
	{
	SendID(0b11010000);
	if(SendByte(Address&0xff)==0)
		return(0);
	if(SendByte(Byte)==0)
		return(0);
	SendStop();
	return(1);
	}

//Read a byte from the RTC
char ReadRTC(char Address)
	{
	char Byte;
	SendID(0b11010000);
	if(SendByte(Address&0xff)==0)
		return(0);
	SendRestart();
	if(SendByte(0b11010001)==0)
		return(0);
	Byte=ReceiveByte();
	SendNack();
	SendStop();
	return(Byte);
	}

void WriteClock(char *array)
	{
	char x;
	for(x=0;x<8;x++)
		WriteRTC(x,array[x]);	
	}	

void ReadClock(char *array)
	{
	char x;
	for(x=0;x<8;x++)
		array[x] = ReadRTC(x);	
	}

Remember to sort the array BIN /BCD and visa versa now you can create two / three or even four 7 byte array's

then compare !!

If you need this code I'll gladly share
 
Last edited:
Ian Rogers,do you have the coding for adding alarm for DS1307 with PIC16F877A?It seem like I'm doing the same thing with lgsg89.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top