Sie sind auf Seite 1von 24

putc(26);

//send the msg with a CNTL-Z

am new to PIC world and also I am moderate on C/C++. I have already communicated GSM Modem (SIMCOM300) using MS VB6.0 from PC successfully. Now I tried to communicate GSM Modem from PIC16F877A. I can able to send and receive sms, ring tone from/to GSM Modem SIM300 and PIC16F877A. All SMS commands works fine. But, when I issue "AT+CLCC" Command, it also works and also the commands followed by are also executed well, but it didn't come out from the loop. I have to switch-off the power and restart. Then only PIC process the command. Can you pl help me to locate the issue and guide me to solve this problem. I will be very humble and thankful to you if anyone provide help to solve this issue. PIC16F877A HI-TECH C PRO Compiler (MPLAB v8.63) SIMCOM300 GSM Modem LCD JHD 162A

Code C - [expand]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Code: #include #include #include #include #include <stdio.h> <htc.h> "usart.h" "lcd.h" "string.h" LVPDIS);

__CONFIG(HS & WDTDIS & UNPROTECT & unsigned char gsmInput[60]; unsigned int i=0; unsigned int lenOfGSMInput = 0; bit OK; bit Error; bit Ring;

void showGSM_DATA(char GSM_DATA[]){ //to show the GSM OUTPUT after eliminating the chars '\r' and '\n' if (strcmp(GSM_DATA, "OK") == 0){ OK = 1; } else if (strcmp(GSM_DATA, "ERROR") == 0){ Error = 1; } else if (strcmp(GSM_DATA, "RING") == 0){ Ring = 1; GSM_DATA[0] = '\x00'; puts("AT+CLCC"); putch(0x0D);

Code C - [expand]
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 } else{ lcd_clear(); lcd_goto(0); lcd_puts(GSM_DATA); } GSM_DATA[0] = '\x00'; } // end function showGSM_DATA void main(int argc, char* argv[]){ unsigned char input; INTCON=0; // purpose of disabling the interrupts.

lcd_init(); // initiate LCD init_comms(); puts("ATE0"); putch(0X0D); puts("AT+CMGF=1"); putch(0X0D); while(1){ input = getch(); switch(input){ case '\x0A': // if line feed detected in the GSM output i.e. '\n' break; case '\x0D': // if carriage return detected i.e. '\r' gsmInput[i] = '\x00'; lenOfGSMInput = strlen(gsmInput); if (lenOfGSMInput > 0){ i = 0; lenOfGSMInput = 0; showGSM_DATA(gsmInput); } break; case '\x3E': // if greater-sign (in order to send SMS) break; default: // if characters received gsmInput[i] = input; i++; break; }//end brace for switch } //end brace for while loop }//end brace for main // read a response from the GSM // set up the USART - settings defined in usart.h

I have changed the code but the same result. I worked with hyperterminal also. It works fine. No problem there. I think problem in my code. I sent you the entire code for your review. Pl go thro' my code and try to find out the problem. I need your help. Pl. For your information: The following code works fine. (I have enabled Clip using AT+CLIP=1 to avoid AT+CLCC command. Before this, If I disconnect the call after processing AT+CLCC response (+CLCC : 1,1, ..), I cannot receive "NO CARRIER" message. Now I receive "NO CARRIER" because of "AT+CLIP" command) Just display the GSM_DATA (No data validation): void showGSM_DATA(char GSM_DATA[]){ //to show the GSM OUTPUT after eliminating the chars '\r' and '\n' if (strcmp(GSM_DATA, "OK") == 0){ OK = 1; if (strcmp(NextCmd, "CMGF") == 0){ strcpy(NextCmd, "CLIP"); puts("AT+CMGF=1"); putch(0X0D); } if (strcmp(NextCmd, "CLIP") == 0){ *NextCmd = 0; puts("AT+CLIP=1"); putch(0X0D); } return; } if (strcmp(GSM_DATA, "ERROR") == 0){ Error = 1; *GSM_DATA = 0; return; } if(strlen(GSM_DATA) > 6){ lcd_clear(); lcd_goto(0); lcd_puts(GSM_DATA); } *GSM_DATA = 0; return; } // end function showGSM_DATA

*** The following also works fine but only one time(Startup Commands like ATE0, AT+CMGF=1, AT+CLIP=1) and Any Phone/SMS received (After Successful Startup Commands Execution). If I send another SMS/Phone nothing happen. I have to restat the device. GSM Modem working correctly. Just I add substring function. Thats all: if(strlen(GSM_DATA) > 6){ substring(0,14,GSM_DATA); } void substring(size_t start, size_t stop, char src[]){ int j = 0; if (start >= stop){ //return substringOfInput; return; } for(i = start;i < stop; i++){ substringOfInput[j] = src[i]; j++; } substringOfInput[j] = '\x00'; lcd_clear(); lcd_goto(0); lcd_puts(substringOfInput); *src=0; *substringOfInput=0; j=0; return; } Can you pl help me? Hi seadolphine, I found the bug I made in the code inside "substring" function. I already declared "i" as global variable. I removed and also change the code line void substring(size_t start, size_t stop, char src[]){ int j = 0; if (start >= stop){ //return substringOfInput; return;

} for(i = start;i < stop; i++){ substringOfInput[j] = src[i]; j++; } substringOfInput[j] = '\x00'; lcd_clear(); lcd_goto(0); lcd_puts(substringOfInput); *src=0; *substringOfInput=0; j=0; return; } to for(int i = start;i < stop; i++){ Now everything works fine. Great Thanks.

#include<16f877a.h> #include"pic1.h" #include"lcd.h" char ch; void main() { lcdinit(); rc3=0; printf("AT\n\r"); while(1) { ch=getch(); printc(ch); delay_ms(200); } } Sample code // RX DATA #INT_RDA void READ_RS232(){ char c; c=getc(GSM); //fprintf(DEBUG"%c",c); buffer[buffer_index] = c; buffer_index++; setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); set_timer1(49910); // Timeout = 50 ms }

casn anyone help me im in the deadlin but my problem is i can communicate my mobile phone to the phone connected to the picsms.JPG and here is my code #define TRIGGER PORTD.F1 // set porta/f5 as output unsigned int index=0,count=0,address=0,addrLength=247,intFlag=0 ; unsigned short intTimer; unsigned char msg[30], mess[20]; void void void void void void textMode(); phoneMem(); readMsg(); delMsg(); check_message(); SMSProcess();

void interrupt() { intTimer++; PIR1.TMR1IF=0; if(intTimer%10==0){ if(intTimer%20==0){ intFlag=1; } } } //SMS processing void textMode() { // Set message format as textmode Usart_Write('A'); Usart_Write('T'); Usart_Write(0x0D); delay_ms(500); Usart_Write('A'); Usart_Write('T'); Usart_Write('+'); Usart_Write('C'); Usart_Write('M'); Usart_Write('G'); Usart_Write('F'); Usart_Write('='); Usart_Write('1'); Usart_Write(0x0D); delay_ms(1000); } void ckpd() { // Received read message Usart_Write('A');

Usart_Write('T'); Usart_Write('+'); Usart_Write('C'); Usart_Write('K'); Usart_Write('P'); Usart_Write('D'); Usart_Write('='); Usart_Write('1'); Usart_Write(0x0D); } void phoneMem() { // Set preferred message storage as Phone Memory Usart_Write('A'); Usart_Write('T'); Usart_Write('+'); Usart_Write('C'); Usart_Write('P'); Usart_Write('M'); Usart_Write('S'); Usart_Write('='); Usart_Write('"'); Usart_Write('M'); Usart_Write('E'); Usart_Write('"'); Usart_Write(0x0D); delay_ms(1000); }

void readMsg() { // Received read message Usart_Write('A'); Usart_Write('T'); Usart_Write('+'); Usart_Write('C'); Usart_Write('M'); Usart_Write('G'); Usart_Write('R'); Usart_Write('='); Usart_Write('1'); Usart_Write(0x0D); } void delMsg() { // Delete message for preferred message memory Usart_Write('A'); Usart_Write('T'); Usart_Write('+'); Usart_Write('C');

Usart_Write('M'); Usart_Write('G'); Usart_Write('D'); Usart_Write('='); Usart_Write('1'); Usart_Write(0x0D); delay_ms(500); } void check_message() { // check message status if(strcmp(mess,"Unlock")==0){ TRIGGER=1; // trigger relay delay_ms(2000); TRIGGER=0; } } void main() { TRISA=0x3F; ADCON1=0x06; PORTA=0x00; TRISB = 0X00; PORTB=0x00; TRISD=0x00; // Set PORT D as OUPTPUT PORTD=0x00; Usart_Init(9600); intTimer = 0; TRIGGER=0; INTCON.GIE = 1; INTCON.PEIE= 1; T1CON=0x30; T1CON.TMR1ON=1; PIR1.TMR1IF=0; PIE1.TMR1IE=0; textMode(); phoneMem(); TMR1H=0x00; TMR1L=0x00; } int extract_message(){ // extract the message from the cellphone int x=0,y=0,flag=1; char a; readMsg();

while(x!=10000){ if(Usart_Data_Ready()){ y++; a=Usart_Read(); if(a=='r'||a=='R'){ flag=0; break; } if(y>=70) { if(a!='O' && a!='K' && isalpha(a)) { msg[x++]=a; } } x++; } PIE1.TMR1IE=1; return flag; } }

hi... gsm man we can send sms from gsm modem.but cant receive sms and to display it on lcd by pic16f877a.. pl help me..we are in deadline.check my code and correct it.

#include <16f877a.h> #include "string.h" #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock=20000000) #use rs232(baud=9600, bits=8, parity=N, xmit=PIN_C6, rcv=PIN_C7, stream=GSM) #define RS PIN_E0 #define E PIN_E1 void out_lcd_string(char *string); char initialize[] = "Initialize...."; void clear(); void cmd(); void lcddata(char value); void LCDInit(); void display(char *x); char char_rcvd; void readindex(void); void ledtest(void); byte data[70]; int t = 0; int i= 0; /*void dwr() { //character to LCD display output_high(RS); output_high(E); output_low(E); delay_ms(2); } LCDPutchar( int t) { delay_ms(25); output_b((unsigned char)t); dwr(); delay_ms(25); }*/ /*#int_rda

void serial_isr() { char_rcvd = fgetc(GSM); while(char_rcvd !='\0') { //char_rcvd = fgetc(GSM); data[i]=char_rcvd; i++; } i=0; //data[i] = 0; // return new line shows that all character has been collected }*/ #int_rda void serial_isr() { int i= 0; //char_rcvd = fgetc(GSM); // while (char_rcvd!='\0') while(i!=2) { char_rcvd = fgetc(GSM); data[i]=char_rcvd; output_high(PIN_B7); i++; } i=0; t=1; //data[i] = 0; // return new line shows that all character has been collected } void cmd() { //command for lcd display output_low(RS); output_high(E); output_low(E); delay_ms(2); }

void readindex(void) { //while(1) //{ delay_ms(1000);

fputc('A',GSM); fputc('T',GSM); fputc('+',GSM); fputc('C',GSM); fputc('M',GSM); fputc('G',GSM); fputc('f',GSM); fputc('=',GSM); fputc('1',GSM); fputc(0x0D,GSM); //fprintf(GSM, "AT+CMGF = 1\r\n"); //set to GSM to text mode delay_ms(1000); enable_interrupts(int_rda); fputc('A',GSM); fputc('T',GSM); fputc('+',GSM); fputc('C',GSM); fputc('M',GSM); fputc('G',GSM); fputc('R',GSM); fputc('=',GSM); fputc('1',GSM); fputc(0x0D,GSM); //fprintf(GSM, "AT+CMGR = 1\r\n"); //read SMS at location inedex one while(t!=1){} // wait til interrupt disable_interrupts(int_rda); delay_ms(100); t = 0; output_high(PIN_B6); out_lcd_string(data); //send SMS string to LCD } void out_lcd_string(char *string) //this function will display all string of SMS from GSM { unsigned int i; for(i =0; i<strlen(string); i++) { output_b((int)string[i]); delay_ms(1); output_high(PIN_E1); //enable

output_high(PIN_E0); //low disable delay_ms(1); output_low(PIN_E1); delay_ms(1); } } void lcddata(char value) { output_b(value); //put the value on the pins output_high(PIN_E0); //rs high output_high(PIN_E1); // enable pins H-L pulse delay_ms(15); output_low(PIN_E1); //enable pins } void LCDInit() { output_b(0x38); cmd(); output_b(0x10); cmd(); output_b(0x0f); cmd(); output_b(0x06); cmd(); output_b(0x01); cmd(); } void clear() { output_b(0x01); cmd(); output_b(0x80); cmd(); } void display(char *x) //this function will display a string of character { int i = 0; for(i = 0; i<strlen(x); i++) lcddata(x[i]); } void main() { clear();

delay_ms(300); LCDInit(); delay_ms(200); //setup_adc_ports(NO_ANALOGS); //setup_adc(ADC_OFF); //setup_psp(PSP_DISABLED); //setup_spi(FALSE); //setup_wdt(WDT_OFF); //setup_timer_1(T1_DISABLED); //setup_timer_2(T2_DISABLED,0,1); enable_interrupts(global); enable_interrupts(int_rda); //cmd(); // display on, cursor on //delay_ms(200); //setup_oscillator(False); /* output_low(PIN_B7); output_low(PIN_B6); output_low(PIN_B5); // goes low for GSM modem*/ /*poweron(); delay_ms(200); delay_ms(500); lcdcmd(0x38); // init LCD 2 lines 5x7 matrix delay_ms(200); lcdcmd(0x0e); // display on, cursor on delay_ms(200); lcdcmd(0x01); // clear LCD delay_ms(200); lcdcmd(0x80); //shift cursor delay_ms(200);*/ display( initialize ); //display initialize on LCD delay_ms(300); readindex(); delay_ms(200); while(1) { //out_lcd_string(data); //delay_ms(200); //} /*void lcdcmd(int value1) { output_low(PIN_E0); //rs low

output_low(PIN_E1); //enable output_d(value1) ; //put the value on the pins output_high(PIN_E1); //strobe the enable pins H-L pulse delay_ms(15); output_low(PIN_E1); //enable }*/

/*void clear_lcd(void) { output_low(PIN_E1); //enable bit output_low(PIN_E0); // Rs= 0 output_d(0x01); //clear display output_high(PIN_E1); // H-L pulse delay_ms(1); output_low(PIN_E1); // H-L pulse }*/ /*void poweron(void) { output_low(PIN_B5); output_high(PIN_B5); delay_ms(2000); output_low(PIN_B5); } */ } }

Advanced Member level 5 Achievements: Awards:

Join Date Location Posts Helped Points Level

Apr 2008 villingen (Germany) / Bangalore 5,050 2058 / 2058 33,726

44 Blog Entries 1

Re: How to get SMS delivery notification by GPRS modem (SIM300)


+CDS : <length> <CR> <LF> <pdu> (PDU mode) or +CDS : <fo>,<mr>, [<ra>] , [<tora>], <scts>,<dt>,<st> (Text mode) 2: SMS-STATUS-REPORTs are stored and routed using the unsolicited result code : +CDSI: SR,<index>

#include <16F877.h> // Defines chip #include <stdio.h> // this gets the printf() definition #include <string.h> // string handling library #use delay(clock=4000000) // tells compiler clock speed #fuses hs, noprotect, nowdt, nolvp, nobrownout // sets the fuses #use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7, stream=GSM, ERRORS) // sets the rs232 up #byte PORTD = 8 // portd is located at file 8 #bit injector1 = PORTD.0 // portd1 is injector1 #bit injector2 = PORTD.1 // portd2 is injector2 #bit injector3 = PORTD.2 // portd3 is injector3 #bit injector4 = PORTD.3 // portd4 is injector4 #bit lift_pump = PORTD.4 // portd5 is lift pump . const int CR=0x0d; const int CZ=0x1A; const int LF=0x0A; char response[8]; char reply[20]; char result[2]; int int int int int read_message; send_message; delete_message; receive_message; shutdown;

void main(void) { set_tris_d(0xE0);

};

void send_message() { fprintf(GSM,"AT\r\n"); delay_ms(500); fgets(GSM); fprintf(GSM,"AT+CMGF=1\r\n"); text mode delay_ms(500); fgets(GSM);

// checks are they communicating //gets the responce and dump it // sends an at command to set messages to //sends a carriage return //gets the responce and dump it

fprintf(GSM,"AT+CMGS=\"07749960685\"\r\n"); // sents the following text to this number delay_ms(500); fgets(GSM); //gets the responce and dump it fprintf(GSM,"Shutdown Procedure Complete. Contact Authorities"); that will actually be sent delay_ms(100); fputc(CZ,GSM); gets(response,GSM); // the text

// sends carriage return // gets the confirmation reply

while(1) { } void receive_message() { char msg_coding1[80]; char msg_coding2[80]; signed int8 result; fgets(GSM); fprintf(GSM,msg_coding1); result = stricmp(msg_coding1, msg_coding2); if(result=0) //if stricmp return 0 ==true read_message(); else if(result=-1) main(); else (result=1) main(); }; void read_message() { char text_code1[6]; char text_code2 = stop char text_code3 = start signed int8 result; fprintf(GSM,"+CMTI: "SM",1"); location 1 delay_ms(500) fgets(GSM); it delay_ms(500) fgets(GSM); delay_ms(500) fgets(GSM); delay_ms(500) fputs(GSM,text_code); delay_ms(500) fgets(GSM); //wait 500ms // gets the second line and dumps it //wait 500ms //gets the 3rd line which is line we want //wait 500ms // puts the string in file called text_code //sends the command to read the message at //wait 500ms //gets the first line of text message and dumps // if stricmp returns -1 == false

// if stricmp returns 1 == false

// gets the blank line

result1 = stricmp(text_code1, text_code2); result2 = stricmp(text_code1, text_code3); if(result1=0 && result2=-1||1) out shut down shutdown(); else if(result1 = 1||-1 && result2 = 0) carry out startup main(); //if result1 is true carry //procedure // if result2 is true

else (result=1||-1 && result2= 1||-1) // if stricmp returns 1 == false delete_message(); fgets(GSM); //gets the carriage return and dumps it

delay_ms(500) };

//wait 500ms

//this function deletes the message in location 1 void delete_message(void) { fprintf(GSM="AT+CMGD=1"); //deletes the text message at location1 so next command can be dealt with delay_ms(500) gets(response,GSM); // gets the ok response main };

// this function carries out the shut down procedure void shutdown(void) { injector1= 0; // switch off injector 1 delay_ms(500); injector2= 0; // switch off injector 2 delay_ms(10000); // wait ten seconds delay_ms(10000); // wait ten seconds delay_ms(10000); // wait ten seconds delay_ms(10000); // wait ten seconds delay_ms(10000); // wait ten seconds delay_ms(10000); // wait ten seconds (1 minute) lift_pump=0; // turn the life pump in tank off //once the pump has been deactivated the engine will have no fuel and so the car will come to a //halt, receive_message(); // calls cool down period where it waits for the // command to turn the car back on };

How to test your GSM device/SIM card using Hyperterminal


To test if your GSM device, phone or GSM modem can work with VisualGSM, you can use Window's hyperterminal. 1). Start Hyperterminal as shown below:-

2). Connect your GSM device to a COM port, e.g. COM port 1. Ensure that there is sufficient COM ports in the first place. For IrDA or infrared connection, please ensure that your IrDA port is mapped to a serial port, e.g. COM4 is commonly used by IrDA. Some mobile devices can only be connected via IrDA, e.g. Nokia 8210. (Note: IrDA is used by only VisualGSM Lite. If you are using IrDA on Win2k, you may need to install a patch. You can download the patch from Lite download page.) 3). Enter a name for the connection, e.g. test.

4). Select COM port 1, if you device is connected to COM 1. If you are not sure, you can try the other COM ports.

5). Click restore default. If you are using a mobile phone via data cable, you may need to change the baud rate to 19200 Bits per second.

6). Go to File - Properties.

7). Click on Settings tab, select ASCII Setup. Ensure that "Append line feeds to incoming line ends", "Wrap lines that exceed terminal width", and "Send line ends with line feeds" are checked.

8.) Test your connection by issuing AT, and AT+CPIN. If you get OK and +CPIN: READY as response, then your device is ready for use with VisualGSM Enterprise. If you cannot get the response as shown below, please check with other ports, and baud rate combinations.

Das könnte Ihnen auch gefallen