/* cpu freq */ #define F_CPU 1000000 #include #include #include #include /* cpu runs at 1mhz */ /* i2c device addresses */ #define L3GD20 0b11010100 #define L3GD20_WHOAMI 0xf #define L3GD20_CTRLREG1 0x20 #define LIS302DL 0b00111000 #define LIS302DL_WHOAMI 0xf #define LIS302DL_CTRLREG1 0x20 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<