3
\$\begingroup\$

I am using ATmega32-A micro controller, coding in C language and compiling using CodeVisionAVR compiler.

I want to display current time, I have tried with "time.h":

int main(void){
 time_t now; 
 time(&now); 
 printf("(percent)s", ctime(&now)); 
}

but it seems this compiler and hardware won't support time.h. I know that all AVRs don't have a RTC/calendar in them.

Is there any other way to display current time. I am very new to programming micro controllers. Can any one help me with some examples.

I don't want to add external hardware to the existing board now. I am displaying information on the PC (hyperterminal) using USART communication. Is there any possibility that I can get current time from PC? if there can you explain about it a little bit?

\$\endgroup\$
4
  • \$\begingroup\$ How are you interfacing with your display (LCD? LED?)? Does your microcontroller have an RTC? \$\endgroup\$
    – Renan
    Commented Nov 27, 2012 at 16:19
  • \$\begingroup\$ We could do with a "microcontrollers" stack exchange.. \$\endgroup\$
    – pjc50
    Commented Nov 27, 2012 at 16:34
  • 1
    \$\begingroup\$ @pjc50 Actually, huge impetus for an Arduino stack exchange, but the last such proposal on Area51 was scrapped because it was felt that this would cannibalize the EE.SE traffic. Might still be a good thing, given that a large chunk of EE.SE questions are Arduino based (not this one) \$\endgroup\$ Commented Nov 27, 2012 at 16:42
  • 5
    \$\begingroup\$ @LeonHeller Your definition of "On topic" is much narrower than the site's scope as defined in the FAQ. From the FAQ "the writing of firmware for bare-metal or RTOS applications" is on-topic, and since he's talking about AVRs, this clearly falls under that category. \$\endgroup\$
    – W5VO
    Commented Nov 27, 2012 at 23:01

4 Answers 4

7
\$\begingroup\$

No ATmega32 AVR has an RTC/calendar built into it AFAICS.

Assuming that the reference is to an external battery backed RTC on the AVR board, there isn't any source for the current time on a board without an RTC chip. At best, the bootloader you use with the microcontroller may keep data on many milliseconds have elapsed since the board was powered on - within the tolerance of the clock source and allowing for any occasional missed timer interrupts.

If you do have an RTC chip configured on the board, with the correct time set up on it, there might be a suitable RTC library for that chip. Such a library would typically provide hour(), minute(), second() and other time retrieval methods for your code to use.

The generic time.h C header does not ship with, and therefore is not supported by, AVRdude or Arduino, apparently.

There is a good tutorial on using AdaFruit's RTC breakout board, here.

\$\endgroup\$
2
  • \$\begingroup\$ I don't want to add external hardware to the existing board now. I am displaying information on the PC(hyperterminal) using USART communication. Is there any possibility that i can get current time from PC? if there can you explain about it little bit. \$\endgroup\$
    – verendra
    Commented Nov 28, 2012 at 9:17
  • \$\begingroup\$ @verendra You could always use serial communications from the PC to send down the current time to the board, and read it from there. Serial communication is actually a separate question, and it might merit a quick search to find earlier questions addressing that. \$\endgroup\$ Commented Nov 28, 2012 at 9:29
1
\$\begingroup\$

This is not only a question of a built-in RTC or not. time.h is part of the C standard library (e.g. glibc), which is not completely available on AVR (or any other MCU). it would require a proper operating system to be fully supported. In the case of time handling, one needs for example proper timezone handling. And a CSV file containing data for a single timezone (e.g. UTC) is about 10k in size - and a proper C library would need to know about all timezones world wide. Then we talk about 1MB CSV file.

So you need a separate RTC. A good candidate would be the DS1307, for which there are several breakouts available. A good tutorial of how to interface it can be found here.

\$\endgroup\$
3
  • \$\begingroup\$ I don't want to add external hardware to the existing board now. I am displaying information on the PC(hyperterminal) using USART communication. Is there any possibility that i can get current time from PC? if there can you explain about it little bit. \$\endgroup\$
    – verendra
    Commented Nov 28, 2012 at 9:16
  • \$\begingroup\$ As said above: What you can do is to have the ATMega ask for the time via UART, parse it and store it. You can run a timer which handle the clock tick (one per second) within the ATMega, and handle the time-keeping there. But this means time is lost with power... \$\endgroup\$
    – hli
    Commented Nov 28, 2012 at 11:08
  • \$\begingroup\$ I am new to controller programming so i can't find any help regarding to get time from PC via USART. any suggestions. \$\endgroup\$
    – verendra
    Commented Nov 28, 2012 at 14:44
1
\$\begingroup\$

The ATmega32-A has three build in counters, and you can configure TIMER1 to function like an RTC (this is probably possible with the other counters too). There are several things you need to do to set this up and ensure error free operation:

  1. Configure TIMER1 to generate an interrupt every second by adjusting the clock prescaler and count compare value. This will require TIMER1 to be in "Clear Timer on Compare Match" mode, and you will need to enable the generating of interrupts on a compare match.

  2. Add your Interrupt Service Routine (ISR) to the interrupt vector table, enable global interrupts, and disable interrupt nesting.

  3. The ISR that services the interrupt you generate should increment a space in memory that you reserve to keep track of seconds. Depending on the application, you might want to make this space 1 or more words wide. In your C code, make sure this space in declared to be volatile:

    volatile unsigned long sec_count;

  4. Finally, at start up, you need to initialize the space in memory you reserve with the current time since some event (normally the UNIX epoch) in seconds. You already have USART setup, so you could get the initial value from your computer, or, alternatively, you could have the user input the initial value.

After you have the initial value, your microcontroller will be able to keep track of time independently. I would write utility functions, like day() or hour(), that convert your "seconds since a certain event" into wall clock time.

The manual will help a lot with all of this: http://www.atmel.com/Images/doc8155.pdf

\$\endgroup\$
0
\$\begingroup\$

time.h is not available in your case.

You will need to read the AVR's documentation on how to use the RTC, or use an external RTC if your device doesn't have it.

You suggest to use the time received from the PC to set the clock; this could work, but without RTC it might start drifting and you will need a way to keep it updated (e.g. an interrupt every second).

\$\endgroup\$
1
  • \$\begingroup\$ I am displaying data in hyperteminal using USART communication. \$\endgroup\$
    – verendra
    Commented Nov 28, 2012 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.