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.

How to read RTC register values on LCD using C

Status
Not open for further replies.

joseph raj

Junior Member level 2
Junior Member level 2
Joined
Mar 16, 2009
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,501
Hi,everyone I am using RTC DS1307 and interfacing with PIC16F877A .I dont no how to read RTC register value i.e.,date and time information on LCD.Help me out.If anyone possible give the source code in C.
I am using MPLAB IDE with hitech C.
 

how to read rtc

joseph raj said:
Hi,everyone I am using RTC DS1307 and interfacing with PIC16F877A .I dont no how to read RTC register value i.e.,date and time information on LCD.Help me out.If anyone possible give the source code in C.
I am using MPLAB IDE with hitech C.


You fist study and understand the datasheet of ds1307.Iterface the RTC with pic using i2c.If you understand the register structure of RTC then you will how to read from RTC.Do something from your side, we all are here to help...

Regards

Sajeev
 

isis ds1307 simulation not working

Hi,
see the attached code.
This i had done using atmel 89s8252.It's worked.
you can use the RTC routines and use straight away in your code with littile modification.

If u have any queries contact me on yviswanathbe@gmail.com.

Bye.

Regards,
Viswanath.
 

    joseph raj

    Points: 2
    Helpful Answer Positive Rating
displaying integers on character lcd

Thanks ,i have gone through the RTC datasheets ,but still i am facing problem in understanding it.i tried manipulating many programs available in internet.but still i am not successful.please help:?::?:
 

displaying rtc data on lcd

Firstly, do you understand what I2C means?, and secondly, do you understand what BCD means?
 

proteus and rtc

ya i I2C is a serial bus used to communicate with peripherals.
BCD is a binary coded decimal format.
my problem how to address the register contents stored in RTC.
please help:?::?:

Added after 1 hours 46 minutes:

thanks for the code.
but its giving an error which says expected ";" for the lines below

unsigned char clock[] {0x00,0x40,0x17,0x01,0x18,0x12,0x06};
unsigned char *s[] {"SUN","MON","TUE","WED","THU","FRI","SAT"};even though i have given the ; why is it giving that error.

help me:?::?:
 

how to display rtc info on lcd

Hi Joseph,

i dint find the "=" in between the clock[] and content in the {data}.
have u put the "=" and compiled?

unsigned char clock[]={0x00,0x40,0x17,0x01,0x18,0x12,0x06};
unsigned char *s[] ={"SUN","MON","TUE","WED","THU","FRI","SAT"};


I am using the same code for my application and it's worked.
Which compiler you r using?

Regards,
Viswanath.
 

working with rtc using i2c bus

I am using Hitech C in MPlab IDE.
 

_nop in pic controllers

are you comfortable that your I2C protocol is functioning? Do you have other devices on that bus that are respondant? Do you have a scope?

What is the device address for the DS1307?
 

rtc i2c in milliseconds

I am not that sure about I2C protocol functioning.
I have not used I2C bus i just directly connected sda and scl pin to microcontroller's sda and scl pin i.e.,RC4 and RC3
the DS1307 device address i am not sure.if you are asking the slave address then it is D0 during read and D1 during write.

my rtc clock is running as i can see it in proteus simulation but its not displaying its value on lcd.
help me out its urgent

Added after 1 hours 30 minutes:

hi, the error which i posted is removed,but now its giving error for _nop .you have not included subruotine for generating 1 microsecond delay.
help me out:?::?:
 

hitech

Hi I am using HitechC .and i am using PIC16F877A.
I want to make a digital clock.
this is the code given below.
when compiled there is no error,but its not displaying time and date.
#include<htc.h>
void _nop_(void);
#define delay_us _nop_(); //generates 1 microsecond

#define LCD PORTD


#define RS RA2 //connect p0.0 to rs pin of lcd
#define EN RA0 //connect p0.1 to en pin of lcd
#define RW RA1

#define SCK RC3 //serial clock pin
#define SDA RC4 //serial data pin

#define SCKHIGH SCK=1
#define SCKLOW SCK=0
#define SDAHIGH SDA=1
#define SDALOW SDA=0

void integer_lcd(int);
void init_lcd(void);
void cmd_lcd(unsigned char);
void write_lcd(unsigned char);
void display_lcd(unsigned char *s);
void delay_ms(unsigned int);

void start(void);
void stop(void);
void send_byte(unsigned char);
unsigned char receive_byte(unsigned char);
void write_i2c(unsigned char,unsigned char,unsigned char);
unsigned char read_i2c(unsigned char,unsigned char);

//clock[]={seconds,minutes,hours,day_of_week,date,month,year}

unsigned char clock[] = {0x00,0x40,0x17,0x01,0x18,0x12,0x06};
const unsigned char *s[] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};

unsigned char slave_ack,add=0,c,k;
unsigned int num;



// Real Time Clock Mode
void main(void)
{
const unsigned char *temp;
ADCON1=0x06;
init_lcd();
cmd_lcd(0x01);
//display_lcd("UTL");
//delay_ms(1000);
TRISA=0;
TRISD=0;
TRISC=0xFF;
start();

while(add<=6) //update real time clock
{
write_i2c(0xd0,add,clock[add]);
add++;
}
stop();


while(1)
{

c=read_i2c(0xD0,0x02);//read hours register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
write_lcd(':');

c=read_i2c(0xD0,0x01);//read minutes register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
write_lcd(':');

delay_ms(10);
c=read_i2c(0xD0,0x00);//read seconds register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
write_lcd(' ');


temp=s[read_i2c(0xD0,0x03)];
display_lcd(temp);//read day register and display
cmd_lcd(0xc0);
c=read_i2c(0xD0,0x04);//read date register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
write_lcd('/');

c=read_i2c(0xD0,0x05);//read month register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
write_lcd('/');

write_lcd('2');
write_lcd('0');
c=read_i2c(0xD0,0x06);//read year register and display on LCD
write_lcd((c/16)+48);
write_lcd((c%16)+48);
delay_ms(100);
cmd_lcd(0x01);
}
}


//starts i2c, if both SCK & SDA are idle
void start(void)
{
if(SCK==0) //check SCK. if SCK busy, return else SCK idle
return;
if(SDA==0) //check SDA. if SDA busy, return else SDA idle
return;
SDALOW; //data low
delay_us;
SCKLOW; //clock low
delay_us;
return;
}

//stops i2c, releasing the bus
void stop(void)
{
SDALOW; //data low
SCKHIGH; //clock high
delay_us;
SDAHIGH; //data high
delay_us;
return;
}

/*void start(void)
{
SCK=0;
SDA=1;
delay_ms(1);
SDA=0;
return;
}

void stop(void)
{

delay_ms(1);
SDA=0;
SCK=1;
delay_ms(1);
SDA=1;
delay_ms(1);
SCK=0;
return;
}*/
//transmits one byte of data to i2c bus
void send_byte(unsigned char c)
{
unsigned mask=0x80;
do //transmits 8 bits
{
if(c&mask) //set data line accordingly(0 or 1)
SDAHIGH; //data high
else
SDALOW;//data low
SCKHIGH; //clock high
delay_us;
SCKLOW; //clock low
delay_us;
mask/=2; //shift mask
}while(mask>0);
SDAHIGH; //release data line for acknowledge
SCKHIGH ; //send clock for acknowledge
delay_us;
slave_ack=SDA; //read data pin for acknowledge
SCKLOW ; //clock low
delay_us;
return;
}

//receives one byte of data from i2c bus
unsigned char receive_byte(unsigned char master_ack)
{
unsigned char c=0,mask=0x80;
do //receive 8 bits
{
SCKHIGH ;//clock high
delay_us;
if(SDA==1) //read data
c|=mask; //store data
SCKLOW ; //clock low
delay_us ;
mask/=2; //shift mask
}
while(mask>0);
if(master_ack==1)
SDAHIGH ;//don't acknowledge
else
SDALOW; //acknowledge
SCKHIGH ;//clock high
delay_us;
SCKLOW; //clock low
delay_us;
SDAHIGH ;//data high
return c;
}



//writes one byte of data(c) to slave device(device_id) at given address(location)
void write_i2c(unsigned char device_id,unsigned char location,unsigned char c)
{
do
{
start(); //starts i2c bus
send_byte(device_id); //select slave device
if(slave_ack==1) //if acknowledge not received, stop i2c bus
stop();
}while(slave_ack==1); //loop until acknowledge is received
send_byte(location); //send address location
send_byte(c); //send data to i2c bus
stop(); //stop i2c bus
delay_ms(4);
return;
}

//reads one byte of data(c) from slave device(device_id) at given address(location)
unsigned char read_i2c(unsigned char device_id,unsigned char location)
{
unsigned char c;
do
{
start(); //starts i2c bus
send_byte(device_id); //select slave device
if(slave_ack==1) //if acknowledge not received, stop i2c bus
stop();
}while(slave_ack==1); //loop until acknowledge is received
send_byte(location); //send address location
stop(); //stop i2c bus
start(); //starts i2c bus
send_byte(device_id+1); //select slave device in read mode
c=receive_byte(1); //receive data from i2c bus
stop(); //stop i2c bus
return c;
}

/*display of 16bit(integers) values on LCD
void integer_lcd(int n)
{
unsigned char c[6];
unsigned int i=0;
if(n<0)
{
write_lcd('-');
n=-n;
}
if(n==0)
write_lcd('0');
while(n>0)//Split integer to 2 bytes and send on 8bit line.
{
c[i++]=(n%10)+48;
n/=10;
}
while(i-->=1)
write_lcd(c);
}*/

//initialize lcd
void init_lcd(void)
{
//delay_ms(10); //delay 10 milliseconds
ADCON1=0x06;
TRISA=0;
TRISD=0;
cmd_lcd(0x38); //8 bit initialize, 5x7 character font, 16x2 display
cmd_lcd(0x0E); //lcd on, cursor on
cmd_lcd(0x06); //right shift cursor automatically after each character is displayed
cmd_lcd(0x01); //clear lcd
return;
}

//transmit command or instruction to lcd
void cmd_lcd(unsigned char c)
{
LCD=c;
RS=0;
RW=0;
EN=1;
delay_ms(20);
EN=0;
return;
}

//transmit a character to be displayed on lcd
void write_lcd(unsigned char c)
{
LCD=c;
RS=1;
RW=0;
EN=1;
delay_ms(20);
EN=0;
return;
}

//transmit a string to be displayed on lcd
void display_lcd(unsigned char *s)
{
while(*s)
write_lcd(*s++);
return;
}

//generates delay in milli seconds
void delay_ms(unsigned int i)
{
unsigned int j;
while(i-->0)
{
for(j=0;j<500;j++)
{
;
}
}
return;
}

void _nop_(void)
{
unsigned int i;
for(i=0;i<10;i++)
{
;
}
return;
}


the display which i get on lcd is 00:00:00 SUN 00/00/00
anyone check this out and reply to me immediately.
i think its only reading initial value, but not present value.
help me out???
 

integer_lcd

"immediately" wow what's the rush? Are you offering compensation?

Please use code tags else your formatting is lost.
 

rtc display

ya its urgent.Because i am doing project on it,Our last date is near by.
help me????
 

display rtc on lcd c code

Hi Joseph,

make sure that ur lcd is working fine.
First check the lcd alone(biapass all other circuits like RTC etc) and send some characters continuously to the lcd.
and u need to send data to the lcd character by character(Ascii equivalent will be given in the lcd datasheet.for example if u wanna print integer 2 u need to send 0x32 on the lcd data port).make sure u r doing the same.

debug and see in which format u r getting the data from the RTC.based on that u manipulate to send data to lcd.

hope u understand?
reply me with ur feedbacks?

Regards,
Y.Viswanath

Added after 4 minutes:

upload ur schematic.
 

    joseph raj

    Points: 2
    Helpful Answer Positive Rating
Re: RTC display problem

yviswanathbe said:
Hi Joseph,

make sure that ur lcd is working fine.
First check the lcd alone(biapass all other circuits like RTC etc) and send some characters continuously to the lcd.
and u need to send data to the lcd character by character(Ascii equivalent will be given in the lcd datasheet.for example if u wanna print integer 2 u need to send 0x32 on the lcd data port).make sure u r doing the same.

debug and see in which format u r getting the data from the RTC.based on that u manipulate to send data to lcd.

hope u understand?
reply me with ur feedbacks?

Regards,
Y.Viswanath

Added after 4 minutes:

thank u vishwanath for your utmost patience to go through the code.
my lcd is working fine i had checked it.
while displaying rtc values ,i can see changes on port connected to lcd,but the rs and en ,there is no change and nothing is diaplayed on lcd.
is it because of delay?
how to generate the i microsecond delay?
reply
upload ur schematic.
:idea::idea:
 

Re: RTC display problem

Hi joseph,

generating 1 microsecond delay is easy and depends on your crystal frequency.
for example if yu are using 12MHz crystal,in 8051 architecture the time it takes to execute 1 instruction is 1uSec(T=12/12MHz=1uSec;because 1 machine cycle is 12 clock cycles).
Time Delay Calcualation:
i am assuming 16-bit timer,so 2 power 16 is 65536(in Hex 0xFFFF)
assume the values u need to load is some x;
(FFFF-x)*1uSec(T calculated above)=1uSec(desired),after manipulating u will get
x=0xFFFE
load this value in the timer registers u will get 1uSec delay.

Hope this help u.try implmenting the same with ur microcontroller.

and regarding RTC,i am not able to guess what is ur actual problem.
I have done it in 8051,not in PIC.i donno pic.
anyway i will try to resolve ur problem.
Take care
bye
 

Re: RTC display problem

Thanks a lot vishwanath for your reply.
the solution you have given to generate i microsecond delay was really helpful.
did your real time value displayed on lcd?
the thing is even after disconnecting rtc i am getting the display as 00:00:00 SUN
00/00/2000
I dont think data transfer is going on between rtc and mc?
why hav you used c/16+48?
reply.???
 

Re: RTC display problem

Hi Joseph,

why hav you used c/16+48?

if yu want to send data to LCD(Suppose if u want to print 0 on lcd,u need to send 48(dec)->0x30(hex)->ascii equivalent is 0 on lcd data port).

so i am adding 48 to the result and sending to the LCD.

//clock[] ={seconds,minutes,hours,day_of_week,date,month,year}
unsigned char clock[] = {0x00,0x40,0x17,0x01,0x18,0x12,0x06};

here i am freezing some fixed time and date.please follow the above comments.After power on this time will be displayed on the lcd and will start updating.

y dont u post ur schematic?
i think ur controller operates with 3.3V power supply and RTC ,LCD with 5V.
May be voltage level is not sufficient for the controller to drive the lcd.
How the controller port pins are configured.Pushpull or Open Collector?
connect Pull-up resistors or Buffer IC's(like 74hc541,74hc245) in between the Controller port and LCD port.
check the voltage levels coming from Microcontroller using an oscilloscope.

By the way where are yu from?

Regards,
Viswanath.
 

Re: RTC display problem

Hi i am from Shimoga,doing my enggineering in bangalore.
well reading of rtc register is still a question.
because even after disconnecting rtc in ISIS simulation it still displays 00:00:00 SUN 00/00/2000
please help?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top