Zu Kommunikation über I2C benutze ich die Library USI_TWI_Master nach
AppNote AVR310. Hie meine Funktion, um den MMa in den Leveldetection
Mode zu setzen)
uint8_t MMA7455_level(void)
{
uint8_t messageBuf[3];
cli();
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_CTL1; // The first byte is used for
commands.
messageBuf[2] = 0b00010000; //7: DFBW=0 125Khz | 6:THOPT=0 | 5: ZDA=0
| 4: YDA=1 | 3: XDA=0 | INTREG[1:0] =0:0 | 0: INTPIN ???
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_LDTH; // The first byte is used for
commands.
messageBuf[2] = 8; //16=1G
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_CTL2; // The first byte is used for
commands.
messageBuf[2] = 0b00000000; // 2: DRVO=0 | 1: PDPL=0 | 0: LDPL=0
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_MCTL; // The first byte is used for
commands.
messageBuf[2] = 0b00000010; //
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
//clear interrupt
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_INTRST; // The first byte is used for
commands.
messageBuf[2] = 0b00000011; //
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
messageBuf[0] = (MMA_ADDR<<1) | (I2C_WRITE); // The first byte must
always consit of General Call code or the TWI slave address.
messageBuf[1] = MMA_INTRST; // The first byte is used for
commands.messageBuf[2] = 0b00000000; //
USI_TWI_Start_Transceiver_With_Data( messageBuf, 3 );
//in am Attiny freischalten
PCMSK0 = 1<<PCINT1; // PCINT1 aktivieren MMA7455
GIFR |= 1<<PCIF0; //clear pending int
GIMSK = 1<<PCIE0 ; //Pinchangeint. PCINT11..8 aktivieren
sei();
return 1;
}