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. --- i2c.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'i2c.c') 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); } -- cgit v1.2.3