-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimerA.c
115 lines (86 loc) · 2.16 KB
/
timerA.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
#include "timerA.h"
#include "LEDDisplay.h"
#define NUMBER_OF_SAMPLES 8
extern volatile int samples_x[];
extern volatile int samples_y[];
extern volatile int samples_z[];
extern volatile int CurrentSample;
extern volatile unsigned int CurrentSampleIndex;
extern int gyro [];
extern int level [];
extern int period;
extern int x;
extern int y;
extern int z;
void ConfigureTimerA(void)
{
//Stopping and clearing
TA0CTL = (MC_0 | TACLR);
TA0CTL |= (TASSEL_2 | MC_1);
TA0CTL |= ID_0;
TA1CTL = (MC_0 | TACLR);
TA1CTL |= (TASSEL_2 | MC_1);
TA1CTL |= ID_0;
//TA0CTL |= TAIE;
TA0CCR0 = period;
TA0CCTL0 |= CCIE;
TA1CCR0 = 16000;
TA1CCTL0 |= CCIE;
}
//controls the length of period
//once 1 pwm cycle ends, change CCR1 (up or down)
#pragma vector = TIMER1_A0_VECTOR
// Interrupt service routine for CCIFG0
__interrupt void Timer1_A0_routine(void)
{
ADC10CTL0 &= ~ENC;
ADC10SA = (unsigned int) gyro;
ADC10CTL0 |= ENC;
ADC10CTL0 |= ADC10SC; // Start sampling and conversion
while ((ADC10CTL1 & ADC10BUSY)) {}
if (CurrentSampleIndex > NUMBER_OF_SAMPLES - 1) CurrentSampleIndex = 0;
samples_x[CurrentSampleIndex] = gyro[2];
samples_y[CurrentSampleIndex] = gyro[1];
samples_z[CurrentSampleIndex] = gyro[0];
CurrentSampleIndex++;
filter();
}
//controls the length of each duty cycle and clock
#pragma vector = TIMER0_A0_VECTOR
// Interrupt service routine for CCIFG1 and TAIFG
__interrupt void Timer0_A0_routine(void)
{
//this controls the brightness of 8 different LEDs
//each led has a level of brightness and a name
// for each LED, check its level and compare if the clock "cycle" is greater/less than the value. if less, stay on. if greater, turn off.
volatile unsigned char LED = 0x00;
static int cycle = 0;
if(cycle >= 200)
{
cycle = 0;
}
int i = 0;
for (; i < 8; i++) {
if (cycle < level[i])
LED |= (0x01 << i);
}
updateDisplay(LED);
cycle++;
}
void filter() //can be optimized
{
//iterate over circular buffer
int sum_x = 0;
int sum_y = 0;
int sum_z = 0;
int i = 0;
while (i < NUMBER_OF_SAMPLES) {
sum_x += samples_x[i];
sum_y += samples_y[i];
sum_z += samples_z[i];
i++;
}
x = sum_x >> 3;
y = sum_y >> 3;
z = sum_z >> 3;
}