Jump to content

liyutao

Members
  • Content Count

    3
  • Joined

  • Last visited

  1. Hello! I am compiling the following code through CCS 6.1.3, it is an imported sketch first then I add some thing else for it. But it seems It stuck in the setup function because it goes to LPM4 when it tries to use any USCI related function( For example: Wire.begin(),write to ()). Can some one help me with it? Thanks. The attach file is the ino file from energia 17 and the following is the Code? The uc is MSP430F5529, and it is trying to use I2C to communicate with ADXL345, UART to communicate with ESP8266. The reset is ADC stuff. And we have a button to do some control. Each of these parts works fine by themselves alone. This is why I can not understand. #include <Wire.h> #include <msp430f5529.h> #define DEBUG true //Prints AT responses to serial terminal, set to False if you do not want to print void ADC_SETUP( void ) ; void TIMER_A2_SETUP( void ) ; void EXTERNAL_SETUP( void ) ; void READ_ACCELERATION( void ) ; void READ_ADC( void ) ; //Global Variables char flag = 0x00 ; //Declares 8-bit flag character and initializes to 0. int btn_cnt = 0 ; //int png_cnt = 0 ; int tmr_cnt = 0 ; int accel_data[3] = { 0 , 0 , 0 } ; double temp = 0 ; int snd = 0 ; int trash = 0 ; #define DEVICE (0x53) // Device address as specified in data sheet byte _buff[6]; char POWER_CTL = 0x2D; //Power Control Register char DATA_FORMAT = 0x31; char DATAX0 = 0x32; //X-Axis Data 0 char DATAX1 = 0x33; //X-Axis Data 1 char DATAY0 = 0x34; //Y-Axis Data 0 char DATAY1 = 0x35; //Y-Axis Data 1 char DATAZ0 = 0x36; //Z-Axis Data 0 char DATAZ1 = 0x37; //Z-Axis Data 1 String wifiSSID= "suddenlink.net-D0B3"; //Set this to your WiFi SSID String wifiKey = "G2PBW489C600863"; //Set this to your WiFi password //String apiKey = "L0LX8JG2D8YETKFB"; //API key from thing speak, used to post data String apiKey = "7N3L4F8YP90A6S82"; //API key from thing speak, used to post data void setup() { __bic_SR_register( CPUOFF ) ; WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer //Setup peripherals ADC_SETUP() ; EXTERNAL_SETUP() ; TIMER_A2_SETUP() ; //pinMode(P4_7, OUTPUT) ; P2SEL &= ~BIT5 ; P2DIR |= BIT5 ; Wire.begin(); //I2C __delay_cycles(100); Serial.begin(115200); //UART __delay_cycles(100); //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register. P2SEL &= ~BIT4 ; P2DIR |= BIT4 ; P2OUT &= ~BIT4 ; __delay_cycles(100); P2OUT |= BIT4 ; __delay_cycles(100); //digitalWrite(P4_7, LOW); P2OUT &= ~BIT5 ; __delay_cycles(100); //digitalWrite(P4_7, HIGH); P2OUT |= BIT5 ; __delay_cycles(100); writeTo(DATA_FORMAT, 0x01); __delay_cycles(100); __bic_SR_register( CPUOFF ) ; __delay_cycles(100); //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register. writeTo(POWER_CTL, 0x08); __delay_cycles(100); __bic_SR_register( CPUOFF ) ; sendData("AT+RST\r\n",200,DEBUG); // reset module __delay_cycles(100); sendData("AT+CWMODE=3\r\n",100,DEBUG); //configure to access AP String cnnct = ("AT+CWJAP=\"" + wifiSSID + "\",\"" + wifiKey +"\"\r\n"); //Set up string to connect to wifi String response = sendData(cnnct,20000,DEBUG); //Connect to wifi using credentials provided while (response.indexOf("FAIL") != -1) { //If connection failed, try again response = sendData(cnnct,20000,DEBUG); } __bis_SR_register( GIE ); // interrupts enabled } void loop() { __bic_SR_register( CPUOFF ) ; //Read acceleration data each loop READ_ACCELERATION () ; READ_ADC() ; //__bis_SR_register( GIE ); // interrupts enabled //ADC12CTL0 |= ADC12SC ; //Flag Handlers if( ( flag>>0 )&1 ) //Flag = 0bxxx1 {//Temp flag handler temp = .8059 * ( ( temp - 2637 ) / ( -13.6 ) ) ; // LSB -> deg C thingSpeak( temp , 0 , 0 ) ; //Reset flag flag &= ~BIT0 ; } if( ( flag>>1 )&1 ) //Flag = 0bxx1x {//Mic flag handler thingSpeak( snd , 0 , 0 ) ; //Reset flag flag &= ~BIT1 ; } if( ( flag>>2 )&1 ) //Flag = 0bx1xx {//External button flag handler //Send button pushed data thingSpeak( 0 , 0 , 0 ) ; //Reset flag flag &= ~BIT2 ; } if( ( flag>>3 )&1 ) //Flag = 0b1xxx {//External accelerometer flag handler thingSpeak( accel_data[0] , accel_data[1] , accel_data[2] ) ; //Reset flag flag &= ~BIT3 ; } if( ( flag>>4 )&1 ) //Flag = 0b1xxxx {//Ping flag handler thingSpeak( 0 , 0 , 0 ) ; //Reset flag flag &= ~BIT4 ; } } void thingSpeak( int datax , int datay , int dataz ) {// datax is used as main data send. If not used for accelerometer, datay and dataz are not used. char flg = flag ; //Set flg to ensure flag doesn't change. String dataWrite ; sendData( "AT+CIPSTART=\"TCP\",\"184.106.153.149\",80\r\n" , 50 , DEBUG ) ; // turn on server on port 80 //Field 1: x data //Field 2: y data //Field 3: z data //Field 4: temp data //Field 5: microphone event //Field 6: ping data and reset button switch( flg ){ case 1: // Temp data dataWrite = "GET /update?api_key=" + apiKey + "&field4=" + String(datax) + "\r\n" ; break ; case 2: // Microphone event dataWrite = "GET /update?api_key=" + apiKey + "&field5=" + String(datax) + "\r\n" ; break ; case 4: // Button pushed data dataWrite = "GET /update?api_key=" + apiKey + "&field6=" + String(1) + "\r\n" ; break ; case 8: // Accelerometer data dataWrite = "GET /update?api_key=" + apiKey + "&field1=" + String(datax) + "&field2=" + String(datay) + "&field3=" + String(dataz) + "\r\n" ; break ; case 16: // Ping case dataWrite = "GET /update?api_key=" + apiKey + "&field6=" + String(0) + "\r\n" ; default: ; } String cmd = "AT+CIPSEND=" ; int length = dataWrite.length() ; cmd += length; cmd += "\r\n"; sendData(cmd,50,DEBUG); //Set up TCP connection sendData(dataWrite,10,DEBUG); //Send data to thing speak //long int time = millis(); //while((time+500)>millis()){} //delay for 5 seconds before sending data __delay_cycles(50) ; } String sendData(String command, const int timeout, boolean debug) { String output = ""; Serial.print(command); // send the read character to the Serial1 //long int time = millis(); long int time = 0 ; //while( (time+timeout) > millis())//wait for a response until timeout is up while( time++ < timeout ) { while(Serial.available()) {// The esp has data so display its output to the serial window char c = Serial.read(); // read the next character. output+=c; } if ((output.indexOf("FAIL") != -1)||(output.indexOf("OK")!=-1)){ //if response received break from the timeout loop break; } __delay_cycles(10) ; } return output; } void writeTo(byte address, byte val) { Wire.beginTransmission(DEVICE); // start transmission to device Wire.write(address); // send register address Wire.write(val); // send value to write Wire.endTransmission(); // end transmission } // Reads num bytes starting from address register on device in to _buff array void readFrom(byte address, int num, byte _buff[]) { Wire.beginTransmission(DEVICE); // start transmission to device Wire.write(address); // sends address to read from Wire.endTransmission(); // end transmission Wire.beginTransmission(DEVICE); // start transmission to device Wire.requestFrom(DEVICE, num); // request 6 bytes from device int i = 0; while(Wire.available()) // device may send less than requested (abnormal) { _buff = Wire.read(); // receive a byte i++; } Wire.endTransmission(); // end transmission } void setRegisterBit(byte regAdress, int bitPos, bool state) { byte _b; readFrom(regAdress, 1, &_ ; if (state) { _b |= (1 << bitPos); // forces nth bit of _b to be 1. all other bits left alone. } else { _b &= ~(1 << bitPos); // forces nth bit of _b to be 0. all other bits left alone. } writeTo(regAdress, _ ; } void ADC_SETUP( void ) {//Multiple conversions ADC12CTL0 = ADC12ON + ADC12MSC + ADC12SHT0_8 ; // Turn on ADC12, extend sampling time to avoid overflow of results ADC12CTL1 = ADC12SHP + ADC12CONSEQ_3 ; // Use sampling timer, repeated sequence ADC12MCTL0 = ADC12INCH_4 ; // ref+=AVcc, channel = A4 ADC12MCTL1 = ADC12INCH_12 + ADC12EOS ; // ref+=AVcc, channel = A12, end seq. //ADC12IE = ADC12IE0 + ADC12IE1 ; // Enable ADC12IFG.3 ADC12CTL0 |= ADC12ENC; // Enable conversions return ; } void TIMER_A2_SETUP( void ) {//Setup registers for Timer_A1 TA2CCR0 = 0x11C1 ; // Count up to 1 sec TA2CCTL0 = CCIE ; TA2CTL = TASSEL_1 + MC_1 + TACLR + ID_3 ; return ; } void EXTERNAL_SETUP( void ) {//Setup registers for external interrupt //P1DIR &= ~BIT1 ; P1OUT |= BIT1 ; // Set P1.1 to Pullup mode P1REN |= BIT1 ; // Enable P1.1 internal resistance P1IES |= BIT1 ; // P1.1 Hi/Lo edge P1IE |= BIT1 ; // P1.1 interrupt enabled P1IFG &= 0x00 ; // P1 IFG cleared return ; } void READ_ACCELERATION( void ) {// Read acceleration data from the accelerometer //Define local variables const int target = 140 ; uint8_t howManyBytesToRead = 6; readFrom( DATAX0, howManyBytesToRead, _buff); // each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!! // thus we are converting both bytes in to one int accel_data[0] = (((int)_buff[1]) << 8) | _buff[0]; accel_data[1] = (((int)_buff[3]) << 8) | _buff[2]; accel_data[2] = (((int)_buff[5]) << 8) | _buff[4]; if( accel_data[0] >= target || accel_data[1] >= target || accel_data[2] >= target ) flag = 0x08 ; return ; } void READ_ADC( void ) { ADC12CTL0 |= ADC12SC; // Start conversion-software trigger while ( !( ADC12IFG ) ); //Reading ADC12 memory registers snd = ADC12MEM0 ; temp = ADC12MEM1 ; if( ( flag>>4 )&1 ) // flag = 0b1xxxx = 0x1x //Every 10 seconds {//If TEMP if( temp >= 0x2460 ) // ADC12MEM1 = A0 > 3.3V? { flag |= BIT0 ; //flag = 0bxxx1 } } if( snd >= 0x0A00 ) //Loud noise { flag |= BIT1 ; //flag = 0bxx1x } } /* //Define Interupts #pragma vector = ADC12_VECTOR __interrupt void ADC12_ISR_1( void ) {//ADC ISR snd = ADC12MEM0 ; //temp = ADC12MEM1 ; if( ( flag>>4 )&1 ) // flag = 0b1xxxx {//If TEMP if( temp >= 0x0000 ) // ADC12MEM = A0 > 3.3V? { flag |= BIT0 ; //flag = 0bxxx1 } } else {//ELSE MIC if( snd >= 0x0A00 ) //Loud noise { flag |= BIT1 ; //flag = 0bxx1x } } ADC12CTL0 |= ADC12SC ; //Clear CPUOFF bit __bic_SR_register_on_exit( CPUOFF ) ; }*/ #pragma vector = PORT1_VECTOR __interrupt void Port_1( void ) {//External Button ISR //Start timer for multifunction button TA2CTL |= MC_1 ; // Starting the timer counting up //Clear interrupt flag P1IFG &= ~BIT1 ; } #pragma vector = TIMER2_A0_VECTOR __interrupt void TIMER0_A2( void ) {//Timer A0 ISR, runs every 1 sec //Server Ping and ADC if( ( ( tmr_cnt%10 )^1 ) == 1 ) { // Ping server every 10 minutes flag = 0x10 ; //Send ping to server tmr_cnt = 0 ; } //Button function if( ( btn_cnt == 2 ) & ( ( flag>>5 )&1 ) ) { //Turn everything on } else if( btn_cnt == 5 ) // After 5 seconds, reset flag flag = 0x10 ; else if ( btn_cnt == 10 ) // After 10 seconds, turn off all interrupts { TA2CTL &= ~MC_1 ; // Turn off timer //DISABLE ALL OTHER MODULES flag = 0x20 ; P1IE |= BIT1 ; // P1.1 interrupt enabled } //Button "pattern" if( P1IN == 0xF0 ) //Masks btn_cnt++ ; else { btn_cnt = 0 ; } tmr_cnt++ ; //Clear interrupt flag TA2CTL &= ~TAIFG ; } Final.ino
  2. Hello! I am using Energia 17 & 18 to code for the MSP430F5529 and ADXL345. The following is the code I used. //Add the SPI library so we can communicate with the ADXL345 sensor #include <SPI.h> //Assign the Chip Select signal to pin 2. int CS = 2; //This is a list of some of the registers available on the ADXL345. //To learn more about these and the rest of the registers on the ADXL345, read the datasheet! char POWER_CTL = 0x2D; //Power Control Register char DATA_FORMAT = 0x31; char DATAX0 = 0x32; //X-Axis Data 0 char DATAX1 = 0x33; //X-Axis Data 1 char DATAY0 = 0x34; //Y-Axis Data 0 char DATAY1 = 0x35; //Y-Axis Data 1 char DATAZ0 = 0x36; //Z-Axis Data 0 char DATAZ1 = 0x37; //Z-Axis Data 1 //This buffer will hold values read from the ADXL345 registers. char values[10]; //These variables will be used to hold the x,y and z axis accelerometer values. long x,y,z; void setup(){ //Initiate an SPI communication instance. SPI.begin(); //Configure the SPI connection for the ADXL345. SPI.setDataMode(SPI_MODE3); //Create a serial connection to display the data on the terminal. Serial.begin(115200); Serial.println("GO!"); //Set up the Chip Select pin to be an output from the Arduino. pinMode(CS, OUTPUT); //Before communication starts, the Chip Select pin needs to be set high. digitalWrite(CS, HIGH); //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register. writeRegister(DATA_FORMAT, 0x00); //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register. writeRegister(POWER_CTL, 0x08); //Measurement mode } void loop(){ //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345. //The results of the read operation will get stored to the values[] buffer. readRegister(DATAX0, 6, values); //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis. //The X value is stored in values[0] and values[1]. x = ((long)values[1]<<8)|(long)values[0]; //The Y value is stored in values[2] and values[3]. y = ((long)values[3]<<8)|(long)values[2]; //The Z value is stored in values[4] and values[5]. z = ((long)values[5]<<8)|(long)values[4]; //Print the results to the terminal. Serial.print(x); Serial.print('\t'); Serial.print(y); Serial.print('\t'); Serial.println(z); delay(100); } //This function will write a value to a register on the ADXL345. //Parameters: // char registerAddress - The register to write a value to // char value - The value to be written to the specified register. void writeRegister(char registerAddress, char value){ //Set Chip Select pin low to signal the beginning of an SPI packet. digitalWrite(CS, LOW); //Transfer the register address over SPI. SPI.transfer(registerAddress); //Transfer the desired register value over SPI. SPI.transfer(value); //Set the Chip Select pin high to signal the end of an SPI packet. digitalWrite(CS, HIGH); } //This function will read a certain number of registers starting from a specified address and store their values in a buffer. //Parameters: // char registerAddress - The register addresse to start the read sequence from. // int numBytes - The number of registers that should be read. // char * values - A pointer to a buffer where the results of the operation should be stored. void readRegister(char registerAddress, int numBytes, char * values){ //Since we're performing a read operation, the most significant bit of the register address should be set. char address = 0x80 | registerAddress; //If we're doing a multi-byte read, bit 6 needs to be set as well. if(numBytes > 1)address = address | 0x40; //Set the Chip select pin low to start an SPI packet. digitalWrite(CS, LOW); //Transfer the starting register address that needs to be read. SPI.transfer(address); //Continue to read registers until we've read the number specified, storing the results to the input buffer. for(int i=0; i<numBytes; i++){ values = SPI.transfer(0x00); } //Set the Chips Select pin high to end the SPI packet. digitalWrite(CS, HIGH); } The output is all -1,-1,-1. When I moved the ADXL345 module, they became 0,0,0 or some wired combinations like -1,0,-678234. Can someone help me to see what is wrong with the code? I changed the chip select pin numbers when I code for them and I am using USCIA port. This code is from other places. Thanks! Yutao Li
  3. liyutao

    Tiva C adxl345 SPI weird readings

    Hello! I am using your code recently and I can communicating with the module. But the outputs are really wired. They are all -1,-1,-1. If I move the ADXL345 module, the outputs would become 0,0,0 or like -2,-1,-768723 or -240,0,1. Can someone help me to see what might be wrong? Thanks.
×