|
|
View previous topic :: View next topic |
Author |
Message |
altra22
Joined: 14 May 2007 Posts: 11
|
Compare array from EEPROM |
Posted: Thu Dec 23, 2010 6:40 am |
|
|
I am trying to compare string from External EEPROM and check if this user is already in PIC
Code: | char const users [3][15]={"JOHN","SAMI","JAMAL"}; |
and every user stored separated in External EEPROM.
Code: | sprintf(name,"%s","JOHN");
write_ext(0*13,name);
delay_ms(20);
|
and check is the user in the list by Strcmp but my code isn't working.
Here is my code:
Code: |
for(i = 0; i < 3; i++){
read_ext((0,13,name));
delay_ms(20);
strcpy(temp,name);
printf("\nT= %s U= %s"temp[i],name);
}
if(strcmp(*temp[i],*users[i])==0) printf("Match");
else printf("Different");
|
Another question, can I add users later in same array by terminal?
Thanks in Advance
P.S: I am using Proteus |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: Compare array from EEPROM |
Posted: Thu Dec 23, 2010 2:11 pm |
|
|
altra22 wrote: | I am trying to compare string from External EEPROM and check if this user is already in PIC
Code: | char const users [3][15]={"JOHN","SAMI","JAMAL"}; |
and every user stored separated in External EEPROM.
Code: | sprintf(name,"%s","JOHN");
write_ext(0*13,name);
delay_ms(20);
|
and check is the user in the list by Strcmp but my code isn't working.
Here is my code:
Code: |
for(i = 0; i < 3; i++){
read_ext((0,13,name));
delay_ms(20);
strcpy(temp,name);
printf("\nT= %s U= %s"temp[i],name);
}
if(strcmp(*temp[i],*users[i])==0) printf("Match");
else printf("Different");
|
Another question, can I add users later in same array by terminal?
Thanks in Advance
P.S: I am using Proteus |
First: Do a search for "proteus" on this forum and you will find a lot of users having issues with proteus as a simulator for PIC's.
If you want the definitive sim for Microchip products, use MPLAB+MPSIM (built in -- AND FREE).
Next, I think your approach is more complex than it needs to be. You have variable sized strings.
You could just as easily use a single string to store all your usernames and use strtok(); to parse through them for you in sort of an argv/argc fashion.
Then you'd define a max size for the single string and just load it all in when you need it -- all at once and then search through it.
You don't have a short compilable example that shows your idea either working or not.
Write an example and then post it with your questions (along with your compiler version) and then we can better help you.
Otherwise, the answer to your question is a generic "YES" -- but it depends on the implementation - just like all programming. :D
Cheers,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
altra22
Joined: 14 May 2007 Posts: 11
|
|
Posted: Fri Dec 24, 2010 6:15 pm |
|
|
Thanks Ben for your reply.
Yes Proteus make troubles
I found example about strtok in CCS manual and I tried to implement it to my need:
Code: |
#include <16f876a.h>
#fuses xt,NOWDT,NOLVP
#device PASS_STRINGS=IN_RAM
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
void main(){
char string[50], term[3], *ptr;
int n;
strcpy(string,"JOHN,SAMI,JAMAL,MIKE,ALEX,ANGELA;");
strcpy(term,",;");
ptr = strtok(string, term);
while(ptr!=0) {
puts(ptr);
ptr = strtok(0, term);
}
while(1);
}
|
It works ok
but when I use strcmp to search user
Code: |
if (strcmp(string,"MIKE")==0)
printf ("found %s\n",string);
|
it doesn't find the name.
My version is 4.105. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Dec 25, 2010 12:15 am |
|
|
It's done more like this:
Code: | #include <16f876a.h>
#fuses xt,NOWDT,NOLVP
#device PASS_STRINGS=IN_RAM
#use delay(clock=4000000)
#use rs232(UART1, baud=9600)
#include <string.h>
void main(){
char string[50], term[3], *ptr, username[10];
strcpy(string,"JOHN,SAMI,JAMAL,MIKE,ALEX,ANGELA;");
strcpy(username,"ANGELA");
strcpy(term,",;");
ptr = strtok(string, term);
while(ptr!=0) {
puts(ptr);
if ( strcmp(username, ptr) == 0 ) {
puts("Houston we have a match");
break;
}
ptr = strtok(0, term);
}
while(1);
}
|
BTW, I ran this 100% from MPLAB using the MPSIM debugger and enabling the UART1 IO in the settings to allow to see the debug output.
Lose the proteus. It seems to cause everyone problems.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
altra22
Joined: 14 May 2007 Posts: 11
|
|
Posted: Sat Dec 25, 2010 1:51 pm |
|
|
Thanks Ben it's working.
Now I want to read the string in eeprom and compare it to usernames.
I will do it in real hardware with real eeprom (24c16).
Hope to be easy to deal with.
Thanks again |
|
|
schmez
Joined: 14 Jun 2011 Posts: 24 Location: St. Louis
|
|
Posted: Sat Jul 02, 2011 7:22 pm |
|
|
You mentioned you were going to try to do this with the eeprom - I am having issues getting strings into eeprom - any pointers? |
|
|
|
|
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
|