-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
165 lines (134 loc) · 3.92 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef TIMER
#define TIMER
#include <stdlib.h>
#include <stdio.h>
#include <p89lpc9351.h>
// include .c files, because crosside gets mad a .h //
// pins should be included in every file
// utilities should probably be included in every file
#include "pins.c"
//We want timer 0 to interrupt every 100 microseconds ((1/10000Hz)=100 us)
#define TIMER0_FREQ 10000L // don't change this from 10000L
//16 bit timer, counting upwards
#define TIMER0_RELOAD_VALUE (65536L-((XTAL)/(2*TIMER0_FREQ)))
//We want timer 1 to interrupt every 10 milliseconds ((1/100Hz)=10 ms)
#define TIMER1_FREQ 100L // don't change this from 100L
//16 bit timer, counting upwards
#define TIMER1_RELOAD_VALUE (65536L-((XTAL)/(2*TIMER1_FREQ)))
//These variables are used in the ISR
volatile unsigned char drive_right_speed;
volatile unsigned char drive_left_speed;
// count timer ticks
volatile unsigned char pwm_ticks = 0;
volatile unsigned t1_ticks = 0;
// timer0_init()
// setup the timer0 and begin running it
void Timer0Start();
// millis()
// gets the number of milliseconds since last reset
long millis();
// reset_millis()
// reset the lap timer
void reset_millis();
// freeze_millis()
// freeze the lap timer
void freeze_millis();
// unfreeze_millis()
// restart the lap timer from its current value
void unfreeze_millis();
// timer0_init()
// setup the timer0 and begin running it
void Timer0Start (void)
{
// Initialize timer 0 for ISR 'pwmcounter' below
TR0=0; // Stop timer 0
TF0=0; // Clear the overflow flag
TMOD=(TMOD&0xf0)|0x01; // 16-bit timer
// load a value into timer0 and restart
TF0=0; // Clear the overflow flag
TR0=0; // Stop timer 0
// load the timer
TH0=TIMER0_RELOAD_VALUE/0x100; // upper8 bits
TL0=TIMER0_RELOAD_VALUE%0x100;
TR0=1; // Start timer 0
ET0=1; // Enable timer 0 interrupt
EA=1; // Enable global interrupts
}
// crosside got made when I tried to make this a prototype
// timer0_event()
// called whenever the timer overflows
// the time is determind by the frequency defined at the top of this file
void Timer0Tick (void) interrupt 1 using 1
{
// load a value into timer0 and restart
TF0=0; // Clear the overflow flag
TR0=0; // Stop timer 0
// load the timer
TH0=TIMER0_RELOAD_VALUE/0x100; // upper8 bits
TL0=TIMER0_RELOAD_VALUE%0x100;
TR0=1; // Start timer 0
// only count 100 ticks before setting to zero
// (arbitrary, but should be multiple of 10 and 100)
if(++pwm_ticks>99) pwm_ticks = 0;
// turn the motors on for the fraction of time specified by
// X_pwm, as a fraction of 100
left_wheel = (drive_left_speed > pwm_ticks)?1:0;
right_wheel = (drive_right_speed > pwm_ticks)?1:0;
}
// timer1_init()
// setup the timer0 and begin running it
void Timer1Start (void)
{
// Initialize timer 0 for ISR 'pwmcounter' below
TR1=0; // Stop timer 0
TF1=0; // Clear the overflow flag
TMOD=(TMOD&0x0f)|0x10; // 16-bit timer
// load a value into timer0 and restart
TF1=0; // Clear the overflow flag
TR1=0; // Stop timer 0
// load the timer
TH1=TIMER1_RELOAD_VALUE/0x100; // upper8 bits
TL1=TIMER1_RELOAD_VALUE%0x100;
TR1=1; // Start timer 0
ET1=1; // Enable timer 1 interrupt
EA=1; // Enable global interrupts
}
// timer0_event()
// called whenever the timer overflows
// the time is determind by the frequency defined at the top of this file
void Timer1Tick (void) interrupt 3 using 3
{
// load a value into timer0 and restart
TF1=0; // Clear the overflow flag
TR1=0; // Stop timer 0
// load the timer
TH1=TIMER1_RELOAD_VALUE/0x100; // upper8 bits
TL1=TIMER1_RELOAD_VALUE%0x100;
TR1=1; // Start timer 0
t1_ticks++;
}
// millis()
// gets the number of milliseconds since last reset
long millis()
{
return t1_ticks*10.0;
}
// reset_time()
// reset the global tenths and hundredths counters
void reset_millis()
{
t1_ticks = 0;
}
// freeze_millis()
// freeze the lap timer
void freeze_millis()
{
TR1 = 0; // stop timer1
}
// unfreeze_millis()
// restart the lap timer from its current value
void unfreeze_millis()
{
TR1 = 1;
}
#endif