Timer and Timers

Document Sample
Timer and Timers Powered By Docstoc
					Timer and Timers                                                                        http://dev.emcelettronica.com/print/51806




                        Your Electronics Open Source
          (http://dev.emcelettronica.com)
          Home > Blog > allankliu's blog > Content




          Timer and Timers
          By allankliu
          Created 10/06/2008 - 02:35

          BLOG Microcontrollers

          I was a marketing engineer for Philips microcontroller. To offer training courses for our customers is
          included in my job description. I still remember a good question from one of my customers. It
          demonstrates how our universities have misguided our students.

                                                                                 The question is how to find
                                                                                 extra timers in a complex
                                                                                 application while a standard
                                                                                 microcontroller usually has
                                                                                 only two or three timers? The
                                                                                 answer is simple. You have to
                                                                                 create the additional timers in
                                                                                 software. Yes, software timers.
                                                                                 You will know that the timer
                                                                                 takes a quite big area in the
                                                                                 silicon when you look at the
                                                                                 silicon picture and find the
                                                                                 timer part. This is a cross
                                                                                 section view of a Cypress
                                                                                 microcontroller. The reason
                                                                                 why a microcontroller can not
                                                                                 have too many timers is that it
                                                                                 takes silicon space and money.
          BTW, have you saw a lot of real time timers in a computer? No, you can only see one real time timer
          with a battery. All other timers are software timers implemented in software. After all, software timers
          should be implemented in the system. But how to implement software timers with one hardware
          timer?

          First we should use the hardware timer as the base timer, which offers time base on every given
          period. For example, the hardware timer can generate timer interrupt every 16ms. Then we can
          derivate software timers for 32ms, 64ms, 128ms, and 256ms. The software timer of a second can be
          4 interrupts of 256ms. You can adjust timer accuracy if you want. But above method is enough for an
          embedded system, since most of the applications do not require high accurate timers. If you want a
          real-time timer, please use a dedicated IC, instead of a software timer, because the external real
          timer can offer higher accuracy with temperature compensation for crystal temperature drift. And
          please do not use the watch dog timer as your time base. Usually it is reserved to save the whole
          system in case the system is out of order.

          Here is a fraction of C source for your reference. You can create many timers provided you have
          enough RAM. The design of software timers are: base timer, timer ISR, and application software
          timers. The 0 of an application software timer stands for inactive and 1 stands for timeout, and any
          other numbers stand for running of a software timer. The timers are count down counters.




1 din 2                                                                                                        11.06.2008 21:45
Timer and Timers                                                                   http://dev.emcelettronica.com/print/51806



            timer_isr.c
            #define OSTI_TIMER_EXPIRED 1            /* The timer has been expired */
            #define OSTI_TIMER_INACTIVE 0           /* The timer is inactive and will not be updated */
            #pragma registerbank(1)
            void USING(1,OSTI_Update_expiring_timer(Byte IDATA *timer_ptr))
            {
                if ( *timer_ptr > OSTI_TIMER_EXPIRED)
                {
                    (*timer_ptr)--;
                }
            }
            /*
                This function is called by assembly code every 128ms.
                The assembly code is machine dependent, not included here.
            */
            void USING(1, OSTI_128ms(void))
            {
                UIBG_128_ms_timer();
            }
            uibg.c
            /*
                This function will update all software timers for application purpose.
                Here, two timers for menu update, refresh whole display is triggered.
            */
            void USING(1,UIBG_128_ms_timer(void))
            {
                OSTI_Update_expiring_timer(&uimn_menu_timer);
                OSTI_Update_expiring_timer(&refresh_display_timer);
            }



          If you are using a RTOS, you can save your effort to manage the timers, a RTOS usually has such
          feature already.




                                                         Trademarks



          Source URL: http://dev.emcelettronica.com/timer-and-timers




2 din 2                                                                                                   11.06.2008 21:45

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:314
posted:6/11/2008
language:English
pages:2
Description: The question is how to find extra timers in a complex application while a standard microcontroller usually has only two or three timers The answer is simple. You have to create the additional timers in software. Yes, software timers. You will know that the timer takes a quite big area in the silicon when you look at the silicon picture and find the timer part.
About