Jason Garner / registers-example

Dependencies:   ARM registers

Committer:
elevatorguy
Date:
Thu Jan 03 02:57:29 2013 +0000
Revision:
3:88da6c0412b0
Parent:
2:e26c096f1946
added Ticker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elevatorguy 2:e26c096f1946 1 #include "Timer.h"
elevatorguy 2:e26c096f1946 2 //This class uses Timer 1
elevatorguy 2:e26c096f1946 3
elevatorguy 2:e26c096f1946 4 // static data initialization (only called once)
elevatorguy 2:e26c096f1946 5 bool Timer::timer1initialized = false;
elevatorguy 3:88da6c0412b0 6 int Timer::resolution = 1000000; //default: microseconds accuracy
elevatorguy 2:e26c096f1946 7
elevatorguy 3:88da6c0412b0 8 void Timer::initTimer1(int res)
elevatorguy 2:e26c096f1946 9 {
elevatorguy 2:e26c096f1946 10 uint8_t pclk;
elevatorguy 2:e26c096f1946 11 uint32_t pclkdiv = (LPC_SC->PCLKSEL0 >> 4) & 0x03; //PCLK for Timer 1 (page 56)
elevatorguy 2:e26c096f1946 12
elevatorguy 2:e26c096f1946 13 switch ( pclkdiv ) // table 42 (page 57 in user manual)
elevatorguy 2:e26c096f1946 14 {
elevatorguy 2:e26c096f1946 15 case 0x00:
elevatorguy 2:e26c096f1946 16 default:
elevatorguy 2:e26c096f1946 17 pclk = 4;
elevatorguy 2:e26c096f1946 18 break;
elevatorguy 2:e26c096f1946 19 case 0x01:
elevatorguy 2:e26c096f1946 20 pclk = 1;
elevatorguy 2:e26c096f1946 21 break;
elevatorguy 2:e26c096f1946 22 case 0x02:
elevatorguy 2:e26c096f1946 23 pclk = 2;
elevatorguy 2:e26c096f1946 24 break;
elevatorguy 2:e26c096f1946 25 case 0x03:
elevatorguy 2:e26c096f1946 26 pclk = 8;
elevatorguy 2:e26c096f1946 27 break;
elevatorguy 2:e26c096f1946 28 }
elevatorguy 2:e26c096f1946 29
elevatorguy 3:88da6c0412b0 30 LPC_TIM1->TCR = 0x02; // reset timer
elevatorguy 2:e26c096f1946 31 LPC_TIM1->PR = (SystemCoreClock / (pclk * res)); //default: microsecond steps
elevatorguy 3:88da6c0412b0 32 LPC_TIM1->MR0 = 2147483647; // highest number a 32bit signed int can store (for us ~ 35.79 minutes, or, for ms ~ 596.52 hours )
elevatorguy 3:88da6c0412b0 33 LPC_TIM1->IR = 0xff; // reset all interrrupts
elevatorguy 3:88da6c0412b0 34 LPC_TIM1->MCR = 0x02; // reset timer on match
elevatorguy 3:88da6c0412b0 35 LPC_TIM1->TCR = 0x01; // start timer
elevatorguy 2:e26c096f1946 36
elevatorguy 3:88da6c0412b0 37 // The timer simply goes on forever! It just resets itself when it hits the max number for TC
elevatorguy 2:e26c096f1946 38 timer1initialized = true;
elevatorguy 2:e26c096f1946 39 resolution = res;
elevatorguy 2:e26c096f1946 40 }
elevatorguy 2:e26c096f1946 41
elevatorguy 2:e26c096f1946 42 Timer::Timer()
elevatorguy 2:e26c096f1946 43 {
elevatorguy 2:e26c096f1946 44 if(!timer1initialized)
elevatorguy 2:e26c096f1946 45 {
elevatorguy 2:e26c096f1946 46 initTimer1(resolution); //default resolution
elevatorguy 2:e26c096f1946 47 }
elevatorguy 2:e26c096f1946 48
elevatorguy 2:e26c096f1946 49 starttime = 0;
elevatorguy 2:e26c096f1946 50 stoptime = 0;
elevatorguy 2:e26c096f1946 51 running = false;
elevatorguy 2:e26c096f1946 52 }
elevatorguy 2:e26c096f1946 53
elevatorguy 2:e26c096f1946 54 //for when we want to allow lower counting resolution, eg. milliseconds
elevatorguy 2:e26c096f1946 55 //so we can count longer than the 35 minutes with microsecond resolution
elevatorguy 2:e26c096f1946 56 Timer::Timer(int res) // millisecond, res = 1000
elevatorguy 2:e26c096f1946 57 {
elevatorguy 2:e26c096f1946 58 if(!timer1initialized)
elevatorguy 2:e26c096f1946 59 {
elevatorguy 2:e26c096f1946 60 initTimer1(res); //custom resolution
elevatorguy 2:e26c096f1946 61 }
elevatorguy 2:e26c096f1946 62
elevatorguy 2:e26c096f1946 63 starttime = 0;
elevatorguy 2:e26c096f1946 64 stoptime = 0;
elevatorguy 2:e26c096f1946 65 running = false;
elevatorguy 2:e26c096f1946 66 }
elevatorguy 2:e26c096f1946 67
elevatorguy 2:e26c096f1946 68 void Timer::start()
elevatorguy 2:e26c096f1946 69 {
elevatorguy 2:e26c096f1946 70 starttime = LPC_TIM1->TC;
elevatorguy 2:e26c096f1946 71 stoptime = 0; //clear previous stoptime
elevatorguy 2:e26c096f1946 72 running = true;
elevatorguy 2:e26c096f1946 73 }
elevatorguy 2:e26c096f1946 74
elevatorguy 2:e26c096f1946 75 void Timer::stop()
elevatorguy 2:e26c096f1946 76 {
elevatorguy 2:e26c096f1946 77 stoptime = LPC_TIM1->TC;
elevatorguy 2:e26c096f1946 78 running = false;
elevatorguy 2:e26c096f1946 79 }
elevatorguy 2:e26c096f1946 80
elevatorguy 2:e26c096f1946 81 void Timer::reset()
elevatorguy 2:e26c096f1946 82 {
elevatorguy 2:e26c096f1946 83 if(running)
elevatorguy 2:e26c096f1946 84 {
elevatorguy 2:e26c096f1946 85 starttime = LPC_TIM1->TC;
elevatorguy 2:e26c096f1946 86 }
elevatorguy 2:e26c096f1946 87 else
elevatorguy 2:e26c096f1946 88 {
elevatorguy 2:e26c096f1946 89 starttime = 0;
elevatorguy 2:e26c096f1946 90 }
elevatorguy 2:e26c096f1946 91
elevatorguy 2:e26c096f1946 92 stoptime = 0;
elevatorguy 2:e26c096f1946 93 }
elevatorguy 2:e26c096f1946 94
elevatorguy 2:e26c096f1946 95 float Timer::read()
elevatorguy 2:e26c096f1946 96 {
elevatorguy 2:e26c096f1946 97 if(running)
elevatorguy 2:e26c096f1946 98 {
elevatorguy 2:e26c096f1946 99 int currenttime = LPC_TIM1->TC;
elevatorguy 2:e26c096f1946 100 return (currenttime - starttime) / (resolution*1.0);
elevatorguy 2:e26c096f1946 101 }
elevatorguy 2:e26c096f1946 102 else // compare startime and stoptime
elevatorguy 2:e26c096f1946 103 {
elevatorguy 2:e26c096f1946 104 return (stoptime - starttime) / (resolution*1.0);
elevatorguy 2:e26c096f1946 105 }
elevatorguy 2:e26c096f1946 106 }
elevatorguy 2:e26c096f1946 107
elevatorguy 2:e26c096f1946 108 Timer::operator float()
elevatorguy 2:e26c096f1946 109 {
elevatorguy 2:e26c096f1946 110 return read();
elevatorguy 2:e26c096f1946 111 }