Forums

atmega128 date/time management

Started by Didrik Pinte August 10, 2006
Hi,

I'm a newbie into embedded programming. The project i'm working on is
based on an Atmel avr atmega128 device (with a GSM device connected to
it). The environment used is IAR EW with AVRStudio.

Using the time.h library, i try to get the actual time with the
following code :

---------------------------------------------------
time_t now;
time(&now);
printf("Time is %s\r\n", ctime(&now));
---------------------------------------------------

The only result I get is Wed Dec 31 23:59:59 1969. This means 1 seconds
before 1970. Even after ten minutes, the command shows the same time...

I found an implementation of the time(...) command on the net where the
show how it was computed. In this code, when there was no RTC, the
result I get is coherent ... but there is an RTC on the hardware I use.
I even have an AT command giving me the RTC time since last boot...

Does someone have an idea on how to solve this problem ?

Didrik

Didrik Pinte wrote:
> Hi, > > I'm a newbie into embedded programming. The project i'm working on is > based on an Atmel avr atmega128 device (with a GSM device connected to > it). The environment used is IAR EW with AVRStudio. > > Using the time.h library, i try to get the actual time with the > following code : > > --------------------------------------------------- > time_t now; > time(&now); > printf("Time is %s\r\n", ctime(&now)); > --------------------------------------------------- > > The only result I get is Wed Dec 31 23:59:59 1969. This means 1 seconds > before 1970. Even after ten minutes, the command shows the same time... > > I found an implementation of the time(...) command on the net where the > show how it was computed. In this code, when there was no RTC, the > result I get is coherent ... but there is an RTC on the hardware I use. > I even have an AT command giving me the RTC time since last boot... > > Does someone have an idea on how to solve this problem ?
By not using a library without exacly knowing what it does. You hope by connecting a GSM to an AVR and calling the time library, the correct time is returned ? How would it ... Rene -- Ing.Buero R.Tschaggelar - http://www.ibrtses.com & commercial newsgroups - http://www.talkto.net
Rene Tschaggelar a =E9crit :

> Didrik Pinte wrote: > > Hi, > > > > I'm a newbie into embedded programming. The project i'm working on is > > based on an Atmel avr atmega128 device (with a GSM device connected to > > it). The environment used is IAR EW with AVRStudio. > > > > Using the time.h library, i try to get the actual time with the > > following code : > > > > --------------------------------------------------- > > time_t now; > > time(&now); > > printf("Time is %s\r\n", ctime(&now)); > > --------------------------------------------------- > > > > The only result I get is Wed Dec 31 23:59:59 1969. This means 1 seconds > > before 1970. Even after ten minutes, the command shows the same time... > > > > I found an implementation of the time(...) command on the net where the > > show how it was computed. In this code, when there was no RTC, the > > result I get is coherent ... but there is an RTC on the hardware I use. > > I even have an AT command giving me the RTC time since last boot... > > > > Does someone have an idea on how to solve this problem ? > > > By not using a library without exacly knowing what it does. > You hope by connecting a GSM to an AVR and calling the time > library, the correct time is returned ? > How would it ... > > Rene
Hi Rene, I think you misunderstood what I wanted to say. I want to get a valid system time in my C program. I have tried to do it with the time library without success. Now, I'm stuck without a valid date and not understanding why. I have looked every where to see what can cause the problem. Doing this, I found that the GSM device connected to the AVR was running ok and giving a clock time. I never said that connecting both will make the AVR return a correct time. Reading your answer, I guess that my ideas about the time library are probably wrong. Do you have any hints to "guide" me ? Didrik
>Hi Rene,
>I think you misunderstood what I wanted to say.
>I want to get a valid system time in my C program. I have tried to do >it with the time library without success. Now, I'm stuck without a >valid date and not understanding why. I have looked every where to see >what can cause the problem. Doing this, I found that the GSM device >connected to the AVR was running ok and giving a clock time. I never >said that connecting both will make the AVR return a correct time.
>Reading your answer, I guess that my ideas about the time library are >probably wrong. Do you have any hints to "guide" me ?
You say you have connected the GPS and know the GPS is receiving the correct time, you have not said how you have implemented the time() function in order that it too reads the time from the GPS. time() is a standard function that is implemented differently for each system, depending on how the system measures/stores time. In your case it must be configured to read the GPS. How have you done this? All you say is that you are calling time(). You have given no information on how you are attempting to obtain the time within this function, so nobody will be able to provide any sort of advice on what is wrong. Regards, Richard. http://www.FreeRTOS.org for Cortex-M3, ARM7, ARM9, HCS12, H8S, MSP430 Microblaze, Coldfire, AVR, x86, 8051 & PIC18 * * * *
Hi Richard,

Thank you for your answer. Here are some details about the problem

[1]  There is no GPS on this hardware. The hardware is the following :
Audiotel GSM/GPRS Industrial I/O : the core of the system is a Atmel
AVR Atmega128 with a Siemens GSM/GPRS modem.

[2] I did not implement any function but only used the official time
library provided with IAR Embedded Workbench for Atmel AVR.  The
project is configured to use the Normal DLIB.

The code I use is the  following (i've just extracted the things
related to time.h, all the reste is audiotel specific):

#include <time.h>
[...]
void MonitorModem( void)
{
   [...]
   time_t now;
   time(&now);
   printf("Time is %s\r\n", ctime(&now));
   [...]
}

I don't understand that there is no tutorial, howto or FAQ available
anywhere that precisely explains how to get and set system time on a
atmega128 system. That's why i'm posting a question here.

> Thank you for your answer. Here are some details about the problem > > [1] There is no GPS on this hardware. The hardware is the following : > Audiotel GSM/GPRS Industrial I/O : the core of the system is a Atmel > AVR Atmega128 with a Siemens GSM/GPRS modem.
I was meaning GSM.
> [2] I did not implement any function but only used the official time > library provided with IAR Embedded Workbench for Atmel AVR. The > project is configured to use the Normal DLIB.
From the IAR manual on the library function time(): "If tod is not a null pointer, the function stores the current calendar time in *tod. The function returns the current calendar time, if the target environment can determine it; otherwise, it returns -1." An AVR has no means of returning time itself. Your hardware has to be designed to include some form of clock (in your case I presume you read it from the GSM interface), then you have to implement the time() function to use that clock. If you don't do this, where are you expecting the time to come from? AVR's are nice devices, but their telepathy peripherals are a little underdeveloped ;-) Regards, Richard. http://www.FreeRTOS.org for Cortex-M3, ARM7, ARM9, HCS12, H8S, MSP430 Microblaze, Coldfire, AVR, x86, 8051 & PIC18 * * * *
"Didrik Pinte" <dpinte@itae.be> wrote in message 
news:1155203740.669867.84190@i3g2000cwc.googlegroups.com...
> Hi, > > I'm a newbie into embedded programming. The project i'm working on is > based on an Atmel avr atmega128 device (with a GSM device connected to > it). The environment used is IAR EW with AVRStudio. > > Using the time.h library, i try to get the actual time with the > following code : > > --------------------------------------------------- > time_t now; > time(&now); > printf("Time is %s\r\n", ctime(&now)); > --------------------------------------------------- > > The only result I get is Wed Dec 31 23:59:59 1969. This means 1 seconds > before 1970. Even after ten minutes, the command shows the same time... > > I found an implementation of the time(...) command on the net where the > show how it was computed. In this code, when there was no RTC, the > result I get is coherent ... but there is an RTC on the hardware I use. > I even have an AT command giving me the RTC time since last boot... > > Does someone have an idea on how to solve this problem ?
From reading your responses to others' responses, I think you're being misunderstood. You say you have an RTC, yet time() is returning -1 (i.e. not set). Logically, you have to set the RTC first, in order for time() to return valid data. How exactly you do this on your platform I have no idea ;). Steve http://www.fivetrees.com
Richard,

You're right. This will close the thread. I don't have a RTC on the
atmega128. That's why the time() function returns -1. I understood a
lot of things about my device this afternoon. Sorry for the waiste of
time. But now, there is somewhere on the internet a thread for ignorant
guys like me stating that Atmel AVR Atmega128 have no RTC, so they
can't give you a valid system time !

Many thanks to all for the information, guidance and enlightenment.

Didrik

Didrik Pinte wrote:
> Richard, > > You're right. This will close the thread. I don't have a RTC on the > atmega128. That's why the time() function returns -1. I understood a > lot of things about my device this afternoon. Sorry for the waiste of > time. But now, there is somewhere on the internet a thread for ignorant > guys like me stating that Atmel AVR Atmega128 have no RTC, so they > can't give you a valid system time ! > > Many thanks to all for the information, guidance and enlightenment. >
Didrik, an Atmega128 is not a system, just a controller. If you want it to do anything sensible you have to do it yourself usually. Even an RTC is not just read by a library, unless this library is specifically made for the cpu and for the RTC with the correct interface (SPI,I2C,twowire). And a hardware realtimeclock doesn't by itself have the correct time. Unless running from an own battery, it looses time and returns garbage. Rene -- Ing.Buero R.Tschaggelar - http://www.ibrtses.com & commercial newsgroups - http://www.talkto.net
> Does someone have an idea on how to solve this problem ?
It doesnt have a RTC. Get a two wire interface RTC and use the SCL SCA lines