-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_acceleromter_main.c
More file actions
110 lines (81 loc) · 2.19 KB
/
led_acceleromter_main.c
File metadata and controls
110 lines (81 loc) · 2.19 KB
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
#include "system.h"
#include "altera_up_avalon_accelerometer_spi.h"
#include "altera_avalon_timer_regs.h"
#include "altera_avalon_timer.h"
#include "altera_avalon_pio_regs.h"
#include "sys/alt_irq.h"
#include <stdlib.h>
#include <time.h>
#define OFFSET -32
#define PWM_PERIOD 16
alt_8 pwm = 0;
alt_u8 led;
int level;
int N =5;
double coeff[] = {-0.1196,0.4105,0.7344,0.4105,-0.1196};
void led_write(alt_u8 led_pattern) {
IOWR(LED_BASE, 0, led_pattern);
}
void convert_read(alt_32 acc_read, int * level, alt_u8 * led) {
acc_read += OFFSET;
alt_u8 val = (acc_read >> 6) & 0x07;
* led = (8 >> val) | (8 << (8 - val));
* level = (acc_read >> 1) & 0x1f;
}
void sys_timer_isr() {
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
if (pwm < abs(level)) {
if (level < 0) {
led_write(led << 1);
} else {
led_write(led >> 1);
}
} else {
led_write(led);
}
if (pwm > PWM_PERIOD) {
pwm = 0;
} else {
pwm++;
}
}
void timer_init(void * isr) {
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0003);
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE, 0x0900);
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE, 0x0000);
alt_irq_register(TIMER_IRQ, 0, isr);
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0007);
}
alt_32 filter(double tmp[],double coeff[]){
alt_32 out = 0;
for(int i=0;i<N;i++)
{
out += coeff[i]*tmp[i];
}
return out;
}
int main() {
alt_32 x_read;
alt_up_accelerometer_spi_dev * acc_dev;
acc_dev = alt_up_accelerometer_spi_open_dev("/dev/accelerometer_spi");
if (acc_dev == NULL) { // if return 1, check if the spi ip name is "accelerometer_spi"
return 1;
}
double tmp[N];
for(int i=0;i<N;i++)
{
tmp[i] = 0;
}
timer_init(sys_timer_isr);
while (1) {
alt_up_accelerometer_spi_read_x_axis(acc_dev, & x_read);
for(int i = N-1;i>0;i--){
tmp[i]=tmp[i-1];
}
tmp[0] = x_read;
alt_printf("filter data: %x\n", filter(tmp,coeff));
convert_read(filter(tmp,coeff), & level, & led);
}
return 0;
}