Wixel SDK
time.c
1 /* \file time.c
2  *
3  * This is the source file for the time component of <code>wixel.lib</code>.
4  * For information on how to use these functions, see time.h.
5  */
6 
7 #include <cc2511_map.h>
8 #include <cc2511_types.h>
9 #include <time.h>
10 
11 PDATA volatile uint32 timeMs;
12 
13 ISR(T4, 0)
14 {
15  timeMs++;
16  // T4CC0 ^= 1; // If we do this, then on average the interrupts will occur precisely 1.000 ms apart.
17 }
18 
20 {
21  uint8 oldT4IE = T4IE; // store state of timer 4 interrupt (active/inactive?)
22  uint32 time;
23  T4IE = 0; // disable timer 4 interrupt
24  time = timeMs; // copy millisecond timer count into a safe variable
25  T4IE = oldT4IE; // restore timer 4 interrupt to its original state
26  return time; // return timer count copy
27 }
28 
29 void timeInit()
30 {
31  T4CC0 = 187;
32  T4IE = 1; // Enable Timer 4 interrupt. (IEN1.T4IE=1)
33 
34  // DIV=111: 1:128 prescaler
35  // START=1: Start the timer
36  // OVFIM=1: Enable the overflow interrupt.
37  // MODE=10: Modulo
38  T4CTL = 0b11111010;
39 
40  EA = 1; // Globally enable interrupts (IEN0.EA=1).
41 }
42 
43 void delayMs(uint16 milliseconds)
44 {
45  // TODO: make this more accurate.
46  // A great way would be to use the compare feature of Timer 4 and then
47  // wait for the right number of compare events to happen, but then we
48  // can't use that channel for PWM in the future.
49  while(milliseconds--)
50  {
51  delayMicroseconds(250);
52  delayMicroseconds(250);
53  delayMicroseconds(250);
54  delayMicroseconds(249); // there's some overhead, so only delay by 249 here
55  }
56 }
#define PDATA
Definition: cc2511_types.h:58
void delayMs(uint16 milliseconds)
Definition: time.c:43
unsigned long uint32
Definition: cc2511_types.h:21
#define ISR(source, bank)
Definition: cc2511_map.h:71
unsigned short uint16
Definition: cc2511_types.h:15
void delayMicroseconds(uint8 microseconds)
unsigned char uint8
Definition: cc2511_types.h:9
uint32 getMs()
Definition: time.c:19
void timeInit()
Definition: time.c:29