Discussion Forum

Display TIME and DATE

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Jimmy Sayavong
Posted
14-May-2003 21:07 GMT
Toolset
C51
New! Display TIME and DATE
Could some body in this forum advise me with
a simple code on how to diplay DATE and TIME
using printf() statement. I tried cut and paste the one posted here, and it did not
even compile ... whole bunch of syntax errors !!!!


Thanks in advance,



JIMMY
Read-Only
Author
Andy Neil
Posted
14-May-2003 23:57 GMT
Toolset
C51
New! RE: Display TIME and DATE
How about printf?

How are you obtaining DATE and TIME?

"I tried cut and paste the one posted here"

Where??
Give the full link!
Read-Only
Author
Drew Davis
Posted
15-May-2003 02:03 GMT
Toolset
C51
New! RE: Display TIME and DATE
Keil C does not have the standard functions found in time.h. Knowing the real time-of-day (TOD) implies that you have access to some hardware that tracks TOD. Many embedded systems don't, and may not even care about TOD. Those that do care will have wildly varying implementations of the hardware. There's no way Keil could support them all.

If you don't have a TOD clock, you can implement it in software using a timer tick interrupt, and counting days, hours, minutes, seconds, down to whatever resolution you need. Two common methods are to actually count days, hours, etc., in separate integers, doing the modulo wraparound as appropriate, or to continuously count one measure in a big integer, and convert to Y/M/D/H/S as required. (Unix, for example, traditionally counts seconds since January 1, 1970, in a 32-bit integer.)

The software will need some means to set the clock correctly once it boots. Perhaps that's a human pushing buttons, maybe it's a network protocol to fetch the time, maybe just TOD from time "zero" is good enough.

So, the first question to answer is Andy's "how are you obtaining date and time"?

Once you know that, then you'll know the exact format the bits that represent your time are in. Then you'll be able to figure out how to convert it to some desired time format (text output over a UART? 7-segment display? Alpha LCD screen?)

You might consider implementing the standard C library time functions on top of your hardware. If so, displaying the current time might be as simple as "printf(ctime())".

If your application is really serious about time (say, an astronomical instrument or maybe navigation), even tracking the actual, precise, time of day gets really complicated.
Read-Only
Author
Jimmy Sayavong
Posted
15-May-2003 14:39 GMT
Toolset
C51
New! RE: Display TIME and DATE
Andy,

Please look at the code I cut and pasted:

"Display time and date" and let us know
what you think. Like I said, the code
does not even compile !!!


Jimmy
Read-Only
Author
Jon Ward
Posted
15-May-2003 15:38 GMT
Toolset
C51
New! RE: Display TIME and DATE
The code referenced in http://www.keil.com/support/docs/471.htm is a technique that allows the user (while debugging with the dScope debugger) to get the time and date a function was compiled.

Is this what you are trying to do?

Since you don't give us the compiler errors/warnings, you make it diffucult for us to figure out what you are doing. So difficult, in fact, that we would have to create a test case to see what happens. So, I did. The following code compiles with no errors or warnings:

#include <string.h>

typedef unsigned char   U8;
#define KC_DATA         data


void main (void)
{
U8 KC_DATA Datestring[sizeof(__DATE__)];
U8 KC_DATA Timestring[sizeof(__TIME__)];


memcpy(Datestring, &__DATE__, sizeof(__DATE__));
memcpy(Timestring, &__TIME__, sizeof(__TIME__));

while (1);
}

The debugger function was written for the dScope debugger. Is that what you are using? If you are using the uVision2 debugger, this function must be modified for uVision2.

func void Time_Stamp (long DateAddr, long TimeAddr ){

long addr;

printf(" ===============================================================\n");
printf(" This is the time and date the function under test was built\n");
printf(" Date: ");


for (addr = DateAddr; _rbyte(addr) != 0; addr++)
  printf ("%c", _rbyte(addr));

printf("\n Time:");

for (addr = TimeAddr; _rbyte(addr) != 0; addr++)
  printf ("%c", _rbyte(addr));


printf("\n =================================================================\n");
}

I suspect you are trying to include the debugger function in your C program--and that certainly won't work. The debugger function is intended for the debugger.

Jon
Read-Only
Author
Jon Ward
Posted
15-May-2003 16:23 GMT
Toolset
C51
New! RE: Display TIME and DATE
I have just added a new knowledgebase article that works for the uVision2 Debugger.

http://www.keil.com/support/docs/2584.htm

Perhaps it will be more helpful.

Jon
Read-Only
Author
Jimmy Sayavong
Posted
15-May-2003 16:26 GMT
Toolset
C51
New! RE: Display TIME and DATE
Thanks for the reply Jon,

What I want is NOT to use with the debugger, but to use within the main
C program in which somewhere in the
code that I can call the function to
display DATE and TIME (of course at Compile time) using printf() function. I look at
your modified code, and the Time_Stamp()
has 2 arguments which should be somehow
reference to parpameters within the Main().
How do I handle this If I were to put
a Printf() function inside the Main() and
call Time_stanp(DateAddr, Timeaddr) when
I need to display DATE and TIME.

Please advise

Thanks,


Jimmy
Read-Only
Author
Stefan Duncanson
Posted
15-May-2003 16:19 GMT
Toolset
C51
New! RE: Display TIME and DATE
If you are just trying to print out the date and time of compilation all you need is:

printf("%s %s",__DATE__,__TIME__);

Stefan
Read-Only
Author
Andrew Neil
Posted
15-May-2003 17:40 GMT
Toolset
C51
New! RE: Display TIME and DATE
OR even just:
printf( __DATE__ __TIME__ );
since __DATE__ and __TIME__ are both string literals, and the compiler concatenates adjacent string literals, and you don't need a format string if you're just printing a string literal.
Read-Only
Author
Andy Neil
Posted
16-May-2003 02:34 GMT
Toolset
C51
New! RE: Display TIME and DATE
What I do is just have a single file - eg, timestamp.c - in the project which just defines a string containing the date & Time:
char timestamp[] = __DATE__ __TIME__;
(note that there's no need for sizeof - the compiler does all that automatically!)

I set this file to 'Always Build' in the uVision Project.
make the timestamp symbol public, and anything in your code can display the build date & time whenever it wants.

You could even include such a variable in each of your source files, to give each one a build timestamp, if you wanted (and if you had ROM space to burn!).
Read-Only
Author
Stefan Duncanson
Posted
16-May-2003 10:06 GMT
Toolset
C51
New! RE: Display TIME and DATE
"and you don't need a format string if you're just printing a string literal"

Might as well dump the printf() as well and just use puts().

Stefan

Next Thread | Thread List | Previous Thread Start a Thread | Settings