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

RFID Code Help

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

RFID Code Help
PostPosted: Mon Jan 31, 2011 2:50 pm     Reply with quote

Hi!

I successfully programmed my PIC using the flexible LCD drivers available on here. My body of code looked like this:

Code:
#include <16F88.H>
#fuses NOMCLR, INTRC_IO, NOBROWNOUT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=8M)
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=ID12, ERRORS)

#define LCD_DB4   PIN_A6
#define LCD_DB5   PIN_A7
#define LCD_DB6   PIN_A0
#define LCD_DB7   PIN_A1
#define LCD_E     PIN_A2
#define LCD_RS    PIN_A3
#define LCD_RW    PIN_A4
#define RFID_START_BYTE 0x0A
#define RFID_END_BYTE   0x0D

#include "flex_lcd2.h"


   
//============================================

void main(){
   lcd_init();  // Always call this first.
   output_high(PIN_B2);
   lcd_putc("All persons to PS3");
   lcd_putc("\nfor COD Black Ops");
   
   while(1);
}


I am now trying to connect my RFID reader (ID-12) to the RX on the pic so that I can read and display the tag ID on the LCD.

I found thread which uses the same module and posts some code:
http://www.ccsinfo.com/forum/viewtopic.php?t=41863

Code:
#include <16F877A.H>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=ID12, ERRORS)
#use rs232(baud=9600,parity=N,xmit=PIN_E0,rcv=PIN_E1,bits=8,stream=CPU, ERRORS)

#define RFID_START_BYTE 0x0A
#define RFID_END_BYTE   0x0D

void get_rfid_string()
{
int8 s[12];
unsigned int8 max=20;
  unsigned int8 len;
  char c;
 
  // Wait for start byte
  do
  {
    c = fgetc(ID12);
  } while (c != RFID_START_BYTE);
 
  // Receive data until END byte is received or buffer full
            // correct for terminating zero
  len=0;
s=0;
  while ((c != RFID_END_BYTE) && (len < max))
  {
 
    c = fgetc(ID12);
    s[len++] = c;
 
  };
  fprintf(CPU, "%s\n", s);

}

     
//3. Main program

VOID main()
{

   
         DO {         
                get_rfid_string();         
            } WHILE(TRUE);
 }


If I wanted to try and integrate the LCD code into the RFID code, should it look something like this:
Quote:
#include <16F88.H>
#fuses NOMCLR, INTRC_IO, NOBROWNOUT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=8M)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,stream=ID12, ERRORS)

#define RFID_START_BYTE 0x0A
#define RFID_END_BYTE 0x0D

void get_rfid_string()
{
lcd_init(); // Always call this first.
int8 s[12];
unsigned int8 max=20;
unsigned int8 len;
char c;

// Wait for start byte
do
{
c = fgetc(ID12);
} while (c != RFID_START_BYTE);

// Receive data until END byte is received or buffer full
// correct for terminating zero
len=0;
s=0;
while ((c != RFID_END_BYTE) && (len < max))
{

c = fgetc(ID12);
s[len++] = c;

};
// fprintf(CPU, "%s\n", s); //commented out
lcd_putc(CPU, "%s\n", s);
}


//3. Main program

VOID main()
{


DO {
get_rfid_string();
} WHILE(TRUE);
}

I am really new to this so I'm sorry if its wrong. I won't be able to test it for a couple of days so I was hoping I could get some input and hopefully learn were I might have gone wrong.

Regards,

James
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 10:25 am     Reply with quote

Hi.

I would really appreciate help on this. I am trying to show the RFID serial string on the LCD.

I've worked on the code above and got it to this stage:
Code:
#include <16F88.H>
#fuses INTRC_IO, NOBROWNOUT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=8M)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,stream=ID12, ERRORS)

#define LCD_DB4   PIN_A6
#define LCD_DB5   PIN_A7
#define LCD_DB6   PIN_A0
#define LCD_DB7   PIN_A1
#define LCD_E     PIN_A2
#define LCD_RS    PIN_A3
#define LCD_RW    PIN_A4
#define RFID_START_BYTE 0x0A
#define RFID_END_BYTE   0x0D

#include "flex_lcd2.h"
int8 s[12];

   
void get_rfid_string()
{
unsigned int8 max=20;
  unsigned int8 len;
  char c;
 
  // Wait for start byte
  do
  {
    c = fgetc(ID12);
  } while (c != RFID_START_BYTE);
 
  // Receive data until END byte is received or buffer full
            // correct for terminating zero
  len=0;
s=0;
  while ((c != RFID_END_BYTE) && (len < max))
  {
 
    c = fgetc(ID12);
    s[len++] = c;
 
  };
  fputs (s,ID12);
}

     
//3. Main program

void main()
{
   lcd_init();  // Always call this first.
//   lcd_putc("Line Test");
   
         do {         
                get_rfid_string();         
            } while(TRUE);
 }

It compiles file but I can't get anything to show up on the LCD when I move the tag to the RFID reader. Can anyone please tell me where I am going wrong?

James
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 4:57 pm     Reply with quote

The code below is a little weird. You have an array of 12 bytes for your
RFID string. Then you write incoming data bytes in the Tag to it, and you
do a length check against a value of 20. But your array is not 20 bytes
long. It's only 12. You have the possibility of writing past the end of array.
Normally a programmer would use the 'sizeof' operator to get the length
of the array for your comparison to 'len'.
Quote:

int8 s[12];

void get_rfid_string()
{
unsigned int8 max=20;
unsigned int8 len;
char c;

// Wait for start byte
do
{
c = fgetc(ID12);
} while (c != RFID_START_BYTE);

// Receive data until END byte is received or buffer full
// correct for terminating zero
len=0;
s=0;

while ((c != RFID_END_BYTE) && (len < max))
{

c = fgetc(ID12);
s[len++] = c;

};
fputs (s,ID12);
}

I don't know what the 's = 0' is doing ? Are you trying to load the array
with all 0's ? That's not how to do it. You can use the memset() function
to fill it with 0's.

I don't know what the fputs() line is supposed to do. Are you sending
the string back to the RFID unit ?



When you finally have your RFID data loaded into array s[], you need to
put a 0x00 byte at the end to convert it into a string. You need to add a
line of code to do that.

To display the string in array 's' on the LCD, you can use printf() and
re-direct the output to lcd_putc() as shown below. You need to clear
the array and set the cursor at the origin, before you display the string
This is done by sending '\f' to the lcd as shown below.
Code:

printf(lcd_putc, "\f%s", s);


Last edited by PCM programmer on Tue Feb 01, 2011 5:12 pm; edited 1 time in total
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 4:59 pm     Reply with quote

Thank you very much for the advice on my code. I will use your help to hopefully get my unit working tomorrow.

- James
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 5:21 pm     Reply with quote

I added one additional comment on zeroing the 's' array while you
were posting your reply.
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 5:25 pm     Reply with quote

This is what I understand so far. The total RFID string which is sent is 20, but the RFID tag portion I need is only 12, this is why the max length is 20, but I understand that this might not be the best way of doing it.

I thought the RIFD might send a trailing 0 but it does not, so I will need to put this in.

fputs() was my attempt at sending the string to the LCD. I put this code in as a replacement as per your suggestion:
printf(lcd_putc, "\f%s", s);

However, I still do not get anything printed on the screen. I think I need to properly terminate the string before the string can be printed to the LCD?

p.s I will check the mem set function
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 01, 2011 5:31 pm     Reply with quote

All strings must have a terminator byte of 0x00 placed after the last of
ASCII bytes.
Chantry



Joined: 18 Jan 2011
Posts: 13

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 12:18 pm     Reply with quote

Hi,

Ive looked at this code and made a few tweaks. It still has the problem of the array size, but I think I have put a terminator on the string correctly and integrated your LCD code.

Code:
#include <16F88.H>
#fuses INTRC_IO, NOBROWNOUT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=8M)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,stream=ID12, ERRORS)

#define LCD_DB4   PIN_A6
#define LCD_DB5   PIN_A7
#define LCD_DB6   PIN_A0
#define LCD_DB7   PIN_A1
#define LCD_E     PIN_A2
#define LCD_RS    PIN_A3
#define LCD_RW    PIN_A4

#define RFID_START_BYTE 0x02
#define RFID_END_BYTE 0x03

#include "flex_lcd2.h"
int8 s[12];

   
void get_rfid_string()
{
unsigned int8 max=20;
  unsigned int8 len;
  char c;
 
  // Wait for start byte
  do
  {
    c = fgetc(ID12);
  } while (c != RFID_START_BYTE);
 
  // Receive data until END byte is received or buffer full
            // correct for terminating zero
  len=0;
  while ((c != RFID_END_BYTE) && (len < max))
  {
 
    c = fgetc(ID12);
    s[len++] = c;
    s[len] = '\0'
 
  };
printf(lcd_putc, "\f%s", s);
}

     
//3. Main program

void main()
{
   lcd_init();  // Always call this first.     
         do {         
                get_rfid_string();         
            } while(TRUE);
 }


Could you confirm this please?

I still don't see anything on my LCD, but the RFID module does bleep (from BZ line) when I move a tag onto it, but I think I'm getting closer.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 5:00 pm     Reply with quote

How do you know that the RFID program works ? Have you displayed
an RFID tag using RS-232 output to your PC ? I don't see any place in
your previous posts where you have proved that the RFID code works
with #use rs232().
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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