CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

time trouble!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








time trouble!!
PostPosted: Wed Nov 09, 2005 8:27 am     Reply with quote

hi everyone!

i am experiencing some trouble in interfacing ds1302 RTC to Pic16f877. i am able to set the time correctly, and also read the correct time ever 5 seconds (see the code below), but when i use to the battery backed mode (i.e +5v power is available to RTC but not to the PIC ) then the time seems to lag behind. i mean, whenever i read the time from the controller at random intervals it always seems to start from 0 seconds so the format is hh:mm:00 and then it moves on. Also it sometimes seems to start the time with the last displayed value i.e lets say it displays the time 12:20:32, after this if i take away the power of the PIC (RTC running on backup power +5v) and after sometime when i check the time again it sometimes gives me the exact same time as before so lets say after 2 minutes when i recheck the time (by using 'rtc_get_time( hour, min, sec )' command) i sometimes get the reading 12:20:32 which is the same as before. i must stress though that both these phenomenons (time not updating and seconds count zero) are random. sometimes the RTC works perfectly, can any one please explain this strange behaviour.

My second question is that, is there any way to convert the time(hh:mm:ss) and date (day/month/year) to strings that can be concatenated with any other string data type (so i want to convert the hrs and minutes etc from the format %2X into string data type).

here is the code that i am using:

Code:

#include <16F877.h>
#device *=16 ADC=10
#include <stdlib.h>
#USE DELAY (CLOCK=4000000)
#include <DS1302.C>
#fuses  XT,NOWDT,NOLVP
#use rs232(baud=4800,xmit=pin_b1,rcv=pin_b2,parity=N,stream=pc)


byte get_bcd()
{
  char first,second;

  do
  {
    first=fgetc(pc);
  }

  while ((first<'0') || (first>'9'));


  fprintf(pc,"first = %C",first);

  first-='0';

  do
  {
    second=fgetc(pc);
  }

  while ((second<'0') || (second>'9'));


  fprintf(pc,"second = %C",second);

  return((first<<4)|(second-'0'));
}

void set_clock()
{
   byte day,mth,year,dow,hour,min;

   fprintf(pc,"\fYear 20: ");

   year=get_bcd();

   fprintf(pc,"\fMonth: ");

   mth=get_bcd();

   fprintf(pc,"\fDay: ");

   day=get_bcd();

   fprintf(pc,"\fWeekday 1-7: ");

   dow=get_bcd();

   fprintf(pc,"\fHour: ");

   hour=get_bcd();

   fprintf(pc,"\fMin: ");

   min=get_bcd();

   rtc_set_datetime(day,mth,year,dow,hour,min);
}



void main()
{
   byte day,mth,year,dow,hour,min,sec;

   rtc_init();

   fprintf(pc,"start");

   set_clock();

   while(1)

   {

      rtc_get_date( day, mth, year, dow);

      rtc_get_time( hour, min, sec );

      fprintf(pc,"\n%2X/%2X/%2X\n%2X:%2X:%2X",day,mth,year,hour,min,sec);

      fprintf(pc,"\n",dow);

      delay_ms(5000);

   }

}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 09, 2005 1:49 pm     Reply with quote

Quote:

when i use to the battery backed mode (i.e +5v power is available to
RTC but not to the PIC ) then the time seems to lag behind. i mean,
whenever i read the time from the controller at random intervals it
always seems to start from 0 seconds so the format is hh:mm:00
and then it moves on.

Are you trying to read/write to the DS1302 while it's only running
from battery backup power ? See this appnote on Dallas RTC's:
http://pdfserv.maxim-ic.com/en/an/app504.pdf
In the section on Battery Backup Function, it says:

Quote:

If a primary battery, such as a lithium coin cell, is used for back-up, the
RTC should be designed to draw as little power as possible when running
from the battery. The switching circuitry, normally powered from the
main supply pin, will switch to the battery and put the RTC into a low
power mode.
Communications between the microprocessor and the RTC are usually
locked out
(often called write protect) to keep the battery current at a
minimum and to prevent data corruption. (emphasis added)
Guest








PostPosted: Wed Nov 09, 2005 10:41 pm     Reply with quote

hi everyone!


is there any way to convert the time(hh:mm:ss) and date (day/month/year) which is presumably in BCD format into strings that can be concatenated with any other string data type (so i want to convert the hrs and minutes etc from the format %2X into string data type).

here is the code that i am using:

Code:

#include <16F877.h>
#device *=16 ADC=10
#include <stdlib.h>
#USE DELAY (CLOCK=4000000)
#include <DS1302.C>
#fuses XT,NOWDT,NOLVP
#use rs232(baud=4800,xmit=pin_b1,rcv=pin_b2,parity=N,stream=pc)


byte get_bcd()
{
char first,second;

do
{
first=fgetc(pc);
}

while ((first<'0') || (first>'9'));


fprintf(pc,"first = %C",first);

first-='0';

do
{
second=fgetc(pc);
}

while ((second<'0') || (second>'9'));


fprintf(pc,"second = %C",second);

return((first<<4)|(second-'0'));
}

void set_clock()
{
byte day,mth,year,dow,hour,min;

fprintf(pc,"\fYear 20: ");

year=get_bcd();

fprintf(pc,"\fMonth: ");

mth=get_bcd();

fprintf(pc,"\fDay: ");

day=get_bcd();

fprintf(pc,"\fWeekday 1-7: ");

dow=get_bcd();

fprintf(pc,"\fHour: ");

hour=get_bcd();

fprintf(pc,"\fMin: ");

min=get_bcd();

rtc_set_datetime(day,mth,year,dow,hour,min);
}



void main()
{
byte day,mth,year,dow,hour,min,sec;

rtc_init();

fprintf(pc,"start");

set_clock();

while(1)

{

rtc_get_date( day, mth, year, dow);

rtc_get_time( hour, min, sec );

fprintf(pc,"\n%2X/%2X/%2X\n%2X:%2X:%2X",day,mth,year,hour,min,sec);

fprintf(pc,"\n",dow);

delay_ms(5000);

}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 09, 2005 10:54 pm     Reply with quote

Quote:
Is there any way to convert the time(hh:mm:ss) and date
(day/month/year) which is presumably in BCD format into strings ?

1. Convert the BCD numbers to binary by using a bcd2bin() function.

2. Then convert the binary numbers into ascii decimal and write them
to a character array by using the sprintf() function.

Do a forum search for bcd2bin and you will find many posts with
bcd2bin() functions or macros.

Look in the CCS manual for information on sprintf().
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group