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
How about printf? How are you obtaining DATE and TIME? "I tried cut and paste the one posted here" Where?? Give the full link!
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.
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
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); }
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"); }
If you are just trying to print out the date and time of compilation all you need is: printf("%s %s",__DATE__,__TIME__); Stefan
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
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
OR even just:
printf( __DATE__ __TIME__ );
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__;
"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