Thank you guys, Would you please take a look at my code and tell me what is wrong with it. I am using this to communicate with BN055 IMU which is connected to and MSP430G2553 launchpad via UART. The problem is in the last line, when I send TXSTRING("\xAA\x01\x08\x06") to the BNO055 to read the acceleration I get BB 06 00 00 00 00 00 00. The BB 06 is header but the data are not there (all 00). Should I clear the uart.rxbuffer in this case before transmitting a new set of bytes? if so, how to clear the uart.rxbuffer. I tried uart.rxbuffer[0]=0, uart.rxbuffer[1]=0, etc. but this did not clear the memory. Thank you M #include "msp430g2553.h" #include char xLSB, xMSB, yLSB, yMSB, zLSB, zMSB; #define TXD BIT2 #define RXD BIT1 typedef struct uart // UART { char *bufTXpnt; // UART TX buffer pointer unsigned int TXbuflen; // the lenght of TX block char *bufRXpnt; // UART RX buffer pointer unsigned int RXbuflen; // the lenght of RX block char TXbuffer[8]; char RXbuffer[8]; } uartstruct; void set_UCS() { //------------------- Configure the Clocks -------------------// WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_12MHZ; // Set range DCOCTL = CALDCO_12MHZ; // Set DCO step + modulation // DCO -> SMCLK (Default) IE1 &= 0xFD; /* Disable UCS interrupt */ return; } void setMSP430Pins() { //--------- Setting the UART function for P1.1 & P1.2 --------// // P2DIR = 0xFF; // All P2.x outputs< //P2OUT &= 0x00; // All P2.x reset P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD // P1OUT &= 0x00; return; } void uart_init(void){ IE2 &= ~(UCA0TXIE | UCA0RXIE | UCB0TXIE | UCB0RXIE); // Disable all USCIx0 TX & RX interrupts UCA0CTL1 = UCSWRST; // Set UCSWRST (hold USCI in Reset state) UCA0CTL1 |= UCSSEL_2; // CLK = SMCLK // ------------ Configuring the UART(USCI_A0) ----------------// // 115200 BAUD, CLK=12MHz UCA0BR0 = 6; UCA0BR1 = 0; // //*ours: UCBRF = 8, UCBRS = 0, UCOS16 = 1 // // BITS| 7 6 5 4 | 3 2 1 | 0 | // UCAxMCTL = | UCBRFx | UCBRSx | UCOS16 | UCA0MCTL = 0x81; //this works fine UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0-UART UCA0CTL1 &= ~UCSYNC; IFG2 |= UCA0TXIFG; // preset IFG flag always left on IE2|=UCA0RXIE; } #define TXSTRING(pnt) (TXdata((pnt), sizeof(pnt)-1)) // macro to get string and string len uartstruct uart; // declare a struct from typedef uartstruct void TXdata( char* pnt, unsigned int len){ uart.bufTXpnt = pnt; uart.TXbuflen = len; uart.bufRXpnt = uart.RXbuffer; // reset it to beginning of ram buffer IE2 |= UCA0TXIE + UCA0RXIE; // enable USCI_A0 TX & RX interrupt } //¦----------------------------- Delay Function ---------------------------------------¦ // This function will give us 1ms wait time, so for getting 10 ms, // then delay_ms(10) will give 10ms and delay_ms(100) will give 100ms void delay_ms(unsigned int ms) { unsigned int i; for (i = 0; i<= ms; i++) __delay_cycles(6000); // 6000 will give us 1ms } //¦----------------------------- US0TX ISR ---------------------------------------¦ #pragma vector=USCIAB0TX_VECTOR __interrupt void USCIAB0TX(void) // Shared A0/B0 TX IRQ vector { if (IFG2 & UCA0TXIFG){ // check for UART TX if (uart.TXbuflen){ // if not zero UCA0TXBUF = *uart.bufTXpnt++; --uart.TXbuflen; } else IE2 &= ~UCA0TXIE; // suspend IE if zero } else IFG2 &= ~UCA0TXIFG; // clear a false UCB0 trigger } //¦----------------------------- US0RX ISR ---------------------------------------¦ #pragma vector=USCIAB0RX_VECTOR __interrupt void USCIAB0RX(void) // A0/B0 RX IRQ vector { if (IFG2 & UCA0RXIFG){ // check for UART RX *uart.bufRXpnt++ = UCA0RXBUF; // copy data byte if (uart.bufRXpnt == uart.RXbuffer+8 && uart.RXbuffer[0]==0xBB) // got 8 bytes in yet? <<<< uart.RXbuflen=uart.RXbuffer[1]; __bic_SR_register_on_exit(LPM3_bits); } else IFG2 &= ~UCA0RXIFG; } //¦----------------------------- Main -------------------------------------------¦ void main(void) { set_UCS(); setMSP430Pins(); uart_init(); delay_ms(500); TXSTRING("\xAA\x00\x3D\x01\x00"); // Send this to the BNO055 to set up the config mode while( !(IFG2 & UCA0TXIFG) && uart.RXbuffer[1]==0x01); TXSTRING("\xAA\x00\x3F\x01\x20"); // reset delay_ms(60); while( !(IFG2 & UCA0TXIFG) && uart.RXbuffer[1]==0x01); TXSTRING("\xAA\x00\x3E\x01\x00"); // Normal power mode delay_ms(10); // while( !(IFG2 & UCA0TXIFG) && uart.RXbuffer[1]==0x01); TXSTRING("\xAA\x00\x07\x01\x00"); // Send this to the BNO055 to set up page 0 delay_ms(10); while( !(IFG2 & UCA0TXIFG) && uart.RXbuffer[1]==0x01); TXSTRING("\xAA\x00\x3D\x01\x07"); // Send this to the BNO055 to set up the 9DOF mode delay_ms(120); // while( !(IFG2 & UCA0TXIFG) && uart.RXbuffer[1]==0x01); TXSTRING("\xAA\x01\x08\x06"); while( !(IFG2 & UCA0TXIFG)); delay_ms(120); xLSB = uart.RXbuffer[2]; xMSB = uart.RXbuffer[3]; yLSB = uart.RXbuffer[4]; yMSB = uart.RXbuffer[5]; zMSB = uart.RXbuffer[6]; zMSB = uart.RXbuffer[7]; __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ int until Byte RXed while(1) { } }
↧