PDA

View Full Version : Problem with SMS Project: Using 16F877A



financecatalyst
- 17th October 2009, 23:13
Hi
I have 3 locations in EEprom to which I want to assign an phone number each. I want to achieve this by sending an text messege from an already programmed phone number in the PIC. Its not working for me at the moment when I try to add the number to location 1 & sometimes 2. It works pretty well when I use location 3. Here is my program

DEFINE OSC 4
ADCON1=7
CMCON=7
INTCON=0
OPTION_REG.7=0
TRISA=0 : PORTA=0
TRISB=0 : PORTB=0
TRISC=%10000000 : PORTC=0
TRISD=0 : PORTD=0
TRISE=0 : PORTE=0
CCP1CON=0
a var byte
b var byte
c var byte
d var byte
e var byte
f var byte
g var byte
num var byte[13]
ph1 var byte[13]
ph2 var byte[13]
ph3 var byte[13]
my1 var byte[13]
my2 var byte[13]
sms var byte[17]
@ ERRORLEVEL -306
yellowled var portb.4
redled var portb.5
greenled var portb.6

Include "modedefs.bas"
EEPROM 200,["+","4","4","7","8","3","2","3","2","3","1","4","6","+","4","4","7","9","3","1","2","2","5","2","3","7"]

gosub load

gosub del

main:
high portb.6 : pause 30 : low portb.6

DEBUG "AT^SMGR=1",13
DEBUGIN 5000,main,[WAIT("UNREAD"),SKIP 3,STR num\13\13,SKIP 27,STR sms\17\13]

gosub chk : pause 500
DEBUG "AT+CMGD=1",13
for a=0 to 15
num[a]="0" : sms[a]="0"
next a
goto main

chk:
a=0 : b=0 : d=0 : e=0 : f=0
for g=0 to 12
if num[g]=ph1[g] then a=a+1
if num[g]=ph2[g] then b=b+1
if num[g]=ph3[g] then d=d+1
if num[g]=my1[g] then e=e+1
if num[g]=my2[g] then f=f+1
next g
if (a=13) OR (b=13) OR (d=13)then gosub chkms
if (e=13) OR (f=13)then gosub adn
return


adn:
for g=0 to 50
toggle yellowled
pause 50
next g
if sms[0]="+" then

SELECT CASE sms[13]
CASE "1"
for a=0 to 12
write a,num[a]
next a

CASE "2"
for a=0 to 12
write a+13,num[a]
next a

CASE "3"
for a=0 to 12
write a+26,num[a]
next a
END SELECT
gosub load

return

chkms:
for g=0 to 30
toggle greenled
pause 30
next g
return




load:
for a=0 to 12
read a,ph1[a] : read a+13,ph2[a] : read a+26,ph3[a]
next a

return

del:
DEBUG "AT+CMGD=1",13
DEBUGIN 5000,del,[WAIT("OK")]
gosub set
return

set:
pause 1000
DEBUG "AT+CMGF=1",13
DEBUGIN 5000,set,[WAIT("OK")]
return

' sms[13] decides which location the number will be stored.
Please help

Jerson
- 18th October 2009, 02:47
Can you try this



CASE "1"
for a=0 to 12
write EE_Ph1+a,num[a]
next a

CASE "2"
for a=0 to 12
write EE_Ph2+a,num[a]
next a

CASE "3"
for a=0 to 12
write EE_Ph3+a,num[a]
next a
END SELECT
gosub load


do the same for reading back. EE_Ph1 to 3 are the eeprom locations of the telephone numbers
This is how to define them

EE_Ph1: data byte[13] ' allocate space for phone 1 in eeprom
EE_Ph2: data byte[13]
EE_Ph3: data byte[13]

financecatalyst
- 18th October 2009, 22:55
Thanks Jerson for your reply. Could you give me single line example of reading as well. I didnīt knew about read write structure like this.
I am using PBP for this.

Jerson
- 19th October 2009, 02:45
Same as the write, you do a read

for a= 0 to 13
read EE_Ph1+a, Ph1[a]
next

will read from The EEPROM to RAM

financecatalyst
- 19th October 2009, 21:48
I did as advised, I also changed the code to send me a text back with the what is stored in it after I send 3 text with 3 numbers at 3 locations.

The problem is with the first location only - the number stored at location one is "00078xxxxxxxx".
Its only the first 3 digits getting stored as "000" instead of "+44"

Thanks

P.s- My PIC is reseting itself automatically, I have an 2.2K between Vdd and MCLR.

financecatalyst
- 19th October 2009, 23:01
I did the same process again with my original ph1 var byte[]13 only, results are the same. Also when I send another messege the previous number should be replaced at the location specified (Yes, it does enters to write the new number as led does toggles) , BUT its not happening as well. I also tried the following approach where I replaced all locations with "0" before writing the number :
for a=0 to 12
write a,"0" : pause 40
write a,num[a] : toggle red : pause 700
read a,ph1[a] : pause 40
next a
Still no luck.

Jerson
- 20th October 2009, 03:10
Looks like you're overstepping your variables boundary. Do a check on the array indices for each array variable that you write. Maybe you're writing more than it can hold and it overwrites the following variable. Variable in point is Num[13] in this case.

financecatalyst
- 20th October 2009, 11:12
Looks like you're overstepping your variables boundary. Do a check on the array indices for each array variable that you write. Maybe you're writing more than it can hold and it overwrites the following variable. Variable in point is Num[13] in this case.

Could you please explain what you mean, I am sorry I dont understand how and what xcatly I have to do?

Jerson
- 20th October 2009, 12:07
What I mean is you should check that if your variable is an array of 12 bytes, you should not be writing more than 12 bytes to it. If you do, you are overwriting another variable in memory which follows the one you are writing to.

financecatalyst
- 20th October 2009, 19:51
Ok, I think I have got your point. I read somewhere that it is advisable to write "0" to all EEprom locations before attempting any write statement. Is it true?
I will otherwise remove statements writing "0" to locations before an sms is received and after and sms is processed and system is ready to receive the new one.

Also I wonder why ph1 locations 0 to 12 gets first 3 digits as 0. As far as overwriting is concerned there are no locations -3,-2,-1 for me to make a mistake and shift 3 places forward?

Jerson
- 21st October 2009, 02:11
Hi

a var byte
b var byte
c var byte
d var byte
e var byte
f var byte
g var byte
num var byte[13]
ph1 var byte[13]
ph2 var byte[13]
ph3 var byte[13]
my1 var byte[13]
my2 var byte[13]
sms var byte[17]
@ ERRORLEVEL -306
yellowled var portb.4
redled var portb.5
greenled var portb.6

Include "modedefs.bas"
EEPROM 200,["+","4","4","7","8","3","2","3","2","3","1","4","6","+","4","4","7","9","3","1","2","2","5","2","3","7"]

gosub load

gosub del

main:
high portb.6 : pause 30 : low portb.6

DEBUG "AT^SMGR=1",13
DEBUGIN 5000,main,[WAIT("UNREAD"),SKIP 3,STR num\13\13,SKIP 27,STR sms\17\13]

gosub chk : pause 500
DEBUG "AT+CMGD=1",13
for a=0 to 15
num[a]="0" : sms[a]="0"
next a
goto main



Well, see the highlights and draw your conclusions. There is no reason to be writing 0s to eeprom unless you really need it. So, what you've heard is not correct

Ioannis
- 21st October 2009, 09:27
...
main:
high portb.6 : pause 30 : low portb.6

DEBUG "AT^SMGR=1",13
DEBUGIN 5000,main,[WAIT("UNREAD"),SKIP 3,STR num\13\13,SKIP 27,STR sms\17\13]
...


Look at the AT^SMGR command. I believe it is typo error. It should be AT+SMGR

Ioannis

financecatalyst
- 21st October 2009, 23:51
Hey Ioannis, AT^SMGR is fine, it reads the messege without setting it to REC READ. I think what you are talking about is AT+CMGR. It helps if the messege is not deleted by the AT command, if it sees the unread messege again it will come back to try again to delete it. Else location one will be rendered useless and this messege will stay there untill system restart takes place (but thats my programming).

Jerson, I will try your solution soon (as soon as I get my monthly text allowance charged again-you wont believe I spent all texts in testing the system) and will update my findings here. Thanks

comwarrior
- 22nd October 2009, 01:14
FAO: financecatalyst

Do you really need to store the "+" in eeprom for the phone numbers?
can you not just hard code it when you send the SMS data? saves 3 bytes of eeprom and one less byte per number to read back...

financecatalyst
- 22nd October 2009, 02:33
FAO: financecatalyst

Do you really need to store the "+" in eeprom for the phone numbers?
can you not just hard code it when you send the SMS data? saves 3 bytes of eeprom and one less byte per number to read back...

At my level, its easier for me to grab all and send all. I will try your approach as well it doesnīt work out this way. :)