/* I2C Programm mit kleinen Fehler x/y/z Werte werden nicht ab i=0 bis 5 sondern ab i=1-5 und dann 0 abgespeichert!!! 1.byte lower byte(x0) 2.byte higher(x1) */ #include #include uint8_t address = 0x53; void i2c_write(uint8_t address, uint8_t* data, uint8_t len); unsigned char I2C_ReadByte(uint8_t address, unsigned char registerAddr, uint8_t* data,int index); void i2c_init(); void uart_init(); int main(void) { WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; uint8_t start0[] = {}; uint8_t data_format[] = {0x31,0x08};//reg0x31, 0x08 = full resolution +-2g uint8_t bw_rate[] = {0x2C,0x0A}; //reg0x2C,0x0A = 100Hz uint8_t power_ctl[] = {0x2D,0x08}; //reg0x2D,0x08 = measure uint8_t offset_x[] = {0x1E,0xFD}; //reg0x1E,x-Axis calibration uint8_t offset_y[] = {0x1F,0xFD}; uint8_t offset_z[] = {0x20,0x02}; //uint8_t system =0; uint8_t axis[26] ; int maxindex = 5; //maxindex = (maximum of read bytes)-1 uart_init(); i2c_init(); i2c_write(address,start0,0); __delay_cycles(5000); i2c_write(address,data_format,2); __delay_cycles(5000); i2c_write(address,bw_rate,2); __delay_cycles(5000); i2c_write(address,power_ctl,2); __delay_cycles(5000); i2c_write(address,offset_x,2); __delay_cycles(5000); i2c_write(address,offset_y,2); __delay_cycles(5000); i2c_write(address,offset_z,2); __delay_cycles(5000); while(1) { I2C_ReadByte(address,0x32,axis,maxindex); //read 6 bytes from address 0x32-0x37 /* die for schleife sollte eigentlich von 0 an laufen, aber bei axis(0) zeigt er Inhalt von Register 0x37=axis(5) an ??? die Speicherinhalte werden genau um 1 verschoben abgespeichert*/ for(int i = 1;i <= maxindex; i++) { UCA0TXBUF = axis[i]; __delay_cycles(5000); } UCA0TXBUF = axis[0]; __delay_cycles(5000); UCA0TXBUF = 0x0D; __delay_cycles(500000); //wait busy } } void i2c_init() { P1SEL |= BIT6 + BIT7; P1SEL2 |= BIT6 + BIT7; UCB0CTL1 |= UCSWRST; UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; UCB0BR0 = 10; UCB0BR1 = 0; UCB0CTL1 = UCSSEL_2; UCA0CTL1 &= ~UCSWRST; //Take UCA0 out of reset } void uart_init() { P1SEL |= BIT1 + BIT2 ; P1SEL2 |= BIT1 + BIT2; UCA0CTL1 = UCSWRST; //UCA0 in Reset UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz 9600 UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 } void i2c_write(uint8_t address, uint8_t* data, uint8_t len) { // Warten, bis Bus frei while (UCB0CTL1 & UCTXSTP) { } UCB0I2CSA = address; // Slave Adresse setzen UCB0CTL1 |= UCTR + UCTXSTT; // W-Mode, Start der Übertragung // Senden der Datenbytes for (uint8_t i = 0; i < len; i++) { while (!(IFG2 & UCB0TXIFG)) { } UCB0TXBUF = data[i]; //UCA0TXBUF = data[i]; } // Senden der Stoppsequenz while (!(IFG2 & UCB0TXIFG)) { } UCB0CTL1 |= UCTXSTP; } unsigned char I2C_ReadByte(uint8_t address, unsigned char registerAddr, uint8_t* data,int index) { UCB0I2CSA = address; // Set slave address UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition // Wait for transmit buffer to be ready while (!(IFG2 & UCB0TXIFG)); UCB0TXBUF = registerAddr; // Send register address // Wait for transmit to finish while (!(IFG2 & UCB0TXIFG)); UCB0CTL1 &= ~UCTR; // I2C RX UCB0CTL1 |= UCTXSTT; // I2C start (repeated) while (!(IFG2 & UCB0RXIFG)) // Wait for receive buffer to be ready //while (UCB0CTL1 & UCTXSTT) // Wait for start to finish {} //if(index != 0) // { for(int i=0;i < index; i++) { data[i] = UCB0RXBUF; // Read data while (!(IFG2 & UCB0RXIFG)); // Wait for receive buffer to be ready } //} UCB0CTL1 |= UCTXSTP; // I2C stop data[index] = UCB0RXBUF; // Read data while ((UCB0CTL1 & UCTXSTP)); // Wait for stop return 0; } /* void Receive(void){ UCB0CTL1 &= ~UCTR ; // Clear UCTR while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition while (UCB0CTL1 & UCTXSTT); // Start condition sent? UCB0CTL1 |= UCTXSTP; // I2C stop condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts } */