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] problem for PIC 18F46K22 interfacing with gsm modem(automation reasearch lab's modem)

Status
Not open for further replies.

tankerpacific

Newbie level 5
Joined
Sep 23, 2013
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
226
Hi all. I interfaced gsm modem with my computer using hyperterminal, it works fine for reading, sending and deleting sms.
But when I connect the same gsm modem to PIC18f46K22, I don't get any response from it.
I have interfaced 2*16 LCD to see the sent commands to the gsm and to view the sms.
I am using MAX232 in between PIC and modem.Microcontrollers tx -> db9conneter's rx and Microcontrollers rx -> db9conneter's tx Modem's rts and cts are shorted together and dtr and dsr are also shorted together while interfacing with PIC18f46k22. Modems ground is made common with pic18f46k22 ground. configuration word for add 300001 is 29 i.e internal 16 Mhz oscillator. Below is the code in mplab ide v8.91 XC8 compiler.
Can anybody help me out , whats going wrong?

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <xc.h>
#include <htc.h>
#include "p18f46k22.h"

// Pin assignments
//** Instead of normal port, use latch ports
//#define LCDRS PORTDbits.RD4 // "RS=0 : send command" "RS=1 : send char"
//#define LCDRW PORTDbits.RD5 // 1= READ, 0 = WRITE;
//#define LCDE PORTDbits.RD6

#define LCDRW LATDbits.LATD5 /* PORT for RW */
#define TRISD_RW TRISDbits.TRISD5 /* TRIS for RW */
#define LCDRS LATDbits.LATD4 /* PORT for RS */
#define TRISD_RS TRISDbits.TRISD4 /* TRIS for RS */
#define LCDE LATDbits.LATD6 /* PORT for enable */
#define TRISD_E TRISDbits.TRISD6

#define TX LATCbits.LATC6
#define RX LATCbits.LATC7
#define TRISC_TX TRISCbits.TRISC6
#define TRISC_RX TRISCbits.TRISC7
#define BAUDRATE 9600 //bps


#define TRISC_GREEN_LED TRISCbits.TRISC5
#define GREEN_LED PORTCbits.RC5

// fn delarations
void nop(void);
void delay(unsigned char);
void fetchstring(const char *);
void writestring (unsigned char);
void commandLCD(char);

void InitUART(void);
void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);

void initialize_GSM_modem(void);
void initialize_serialcommunication(void);


// variable declarations
char temp1,temp2,temp3,Byte;
unsigned char s1[] = "GREEN AUTOMATION";
unsigned char s2[] = "GSM AUTO STARTER";

unsigned char Command_CMGF[]="AT+CMGF=1\r"; // AT+CMGF for selecting Text Mode
unsigned char Command_CMGS[]="AT+CMGS=+919922958957\r";// recepient mobile number
unsigned char Command_CMGR[]="AT+CMGR=1\r";//READ 1ST SMS STORED IN SIM MEMORY
unsigned char Command_AT[]="AT\r";
unsigned char CtrlZ=0x1A; // CTRL+Z for sedning SMS after the message has been entered
unsigned char msg01[]="Hello!";


// fn definations

void InitUART(void)
{
SPBRG1 = 0X19;//25D=0X19H for BR=9615:taken from 18f46k22 datasheet: page no.: 281
BAUDCON1 = 0X00;

TRISC_TX = 1; //TX Pin AS I/P : CHK AFTER
TRISC_RX = 1; //RX Pin AS I/P

ANSELCbits.ANSC6 =0; // DIGITAL I/P BUFF OF TX1 ENABLED
ANSELCbits.ANSC7 =0; // DIGITAL I/P BUFF OF RX1 ENABLED

TXSTA1 = 0X00;
RCSTA1 = 0X00;
RCSTA1bits.SPEN = 1; //enable serial port pins

TXSTA1bits.TXEN = 1; //enable the transmitter

PIE1bits.TX1IE = 0; //disable tx interrupts
PIE1bits.RC1IE = 1; //enable rx interrupts


RCSTA1bits.CREN = 1; //enable reception
//RCSTA1bits.SREN = 0; //no effect
//RCSTA1bits.RX9 = 0; //8-bit reception

//TXSTA1bits.BRGH = 0; //LOW SPEED baudrate
//TXSTA1bits.SYNC = 0; //asynchronous
//TXSTA1bits.TX9 = 0; //8-bit transmission
//TXSTA1bits.TXEN = 0; //reset transmitter

//BAUDCON1bits.BRG16 = 0; // 8 BIT BAUD RATE GENERATOR IS USED : SPBRG1 ONLY
//BAUDCON1bits.ABDEN = 0; // AUTO BAUD DETECT MODE IS DISABLED

}

void commandLCD(char temp2)
{
TRISD_RW = 0;
TRISD_RS = 0;
TRISD_E = 0;
PORTB = temp2;
LCDRS = 0; // cmd mode
LCDRW = 0; // LCDRW = 0 : write to LCD
LCDE = 1;
delay(40);
LCDE = 0;
}

void fetchstring(const char *s)
{
while(*s)
writestring(*s++);
}

void writestring(unsigned char data)
{
PORTB = data;
LCDRS = 1; // char mode
LCDRW = 0; // LCDRW = 0 : write to LCD
LCDE = 1;
delay(40);
LCDE = 0;
}

/************delay***************/
void nop(void)
{
CLRWDT();
unsigned int i=0;
for(;i<100;i++);

}

void delay(unsigned char i)
{
for(;i!=0;i--)
{
nop();
nop();
nop();
}
}

/*USART FUNCTIONS DEFINATIONS*/
void SendStringSerially(const unsigned char* st)
{
while(*st)
SendByteSerially(*st++);
}

void SendByteSerially(unsigned char Byte) //Writes a character to the serial port
{
while(!PIR1bits.TX1IF);//WHILE NOTTX1IF ,MEANS NOT1 ,MEANS WHILE TX1IF IS ZERO, //wait for previous transmission to finish
// PIR1bits.TX1IF = 0;//TX1IF =0 MEANS TXREG1 IS NOT EMPTY
// TX1IF = 1 : MEANS TXREG1 IS EMPTY

while(!TXSTA1bits.TRMT);//IF TRMT =1 GO DOWN,WHILE NOTTX1IF ,MEANS NOT1 ,MEANS WHILE TX1IF IS ZERO, //wait for previous transmission to finish
TXREG1 = Byte; // AFTER LOADING TXREG1, TX1IF BCOMES VALID IN 2ND INST CYCLE FOLLOWING WRITE EXECUTION
nop();
nop();
nop();
writestring(TXREG1);// disply THE COMMAND GIVEN BY MICROCON TO MODEM
}

unsigned char ReceiveByteSerially(void) //Gets a character from the serial port
{
if(RCSTA1bits.OERR) //if over run error, then reset the receiver
{
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}

while(!PIR1bits.RC1IF); //wait for transmission to receive
return RCREG1;//this should clear the RC1IF bit automatically i think as
// this bit is read only to user or to software.

// **** write code here to delete the SMS and make the SMS box empty.
}

////////////////////// MAIN PROGRAM ///////////////////////
void main(void)
{
start1:
TRISB = 0X00; //LCD DATA PORT : PORT B AS AN O/P PORT
TRISD_RW = 0;
TRISD_RS = 0;
TRISD_E = 0;
TRISC_GREEN_LED = 0;
PORTB = 0X00;
InitUART();
OSCTUNE = 0X80;
OSCCON = 0X76; // 16Mhz INTERNAL OSCILLATOR : CONFIG WORD VALUE - 29
OSCCON2 = 0X00;

INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
// goto start1;

/***************************TO BE SENT TO LCD******************************************/
unsigned char a[5]={0x38,0x0c,0x01,0x06,0x80};//8 bit, sh cur,no bl, cur off,scr on, first line
unsigned char i;
for(i=0;i<5;i++)
{
commandLCD(a);
delay(40);
}
fetchstring(&s1[0]);
delay(40);
unsigned char a[5]={0x38,0x0c,0x01,0x06,0xc0};//8 bit, sh cur,no bl, cur off,scr on, first line
unsigned char i;
for(i=0;i<5;i++)
{
commandLCD(a);
delay(40);
}

fetchstring(&s2[0]);

CLRWDT();
/*****************************************************************/
unsigned char b[2]={0x01,0xC0};//8 bit, sh cur,no bl, cur off,scr on, first line
unsigned char i;
for(i=0;i<2;i++)
{
commandLCD(b);
delay(40);
}
/*****************************************************************/
SendStringSerially(&Command_AT[0]); // INTIALIZE MODEM
delay(500);
/*****************************************************************/
/*****************************************************************/
unsigned char b[2]={0x01,0xC0};//8 bit, sh cur,no bl, cur off,scr on, first line
unsigned char i;
for(i=0;i<2;i++)
{
commandLCD(b);
delay(40);
}
/*****************************************************************/
SendStringSerially(&Command_CMGF[0]);// MODEM IN TEXT MODE
delay(500);
/*****************************************************************/
/*****************************************************************/
unsigned char b[2]={0x01,0xC0};//8 bit, sh cur,no bl, cur off,scr on, first line
unsigned char i;
for(i=0;i<2;i++)
{
commandLCD(b);
delay(40);
}
/*****************************************************************/
SendStringSerially(&Command_CMGR[0]);// READ SMS
delay(500);
/*****************************************************************/
start:
CLRWDT();
/*********sending of sms in 3 steps 1.cmgs, 2. sms msg, 3. ctrlZ**********/
//SendStringSerially(&Command_CMGS[0]);// SEND SMS
// delay(1025);
//SendStringSerially(&msg01[0]);
// delay(1025);
//SendByteSerially(CtrlZ); // terminate and send msg01.
// delay(1025);
/************************************************************************/
goto start;
}

void interrupt ISR(void)
{
GREEN_LED =1;
delay(100);
GREEN_LED =0;
delay(100);


if(PIR1bits.RC1IF == 1) //If UART Rx Interrupt
{
if(RCSTA1bits.OERR == 1) //if over run error, then reset the receiver
{
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
while(!PIR1bits.RC1IF); //wait for transmission to receive

// **** write code here to delete the SMS and make the SMS box empty
GREEN_LED =1;
delay(100);
GREEN_LED =0;
delay(100);

PORTB = RCREG1; //this should clear the RC1IF bit automatically i think as
// this bit is read only to user or to software.
LCDRS = 1; // char mode
LCDRW = 0; // LCDRW = 0 : write to LCD
LCDE = 1;
delay(40);
LCDE = 0;
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top