aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--accel.c2
-rw-r--r--common.h4
-rw-r--r--i2c.c15
-rw-r--r--main.c12
-rw-r--r--uart.c15
5 files changed, 40 insertions, 8 deletions
diff --git a/accel.c b/accel.c
index 670121e..b58f7f7 100644
--- a/accel.c
+++ b/accel.c
@@ -67,7 +67,7 @@ bool accelProcess () {
reading = false;
}
} else {
- if (/*drdy &&*/ twr.status != TWST_WAIT) {
+ if (drdy && twr.status != TWST_WAIT) {
/* new data available in device buffer and bus is free, we are
* reading the registers inbetween out_x/y/z and ignore them */
if (!twRequest (TWM_READ, LIS302DL, LIS302DL_UNUSED1, (uint8_t *) val, 6)) {
diff --git a/common.h b/common.h
index ecf2c27..eba8d6e 100644
--- a/common.h
+++ b/common.h
@@ -1,8 +1,8 @@
#ifndef COMMON_H
#define COMMON_H
-/* cpu runs at 1mhz */
-#define F_CPU 1000000
+/* cpu runs at n mhz */
+#define F_CPU 8000000
#define sleepwhile(cond) \
sleep_enable (); \
diff --git a/i2c.c b/i2c.c
index 57ee551..7855e88 100644
--- a/i2c.c
+++ b/i2c.c
@@ -3,6 +3,7 @@
#include <avr/interrupt.h>
#include "i2c.h"
+#include "common.h"
volatile twReq twr;
@@ -42,9 +43,21 @@ static bool twWriteRaw (const uint8_t data) {
}
void twInit () {
- /* set scl to 3.6 kHz (at 1Mhz CPU speed)*/
+#if F_CPU == 1000000
+ /* set scl to 3.6 kHz */
TWBR = 2;
TWSR |= 0x3; /* set prescaler to 64 */
+#elif F_CPU == 4000000
+ /* set scl to 50 kHz ? */
+ TWBR = 32;
+ TWSR |= 0x0; /* set prescaler to 0 */
+#elif F_CPU == 8000000
+ /* set scl to 100 kHz */
+ TWBR = 32;
+ TWSR |= 0x0; /* set prescaler to 0 */
+#else
+#error "cpu speed not supported"
+#endif
twr.mode = TWM_INVALID;
twr.status = TWST_ERR;
diff --git a/main.c b/main.c
index 372c070..9c35e99 100644
--- a/main.c
+++ b/main.c
@@ -28,9 +28,17 @@ static void ledShow (const unsigned char val) {
static void cpuInit () {
/* enter change prescaler mode */
- CLKPR = CLKPCE << 1;
- /* write new prescaler = 8 (i.e. 1Mhz clock frequency) */
+ CLKPR = (1 << CLKPCE);
+ /* write new prescaler */
+#if F_CPU == 1000000
CLKPR = 0b00000011;
+#elif F_CPU == 4000000
+ CLKPR = 0b00000001;
+#elif F_CPU == 8000000
+ CLKPR = 0b00000000;
+#else
+#error "cpu speed not supported"
+#endif
}
int main () {
diff --git a/uart.c b/uart.c
index 4b7ca39..3528a22 100644
--- a/uart.c
+++ b/uart.c
@@ -2,6 +2,7 @@
#include <avr/io.h>
#include "uart.h"
+#include "common.h"
/* blocking uart send
*/
@@ -23,13 +24,23 @@ static int uartPutc (char c, FILE *stream) {
static FILE mystdout = FDEV_SETUP_STREAM (uartPutc, NULL, _FDEV_SETUP_WRITE);
void uartInit () {
- /* Set baud rate (9600, double speed, at 1mhz) */
UBRR0H = 0;
+#if F_CPU == 1000000
+ /* Set baud rate (9600, double speed) */
UBRR0L = 12;
+#elif F_CPU == 4000000
+ /* Set baud rate (9600, double speed) */
+ UBRR0L = 51;
+#elif F_CPU == 8000000
+ /* baudrate 38.4k */
+ UBRR0L = 25;
+#else
+#error "cpu speed not supported"
+#endif
/* enable double speed mode */
UCSR0A = (1 << U2X0);
/* Enable receiver and transmitter */
- UCSR0B = (1<<RXEN0)|(1<<TXEN0);
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0);
/* Set frame format: 8 data, 1 stop bit, even parity */
UCSR0C = (1<<UPM01) | (0 << UPM00) | (0<<USBS0)|(3<<UCSZ00);