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
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
 

thedemme

Newbie level 3
Newbie level 3
Joined
Feb 8, 2010
Messages
4
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,283
Activity points
1,320
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);
    }
      
 }



 

}
 

ZASto

Advanced Member level 3
Advanced Member level 3
Joined
Jan 9, 2008
Messages
822
Helped
158
Reputation
321
Reaction score
151
Trophy points
1,323
Location
Messy Corner, 44°47'31.56"N, 20°28'9.16"E
Activity points
5,765
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
 

thedemme

Newbie level 3
Newbie level 3
Joined
Feb 8, 2010
Messages
4
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,283
Activity points
1,320
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;

}
 

Buriedcode

Full Member level 6
Full Member level 6
Joined
May 6, 2004
Messages
357
Helped
43
Reputation
86
Reaction score
7
Trophy points
1,298
Location
London
Activity points
8,887
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.
 

hangman

Newbie level 1
Newbie level 1
Joined
Dec 17, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,287
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);
}
 

seadolphine2000

Advanced Member level 3
Advanced Member level 3
Joined
Apr 12, 2005
Messages
880
Helped
122
Reputation
244
Reaction score
87
Trophy points
1,308
Activity points
7,372
To delete the message stored in location number 1, use the command:
AT+CMGD=1
 

DineshSL

Advanced Member level 1
Advanced Member level 1
Joined
May 10, 2010
Messages
403
Helped
121
Reputation
242
Reaction score
116
Trophy points
1,343
Location
Sri Lanka
Activity points
3,551
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:

mustasktogain

Newbie level 1
Newbie level 1
Joined
Aug 5, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,284
Re: at command code

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

Kirk Macaraeg

Newbie level 1
Newbie level 1
Joined
Jul 20, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
16
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..
 

fouwad

Full Member level 4
Full Member level 4
Joined
Nov 29, 2009
Messages
199
Helped
19
Reputation
38
Reaction score
17
Trophy points
1,298
Location
Pakistan
Activity points
2,466
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;

}
 

Anoukh.Ashley

Newbie level 2
Newbie level 2
Joined
May 20, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
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.
 

milan.rajik

Banned
Advanced Member level 5
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Read SMS. Extract msg part of SMS (strip off +CMGR header). Convert msg '1' to 1 by subtracting 48 from '1' using msg - 48;.
 

Anoukh.Ashley

Newbie level 2
Newbie level 2
Joined
May 20, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
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?
 

fouwad

Full Member level 4
Full Member level 4
Joined
Nov 29, 2009
Messages
199
Helped
19
Reputation
38
Reaction score
17
Trophy points
1,298
Location
Pakistan
Activity points
2,466
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

Top