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.

[SOLVED] Need a code to interface a GSM modem using UART interface

Status
Not open for further replies.

ADGAN

Full Member level 5
Full Member level 5
Joined
Oct 9, 2013
Messages
295
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Visit site
Activity points
1,837
Hello everybody! I want to interface a GSM modem via the UART interface. I only need to send a SMS. Does anybody have a example written in MPLAB C18 ?

Note:

I'm using the TC35 GSM modem, 4MHz crystal and PIC18F45K22
 

in sim900
for sending sms '
AT+CMGF = 1;
//It configures as text mode '
AT+CMGS="phone_number";
>then text ur message

in the same to interface with uart just send the string via Tx line of PIC microcontroller and give some delay
like first send AT+CMGF=1 followed by carriage return ("\r")
 

Thanks, but I know about that. I just want to see how to initialize UART in MPLAB, etc
 

In C18 Compiler there is a uart library you have just to include that
there is some function
like
Open_usart(baud or SPBRG value,Config)

SPBRG is nothing but a register which contains the value like you are using 4Mhz
so SPBRG value is around 207 for 9600 baud rate, the formula for that is

SPBRG = ((Fosc/Desiredbaud)/64) - 1

and when once USART is initialized then just send and receive character either by using Mplab Libray or by creating your own just loading the data TXREG Register for transmitting and RCREG for receiving
 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
Which Compiler are you using with MPLAB and what Fosc are you using?

Edit: I see you are using C18. Which PIC are you using? C18 has USART library. You have to include usart.h file.
 

Thanks for the replies. I'm using PIC18F45K22. This is the code I'm hoping to use, but don't whether it would work. It would be great if somebody could point out if there is any error. I don't know the baud rate of the GSM modem. But when I check the modem using the PC, I used 9600 bps.

Code:
#include <usart.h>

void GSM_Send(char min,char hr)
{

if (min%5==0)
{
unsigned char config=0,spbrg=0,baudconfig=0;

unsigned char Command_CMGF[]="AT+CMGF=1\r"; // AT+CMGF for selecting Text Mode
unsigned char CtrlZ=0x1A; // CTRL+Z for sedning SMS after the message has been entered
unsigned char Command_CMGS[]="AT+CMGS =+9xxxxxxxxxx\r"; // recepient mobile number
unsigned char Command_AT[]="AT\r";

unsigned int i=0,j=0;

unsigned char msg01[]="Hello! This is a test message from UART\n\r\n\r";
                                      

    Close2USART();//turn off usart if was previously on

     // initialize USART module - ( 8 bit, 57600 baud rate, no parity bit... )
    //-----configure USART -----
    config = USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_LOW;
    //-----SPBRG needs to be changed depending upon oscillator frequency-------
    spbrg = 207;                    //At 4Mhz of oscillator frequency & baud rate of 9600.

    Open2USART(config, spbrg);        //API configures USART for desired parameters

    baudconfig =  BAUD_8_BIT_RATE | BAUD_AUTO_OFF;
    baud2USART (baudconfig);


     while(Busy2USART());  

     for(j=0;j<sizeof(msg01);j++)
     Write2USART(msg01[j]);
     Init_Delay(); 

     //*************************************
     // send data via USART
     for(i=0;i<sizeof(Command_AT);i++)
     Write2USART(Command_AT[i]);
     //*************************************
     Init_Delay();


     //*************************************
     // send data via USART
     for(i=0;i<sizeof(Command_CMGF);i++)
     Write2USART(Command_CMGF[i]);
     Init_Delay();
     // send data via USART
     for(i=0;i<sizeof(Command_CMGS);i++)
     Write2USART(Command_CMGS[i]);

     Init_Delay();
     Write2USART( 0x1A );   // send data via USART - ctrl+z
     /*********************************************/
 
}

}
 

Not tested code. Calculate SPBRG value for your Fosc. I have already given you working C18 USART code a few days back.


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
#include <usart.h>
 
 
void UART2_Write_Text(char text[]);
GSM_Send(char min, char hr);
 
void UART2_Write_Text(char text[]){
 
    unsigned int i = 0;
 
    while(text[i])Write2USART(text[i++]);
 
 
}
 
void GSM_Send(char min, char hr){
 
    unsigned char spbrg = 0;
    unsigned char Command_AT[] = "AT\r\n";
    unsigned char Command_CMGF[] = "AT+CMGF=1\r\n";
    unsigned char Command_CMGS[] = "AT+CMGS=\"+9xxxxxxxxx\"\r\n";
    unsigned char msg01[] = "Hello! This is a test message from UART";  
    unsigned char CtrlZ = 0x1A; 
    unsigned int i = 0,j = 0;
 
    if (min%5==0){
                              
            Close2USART();    
    
            Open2USART(USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_LOW,  207);        
 
            baud2USART (BAUD_8_BIT_RATE | BAUD_AUTO_OFF);
        Init_Delay();   //1 sec delay
 
            while(Busy2USART());  
 
            UART2_Write_Text(Command_AT);
            Init_Delay();   //1 sec delay
            UART2_Write_Text(Command_CMGF);
            Init_Delay();   //2 sec delay
        UART2_Write_Text(Command_CMGS);
            Init_Delay();   //5 sec delay
            UART2_Write_Text(msg01);
        Init_Delay();   //0.3 sec delay
        Write2USART(CtrlZ);
        Write2USART(13);
        Write2USART(10);         
 
    }
}

 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
Thank you. But you didn't gave me an USART code before :roll: I would like to know what is Write2USART(13) and Write2USART(10) at the bottom of the code. Anyway thanks for the help again.
 

@ADGAN

13 is nothing but Carriage return
10 is just Line Feed
They are decimal value for CR & LF which is required to get response from the modem whenever AT command is sent
but you can just write "\r" also for that and you can apply this also even you can check this value using your calculator on your PC set on programmer's mode

in the same to send a SMS (ctrl+Z) is required so in that sense we have to use (0x1A) as sent by another one in early post

these are just values send by USART (Tx )Line to GSM Rx
 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
Thank you! But why do we need to set both Rx and Tx pins as inputs? I thought only Tx should be set as input.
 

Don't worry about the condition of the pin as input
the USART module will automatically configured the pins from input to output as whenever needed, it's mention in the datasheet too
 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top