1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

GSM modem interfacing to AT89C51

Discussion in '8051/8951' started by amit251291, Jan 3, 2013.

  1. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    hello,
    I want to interface GSM modem to 8051. As other part of my project is in assembly language i am preferring the same programming language for this part too! I am NOT using any LCD to display Commands and responses.
    Can someone please share the ASSEMBLY LANGUAGE CODE TO SEND SMS BY USING GSM MODEM AND MICROCONTROLLER. please?

    Thank you :)
     
    Last edited: Jan 3, 2013
  2. WTP Pepper

    WTP Pepper Active Member

    Joined:
    Jan 24, 2012
    Messages:
    648
    Likes:
    41
    Location:
    UK
    I think once you read the AT SMS commands for your specific modem, then all will come clear. I have C routines to set up a uBlox modem and send SMS.
    Pretty easy to convert to asm as it's the commands needed that are hard to determine. create the message in an array, terminate with NULL and send sequentionally until NULL detected. Don't forget the CRTL(Z) or ASCII 26 at the end.
    If you do not know how to write a string to XRAM and then sequentially send it to a port in asm then you are stuffed. That is basic computing.

    /*********************************************************/
    void sms_message(unsigned char *num,unsigned char *data)
    /*********************************************************/
    {
    unsigned char str[20]={"AT+CMGS=,"};
    unsigned char i;
    i=0;


    sprintf(modem,"AT+CMGS=");
    send_modem(&modem[0]);


    WriteChar(port,'"');
    i=0;
    while (*(num+i) != '\0') // NULL
    {
    WriteChar(port,(unsigned char)toupper(*(num+i)));
    i++;
    }
    WriteChar(port,'"');
    WriteChar(port,CARRIAGE_RETURN);

    i=0;
    delay(0.5f);



    while (*(data+i) != '\0') // NULL
    {
    WriteChar(port,(unsigned char)toupper(*(data+i)));
    i++;
    }

    WriteChar(port,26);



    }
    /*************************************************/
    void send_modem(unsigned char *ch)
    /*************************************************/
    {
    unsigned char i;

    i=0;

    while ((*(ch+i)) != '\0') // NULL
    {
    WriteChar(port,(unsigned char)toupper(*(ch+i)));
    i++;
    }

    }
    /*********************/
    void modem_init(void)
    /*********************/
    {
    int i;

    delay(0.5f);
    printf("\n\rInitialising Modem.......");
    modem_buffer_pointer=0;
    sprintf(modem,"AT\r");
    send_modem(&modem[0]);
    delay(1.0f);
    if (modem_buffer_pointer==0)
    {
    printf("\n\rModem Not Responding");
    // done();
    }
    //return;
    sprintf(modem,"ATE0\r"); /*No echo */
    send_modem(&modem[0]);
    delay(1.0f);

    sprintf(modem,"ATS10=1\r"); /*immediate NO CARRIER */
    send_modem(&modem[0]);
    delay(1.0f);


    sprintf(modem,"ATS0=1\r"); /*auto answer */
    send_modem(&modem[0]);
    delay(1.0f);


    sprintf(modem,"AT+CMGF=1\r"); /*sms init */
    send_modem(&modem[0]);
    delay(1.0f);

    sprintf(modem,"AT+CLIP=1\r"); /*clid init */
    send_modem(&modem[0]);
    delay(1.0f);

    sprintf(modem,"AT+CNMI=0,2,0,0,0\r"); /*sms init with auto alert*/
    send_modem(&modem[0]);
    delay(1.0f);
    for (i=0;i<100;i++)
    {
    printf("%c",modem_buffer);
    }
    delay(1.0f);

    modem_buffer_pointer=0;

    }
     
  3. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    Thank you so much for this code.
    i had prepared my code before seeing your reply.
    i am posting my code here. its not showing any syntax error. but i am not sure whether its logically correct or not :confused:
    So please go through this code once and suggest me corrections if any :)

    Code (text):
    ORG 0000H
            MOV TMOD,#20H           //TIMER 1, MODE 2
            MOV TH1,#-3             //9600 BAUD RATE
            MOV SCON,#50H           //8 BIT, 1 STOP, EN ENABLED
            SETB TR1
                   
            MOV DPTR,#MSG1
            ACALL H1
            ACALL DELAY
            MOV DPTR,#MSG2
            ACALL H1
            ACALL DELAY
            MOV DPTR,#MSG3
            ACALL H1
            ACALL DELAY
            MOV DPTR,#MSG4
            ACALL H1
            ACALL DELAY
            MOV DPTR,#MSG5
            ACALL H1
            ACALL DELAY
    H1:     CLR A
            MOVC A,@A+DPTR
            JZ B1
            ACALL SEND
            INC DPTR
            SJMP H1
    B1:
            RET
    SEND:   MOV SBUF,A
    H2:     JNB TI,H2
            CLR TI
            RET
    DELAY:  MOV R3,#50H
    HERE2:  MOV R4,#50H
    HERE:   DJNZ R4,HERE
            DJNZ R3,HERE2
            RET
       

    ORG 300H
    MSG1: DB "AT",0DH
          DB 0H
    MSG2: DB "AT+CMGF=1",0DH
          DB 0H
    MSG3: DB "ATE=0",0DH
          DB 0H
    MSG4: DB "AT+CMGS=",'"8149111111"',0DH
          DB 0H
    MSG5: DB "TEXT",0X1A
          DB 0H

          END
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. rushi53

    rushi53 Member

    Joined:
    May 8, 2009
    Messages:
    112
    Likes:
    6
    Location:
    Pune, India

    Hi amit251291,
    I would like to suggest 2 things here,

    1st is that connect microcontroller circuit to your PC then go to Hyper terminal, set baud rate to 9600. Then check if these AT commands are received on hyper terminal.... If not then check your hardware.

    2nd thing, at the end of the msg1, msg2,3,4,5 you have sent 0DH.... I used to send 0AH, 0DH... try with this....
     
  6. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    hello rushi53,
    i din't get what 0AH, 0DH stand for? 0DH is for ctrl+m.
    Can you please share your code here?
    Thank you :)
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    0Ah is line feed and 0Dh is carraige return.... Modems only need 0Dh.. You code is fine.

    The only issue you may find is that modems are usually hardware handshake from the off. So you may need to hook the modem up to a PC and configure for 3 wire hook up...
     
  8. WTP Pepper

    WTP Pepper Active Member

    Joined:
    Jan 24, 2012
    Messages:
    648
    Likes:
    41
    Location:
    UK
    Your code looks workable. Check the assembler directive for DB. Some will do just that and allow a single byte in the definition whereas you are looking at a reserving a string. KEIL handles this correctly, but I have come across others that don't.

    As already said, hook up hyperterm and check what is being sent to the modem is correct. You may also want to start the whole conversation with AT and check it returns OK. Most autobaud modems use this to determine the speed you are sending at.
     
  9. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    Thank you WTP Pepper and Ian Rogers

    I have just made a correction in program. i put

    Stay: sjmp Stay

    just after the last ACALL DELAY command. This i did by thinking that microcontroller would enter in subroutines after execution of the last ACALL DELAY command. whether this correction is right?


    I dint get this.. :( I had connected my modem only to PC by using DB9 connector.. Sent AT commands from hyper terminal and MODEM was working fine..
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Most modems use "Hadware Handshaking" RTS,CTS, DTR etc... To use on a micro, its easier to use "Software Handshaking" 3 wire hook up. It will be in the datasheet.
     
  11. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    hello Ian Rogers ,

    Actually my modem is sim300. When i see hardware of this modem, i saw 1 MAX232 IC on it which is further connected to RS232 Female connector. Also only 3 pins are conneted between MAX 232 and RS232 connector i.e TXD, RXD, and ground. I can see this on PCB. In short, only TXD, RXD, and ground come out of the modem via RS232. Other pins of RS232 connector have been left unconnected..
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Last edited: Jan 10, 2013
  13. WTP Pepper

    WTP Pepper Active Member

    Joined:
    Jan 24, 2012
    Messages:
    648
    Likes:
    41
    Location:
    UK
    You can fool the modem by wiring certain CTS/DTR lines together to avoid hardware handshaking. I cannot remember exactly what needs doing on a DB9, but can find out if wanted.

    Ian Rogers. Yep correct. You need to run these devices off a good PSU or as I have done before, a slowish charging Li-Ion/Li-Pol cell as they can supply the high instantaneous current required. Especially when a call is being made or the device is registering on the network. Any other cell technology apart from maybe lead acid (bulky) is impractical. Mobile phones use Li-ion/Li-Pol battery technology for a reason.

    A poor power supply can lock up a modem and send you looking for a protocol solution that isn't needed.
     
  14. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    hello, Ian Rogers & WTP Pepper,

    Actually my modem came with a adapter, so i don't think that there is problem of supply... :|

    The last program which i have posted here, is not working.



    GSM modem has following on-board connections:

    MAX232----->> RS232 (Female connector)

    pin 7 ---> Pin 2
    pin 8 ---> Pin 3

    So i have done following connections after this:

    RS232 (Female connector) --->> RE232(PCB male connector ) ( This o did because it was difficult to take connections from on-board Female connetor)

    I took out following pins from RS232 male connector and connected to microcontroller:

    RS232(PCB male connector ) ---> AT89C51
    Pin 2 --> Pin 11
    Pin 3--> Pin 10
    Pin 5 Grounded.

    Rest AT89C51 connetions:

    Crystal oscillator 12 Mhz.
    EN (Pin 31) -VCC
    Pin 40-- VCC
    Pin 20-- Gnd


    Are these connetions are correct??
    why my program is not working ??
     
  15. rushi53

    rushi53 Member

    Joined:
    May 8, 2009
    Messages:
    112
    Likes:
    6
    Location:
    Pune, India
    Amit, what you have written seems very confusing to me so please post circuit diagram

    And reverse the above connections and try again.
    I mean,
    Pin 2 --> Pin 10
    Pin 3--> Pin 11
     
  16. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    GSMCIRCUIT1.jpg

    here is my circuit diagram... please let me know where i am going wrong...
     
  17. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    hi Rushikesh
    please check above diagram and let me know...
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    I'm drawn to the swapping of the pins as well.... "Null modem" cables are swapped but modem cables are usually pin to pin...
     
  19. amit251291

    amit251291 New Member

    Joined:
    Jan 3, 2013
    Messages:
    9
    Likes:
    0
    I didn't get what you want to say exactly sir :confused:
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Just try swapping Rx and Tx to see if it works..
     
  21. manasap

    manasap New Member

    Joined:
    Feb 14, 2013
    Messages:
    1
    Likes:
    0
    Hey amit,
    Did it work for u?
     

Share This Page