CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

read sms without interrupts
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

read sms without interrupts
PostPosted: Mon May 26, 2008 9:57 am     Reply with quote

Hi

is possible read an sms without use interrupts?

I have mobile phone on virtual port RS232 and I like read an sms how stay on phone... I have this...

Code:

#include <18F252.h>
#include <stdlib.h>
#include <string.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=19200, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=gpsport)
#use rs232(baud=19200, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8, stream=tlmport)

void read()
{
char sms[];
char lsms;
int i,k;

i=0;
k=0;

//ler SMS recebida
fprintf(tlmport,"at\n\r");
delay_ms(500);
fprintf(tlmport,"at+cmgf=1\r");
delay_ms(500);
fprintf(tlmport,"at+cmgr=1\r");

do {
   i++;
   lsms=getc(tlmport);
   sms[i]=lsms;
   }while(sms!='\n');
//comparar SMS's

   fprintf(gpsport,"mensagem:\n\r");
   for (k=6;k<=15;k++)
   {
   fprintf(gpsport,"%c"sms[k]);
   }

}

void main()
{

while(1)
   {
   if (input(PIN_B7)==1)
   {
   read();
   }
   }
}

this program stop on "do{} while" some one can give me an idea?

regards
Indy



Joined: 16 May 2008
Posts: 24

View user's profile Send private message

PostPosted: Mon May 26, 2008 11:07 am     Reply with quote

Code:
   }while(sms!='\n');
sms is the start address of the array, it will never become equal to '\n'. Change to
Code:
   }while(lsms!='\n');
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue May 27, 2008 4:00 am     Reply with quote

hi

I correct this bug but it dont work Crying or Very sad

when I write on terminal at+cmgr=1 phone return this:
Quote:

+CMGR:"REC UNREAD","+351965542750", "0", "0",,"08/05/26,16:16:36+04"

localizacao <SMS via www.tmn.pt>

OK


is possible copy this to a array?

or if is possible generate an interrupt on a virtual RS232...

best regards
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 27, 2008 8:16 am     Reply with quote

First time I only had a quick look at your code but there appear to be more problems.
Code:
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
The XT fuse and a clock speed of 40MHz are an invalid combination. What speed are you running?

Code:
char sms[];
This only declares a pointer to an array. There is no space allocated for the array which means that in your code you are writing to random memory. A huge bug!
Change to
Code:
#define MAX_SMS_LENGTH  200      // Set this to the maximum SMS length you expect to receive
char sms[MAX_SMS_LENGTH];


A minor bug:
Code:
fprintf(gpsport,"%c"sms[k]);
A comma is missing between the specifier string and the parameter, correct code is
Code:
fprintf(gpsport,"%c",sms[k]);
Strange, the CCS compiler should have given an error message here but instead it compiles correctly.

Quote:
is possible copy this to a array?
Yes, you can copy the received data to an array. The only change to your program required is not to stop at '\n' but stop on 'OK'.

Quote:
or if is possible generate an interrupt on a virtual RS232...
It is not a virtual RS232, the correct name is 'software UART'. Only with a trick you can get an interrupt for a software UART, that is when you feed the receive signal to the INT_EXT pin. For example code on how to do this you will have to search this forum.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue May 27, 2008 3:47 pm     Reply with quote

Hi

thanks ckielstra, my project runing on 40Mhz with oscilator external...

I make this updates to my program but I cant save sms at array Crying or Very sad I dont know what stay wrong... Confused
Code:

#include <18F252.h>
#include <stdlib.h>
#include <string.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=19200, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=gpsport)
#use rs232(baud=19200, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8, stream=tlmport)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
  nop
#endasm
} // Reserve space for the bootloader

#define MAX_SMS_LENGTH  200      // Set this to the maximum SMS length you expect to receive

void read()
{
char sms[MAX_SMS_LENGTH];
char lsms;
int i,k;

i=0;
k=0;

//ler SMS recebida
fprintf(tlmport,"at\n\r");
delay_ms(500);
fprintf(tlmport,"at+cmgf=1\r");
delay_ms(500);
fprintf(tlmport,"at+cmgr=1\r");

fprintf(gpsport,"SMS:\n\r");
/*
while(1)
{
lsms=fgetc(tlmport);  //escrever directo o que esta a ler do GPS
fputc(lsms,gpsport);
}
*/

do {
   i++;
   lsms=fgetc(tlmport);
   sms[i]=lsms;
   }while(lsms!='K');


   fprintf(gpsport,"print:\n\r");
   for (k=6;k<=15;k++)
   {
   fprintf(gpsport,"%c",sms[k]);
   }

}


void main()
{
   int a;
   
a=0;
a=input(PIN_B7);

while(1)
   {
   fprintf(gpsport,"cicle...\n\r");
   
   if (input(PIN_B7)==1)
   {
   read();
   }
   }
}

on this simple example I like print on terminal the sms number one... if I write this commands on terminal mobile phone give me the sms but this way no...

now when it program runing on terminal I see "cicle..." and when I put a positive signal on pin B7 appear "SMS:" and program break... dont give more data...

if some have ideias....

regards
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 27, 2008 5:18 pm     Reply with quote

Quote:
now when it program runing on terminal I see "cicle..." and when I put a positive signal on pin B7 appear "SMS:" and program break... dont give more data...
Strange. I only have some minor suggestions, maybe one of them will fix the problem.

filjoa wrote:
my project runing on 40Mhz with oscilator external...
Than you have to change the XT fuse to EC or EC_IO (this makes OSC2 pin available for I/O). Using the XT fuse probably will work but is wasting current.
Don't forget to change the fuse in your bootloader as well. The bootloader can not change the fuses so effectively only the fuses of the bootloader are used.

Code:
fprintf(tlmport,"at+cmgr=1\r");

fprintf(gpsport,"SMS:\n\r");
Try reversing these two lines. Now you are running the risk that the phone is already sending a response while you are still transmitting at the gpsport. You could loose up to 6 or 7 characters.

Code:
do {
   i++;                 
   lsms=fgetc(tlmport);
   sms[i]=lsms;
   }while(lsms!='K');
It's not a bug, but in C an array starts at offset 0. You are writing to offset 1, wasting 1 byte of memory. The fix is easy, increment i at the end of the loop, not at the start.
Another problem is that we have no protection for receiving a message larger than the array size. This could lead to unpredictable results. Here is an improved version of the above code:

Code:
  do
  {
    lsms=fgetc(tlmport);
    sms[i]=lsms;
    i++;                 
  }while( (lsms!='K') && (i<MAX_SMS_LENGTH) );
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 28, 2008 11:43 am     Reply with quote

Hi

thanks for opinion of oscilator... you have reason I put EC_IO .... on bootloadr stay all correct

now I make how tell me and change "for cicle" to:
Quote:

for (k=0;k<=30;k++)
{
fprintf(gpsport,"%c",sms[k]);
}


and now on terminal when I click on PIN_B7 I see:

SMS:
print:
cicle...

it continue dont save sms on array...

this is code after your ideia...

Quote:

#include <18F252.h>
#include <stdlib.h>
#include <string.h>
#fuses EC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=19200, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=gpsport)
#use rs232(baud=19200, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8, stream=tlmport)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader

#define MAX_SMS_LENGTH 200 // Set this to the maximum SMS length you expect to receive

void read()
{
char sms[MAX_SMS_LENGTH];
char lsms;
int i,k;

i=0;
k=0;

fprintf(gpsport,"SMS:\n\r"); //ler SMS recebida

fprintf(tlmport,"at\n\r");
delay_ms(500);
fprintf(tlmport,"at+cmgf=1\r");
delay_ms(500);
fprintf(tlmport,"at+cmgr=1\r");

/*
while(1)
{
lsms=fgetc(tlmport); //escrever directo o que esta a ler do GPS
fputc(lsms,gpsport);
}
*/

do
{
lsms=fgetc(tlmport);
sms[i]=lsms;
i++;
}while( (lsms!='K') && (i<MAX_SMS_LENGTH) );

fprintf(gpsport,"print:\n\r");
for (k=0;k<=30;k++)
{
fprintf(gpsport,"%c",sms[k]);
}

}

void main()
{
int a;

a=0;
a=input(PIN_B7);

while(1)
{
fprintf(gpsport,"cicle...\n\r");

if (input(PIN_B7)==1)
{
read();
}
}
}


I dont know what stay wron... Crying or Very sad

regards
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed May 28, 2008 5:47 pm     Reply with quote

What is your compiler version number?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 28, 2008 6:09 pm     Reply with quote

IDE Ver.4.058
PCB Ver.4.058
PCM Ver.4.058
PCH Ver.4.058
PCD Ver.4.057

you think how this program stay correct and problem is cause compiler?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed May 28, 2008 6:55 pm     Reply with quote

Strange...

Can you change the
Code:
fprintf(gpsport,"%c",sms[k]);
to
Code:
fprintf(gpsport," %2X",sms[k]);
Note the space before the % sign.

If you see all '00' values printed than check your hardware.
How is the phone connected to the PIC? Is there a MAX232 or similar voltage level converter used?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 29, 2008 9:32 am     Reply with quote

Hi

when arrive at home I try modify this print in code..

my mobile phone is samsung sgh-x450 and I dont have any interface for connect phone to PIC this stay direct...

I need put MAX232? TTL phone work at 3.3V.. if I need put an MAX232 how I conect them?

regards
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 29, 2008 9:47 am     Reply with quote

You said you connected the phone to a terminal program. I assume this terminal program was running on the PC? How do you connect the phone to the PC?

I'm not sure you need a max232. Check out the hardware of your phone for:
- What is the voltage on the serial line when no data is sent?
- And what is the voltage on the serial line when data is sent?

If the phone is running at 3.3V, then what voltage is the PIC running at?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 29, 2008 5:08 pm     Reply with quote

Hi

with this modification on terminal I see this many times when I press B7:

Quote:

SMS:
print:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
0 00 00 00 00cicle...


what this mean "00 00 00...."??

My conections is:

phone conect to the PIC (virtual RS232) and PIC to the PC (terminal)

I use an USB to RS232 converter.

I see voltage on PIC and phone and on PIC(TX=4.4V RX=4.4V) on PHONE(TX=2.4V RX=4.4V)

phone try talk with PIC with 2.4V dont is litle?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 29, 2008 5:44 pm     Reply with quote

The software UART generated by the CCS compiler is very simple. It expects the inactive state to have a logic high and the first start bit will then cause the signal to go low. As soon as the soft UART sees a logic low it starts sampling data.

You received all '00' data, this means the soft UART always sees a logic low. This is a hardware related problem.

Quote:
PHONE(TX=2.4V RX=4.4V)
If your PIC is running at 5V than the 2.4V is too low, you will have to insert a 3V to 5V voltage level translator. Search the internet on this subject. Texas Instruments sells specialized chips but you can also create a simple converter with a transistor and few resistors.
From the PIC to the phone for safety it is best to add a 5V to 3V level translator (just two resistors). Otherwise if your phone input is not 5V compliant you are running the risk of destroying your phone.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Sat May 31, 2008 10:50 am     Reply with quote

Hi

today I edit my circuit and put one max232 but I only can conect line TX of mobile phone... have this:

Phone(TX)--->>max(11)-->max(14)---->>PIC(RX)
or
Phone(TX)--->>max(13)-->max(12)---->>PIC(RX)

with this program:
Quote:

void read()
{
char sms[MAX_SMS_LENGTH];
char lsms;
int i,k;

i=0;
k=0;

fprintf(gpsport,"SMS:\n\r"); //ler SMS recebida

fprintf(tlmport,"at\n\r");
delay_ms(500);
fprintf(tlmport,"at+cmgf=1\r");
delay_ms(500);
fprintf(tlmport,"at+cmgr=1\r");

do
{
lsms=fgetc(tlmport); //escrever directo o que esta a ler do GPS
fputc(lsms,gpsport);
i++;
}while((lsms!='K') && (i<MAX_SMS_LENGTH) );

fprintf(gpsport,"\n\r");



but result on terminal is this:

Quote:

cicle...
SMS:
º”—*ŠÚTÜV
cicle...
SMS:
к”–*ŠÚTÖV
cicle...
SMS:
=©9%1å
cicle...
SMS:
º”—*ŠÚTÜV
cicle...


if I try connect phone(RX) to the max232 it stop write on terminal...

some ideias?

regards
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group