blob: f01710966e3b7336082ee81a8e8e10dc45ceded9 (
plain)
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
|
/* cpu runs at 1mhz */
#define F_CPU 1000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdbool.h>
#include "i2c.h"
#include "uart.h"
#include "timer.h"
#include "gyroscope.h"
static void ledInit () {
/* set led1,led2 to output */
DDRB |= (1 << PB6) | (1 << PB7);
/* set led3,led4,led5,led6 to output */
DDRD |= (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5);
}
/* show data with leds */
static void ledShow (const unsigned char val) {
PORTB = (PORTB & ~((1 << PB6) | (1 << PB7))) | ((val & 0x3) << PB6);
PORTD = (PORTD & ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5))) | (((val >> 2) & 0xf) << PD2);
}
static void cpuInit () {
/* enter change prescaler mode */
CLKPR = CLKPCE << 1;
/* write new prescaler = 8 (i.e. 1Mhz clock frequency) */
CLKPR = 0b00000011;
}
#define sleepwhile(cond) \
sleep_enable (); \
while (cond) { sleep_cpu (); } \
sleep_disable ();
int main () {
cpuInit ();
ledInit ();
twInit ();
uartInit ();
gyroscopeInit ();
set_sleep_mode (SLEEP_MODE_IDLE);
printf ("initialization done\n");
/* global interrupt enable */
sei ();
gyroscopeStart ();
//timerStart ();
while (1) {
//sleepwhile (!timerHit ());
//printf ("running for %u seconds\n", seconds);
sleepwhile (!gyroscopeRead ());
sleepwhile (twr.status == TWST_WAIT);
switch (twr.status) {
case TWST_OK: {
volatile const int16_t *val = gyroscopeGet ();
printf ("%i/%i/%i\n", val[0], val[1], val[2]);
break;
}
case TWST_ERR:
goto fail;
break;
}
}
//timerStop ();
fail:
printf ("fail\n");
/* global interrupt disable */
cli ();
while (1);
}
|