diff options
| -rw-r--r-- | accel.c | 2 | ||||
| -rw-r--r-- | common.h | 4 | ||||
| -rw-r--r-- | i2c.c | 15 | ||||
| -rw-r--r-- | main.c | 12 | ||||
| -rw-r--r-- | uart.c | 15 | 
5 files changed, 40 insertions, 8 deletions
@@ -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)) { @@ -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 (); \ @@ -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; @@ -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 () { @@ -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);  | 
