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.

[PIC] No response from GSM module with PIC16f877a+uart

Status
Not open for further replies.

mayasunny

Member level 3
Joined
Mar 21, 2019
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
481
hie all, controller : PIC16F877A , module :sim800 , Xc8 compiler, Crystal frequency: 20MHz.
the problem is, when i am giving "AT\r" to microcontroller GSM modem not responding.
i checked
1. tested GSM, Connected to serial port using com port in my pc, the result is good gsm modem responded with "OK"
2. Connected my board to Serial port using com i got "AT" in my hyper terminal and i gave "OK" then the code working perfectly
3. When i combined the two with a connector wire of three pin then the system is not working properly

will you please suggest any remedies or solutions with your experience? and if you can please describe what is the problem actually?
below is my code:


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
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
89
90
91
92
93
94
95
96
97
98
99
100
#pragma config FOSC = HS        // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 
#include <xc.h>
#include<string.h>
#define _XTAL_FREQ 20000000
char a[]={"AT \n"}; 
char buf,*Rec; 
static char data[10];
unsigned int res,res1,i,j;
 
void UART_Init(int baudRate)
{    
    BRGH=0;
    SYNC=0;
    TXSTA = 0X20;                                      // Asynchronous mode, 8-bit data & enable transmitter
    RCSTA = 0X90;                                      // Enable Serial Port and 8-bit continuous receive           
    SPBRG = 31;                                        // baud rate @20Mhz Clock
}
 
void UART_TxChar(char ch)
{   
    while(TXIF == 0);                                   // Wait till the transmitter register becomes empty
    TXIF = 0;                                           // Clear transmitter flag
    TXREG = ch;                                         // load the char to be transmitted into transmit reg
}    
 
char UART_RxChar(void)
{
    while(RCIF == 0);                                   // Wait till the data is received  
    if (OERR) 
    {
        CREN=0;                                          // toggle CREN off and on to clear overrun 
        CREN=1;
    }
    
    RCIF = 0;                                           // Clear receiver flag
    return(RCREG);                                      // Return the received data to calling function
}
 
char *UART_Read_Text(unsigned int length)                // to read data continously
{
    for(j=0; j<length; j++)
    {    
        buf = UART_RxChar();                             //  Receive a char from serial port
        data[j] = buf;
        UART_TxChar(data[j]);
        
    }
       return(data);
}
 
void str(char ch)                                       // to write data continously
{
    for(i=0;a[i]!=0;i++)
    {
        UART_TxChar(a[i]); 
    }
}
void main(void)
{
    char ch;
    TRISD = 0X00;
    PORTD = 0X00;
    TRISC = 0x80;                                       // Configure Rx pin(rc7) as input and Tx(rc6) as output 
    UART_Init(9600);                                    //Initialize the UART module with 9600 baud rate
    while(1)
    {
        str(ch);
        Rec = UART_Read_Text(2); 
       // res = Rec[0];
       // res1 = Rec[1];
        res = strncmp(data,2,"OK");
       if(res == 0)             // if(res == 'O' && res1 == 'K')
        {
            RD0 = 1;                                     // turn on led
            __delay_ms(3000);
            RD0 = 0;
        }
        else{
            RD1 = 1;
             __delay_ms(3000);
            RD1 = 0;
        }
        /*res1 = strcmp(data,"ERROR");
        if(res == 0)
        { 
            RD1 = 1;
            __delay_ms(3000);
            RD1 = 0;
        } */  
    }
    return;
}

 
Last edited by a moderator:

In the text you say that you send "AT\r", but in the code it is "AT \n".
There should be no space after "AT", and the last character should be '\r'.
Change line 13 to this
Code:
char a[]={"AT\r"};
 

Please read the steps wt the top of page 116 and page 118 of the data sheet. The UART on these devices must be configured in the correct order. WHle it is possible to set al of the bits in the various registers at the same time, I prefer to set the various configuration bits and then the 'enable' bits in the correct order - I have never had an issue with the UARTs doing this.
I assume that you will be using the 'baudRate' parameter in the 'UART_Init' function at some time in the future.
Your 'str' function is rather strange - again I assume this is test code.
The logic behind your functions to read and write strings will work (up to a point) with the tests you are using now, but will cause you all sorts of problems later on. For example, the external device will response with "OK\r\n" (typically) but you tell 'UART_Read_Text' to only read 2 characters. The external device will continue to send characters after the "OK" and the UART Rx hardware will process them but, because you are not reading them, will generate an "OERR" error and ignore all following characters that are received until the bit is cleared in your software. While you do check for the OERR error, it is not clear exactly what values can be read from the RCREG in this situation.
Although it is a bit more advanced, I would recommend that you use interrupts on the Rx side and save the characters into a ring buffer. Also you need to expect and process line termination characters. Further when you are looking for a specific response ("OK\r\n" i this case) you need to be ready to handle other characters that might be (erroneously) received.
I see that you are blocking the main loop for 3 seconds as you flash the LED - while this is not wrong, especially in the early stages of testing, it will exacerbate any problems with extraneous characters being received. Again, using a ring buffer to receive the UART characters will avoid this issue.
Susan
 

Please read the steps wt the top of page 116 and page 118 of the data sheet. The UART on these devices must be configured in the correct order. WHle it is possible to set al of the bits in the various registers at the same time, I prefer to set the various configuration bits and then the 'enable' bits in the correct order - I have never had an issue with the UARTs doing this.
I assume that you will be using the 'baudRate' parameter in the 'UART_Init' function at some time in the future.
Your 'str' function is rather strange - again I assume this is test code.
The logic behind your functions to read and write strings will work (up to a point) with the tests you are using now, but will cause you all sorts of problems later on. For example, the external device will response with "OK\r\n" (typically) but you tell 'UART_Read_Text' to only read 2 characters. The external device will continue to send characters after the "OK" and the UART Rx hardware will process them but, because you are not reading them, will generate an "OERR" error and ignore all following characters that are received until the bit is cleared in your software. While you do check for the OERR error, it is not clear exactly what values can be read from the RCREG in this situation.
Although it is a bit more advanced, I would recommend that you use interrupts on the Rx side and save the characters into a ring buffer. Also you need to expect and process line termination characters. Further when you are looking for a specific response ("OK\r\n" i this case) you need to be ready to handle other characters that might be (erroneously) received.
I see that you are blocking the main loop for 3 seconds as you flash the LED - while this is not wrong, especially in the early stages of testing, it will exacerbate any problems with extraneous characters being received. Again, using a ring buffer to receive the UART characters will avoid this issue.
Susan

thanks susan for your reply , i modified my code and now im getting response form gsm, but i have one more problem,
 

i succeed in sending sms from mobile to gsm, my task is i have to control the load of pic with sms, the problem is the response of gsm is varying each time. i dont know why>
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top