#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    extracting gps coordinates and sending it as a message using LPC2148


    Hi, I am connecting a GPS and GSM module to LPC2148. I need to extract the gps coordinates and send it as a message. But the message contains only part of the code and there it is not constant either. Could you help me out! Here is the code:

    extern unsigned char cmgf[]="AT+CMGF=1"; //Text format in GSM modem
    extern unsigned char cmgs[]="AT+CMGS=\"9844420844\""; //Mobile number to which the msg is sent
    extern unsigned char msg[]=","; //secret code
    extern unsigned char readall[]="AT+CMGR=\"REC UNREAD\"\r\n";
    static unsigned char lat[10];
    static unsigned char lon[11];
    static unsigned char rxbuff[16];

    void txu1(unsigned char data) //Transmit a byte of data through UART1
    {
    while(!(U1LSR & 0x20)); // Wait until UART1 ready to send character
    U1THR = data;

    }
    unsigned char rxu1()
    {
    unsigned char p;
    while ((U1LSR&0x01)!=1);
    p=U1RBR;
    return p;
    }

    unsigned char rxu0()
    {
    unsigned char p;

    while (!(U0LSR & 0x01));
    p=U0RBR;
    return p;

    }
    void sendstring(unsigned char *p) //Sends a string of data through UART1
    {
    while(1)
    {
    if(*p=='\0') break;
    txu1(*p++);
    }
    }

    void delayfunc() //delay function
    {
    int i,j;
    for(i=0;i<60000;i++)
    for(j=0;j<200;j++);
    }
    void init_uart(void)
    {
    //static unsigned char ack[3];
    //strcpy(ack,"xy");
    PINSEL0|=0x00050005;
    PINSEL1|=0x00000000;
    PINSEL2|=0x00000000;

    PINSEL0 &= 0xFFFFFFF0; // Reset P0.8,P0.9 Pin Config
    PINSEL0 |= 0x00000001; // Select P0.0 = TxD(UART0)
    PINSEL0 |= 0x00000004; // Select P0.1 = RxD(UART0)
    // NSK UART0 Initialization
    U0LCR &= 0xFC; // Reset Word Select(1:0)
    U0LCR |= 0x03; // Data Bit = 8 Bit
    U0LCR &= 0xFB; // Stop Bit = 1 Bit
    U0LCR &= 0xF7; // Parity = Disable
    U0LCR &= 0xBF; // Disable Break Control
    U0LCR |= 0x80; // Enable Programming of Divisor Latches

    // U1DLM:U1DLL = 60.00 MHz / [16 x Baud]
    // = 60.00 MHz / [16 x 9600]
    // = 390.6 = 391 = 0187H
    U0DLM = 0x00; // Program Divisor Latch(391) for 9600 Baud
    U0DLL = 0x5D;

    U0LCR &= 0x7F; // Disable Programming of Divisor Latches

    U0FCR |= 0x01; // FIF0 Enable
    U0FCR |= 0x02; // RX FIFO Reset
    U0FCR |= 0x04; // TX FIFO Reset
    U0FCR &= 0x3F;

    //NSK UART1 Initialization

    U1LCR &= 0xFC; // Reset Word Select(1:0)
    U1LCR |= 0x03; // Data Bit = 8 Bit
    U1LCR &= 0xFB; // Stop Bit = 1 Bit
    U1LCR &= 0xF7; // Parity = Disable
    U1LCR &= 0xBF; // Disable Break Control
    U1LCR |= 0x80; // Enable Programming of Divisor Latches

    // U1DLM:U1DLL = 60.00 MHz / [16 x Baud]
    // = 60.00 MHz / [16 x 9600]
    // = 390.6 = 391 = 0187H
    U1DLM = 0x00; // Program Divisor Latch(391) for 9600 Baud
    U1DLL = 0x5D;

    U1LCR &= 0x7F; // Disable Programming of Divisor Latches

    U1FCR |= 0x01; // FIF0 Enable
    U1FCR |= 0x02; // RX FIFO Reset
    U1FCR |= 0x04; // TX FIFO Reset
    U1FCR &= 0x3F;

    }

    void receive_gps_coord(void)
    {
    unsigned char p;
    int i,j;



    int gps=0;

    while(gps==0)
    {
    p=rxu0();
    while(p!='$')
    {
    p=rxu0();
    }
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='P')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='A')
    {
    while(p!=',')
    {
    p=rxu0();

    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    for(i=0;i<9;i++)
    {
    lat[i]=rxu0();
    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    for(i=0;i<10;i++)
    {
    lon[i]=rxu0();
    }
    lat[9]='\0';
    lon[10]='\0';
    gps=1;
    }
    else
    continue;
    }


    }

    void sendsms(void)
    {
    sendstring(cmgf);
    txu1(0x0d); // equivalent of
    txu1(0x0a); // enter key
    delayfunc();
    sendstring(cmgs);
    txu1(0x0d);
    txu1(0x0a);
    delayfunc();

    sendstring(lat);
    sendstring(msg);
    sendstring(lon);

    delayfunc();
    txu1(0x1a);
    delayfunc();
    txu1(0x1a);

    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Code:
    extern unsigned char cmgf[]="AT+CMGF=1";                            //Text format in GSM modem
    extern unsigned char cmgs[]="AT+CMGS=\"9844420844\"";               //Mobile number to which the msg is sent
    extern unsigned char msg[]=",";                                //secret code
    extern unsigned char readall[]="AT+CMGR=\"REC UNREAD\"\r\n";
    static unsigned char lat[10];
    static unsigned char lon[11];
    static unsigned char rxbuff[16];
    
    void txu1(unsigned char data)                 //Transmit a byte of data through UART1
    {
    while(!(U1LSR & 0x20));                         // Wait until UART1 ready to send character  
        U1THR = data; 
    
    }
    unsigned char rxu1()
    {
      unsigned char p;
    while ((U1LSR&0x01)!=1);
    p=U1RBR;
    return p;
    }
    
    unsigned char rxu0()
    {
    unsigned char p;
    
    while (!(U0LSR & 0x01));
    p=U0RBR;
    return p;
    
    }
    void sendstring(unsigned char *p)            //Sends a string of data through UART1
    {
    while(1)
    {
    if(*p=='\0') break;
    txu1(*p++);
    }
    }
    
    void delayfunc()                              //delay function
    {
    int i,j;
    for(i=0;i<60000;i++)
    for(j=0;j<200;j++);
    }
    void init_uart(void)
    {
    //static unsigned char ack[3];
    //strcpy(ack,"xy");
    PINSEL0|=0x00050005;
    PINSEL1|=0x00000000;
    PINSEL2|=0x00000000;
    
    PINSEL0 &= 0xFFFFFFF0;                                  // Reset P0.8,P0.9 Pin Config
    PINSEL0 |= 0x00000001;                                  // Select P0.0 = TxD(UART0)
    PINSEL0 |= 0x00000004;                                  // Select P0.1 = RxD(UART0)
     // NSK UART0 Initialization
      U0LCR &= 0xFC;                                            // Reset Word Select(1:0)
      U0LCR |= 0x03;                                            // Data Bit = 8 Bit
      U0LCR &= 0xFB;                                            // Stop Bit = 1 Bit
      U0LCR &= 0xF7;                                            // Parity = Disable
      U0LCR &= 0xBF;                                            // Disable Break Control
      U0LCR |= 0x80;                                            // Enable Programming of Divisor Latches
    
      // U1DLM:U1DLL = 60.00 MHz / [16 x Baud]
      //             = 60.00 MHz / [16 x 9600]
      //             = 390.6 = 391 = 0187H
      U0DLM = 0x00;                                             // Program Divisor Latch(391) for 9600 Baud
      U0DLL = 0x5D;
    
      U0LCR &= 0x7F;                                            // Disable Programming of Divisor Latches
    
      U0FCR |= 0x01;                                            // FIF0 Enable
      U0FCR |= 0x02;                                            // RX FIFO Reset
      U0FCR |= 0x04;                                            // TX FIFO Reset
      U0FCR &= 0x3F;
    
      //NSK UART1 Initialization
    
    U1LCR &= 0xFC;                                          // Reset Word Select(1:0)
      U1LCR |= 0x03;                                            // Data Bit = 8 Bit
      U1LCR &= 0xFB;                                            // Stop Bit = 1 Bit
      U1LCR &= 0xF7;                                            // Parity = Disable
      U1LCR &= 0xBF;                                            // Disable Break Control
      U1LCR |= 0x80;                                            // Enable Programming of Divisor Latches
    
      // U1DLM:U1DLL = 60.00 MHz / [16 x Baud]
      //             = 60.00 MHz / [16 x 9600]
      //             = 390.6 = 391 = 0187H
      U1DLM = 0x00;                                             // Program Divisor Latch(391) for 9600 Baud
      U1DLL = 0x5D;
    
      U1LCR &= 0x7F;                                            // Disable Programming of Divisor Latches
    
      U1FCR |= 0x01;                                            // FIF0 Enable
      U1FCR |= 0x02;                                            // RX FIFO Reset
      U1FCR |= 0x04;                                            // TX FIFO Reset
      U1FCR &= 0x3F; 
    
    }
    
    void receive_gps_coord(void)
    {
    unsigned char p;
    int i,j;
    
    
    
    int gps=0;
    
    while(gps==0)
    {
    p=rxu0();
    while(p!='$')
    {
    p=rxu0();
    }
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='P')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='G')
    {
    }
    else
    continue;
    p=rxu0();
    if(p=='A')
    {
    while(p!=',')
    {
    p=rxu0();
    
    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    for(i=0;i<9;i++)
    {
    lat[i]=rxu0();
    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    p=rxu0();
    while(p!=',')
    {
    p=rxu0();
    }
    for(i=0;i<10;i++)
    {
    lon[i]=rxu0();
    }
    lat[9]='\0';
    lon[10]='\0';
    gps=1;
    }
    else
    continue;
    }
    
    
    }
    
    void sendsms(void)
    {
    sendstring(cmgf);
    txu1(0x0d);                                     // equivalent of 
    txu1(0x0a);                                     //   enter key
    delayfunc();
    sendstring(cmgs);
    txu1(0x0d);
    txu1(0x0a);  
    delayfunc();
    
    sendstring(lat);
    sendstring(msg);
    sendstring(lon);
    
    delayfunc();
    txu1(0x1a);
    delayfunc();
    txu1(0x1a);
    
    }
    The first thing to do is learn how to indent code properly. What indentation there is (most likely from code you copy/pasted from elsewhere) was recovered by me doing 'reply with quote' and then editing.

    This helps you understand what's going on, and makes it more likely people will help you when you post code.

    This copying is important, because if your chip is configured differently (say clock frequency), then all those bit calculations for setting up various dividers to get to a baud rate are going to be WRONG. You need to read and understand this stuff, you can't copy/paste your way through embedded code.


    Next, verify individually that sendsms() and receive_gps_coord() actually work.
    Try sending yourself a fixed buffer containing an example lat/lon message. Until this works, you're wasting effort getting anything else to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo