liyutao 0 Posted December 6, 2016 Share Posted December 6, 2016 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 Quote Link to post Share on other sites
NurseBob 111 Posted December 7, 2016 Share Posted December 7, 2016 My limited experience with the wire library and CCS is that the library code is interrupt-driven, and goes to lpm4 between events. So, a good practice for running code that conserves batteries, but it can present a debugging challenge. Bob Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.