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);
}
}
}
As far as I can see, you have serious problems with timing. You will, for sure, miss some characters from your message.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); } } }
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;
}
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
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;
}
zasto said:As far as I can see, you have serious problems with timing. You will, for sure, miss some characters from your message.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); } } }
I've marked the delays that you need to omit from your code.
HTH
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;
}
do you have any codes for sending a msg and deleting.. ireally need your help..pls..
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..
Read SMS. Extract msg part of SMS (strip off +CMGR header). Convert msg '1' to 1 by subtracting 48 from '1' using msg - 48;.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?