Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need help on sending SMS from p18f4520

Status
Not open for further replies.

flyingCOW

New Member
Hi, I'm now trying to send SMS from p18f4520 to my phone. But I can't seem to send or receive the SMS.

I'm using BluOcean Serial GSM / GPRS Modem, attached is the connection from p18f4520 to the modem

and my code:

#include <p18f4520.h>
#include <stdio.h>
#include <delays.h>
#include <string.h>
#include <usart.h>
#include <stdlib.h>

void ok_sms(void);

void main(void)
{

//Configuration of Modem
ADCON1 = 0x0F; // default all pins to digital
TRISC = 0b10111111; // RC7(RX) as input , RC6(TX) as output
SPBRG = 25; // baud rate of 9600
TXSTA = 0b00100100; // asynchronous mode, 8-bit data
// transmit enable, high baud rate select bit
RCSTA = 0b10010000; // serial port enable, 8-bit data
// continuous reception

TRISA = 0XF0; // bit 4 of PORTA as input
TRISB = 0b11110001; // bit 1-2 of PORTB as outputs, RB0 as input

while(1){

ok_sms();
}
}

void ok_sms(void)
{

putrsUSART((const far rom char *)"AT\r\n");
while(!BusyUSART());
Delay10KTCYx(50); //50x10k instruction cycle = 0.5s delay

putrsUSART((const far rom char *)"AT+CMGF=1\r\n"); //Operatng in SMS text mode
while(!BusyUSART());
Delay10KTCYx(50); //50x10k instruction cycle = 0.5s delay

putrsUSART((const far rom char *)"AT+CNMI=2,1,0,0,1\r\n"); //setting for reading SMS
while(!BusyUSART());
Delay10KTCYx(50); //50x10k instruction cycle = 0.5s delay

putrsUSART((const far rom char *)"AT+CMGS=\"96******\"\r"); //Sending SMS to recipant
while(!BusyUSART());
Delay10KTCYx(50); //50x10k instruction cycle = 0.5s delay

putrsUSART((const far rom char *)"OK\x1A\n"); //message to be sent
while(!BusyUSART());
Delay10KTCYx(50); //50x10k instruction cycle = 0.5s delay

}
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Are you sure that the ROM pointers should be far and not near. There is no need for far pointers on that chip so I suspect the library may be expecting near pointers.

Have you tried sending to a PC first?

Mike.
 

flyingCOW

New Member
Are you sure that the ROM pointers should be far and not near. There is no need for far pointers on that chip so I suspect the library may be expecting near pointers.

Have you tried sending to a PC first?

Mike.

What do you mean by far and near pointers?
 

Pommie

Well-Known Member
Most Helpful Member
A far pointer is a 24 bit pointer, a near is 16 bits. Routines written for one type will not work properly when passed the other type. The compiler should pick this up but I don't know which compiler you are using.

Mike.
 

flyingCOW

New Member
A far pointer is a 24 bit pointer, a near is 16 bits. Routines written for one type will not work properly when passed the other type. The compiler should pick this up but I don't know which compiler you are using.

Mike.

I'm using MPLAB ICD2.
 

WTP Pepper

Active Member
This worked for one of my projects using a Wismo GSM900 module. No worry about near or far pointers. This has worked on both an old MSDOS application using MSVC1.5 and an embedded 68HC11 job without issues using the IAR compiler. Pass the number and message to the function e.g.
sms_message("0798123123","Hello Fred");
send_modem() spews passed data out of the serial port until it encounters a NULL then returns.


/*********************************************************/
void sms_message(unsigned char *num,unsigned char *data)
/*********************************************************/
{
unsigned char str[20]={"AT+CMGS=,"};
unsigned char i;
i=0;

sprintf(modem,"AT\r");
send_modem(&modem[0]);
delay(0.5f);


sprintf(modem,"AT+CMGS=");
send_modem(&modem[0]);


WriteChar(port,'"');
i=0;
while (*(num+i) != '\0')
{
WriteChar(port,(unsigned char)toupper(*(num+i)));
i++;
}
WriteChar(port,'"');
WriteChar(port,CARRIAGE_RETURN);

i=0;
delay(0.5f);



while (*(data+i) != '\0')
{
WriteChar(port,(unsigned char)toupper(*(data+i)));
i++;
}

WriteChar(port,26);



}

/*************************************************/
void send_modem(unsigned char *ch)
/*************************************************/
{
unsigned char i;

i=0;

while ((*(ch+i)) != '\0')
{
WriteChar(port,(unsigned char)toupper(*(ch+i)));
i++;
}

}
 
Last edited:

flyingCOW

New Member
Hi, thanks for help and advices.

I can send and recevice sms from the modem. I try using 4MHz crystal for circuit oscillator, it works.
But I test using RC for circut oscillator, it won't work. R is 5.1kohm and C is 100pF. Need advice, Thanks.
 

WTP Pepper

Active Member
The UART on a micro is very fussy about what baud rate it will use. Often 16x oversampling that with an RC can easily drift out of spec.

This is at both ends, the micro driving and the modem. You need to use a crystal as the modem will only look so far for a compatible baud rate. R &C oscillators will drift and will rarely work. Unless you use a micro with a laser trimmed oscillator.

If it works with a crystal, why are you wasting time with an RC osc? Use a crystal and move on.
 

flyingCOW

New Member
The UART on a micro is very fussy about what baud rate it will use. Often 16x oversampling that with an RC can easily drift out of spec.

This is at both ends, the micro driving and the modem. You need to use a crystal as the modem will only look so far for a compatible baud rate. R &C oscillators will drift and will rarely work. Unless you use a micro with a laser trimmed oscillator.

If it works with a crystal, why are you wasting time with an RC osc? Use a crystal and move on.

Hmm, I see. Because for my project I'm doing something like a charging station to charge the battery to get the reading and once it reach 100%, it will send sms to the user.

I use RC osc i can get the reading from the battery, but my modem can't work. If I use crystal, my modem works but can't get the reading.

I set my ADCON2 = 0b10000100 - when using 4MHz crystal, ADCON2 = 0b10000011 - when using RC osc

Is there any other register i need to change?
 
Last edited:
Status
Not open for further replies.

Latest threads

Top