View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
RFID read codes problem |
Posted: Wed May 05, 2010 12:47 pm |
|
|
Hi
I try to make a simple codeloock with RFID, but I have some problems.
I like to turn on the led when card is correct and turn off when other card is not correct.
Code: |
#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT,STVREN
#use delay(clock=20000000)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <string.h>
//##### Globals
char code[15];
int c1ok;
//##### function declaration
void cmpcode();
#int_rda
void irs_rda()
{
char c;
int i=0;
c=getc();
if (c==0x0A)
{
output_high(PIN_B7);
do {
c=getc();
code[i]=c;
i++;
} while (c!=0x0D);
}
disable_interrupts(INT_RDA);
cmpcode();
enable_interrupts(INT_RDA);
}
void cmpcode()
{
char card1[10];
strcpy(card1,"0F02782DC0");
c1ok=strcmp(card1,code);
if (c1ok==255)
{
output_high(PIN_B7);
}
else output_low(PIN_B7);
delay_ms(1000);
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(TRUE)
{
}
}
|
Now the program flash led when I pass on RFID reader and after go to ON and crash...
Someone can help me?
best regards |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed May 12, 2010 9:18 am |
|
|
hi
I edit my program but now I have a strange problem...
when I make 1ยบ printf string stay correct (in case 0F027812A7) after I make print "i" and it give i=23 (I don't why.... I wait for i=10), but when I go to cmpcode() function and try print my string on terminal I see an stupid result..
Hyperterminal
Quote: |
0F027812A7-23-
027END
|
DATASHEET: [URL=http://img693.imageshack.us/i/35439920.jpg/]
PROGRAM
Code: |
#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT,STVREN
#use delay(clock=20000000)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader
//##### Includes
#include <string.h>
//##### Globals
char c;
char code[];
//##### function declaration
void cmpcode();
void card ();
#int_rda
void irs_rda()
{
c=getc();
}
void card()
{
int8 i=0;
do {
i++;
code[i]=c;
printf("%c",code[i]);
}while (c!=0x0D);
printf("-%u-",i);
}
void cmpcode()
{
int8 k;
for (k=0;k<=10;k++)
{
printf("%c",code[k]);
}
printf("END\n\r");
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(TRUE)
{
if (c==0x0A)
{
card();
cmpcode();
}
}
}
|
someone have an opinion where stay error? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 12, 2010 1:41 pm |
|
|
Quote: | ##### Globals
char c;
char code[];
|
This array has no memory space allocated to it. It's an empty array.
How can it work ? You need to think about these topics. When you
look at your program, that line should stand out like a red flag. |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed May 12, 2010 3:56 pm |
|
|
Code: |
char card1[10];
strcpy(card1,"0F02782DC0"); |
that is wrong, because the string "0F02782DC0" is 11 characters long in fact.
Do not forget the end-of-string '\0' character which is being also copied into card1 array and goes over its size! |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu May 13, 2010 2:02 am |
|
|
When using int# rda it will interrupt when ever a byte arrives. One interrupt per byte. In the interrupt perform only a single getc (). Test the single byte and see if it is a start byte. If so set a global flag that a sentence has started. Write code in the rda isr so that if the sentence_started flag is true then when a newly received byte arrives it is placed in the slot in card1[ ] starting at position 0 and incrementing thereafter. Next write code in the rda isr to test if an end of sentence byte has arrived and set a global flag sentence_ended and place a null terminator in the array card1. In the main routine initialize sentence_started and sentence ended to false, wait in a loop until the end of sentence flag is set to true. Then read the card1 array it will have the card info and then a null terminator then set the sentence started and sentence ended flags so another card can be read in. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu May 13, 2010 2:28 am |
|
|
Another note
Code: |
do {
i++;
code[i]=c;
printf("%c",code[i]);
}while (c!=0x0D);
|
This will NOT wait for a new char but will continue to loop with the old value of c until c = a new value and then continue to loop with that until c == 0x0D.
You code will proberbly run a lot faster than the rate at which chars arrive over your serial port so your count will increase well past the end of your array before it sees the 0x0D char.
You need to put a flag in to wait until a new char is available before the next iteration of the loop!
Arrays have a zero based index in C and you are incrementing i before using it so you will be storing the first char at code[1], this is ok IF your array is at least 2 chars bigger than what you expect to recieve and the rest of your code realise that you are starting at 1 and not 0.
It would be better to increment i after the printf("%c",code[i]); in this case. |
|
|
|