This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

msp430与GPRS模块的连接例程

硬件电路:


  • 2. #include <msp430x14x.h> 

    3. #include “string.h” 

    4.   
    5.  typedef unsigned short int    u16 

    6.  typedef unsigned char       u8 

    7. 
     typedef signed short int      s16 
    8.  typedef signed char         s8 

    9.   
    10.  #define SYS_SUCCESS    0 

    11.  #define SYS_FAIL          1 
    12. //============================================================== 

    13.  // DTU Pin define 
    14.  #define DTU_IGT_PORT_DIR      P1DIR 

    15.  #define DTU_IGT_PORT_OUT     P1OUT 

    16.  #define DTU_IGT_PIN             BIT6 

    17.   
    18.  #define DTU_RTS_PORT_DIR      P1DIR 

    19.  #define DTU_RTS_PORT_OUT     P1OUT 

    20.  #define DTU_RTS_PIN             BIT2 

    21.  
    22.  #define DTU_CTS_PORT_DIR      P1DIR 

    23.  #define DTU_CTS_PORT_IN       P1IN 
    24.  #define DTU_CTS_PIN             BIT4 

    25.  
    26.  #define DTU_IGT_FIRE      DTU_IGT_PORT_DIR  |= DTU_IGT_PIN;\

    27.                             DTU_IGT_PORT_OUT  |= DTU_IGT_PIN 

    28.  #define DTU_IGT_SHUT     DTU_IGT_PORT_DIR  |= DTU_IGT_PIN;\ 

    29.                             DTU_IGT_PORT_OUT  &= ~DTU_IGT_PIN 

    30.  #define DTU_RTS_HIGH     DTU_RTS_PORT_DIR |= DTU_RTS_PIN;\ 

    31.                             DTU_RTS_PORT_OUT |= DTU_RTS_PIN 

    32.  #define DTU_RTS_LOW     DTU_RTS_PORT_DIR |= DTU_RTS_PIN;\ 

    33.                             DTU_RTS_PORT_OUT &=~DTU_RTS_PIN 

    34.  #define DTU_CTS_FLAG    DTU_CTS_PORT_DIR &= ~ DTU_CTS_PIN  

    35.  #DTU_CTS                 (DTU_CTS_PORT_IN & DTU_CTS_PIN) 

    36.   
    37.  #define DTU_DAT_LEN    100 

    38.   
    39.  u8 g_rec_buf[DTU_DAT_LEN]  

    40.  u16 g_count = 0  
    41.  u8 g_sys_error = SYS_SUCCESS  

    42.   
    43.  /*********************************************************************************************** 

    44.  ** Function Name : Delay                                                  ** 

    45.  ** Parameter     : uTime                                                  ** 

    46.   ** Return     : 
    47.   ********************************************************************************************/

    48.   void Delay(u16 uTime) 

    49.   { 
    50.       u16 i=0; 

    51.       u16 j=0; 
    52.       for (i = 0  I < 1000; i++) 

    53.       { 
    54.            for (j = 0; j < uTime; j++); 

    55.       } 

    56.   } 
    57.   
    58.  /*********************************************************************************************** 

    59.  ** Function Name : InitClock                                                **

     60.  ** Parameter     : None                                                  ** 

    61.  ** Return        :                                                        ** 

    62.  ***********************************************************************************************/

    63.   void InitClock() 

    64.   { 
    65.       u8 i= 0; 
    66.  
    67.       BCSCTL1 &= ~XT2OFF; 
    68.       BCSCTL2 |= SELM1+SELS;       // MCLK 8M, SMCLK 1M            

    69.        

    70.       do

    71.       { 
    72.           IFG1 &= ~OFIFG; 

    73.           for (i = 0; I < 100; i++) 

    74.           { 
    75.               _NOP(); 
    76.           } 
    77.       } while(0 != (IFG1 & OFIFG)); 

    78.       IFG1 &= ~OFIFG; 

    79.   } 

    80.   
    81. /*********************************************************************************************** 

    82.  ** Function Name : InitUart1                                                ** 

    83.  ** Parameter     : None                                                  ** 

    84.  ** Return        :                                                        ** 

    85.  ***********************************************************************************************/ 

    86.  void InitUart1(void) 
    87.  { 
    88.      U1CTL |= SWRST;    // Rest UART1 

    89.      U1CTL |= CHAR;     // 8 bit data 
    90.      U1TCTL |= SSEL1; // set SMCLK as uart1 clock 

    91.      UBR01 = 0x8A;  // Baud rate 19200bps 

    92.      UBR11 = 0x00;

    93.   
    94.      UMCTL1 = 0xDE; 

    95.      ME2 |= 0x30;  

    96.      uctl1 &= ~SWRST;  // 

    97.    
    98.      IE2 |= URXIE1; 
    99.      P3SEL = 0XC0; // P3.6 and P3.7 as TXD and RXD 

    100.      P3DIR = 0x40; 

    101.  } 

    102.  
    103.  *********************************************************************************************** 

    104.  ** Function Name : UART1_RX_ISR                                        ** 

    105.  ** Parameter     : None                                                  ** 

    106.  ** Return        :                                                       ** 

    107.  **********************************************************************************************/ 

    108.  #pragma vector = UART1RX_VECTOR 

    109.  __interrupt void UART1_RX_ISR(void) 

    110.  { 

    111.      char dat = 0  
    112.      data = U1RXBUF  

    113.      g_rec_buf[g_count] = data  

    114.      if (g_count > (DTU_DAT_LEN -1)) 

    115.      { 
    116.          g_count = 0; 

    117.      } 

    118.  } 

    119.   
    120.  *********************************************************************************************** 

    121.  ** Function Name : Uart1SendByte                                         ** 

    122.  ** Parameter     : u8 dat                                                 ** 

    123.  ** Return        :                                                       ** 

    124.  **********************************************************************************************/ 

    125.   void Uart1SendByte(u8 dat) 

    126.   { 
    127.       while(0 == (IFG2 & UTXIFG1); 

    128.       U1TXBUF = dat; 

    129.   }

     130.    
    131.   ********************************************************************************************* 

    132.  ** Function Name : ClearDtuBuf                                            ** 

    133.  ** Parameter     :                                                       ** 

    134.  ** Return        :                                                       ** 

    135.  **********************************************************************************************/

    136.   void ClearDtuBuf(void) 

    137.   { 

    138.       u8 len = 0; 

    139.       g_count = 0; 
    140.        

    141.       IE2 &= ~URXIE1; // Close receive interrupt 

    142.       for (; len < DTU_DAT_LEN; len++) 

    143.       { 

    144.           g_rec_buf[len] = „\0‟  

    145.       } 

    146.       IE2 |= URXIE1; 

    147.   } 

    148.   
    149.   void SendDat(s8 * strptr) 

    150.   { 151.       u8 i.j; 
    152.       j = strlen(strptr);

     153.       for (i=0;i<j;i++) 
    154.       { 

    155.           Uart1SendByte((u8)strptr[i]); 
    156.       } 

    157.   } 

    158.   *********************************************************************************************

    159.  **  DTU MODULE: Start, Shut, Initialize 
    160.  **********************************************************************************************/ 

    161.   void StartDtu(void) 

    162.   {

     163.       DTU_IGT_FIRE; 

    164.       Delay(1); 

    165.       DTU_IGT_SHUT; 

    166.       Delay(100); 

    167.       DTU_IGT_FIRE; 

    168.   } 

    169.   
    170.  void ShutdownDtu(void)

     171.   { 

    172.       Delay(100); 
    173.       SendDat(“AT^smso\r\n”); 

    174.       Delay(100); 

    175.   } 

    176.   

    177.  void InitDtu(void) 
    178.   { 

    179.       SendDat(“AT\\Q3\r\n”); 

    180.       Delay(1000); 

    181.       SendDat(“ATE0\r\n”); 

    182.       Delay(100); 

    183.       SendDat(“AT+CMGF=1\r\n”);

    184.       Delay(100);

     185.       SendDat(“AT+CFUN=7\r\n”); 

    186.       Delay(100); 

    187.       SendDat(“AT+CFUN\?\r\n”); 

    188.       Delay(100); 

    189.   }

     190.    
    191.  void SendDtuMsg(char *num, char * msg) 

    192.  { 

    193.      char instruct[] = “AT+CMGS=\””; 

    194.      char numinstuct[27]; 

    195.       
    196.      strcpy(numinstuct, instruct); 

    197.      strcat(numinstuct, num); 

    198.      strcat(numinstuct, “\”\r\n”); 

    199.      SendDat(numinstuct); 
    200.      SendDat(msg); 

    201.      Uart1SendByte(0x1A);   // CTRL+Z 

    202.

    203.  } 
    204.  

     205.  void main() 

    206.  { 

    207.     WDTCTL = WDTPW + WDTHOLD; // close WATCH DOG 

    208.     InitClock(); 209.     InitUart1(); 

    210.     DTU_RTS_LOW  

    211.      

    212.     StartDtu()  

    213.     _EINT()  

    214.     Delay(2000)  

    215.     InitDtu()  

    216.      
    217.     SendDat(“AT\r\n“)  

    218.     SendDtuMsg ( ”18615771975”, ”Hello world! Just a Test!”); 

    219.     CloseDtu();

     220.      

    221.     while(1) 

    222.      { 

    223.      } 

    224.  } 

    225.