Search

Friday, July 17, 2015

GSM Message Sending Using PIC Microcontroller

//********************************************************************************
//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:GSM Message Sending Using PIC Microcontroller
//Platform: pic18f4520
//Software:MPLAB


//********************************************************************************


Program:

#include<p18f4520.h>

#pragma config OSC =HS

#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config  WDT = OFF
#pragma config PBADEN = OFF
#pragma config MCLRE = ON
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF   

#define LCD PORTB

#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

void Delay_ms(unsigned int);

void cmd(unsigned char);
void dat(unsigned char);
//void transmitt(unsigned char p[]);
void send_to_modem1(unsigned char p[]);
void send_sms();
void send_msg();

char AT[]="AT";   // To initialize mode

char mode_text[]="AT+CMGF=1";   // to set text mode
//char char_mode[]="AT+CSCS=\"GSM\"";   // to set character mode
char param[]="AT+CSMP=17,167,0,0";   // set the parameter of character
char mobile_no[]="AT+CMGS=\"+919173211683\"";   //use to set receinpent number and mesg
char terminator=0x1A;     // chartacter form of control + z terminator character
char mesg[]="Emergancy";   // mesg we want to send

void main()

{
       ADCON1=0x0F;
        TRISD=0x00;
       TRISCbits.TRISC0=0;
       TRISCbits.TRISC1=0;
       TRISCbits.TRISC2=0;
        TRISCbits.TRISC6 = 0;                 // make the TX pin a digital output
       TRISCbits.TRISC7 = 1;                 // make the RX pin a digital input
       TRISB=0x00;

        cmd(0x01);

         cmd(0x80);
         cmd(0x0C);
         cmd(0x06);
         cmd(0x38);
           
        TXSTAbits.CSRC=1;               //Master Mode
        TXSTAbits.TX9=0;                   //8 Bit
        TXSTAbits.TXEN=1;                //Transmit enabled
        TXSTAbits.SYNC=0;                //Asynchronous Mode
        TXSTAbits.SENDB=0;             //Sync Break transmission completed
        TXSTAbits.BRGH=1;               //High Speed
RCSTA=0x90;
SPBRG=125;   
       RCONbits.IPEN   = 0;                 // disable interrupt priority
        INTCONbits.GIE  = 1;                 // enable interrupts
        INTCONbits.PEIE = 1;                // enable peripheral interrupts.
        PIE1bits.RCIE   = 1;                    // enable USART receive interrupt
      send_msg();
while(1);

}

void cmd(unsigned char k)

{
        LCD=k;
        RS=0;
        RW=0;
        EN=1;
        Delay_ms(20);
        EN=0;
}

void dat(unsigned char k)

{
        LCD=k;
        RS=1;
        RW=0;
        EN=1;
        Delay_ms(20);
        EN=0;
}

void Delay_ms(unsigned int p)

{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<300;j++);
}
}

void send_to_modem1(unsigned char p[])

{
unsigned char i=0;
while(p[i]!='\0')
       {
             while(PIR1bits.TXIF==0);// TMRT Empty
             TXREG=p[i];       //Transmit buffer
             i++;
       }
while(PIR1bits.TXIF==0);// TMRT Empty
       TXREG=0x0D;       //Transmit buffer
}

void send_sms()

{
send_to_modem1(mesg);
Delay_ms(100);
       while(PIR1bits.TXIF==0);// TMRT Empty
        TXREG=0x1A;       //Transmit buffer
Delay_ms(1000);
}
  
void send_msg()
{
send_to_modem1(AT);
Delay_ms(2000);
//send_to_modem1(noecho);
//Delay_ms(2000);
send_to_modem1(mode_text);
Delay_ms(2000);
send_to_modem1(mobile_no);
Delay_ms(2000);
send_sms();
}



Simulation:



No comments:

Post a Comment