Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to write the AT-commands in mikroC language?

Status
Not open for further replies.

chancelier

Junior Member level 1
Joined
Jan 26, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
tunisia
Activity points
1,433
at command code

Hi

I have a sagem mobile and I want to connect it with PIC16F877 via serial cable. My problem is:
How to write the AT-commands in mikroC language?

For example:
AT+CMGR=1 --> This command will read the sms stored in location 1.

I want to know how to translate this command into mikroC?
can any one give me an example of code

please relpy soon
 

Re: at command code

Hi!....
We actually have the same problem, and I'm still searching for the right code.....
I'm using P16F877A and SONY ERICSON T230 phone...


Here's my code.... It's not actually working, but I'm still working on it....

Initially, the message storage in the phone memory is empty and the program tries to read and read and read until there is such a message located at index 1 in the message storage.... It reads the message and displays it in PORTB.... Assuming that the text message received is "1", the value of PORTB should also be 1... And then, finally, it deletes the message to make the message memory empty for the next message to be read, the same manner as the first text message....






Code:
 unsigned char* MSG1 = "AT+CPMS=\"ME\"\n";  
unsigned char* MSG2 = "AT+CMGR=1";
unsigned char* MSG3 = "AT+CMGD=1";
unsigned char* MSG4 = "\r";   
char message;

void main() {
int ctr;
TRISD= 0x00;
TRISB=0x00;



UART1_Init(9600);               // Initialize UART module 9600 bps

 while(1)
 {
     for(ctr=0;ctr<3;ctr++)
    {
     PORTB = 0X01;                        // Test to see if programme 

started running.
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }



     UART1_Write_Text(MSG1); //selects the storage to be read as the 

phone memory
     Delay_ms(1000);
     
     UART1_Write_Text(MSG4);  // ENTER
     Delay_ms(1000);

     UART1_Write_Text(MSG2);   // Command for reading message on the 

phone memory at index 1;
     Delay_ms(1000);

      UART1_Write_Text(MSG4);   // ENTER
      Delay_ms(2000);
 
    
    
     if(UART1_Data_Ready()==1)
       {     
             message = UART1_Read();
       }

             PORTB = message;   //dISLAYS the message received in PORTB
 
                                    // The message should be "1", "2", Or  "4" 
     


   




     for(ctr=0;ctr<3;ctr++)                 //end of program execution
    {
     PORTB = 0X01;         
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }
      
 }



 

}
 

Re: at command code

thedemme said:
Hi!....
We actually have the same problem, and I'm still searching for the right code.....
I'm using P16F877A and SONY ERICSON T230 phone...


Here's my code.... It's not actually working, but I'm still working on it....

Initially, the message storage in the phone memory is empty and the program tries to read and read and read until there is such a message located at index 1 in the message storage.... It reads the message and displays it in PORTB.... Assuming that the text message received is "1", the value of PORTB should also be 1... And then, finally, it deletes the message to make the message memory empty for the next message to be read, the same manner as the first text message....






Code:
 unsigned char* MSG1 = "AT+CPMS=\"ME\"\n";  
unsigned char* MSG2 = "AT+CMGR=1";
unsigned char* MSG3 = "AT+CMGD=1";
unsigned char* MSG4 = "\r";   
char message;

void main() {
int ctr;
TRISD= 0x00;
TRISB=0x00;



UART1_Init(9600);    // Initialize UART module 9600 bps

 while(1)
 {
     for(ctr=0;ctr<3;ctr++)
    {
     PORTB = 0X01;       // Test to see if programme started running.
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }



     UART1_Write_Text(MSG1); //selects the storage to be read as the phone memory
     Delay_ms(1000); <===  TAKE THIS ONE OUT

    // INSTEAD THE 1 SEC DELAY YOU SHOULD WAIT FOR "OK" STRING
     
     UART1_Write_Text(MSG4);  // ENTER
     Delay_ms(1000); <=== THIS ONE, ALSO

    // INSTEAD THE 1 SEC DELAY YOU SHOULD WAIT FOR "OK" STRING

     UART1_Write_Text(MSG2);   // Command for reading message on the phone memory at index 1;
     Delay_ms(1000); <=== THIS ONE, ALSO

      UART1_Write_Text(MSG4);   // ENTER
      Delay_ms(2000); <=== THIS ONE, ALSO
 
    
    
     if(UART1_Data_Ready()==1)
       {     
             message = UART1_Read();
       }

             PORTB = message;   //dISLAYS the message received in PORTB
 
                                    // The message should be "1", "2", Or  "4" 
     


   




     for(ctr=0;ctr<3;ctr++)                 //end of program execution
    {
     PORTB = 0X01;         
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }
      
 }



 

}
As far as I can see, you have serious problems with timing. You will, for sure, miss some characters from your message.

I've marked the delays that you need to omit from your code.

HTH
 

Re: at command code

Hello!....
Try this code!... It's working now!....


Code:
 unsigned char* MSG1 = "AT";
unsigned char* MSG2 = "at+CMGF=1";
unsigned char* MSG3 = "AT+CPMS=\"ME\"\n";
unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
unsigned char* MSG5 = "AT+CMGR=1";
unsigned char* MSG6 = "AT+CMGD=1";
unsigned char output[70];
int i=0,flag=0;
void main() {
int ctr,x=0;
TRISB=0x02;
TRISD=0;
PORTB=0;
PORTD=0;

UART1_Init(9600);

     for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }

      //UART1_Write_Text(MSG1);    //at
      //Delay_ms(1000);
      //UART1_Write(0X0D);      // ENTER
      //Delay_ms(1000);

      UART1_Write_Text(MSG2);    //cmgf=1
      Delay_ms(1000);
      UART1_Write(0X0D);      // ENTER
      Delay_ms(1000);

      UART1_Write_Text(MSG3);    //cpms
      Delay_ms(1000);
      UART1_Write(0X0D);        // ENTER
      Delay_ms(1000);
    a:
      UART1_Write_Text(MSG5);    //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      Delay_ms(3000);




     UART1_Write_Text(MSG5); //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      
      


     do{

        UART1_Read_Text(output, "OK", 65);  reads text until 'OK' is 

found
        PORTD=output[x];
        UART1_Write_Text(output);           // sends back text
        
       }
        
          x++ ;


     } while(x<2);

       Delay_ms(1000);

      Delay_ms(3000);
      


       for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }
     
      x=0;
      goto a;

}

Added after 1 minutes:

chancelier said:
Hi

I have a sagem mobile and I want to connect it with PIC16F877 via serial cable. My problem is:
How to write the AT-commands in mikroC language?

For example:
AT+CMGR=1 --> This command will read the sms stored in location 1.

I want to know how to translate this command into mikroC?
can any one give me an example of code

please relpy soon








Hello!....
Try this code!!!.... It's working now!...

Code:
         unsigned char* MSG1 = "AT";
unsigned char* MSG2 = "at+CMGF=1";
unsigned char* MSG3 = "AT+CPMS=\"ME\"\n";
unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
unsigned char* MSG5 = "AT+CMGR=1";
unsigned char* MSG6 = "AT+CMGD=1";
unsigned char output[70];
int i=0,flag=0;
void main() {
int ctr,x=0;
TRISB=0x02;
TRISD=0;
PORTB=0;
PORTD=0;

UART1_Init(9600);

     for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }

      //UART1_Write_Text(MSG1);    //at
      //Delay_ms(1000);
      //UART1_Write(0X0D);      // ENTER
      //Delay_ms(1000);

      UART1_Write_Text(MSG2);    //cmgf=1
      Delay_ms(1000);
      UART1_Write(0X0D);      // ENTER
      Delay_ms(1000);

      UART1_Write_Text(MSG3);    //cpms
      Delay_ms(1000);
      UART1_Write(0X0D);        // ENTER
      Delay_ms(1000);
    a:
      UART1_Write_Text(MSG5);    //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      Delay_ms(3000);




     UART1_Write_Text(MSG5); //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      
      


     do{

        UART1_Read_Text(output, "OK", 65);  reads text until 'OK' is 

found
        PORTD=output[x];
        UART1_Write_Text(output);           // sends back text
        
       }
        
          x++ ;


     } while(x<2);

       Delay_ms(1000);

      Delay_ms(3000);
      


       for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }
     
      x=0;
      goto a;

}

Added after 3 minutes:

zasto said:
thedemme said:
Hi!....
We actually have the same problem, and I'm still searching for the right code.....
I'm using P16F877A and SONY ERICSON T230 phone...


Here's my code.... It's not actually working, but I'm still working on it....

Initially, the message storage in the phone memory is empty and the program tries to read and read and read until there is such a message located at index 1 in the message storage.... It reads the message and displays it in PORTB.... Assuming that the text message received is "1", the value of PORTB should also be 1... And then, finally, it deletes the message to make the message memory empty for the next message to be read, the same manner as the first text message....






Code:
 unsigned char* MSG1 = "AT+CPMS=\"ME\"\n";  
unsigned char* MSG2 = "AT+CMGR=1";
unsigned char* MSG3 = "AT+CMGD=1";
unsigned char* MSG4 = "\r";   
char message;

void main() {
int ctr;
TRISD= 0x00;
TRISB=0x00;



UART1_Init(9600);    // Initialize UART module 9600 bps

 while(1)
 {
     for(ctr=0;ctr<3;ctr++)
    {
     PORTB = 0X01;       // Test to see if programme started running.
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }



     UART1_Write_Text(MSG1); //selects the storage to be read as the phone memory
     Delay_ms(1000); <===  TAKE THIS ONE OUT

    // INSTEAD THE 1 SEC DELAY YOU SHOULD WAIT FOR "OK" STRING
     
     UART1_Write_Text(MSG4);  // ENTER
     Delay_ms(1000); <=== THIS ONE, ALSO

    // INSTEAD THE 1 SEC DELAY YOU SHOULD WAIT FOR "OK" STRING

     UART1_Write_Text(MSG2);   // Command for reading message on the phone memory at index 1;
     Delay_ms(1000); <=== THIS ONE, ALSO

      UART1_Write_Text(MSG4);   // ENTER
      Delay_ms(2000); <=== THIS ONE, ALSO
 
    
    
     if(UART1_Data_Ready()==1)
       {     
             message = UART1_Read();
       }

             PORTB = message;   //dISLAYS the message received in PORTB
 
                                    // The message should be "1", "2", Or  "4" 
     


   




     for(ctr=0;ctr<3;ctr++)                 //end of program execution
    {
     PORTB = 0X01;         
     Delay_ms(1000);
     PORTB = 0;
     Delay_ms(1000);
    }
      
 }



 

}
As far as I can see, you have serious problems with timing. You will, for sure, miss some characters from your message.

I've marked the delays that you need to omit from your code.

HTH










Thanks!.... I have a working program now..... It reads the message on the phone through the PIC and displays the message being read....

Code:
    unsigned char* MSG1 = "AT";
unsigned char* MSG2 = "at+CMGF=1";
unsigned char* MSG3 = "AT+CPMS=\"ME\"\n";
unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
unsigned char* MSG5 = "AT+CMGR=1";
unsigned char* MSG6 = "AT+CMGD=1";
unsigned char output[70];
int i=0,flag=0;
void main() {
int ctr,x=0;
TRISB=0x02;
TRISD=0;
PORTB=0;
PORTD=0;

UART1_Init(9600);

     for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }

      //UART1_Write_Text(MSG1);    //at
      //Delay_ms(1000);
      //UART1_Write(0X0D);      // ENTER
      //Delay_ms(1000);

      UART1_Write_Text(MSG2);    //cmgf=1
      Delay_ms(1000);
      UART1_Write(0X0D);      // ENTER
      Delay_ms(1000);

      UART1_Write_Text(MSG3);    //cpms
      Delay_ms(1000);
      UART1_Write(0X0D);        // ENTER
      Delay_ms(1000);
    a:
      UART1_Write_Text(MSG5);    //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      Delay_ms(3000);




     UART1_Write_Text(MSG5); //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      
      


     do{

        UART1_Read_Text(output, "OK", 65);  reads text until 'OK' is 

found
        PORTD=output[x];
        UART1_Write_Text(output);           // sends back text
        
       }
        
          x++ ;


     } while(x<2);

       Delay_ms(1000);

      Delay_ms(3000);
      


       for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }
     
      x=0;
      goto a;

}
 

Hi,

Just my two cents as there is someone else trying the same thing on another thread and I was trying to help.

When you send an AT command, seeing as almost all commands start with 'AT', then instead of including AT in every message, wouldn't it save a bit more memory (ROM) to have your routine to send that at the start then your AT command, then send the carriage return? example:

UART1_Write_Text(AT);
UART1_Write_Text(MSG1);
UART1_Write(0x0D);

I realise that is obvious, and simple but if you have to send 50 messages....then you're using up an extra 200 bytes of rom just for the AT at the start.
 
do you have any codes for sending a msg and deleting.. ireally need your help.. :) pls..

void main() {
UART1_Init(9600); // Initialize UART module at 4800 bps
UART1_Write_Text("AT+CMGS=");
Delay_ms(1000);
UART1_Write(0x22); //" double qoute
Delay_ms(2000);
UART1_Write_Text("66832387739"); // Your Telephone Number
Delay_ms(2000);
UART1_Write(0x22); //" double qoute
UART1_Write(0x0D); //<CR> mean Enter
Delay_ms(2000);
UART1_Write(0x22);
UART1_Write_Text("SMS Test "); //My Text Test
UART1_Write(0x22); //" double qoute
Delay_ms(2000);
UART1_Write(26); //Ctr +Z
Delay_ms(2000);
}
 

To delete the message stored in location number 1, use the command:
AT+CMGD=1
 

Hi,
I think hangman's method to send SMS will only work for Text mode SMS. It will not work for phones like T230 since they only support for PDU sms. Then you have to convert all send info to PDU string and must use PDU-SMS format.

AT command set for SE T230 can be found on following link;
**broken link removed**

Read the best introduction to 'SMS and the PDU format' on the web here;
**broken link removed**

Wish you success !
 
Last edited:

Re: at command code

is this ur full code? where is the library declaration?
 

Re: at command code

Thanks!.... I have a working program now..... It reads the message on the phone through the PIC and displays the message being read....

Code:
    unsigned char* MSG1 = "AT";
unsigned char* MSG2 = "at+CMGF=1";
unsigned char* MSG3 = "AT+CPMS=\"ME\"\n";
unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
unsigned char* MSG5 = "AT+CMGR=1";
unsigned char* MSG6 = "AT+CMGD=1";
unsigned char output[70];
int i=0,flag=0;
void main() {
int ctr,x=0;
TRISB=0x02;
TRISD=0;
PORTB=0;
PORTD=0;

UART1_Init(9600);

     for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }

      //UART1_Write_Text(MSG1);    //at
      //Delay_ms(1000);
      //UART1_Write(0X0D);      // ENTER
      //Delay_ms(1000);

      UART1_Write_Text(MSG2);    //cmgf=1
      Delay_ms(1000);
      UART1_Write(0X0D);      // ENTER
      Delay_ms(1000);

      UART1_Write_Text(MSG3);    //cpms
      Delay_ms(1000);
      UART1_Write(0X0D);        // ENTER
      Delay_ms(1000);
    a:
      UART1_Write_Text(MSG5);    //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      Delay_ms(3000);




     UART1_Write_Text(MSG5); //cmgr
      Delay_ms(1000);
      UART1_Write(0X0D);     // ENTER
      
      


     do{

        UART1_Read_Text(output, "OK", 65);  reads text until 'OK' is 

found
        PORTD=output[x];
        UART1_Write_Text(output);           // sends back text
        
       }
        
          x++ ;


     } while(x<2);

       Delay_ms(1000);

      Delay_ms(3000);
      


       for(ctr=0;ctr<3;ctr++)
     {
       PORTB=0x01;
       Delay_ms(500);
       PORTB=0;
       Delay_ms(500);
     }
     
      x=0;
      goto a;

}

HI i tried this code in mikroC, this doesn't actually work..there is an error..please help me out..it's error is "while expected, but 'x' found" then it highlights the curly brace above x++; statement..am i missing something??please help out..thanks..
 

see your code mentioned below, it has a syntax error with } of do while loop


do{

UART1_Read_Text(output, "OK", 65); reads text until 'OK' is

found
PORTD=output[x];
UART1_Write_Text(output); // sends back text

}

x++ ;


} while(x<2);

Delay_ms(1000);

Delay_ms(3000);



for(ctr=0;ctr<3;ctr++)
{
PORTB=0x01;
Delay_ms(500);
PORTB=0;
Delay_ms(500);
}

x=0;
goto a;

}
 

Can anyone help me to receive an SMS message tell me how to save the SMS in a variable so that I can send signals depending on the message. For example if the SMS says "1" I want to send a signal to RA0 pin. If it is "2" i want to send signal to RA1.
It would be grateful if I can know how to save the SMS as an integer in a variable.
 

Read SMS. Extract msg part of SMS (strip off +CMGR header). Convert msg '1' to 1 by subtracting 48 from '1' using msg - 48;.
 

Read SMS. Extract msg part of SMS (strip off +CMGR header). Convert msg '1' to 1 by subtracting 48 from '1' using msg - 48;.

Can you give me a Mikro C code example. It would be a great help.

do{

UART1_Read_Text(output, "OK", 65);
PORTD=output[x];
UART1_Write_Text(output); // sends back text

}

I should run the above loop to see if I have received a message? How do I extract the "1" or "2" or "3" that has been typed in the text message?
 

you can count the number of characters after which the TEXT start to appear in your recieving sms, u can do this by connecting your module or phone with hyperterminal and simply count the characters, discard the previous characters and save the incoming text into a string, or a character, now u can simply use a switch statement with receicved character and perform the respective operation
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top