timer.c

Go to the documentation of this file.
00001 /*
00002  ####        # 
00003  #           #
00004  ## ### #  # #
00005  #  #-# #\ # #
00006  #  # # # \# ####
00007  Author: Felipe de Andrade Neves Lavratti
00008 
00009  Copyright: There are no restrictions. Use as you want.   
00010 */
00011 
00012 /*
00013         LPC 2478
00014         + TFT DISPLAY
00015         + TOUCH PANNEL
00016         + SWIM
00017         + SDRAM
00018 
00019         Get more at: selivre.wordpress.com
00020 */
00021 #include "timer.h"
00022 
00023 void T0Init(int ms)
00024 {
00025         T0TCR = 0;
00026         T0TCR = 2;
00027         T0PR = (CPU_CLOCK_HZ*ms/4000) - 1;
00028         T0TCR = 0;
00029 }
00030 
00031 void T0Stop()
00032 {
00033         T0TCR = 0;
00034 }
00035 void T0Start()
00036 {
00037         T0TCR = 1;
00038 }
00039 
00040 int T0Get()
00041 {
00042         return T0TC;
00043 }
00044 
00045 void delayMs(unsigned int delayInMs)
00046 {
00047         /*
00048         * setup timer #1 for delay
00049         */
00050         T1TCR = 0x02;           /* reset timer */
00051         T1PR  = (CPU_CLOCK_HZ/4000 -1);         /* set prescaler to zero */
00052         T1MR0 = delayInMs;
00053         T1IR  = 0xff;           /* reset all interrrupts */
00054         T1MCR = 0x04;           /* stop timer on match */
00055         T1TCR = 0x01;           /* start timer */
00056 
00057         /* wait until delay time has elapsed */
00058         while (T1TCR & 0x01);
00059   return;
00060 }
00061 
00062 void delayUs(unsigned int delayInUs)
00063 {
00064         /*
00065         * setup timer #1 for delay
00066         */
00067         T2TCR = 0x02;           /* reset timer */
00068         T2PR  = (CPU_CLOCK_HZ/4000000 -1);
00069         T2MR0 = delayInUs;
00070         T2IR  = 0xff;           /* reset all interrrupts */
00071         T2MCR = 0x04;           /* stop timer on match */
00072         T2TCR = 0x01;           /* start timer */
00073 
00074         /* wait until delay time has elapsed */
00075         while (T2TCR & 0x01);
00076   return;
00077 }
00078 
00079 int timeoutMs(unsigned int delayInMs, unsigned int condition)
00080 {
00081         switch(condition)
00082         {
00083         case START:
00084                 T1TCR = 0x02;           /* reset timer */
00085                 T1PR  = (CPU_CLOCK_HZ/4000 -1);
00086                 T1MR0 = delayInMs;
00087                 T1IR  = 0xff;           /* reset all interrrupts */
00088                 T1MCR = 0x04;           /* stop timer on match */
00089                 T1TCR = 0x01;           /* start timer */
00090                 break;
00091 
00092         case CHECK_IF_MATCH:
00093                 if (T1TCR&0x01) return NOT_MATCH;
00094                 else return MATCH;
00095 
00096         }
00097 
00098         return 1;
00099 }
00100 
00101 void delay_1us(int t)   //configurar ini_timer(1000000);
00102 {
00103      unsigned int tf;
00104      tf = T0TC + t;
00105      while(tf != T0TC);
00106 }
00107 
00108 
00109 
00110