From 92ec11b505a5b68c33e6158dbc68cea1d23cbd35 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 10 Feb 2015 12:33:58 +0100 Subject: Reduce CPU frequency Also reduces PWM frequency. The speakers resonance frequency is 2kHz, with 1MHz clock speed and prescaler 256 we get pretty close. --- accel.c | 28 +++++++++++++--------------- common.h | 2 +- gyro.c | 17 ++++++++--------- i2c.c | 19 ++++++++++++------- pwm.c | 4 ++-- timer.c | 28 +++++++++++++++++----------- 6 files changed, 53 insertions(+), 45 deletions(-) diff --git a/accel.c b/accel.c index 9f38e8e..77126d4 100644 --- a/accel.c +++ b/accel.c @@ -163,22 +163,20 @@ bool accelProcess () { if (reading && shouldWakeup (WAKE_I2C)) { disableWakeup (WAKE_I2C); reading = false; - if (twr.status == TWST_OK) { - accelProcessHorizon (); - - /* calculate normalized z (i.e. without earth gravity component) */ - if (horizonSign == HORIZON_NEG) { - zvalnormal = zval - (-ACCEL_1G); - } else if (horizonSign == HORIZON_POS) { - zvalnormal = zval - ACCEL_1G; - } - - accelProcessShake (); - /* new data transfered */ - return true; - } else if (twr.status == TWST_ERR) { - assert (0); + /* the bus might be in use again already */ + //assert (twr.status == TWST_OK); + accelProcessHorizon (); + + /* calculate normalized z (i.e. without earth gravity component) */ + if (horizonSign == HORIZON_NEG) { + zvalnormal = zval - (-ACCEL_1G); + } else if (horizonSign == HORIZON_POS) { + zvalnormal = zval - ACCEL_1G; } + + accelProcessShake (); + /* new data transfered */ + return true; } else { if (shouldWakeup (WAKE_ACCEL) && twr.status == TWST_OK) { /* new data available in device buffer and bus is free */ diff --git a/common.h b/common.h index 42737eb..5730f47 100644 --- a/common.h +++ b/common.h @@ -2,7 +2,7 @@ #define COMMON_H /* cpu runs at n mhz */ -#define F_CPU 8000000 +#define F_CPU 1000000 #define sleepwhile(cond) \ sleep_enable (); \ diff --git a/gyro.c b/gyro.c index 3079c20..5b32314 100644 --- a/gyro.c +++ b/gyro.c @@ -136,16 +136,15 @@ bool gyroProcess () { if (shouldWakeup (WAKE_I2C)) { disableWakeup (WAKE_I2C); state = IDLE; - assert (twr.status != TWST_ERR); - if (twr.status == TWST_OK) { - /* new data transfered, process it */ - /* poor man's noise filter */ - if (abs (zval) > 64) { - zaccum += zval; - } - gyroProcessTicks (); - return true; + /* the bus might be in use again already */ + //assert (twr.status == TWST_OK); + /* new data transfered, process it */ + /* poor man's noise filter */ + if (abs (zval) > 64) { + zaccum += zval; } + gyroProcessTicks (); + return true; } break; diff --git a/i2c.c b/i2c.c index 54e1f29..5505632 100644 --- a/i2c.c +++ b/i2c.c @@ -47,17 +47,20 @@ static bool twWriteRaw (const uint8_t data) { void twInit () { #if F_CPU == 1000000 - /* set scl to 3.6 kHz */ + /* set scl to 50 kHz */ TWBR = 2; - TWSR |= 0x3; /* set prescaler to 64 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #elif F_CPU == 4000000 /* set scl to 50 kHz ? */ TWBR = 32; - TWSR |= 0x0; /* set prescaler to 0 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #elif F_CPU == 8000000 /* set scl to 100 kHz */ TWBR = 32; - TWSR |= 0x0; /* set prescaler to 0 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #else #error "cpu speed not supported" #endif @@ -135,6 +138,8 @@ static void twIntWrite () { if (TW_STATUS == TW_MT_DATA_ACK) { twStopRaw (); twr.status = TWST_OK; + enableWakeup (WAKE_I2C); + ++twr.step; } else { twr.status = TWST_ERR; } @@ -232,6 +237,8 @@ static void twIntRead () { if (status == TW_MR_DATA_NACK) { twStopRaw (); twr.status = TWST_OK; + enableWakeup (WAKE_I2C); + ++twr.step; } else { twr.status = TWST_ERR; twr.error = status; @@ -258,8 +265,6 @@ ISR(TWI_vect) { assert (0 && "nope\n"); break; } - if (twr.status == TWST_ERR || twr.status == TWST_OK) { - enableWakeup (WAKE_I2C); - } + assert (twr.status != TWST_ERR); } diff --git a/pwm.c b/pwm.c index 250e489..0c9e191 100644 --- a/pwm.c +++ b/pwm.c @@ -79,8 +79,8 @@ void pwmStart () { TIMSK0 = (1 << OCIE0A); /* compare value */ OCR0A = 1; - /* io clock with prescaler 64; ctc (part 2) */ - TCCR0B = (1 << CS02) | (0 << CS01) | (1 << CS00); + /* io clock with prescaler 256; ctc (part 2) */ + TCCR0B = (1 << CS02) | (0 << CS01) | (0 << CS00); } void pwmStop () { diff --git a/timer.c b/timer.c index 0ca3586..bd2ed1e 100644 --- a/timer.c +++ b/timer.c @@ -5,10 +5,22 @@ #include "timer.h" -/* useconds per clock tick */ +/* prescaler is 1024 */ +#define PRESCALER ((1 << CS12) | (1 << CS10)) + +#if F_CPU == 1000000 +/* with divider 1024 each tick is 1024us */ +#define US_PER_TICK 1024 +#define MAX_US ((uint32_t) 67107840) +#elif F_CPU == 8000000 +/* with divider 1024 each clock tick is 128us with a rounding error of 64us + * per second/4ms per minute, since it’s a 16 bit timer we support up to + * 2**16-1 ticks, which is 8388480us (8s) */ #define US_PER_TICK 128 -/* max useconds with max OCRA1 */ #define MAX_US ((uint32_t) 8388480) +#else +#error "cpu speed not supported" +#endif /* max/current timer hits */ static volatile uint8_t maxhits, hits; @@ -66,10 +78,6 @@ void timerStart (const uint32_t t, const bool once) { /* enable compare match interrupt */ TIMSK1 = (1 << OCIE1A); /* set compare value */ -#if F_CPU == 8000000 - /* with divider 1024 each clock tick is 128us with a rounding error of 64us - * per second/4ms per minute, since it’s a 16 bit timer we support up to - * 2**16-1 ticks, which is 8388480us (8s) */ if (t >= MAX_US) { maxhits = t/MAX_US+1; count = UINT16_MAX; @@ -82,11 +90,9 @@ void timerStart (const uint32_t t, const bool once) { lastcount = tdiv; OCR1A = tdiv; } -#else -#error "cpu speed not supported" -#endif - /* io clock with 1024 prescaler; ctc (part 2) */ - TCCR1B = (1 << CS12) | (1 << CS10) | (1 << WGM12); + + /* prescaler and ctc (part 2) */ + TCCR1B = PRESCALER | (1 << WGM12); } void timerStop () { -- cgit v1.2.3