EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / RTC interfacing with 8051

RTC interfacing with 8051

|

Microcontroller › 8051 › RTC DS1307 interfacing with 8051 in C using I2C……. › RTC interfacing with 8051

June 26, 2013 at 6:21 am #10035
Anonymous
Guest

RTC interfacing with 8051 using I2C

 

this is my code…….it displays time on lcd but the time doesnt change (my rtc also not changing)…….

 

Can someone please help me…… im stuck at this…..

 

******************main program**********************

 

 

#include<reg51.h>
#include<string.h>
#include “lcd.h”
#include “i2c.h”
 
 
#define SEC   0x00
#define MIN   0x01
#define HOUR  0x02
#define DATE  0x04
#define MONTH 0x05
#define YEAR  0x06
 
 
 
unsigned char read(unsigned char);
void send2lcd(unsigned char );
 
 
void send2lcd(unsigned char value)
{
unsigned char buf = 0;
 
buf = value & 0xF0; /* Filter for high byte */
buf = (buf>>4)|(0x30); /* Convert  to ascii code */
 
lcd_data(buf); /* Show on LCD */
 
buf = value & 0x0F; /* Filter for low byte */
buf = buf | 0x30;       /* Convert to ascii code */
 
lcd_data(buf); /* Show on LCD */
}
 
 
 
unsigned char read(unsigned char addr)
{
unsigned char ret;
start();
write_get(0xD0);
write_get(addr);
start();
write_get(0xD1);
ret = read_get();
stop();
return ret;
 
}
void main()
{
unsigned char sec, min, hour,date,month,year;
 
   while(1) 
{
  lcd_init();
  lcd_cmd(0x80);
  lcd_string(“TIME-“);
 
/* Get Time */
lcd_cmd(0x85);
hour = read(HOUR);
if(hour >= 23)
{
send2lcd(hour);
}
else
{
send2lcd(hour);
}
 
 
lcd_data(‘:’);
 
min = read(MIN);
send2lcd(min);
 
 
lcd_data(‘:’);
 
sec = read(SEC);
if (sec <= 59)
{
send2lcd(sec);
}
else
{
send2lcd(0x00);
}
 
 lcd_cmd(0xC0);
lcd_string(“DATE-“);
 
lcd_cmd(0xC5);
 
 
date = read(DATE);
send2lcd(date);
 
lcd_data(‘/’);
 
month = read(MONTH);
send2lcd(month);
 
lcd_data(‘/’);
 
year = read(YEAR);
send2lcd(year);
 
  }
 
}
 
 
 
**************i2c.h*****************
 
 
sbit SCL = P2^0;
sbit SDA = P2^1;
 
void start(void);
void stop(void);
unsigned char read_get(void);
void write_get(unsigned char);
void ack();
void noack();
void delay(unsigned int);
 
 
void delay(unsigned int s)
{
unsigned int i,j;
for(i=0;i<=s;i++)
  for(j=0;j<=10;j++);
}
 
 
void start()
{
  SDA = 1;
SCL = 1;
delay(10);
SDA = 0;
SCL = 0;
}
 
void write_get(unsigned char value)
{
unsigned char i,write;
for(i = 0;i < 8;i++)
  {
write = value&0x80;
SDA = write;
SCL = 1;
delay(10);
SCL = 0;
value = value<<1;
  }
 
SDA = 1;
SCL = 1;
ack();
  delay(10);
SCL = 0;
}
 
 
unsigned char read_get()
{
  unsigned char read = 0x00;
  unsigned char i;
SDA = 1;
 
for(i=0;i<8;i++)
{
SCL = 0;
delay(20);
SCL = 1;
read = read| SDA;
if(i<7)
read = read <<1;
}
noack();
return read;
 
 
}
 
void stop()
{
SDA = 0;
SCL = 1;
delay(10);
SDA = 1; 
SCL =0;
 
}
 
void ack()
{
SDA = 0; /* Clear SDA */
 
delay(10);    
 
SCL = 1;
delay(10);
SCL = 0;
 
SDA = 1; /* Set SDA */
}
 
 
void noack()
{
SDA = 1; /* Set SDA */
 
delay(10);
SCL = 1;
delay(10);
SCL = 0;
 
SCL = 1; /* Set SCL */
}
 
**********************lcd.h**************
 
sfr lcd = 0x90;
 
sbit rs = P3^0;
sbit rw = P3^1;
sbit en = P3^2;
 
void lcd_cmd(unsigned char);
void lcd_init();
void lcd_data(unsigned char);
void delay1(unsigned int);
void lcd_string(unsigned char a[]);
 
void lcd_init()
{
  lcd_cmd(0x01);
lcd_cmd(0x38);
lcd_cmd(0x06);
lcd_cmd(0x0E);
}
 
void lcd_cmd(unsigned char x)
{
  lcd = x;
rs = 0;
rw = 0;
en = 1;
delay1(10);
en = 0;
}
 
void lcd_data(unsigned char z)
{
  lcd = z;
rs = 1;
rw = 0;
en = 1;
delay1(10);
en = 0;
}
 
void delay1(unsigned int y)
{
unsigned int j;
for(;y>0;y–)
for(j=0;j<100;j++);
}
 
void lcd_string(unsigned char a[])
{
unsigned int i;
for(i = 0;i < strlen(a);i++)
{
lcd_data(a);;
}
 
}
 
 

RSS Recent Posts

  • PIC KIT 3 not able to program dsPIC June 23, 2025
  • Fun with AI and swordfish basic June 23, 2025
  • Is AI making embedded software developers more productive? June 23, 2025
  • Microinverters and storeage batteries? June 22, 2025
  • FFC connector white June 22, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise