From 4ebf0dc69153c1f715f934a7abce4fdefbc4453f Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Thu, 30 Jan 2014 14:31:34 +0100 Subject: Initial import --- main.c | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..e5a1056 --- /dev/null +++ b/main.c @@ -0,0 +1,359 @@ +/* cpu freq */ +#define F_CPU 1000000 + +#include +#include +#include + +/* cpu runs at 1mhz */ + +/* i2c device addresses */ +#define L2GD20 0b11010100 +#define L2GD20_WHOAMI 0xf +#define L2GD20_CTRLREG1 0x20 +#define LIS302DL 0b00111000 +#define LIS302DL_WHOAMI 0xf + +/* i2c constants */ +#define TW_READ 1 +#define TW_WRITE 0 +#define TW_STATUS (TWSR & 0xF8) + +void twStart () { + /* disable stop, reset twint, enable start, enable i2c */ + TWCR = (TWCR & ~(1 << TWSTO)) | (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); +} + +void twStop () { + /* disable start, reset twint, enable stop, enable i2c */ + TWCR = (TWCR & ~(1 << TWSTA)) | (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); +} + +void twFlush () { + /* disable start/stop, reset twint, enable i2c */ + TWCR = (TWCR & ~((1 << TWSTA) | (1 << TWSTO))) | (1 << TWINT) | (1 << TWEN); +} + +void twWait () { + while (!(TWCR & (1 << TWINT))); +} + +void twInit () { + /* set scl to 3.6 kHz (at 1Mhz CPU speed)*/ + TWBR = 2; + TWSR |= 0x3; /* set prescaler to 64 */ +} + +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 */ +void ledShow (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); +} + +void uartInit () { + /* Set baud rate (9600, double speed, at 1mhz) */ + UBRR0H = 0; + UBRR0L = 12; + /* enable double speed mode */ + UCSR0A = (1 << U2X0); + /* Enable receiver and transmitter */ + UCSR0B = (1<