help over microcontroller code

Discussion in 'Programmer's Corner' started by adityabhandari54, Jun 16, 2014.

  1. adityabhandari54

    Thread Starter New Member

    Jun 10, 2014
    7
    0
    Hello,
    i am facing problems understanding the following C code for microcontroller. Actually the task is to configure an external GSM modem via serial microcontroller port but i am not able to get the coding exactly. can someone please help me understand it. also it would be nice if some one can give me the assemble code for same.

    Code ( (Unknown Language)):
    1.  
    2. void gsmcmdsend(unsigned char *cmd);
    3.  
    4. unsigned char gsm_cmd3[]="AT&W";
    5. unsigned char gsm_cmd4[]="AT+CMGF";
    6. void gsmcmdsend(unsigned char *cmd)        
    7. {
    8.     unsigned char i;
    9.     for(i=0;*cmd!='\0';i++)
    10.     {
    11.         SBUF=*cmd;
    12.            while(TI==0);
    13.         TI=0;
    14.         cmd++;
    15.         }
    16.         SBUF=0x0A;
    17.         while(TI==0);
    18.         TI=0;
    19.         SBUF=0x0D;
    20.         while(TI==0);
    21.         TI=0;
    22.         while(RI==0);
    23.         RI=0;
    24. }
    25.  
    26. void main()
    27. {
    28. gsmcmdsend(gsm_cmd3);
    29. gsmcmdsend(gsm_cmd4);
    30. }
    31.  
     
    Last edited by a moderator: Jun 16, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The code sets up two string buffers: gsm_cmd3 and gsm_cmd4. The gsmcmdsend function sends the contents of one of these buffers one at a time until it reaches the null character (/0), then exits.

    On each loop, the gsmcmdsend function also sends a newline character (0x0A) followed by a carriage return (0x0D) after each character sent. The function then seems to wait for something to be sent back, but without knowing more about your setup, it would be impossible to say for sure.

    No one can give you "assemble" code (even if they wanted to) as you haven't said anything specific about your hardware.
     
  3. adityabhandari54

    Thread Starter New Member

    Jun 10, 2014
    7
    0
    this is the entire program of my project. The project is SMS based homeautomation which activates or deactivates the home appliance connected to a relay when a particulat text is sent. also there is a LCD connected to see the process. I am getting all the portion of LCD interface but am unknown to the serial interface part. All i want to do is convert the code below to "assembly"

    #include<reg51.h>
    sbit rs=P3^7;
    sbit rw=P3^6;
    sbit e=P3^5;
    sbit fan=P1^0;
    sbit lgt=P1^1;
    void gsmcmdsend(unsigned char *cmd);
    void cmm(char value);
    void dat(char value);
    void compare();
    unsigned char card_id[96];
    unsigned char header[]="EFY";
    unsigned char gsm_cmd3[]="AT&W";
    unsigned char gsm_cmd4[]="AT+CMGF";
    void gsmcmdsend(unsigned char *cmd)
    {
    unsigned char i;
    for(i=0;*cmd!='\0';i++)
    {
    SBUF=*cmd;
    while(TI==0);
    TI=0;
    cmd++;
    }
    SBUF=0x0A;
    while(TI==0);
    TI=0;
    SBUF=0x0D;
    while(TI==0);
    TI=0;
    while(RI==0);
    RI=0;
    }
    void delay(int count) //Function to provide delay
    {
    int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
    }
    void cmm(char value)
    {
    P2 = value;
    rs=0;
    rw=0;
    e=1;
    delay(1);
    e=0;
    delay(1);
    return;
    }
    void dat(char value)
    {
    P2 =value;
    rs=1;
    rw=0;
    e=1;
    delay(1);
    e=0;
    delay(1);
    return;
    }
    void lcdin()
    { cmm(0x38);
    cmm(0x0e);
    cmm(0x01);
    cmm(0x06);
    cmm(0x85);
    return;
    }
    void recieve() //Function to recieve data serialy from RS232
    {
    unsigned char k;
    for(k=0;k<=87;k++)
    {
    while(RI==0);
    card_id[k]=SBUF;
    RI=0;
    }
    }
    void main()
    {
    int l,i;
    P1=0x00;
    lcdin();
    TMOD=0x20; //Enable Timer 1
    TH1=0XFD;
    SCON=0x50;
    TR1=1;
    gsmcmdsend(gsm_cmd3);
    cmm(0x01);
    gsmcmdsend(gsm_cmd4);
    cmm(0x01);
    cmm(0x85);
    for(i=0;i<3;i++)
    {dat(header);}
    cmm(0xc0);
    while(1)
    {
    recieve();
    for(l=83;l<=87;l++)
    {
    dat(card_id[l]);
    }
    compare();
    }
    }
    void compare()
    {
    if ((card_id[83]=='f')&&(card_id[84]=='a') && (card_id[85]=='n') && (card_id[86]=='o') && (card_id[87]=='n'))
    fan=1;
    else if((card_id[83]=='f')&&(card_id[84]=='a') && (card_id[85]=='n') && (card_id[86]=='o') && (card_id[87]=='f'))
    fan=0;
    else if((card_id[83]=='l')&&(card_id[84]=='g') && (card_id[85]=='t') && (card_id[86]=='o') && (card_id[87]=='n'))
    lgt=1;
    else if((card_id[83]=='l')&&(card_id[84]=='g') && (card_id[85]=='t') && (card_id[86]=='o') && (card_id[87]=='f'))
    lgt=0;
    }

    end; //end of program
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Simple. Compile your program and look at the dissasembled code in your debugger.
     
Loading...