Tool/software: Code Composer Studio
Hi all!
I am trying to get the MSP430G2553 to receive string and device control. But, it's not working. Please help me!
I want to send "LED_ON" to open and send "LED_OFF" to close device.
Source code:
#include <msp430g2553.h> #include <string.h> #include <stdlib.h> /*****************define*********************************/ #define ENTER 0x0D // #define SEND 0x1A // #define SETUP_SMS "AT+CMGF=1" // #define SEND_SMS "AT+CMGS=" // #define my_number "0972156484" #define SHOW_SMS "AT+CNMI=2,2,0,0,0" //#define LED_ON 999 //#define LED_OFF 666 // char *string; char buff[30]; //char fromsmsnumber[30]; // so dien thoai nguoi gui //char fromsmsdatetime[30]; // ngay gio char content[20]; // noi dung tin nhan char content1[10] = "LED_ON"; int n, i, t; // /********************************************************/ /**********khai bao ham**********************************/ // // void uart_init(void); // void uart_gets(char *s); // char uart_getc(); // void uart_puts(char* string); // void uart_putc(unsigned char data); // //void sim_init(void); // void sms_setup(void); // void sms_show_setup(void); // void sms_decode(char str[]); // void send_sms(); unsigned long rev = 0; /********************************************************/ /* * main.c */ int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; uart_init(); //sim_init(); sms_setup(); sms_show_setup(); __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled while(1){}; } #pragma vector=USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void) { uart_gets(buff); sms_decode(buff); //rev = atol(content); //if (rev == LED_ON) P2OUT|=BIT3; //if (rev == LED_OFF) P2OUT &=~BIT3; if (strcmp(content,"LED_ON")==0) P1OUT|=BIT6; //uart_puts(content); if (strcmp(content, "LED_OFF")==0) P1OUT&=~BIT6; // rev=0; } /************************noi dung ham***************************************/ void uart_init(void) { P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2 ; P1DIR|=BIT0+BIT6; P1OUT |= BIT0+BIT6; P1OUT &= ~BIT0+BIT6;// P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz 9600, UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt } //ham truyen 1 ki tu void uart_putc(unsigned char data) { while (!(IFG2&UCA0TXIFG)); // Doi gui xong ki tu truoc UCA0TXBUF= data; // gui ki tu tiep (xuong transmit buffer) } // ham truyen 1 chuoi ki tu // dung pointer de lay tung byte trong chuoi // vi transceiver la thanh ghi 1 byte // Co the dung mang ki tu void uart_puts(char* string) { while(*string) // lap den het chuoi ki tu { while (!(IFG2&UCA0TXIFG)); // Doi gui xong ki tu truoc UCA0TXBUF= *string; // gui ki tu tiep theo string ++; // Lay ki tu tiep theo } } //***************************************************************************** // Wait to receive a character through UART //***************************************************************************** char uart_getc() { while(!(IFG2&UCA0RXIFG)); // Wait until USCI_A0 RX receive a complete character return UCA0RXBUF; // assign RX buffer to function 's name } //***************************************************************************** // Wait to receive a string through UART // Note that the string is ended with '/0' (or 0x00) //***************************************************************************** void uart_gets(char *s) { *s = uart_getc(); while(*s!='#') { s++; *s = uart_getc(); } } void uart_put_int(unsigned long n) //Problem here { unsigned char buffer[16]; unsigned char i,j; if(n == 0) { uart_putc('0'); return; } for (i = 15; i > 0 && n > 0; i--) { buffer[i] = (n%10)+'0'; n /= 10; } for(j = i+1; j <= 15; j++) { uart_putc(buffer[j]); } } void sms_setup(void) { __delay_cycles(1000000); uart_puts(SETUP_SMS); uart_putc(ENTER); } void sms_show_setup(void) { __delay_cycles(10000); uart_puts(SHOW_SMS); uart_putc(ENTER); } void sms_decode(char str[]) { int n, begin,end ,i; int k = 0; n= strlen(str); for(i=0; i<n; i++) { if(str[i]=='*') { begin = i+1; } if(str[i]=='#') { end= i; } } for(i=begin;i<end; i++) { content[k]=str[i]; k++; } }