Oct 10, 2010

RFID reader

RFID Reader Test Program for avr-gcc



/**************************************************************

Simple program to demonstrate the use of USART based Plug

and Play RFID reader. The program uses the following components.

* LCD Library : For Controlling a Standard 162 LCD Module
* USART Library : For Receiving Data from RFID Reader.

Program sets up USART @ 9600bps with following configuration.

*8 bit frame
*1 stop bit
*NO Parity

The UBRR Value Required is 103 (See page 168 on datasheet)

Program waits until more than 13 bytes are received over USART.

When 13 or more bytes are received we may have got one or
more RFID data packet. The program checks if the first data
byte is 2 or not. If it is 2 then it indicates that a valid
data packet is recived. The program then dumps the next 11
bytes to LCD Screen. These are the card data bytes. It then

ignores the last byte.

More information on every subject including

*Basic AVR MCU tutorials.
*USART and RS-232 Tutorial.
*LCD Interfacing Tutorial

and many other information is available on site

http://www.eXtremeElectronics.co.in

                            NOTICE
                           --------
NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN 
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.


WRITTEN BY:
AVINASH GUPTA
Mail: me@avinashgupta.com
Bio:  www.avinashgupta.com


**************************************************************/
#include <avr/io.h>

#include "lcd.h"
#include "usart.h"

/* Simple Wait Function */
void Wait(uint8_t t)
{
   uint8_t i;

   for(i=0;i<t;i++)
      _delay_loop_2(0);
}


void main()
{
   Wait(50);

   //Init LCD Module with NO Cursor
   LCDInit(LS_NONE);

   //Init USART @ 9600bps
   USARTInit(103);   //UBRR Value = 103 for 9600bps 

               //(See page 168 on Datasheet)

   LCDClear();

   LCDWriteString("RDIF Test #1");

   Wait(50);

   while(1)
   {

      LCDWriteStringXY(0,0,"Show Your Card");   //Message on line 1

      LCDWriteStringXY(0,1,"Waiting    ");   //Message on line 2

      //Check if USART has Got data from RFID
      //RFID packet is always 13 bytes long

      if(UDataAvailable() >= 13)
      {

         //Check the First byte it must be 2
         if(UReadData() == 2)
         {
            //We got correct packet!
            //Display it on LCD

            LCDClear();
            LCDWriteString("Card Data >>");

            //Go to start of 2nd line on LCD
            LCDGotoXY(0,1);

            //Dump Remaining 11 bytes
            uint8_t i;
            for(i=0;i<11;i++)
            {
               LCDData(UReadData());
            }

            //Read and discard the last byte (Stop Byte)

            UReadData();

            //Wait so that user can read the LCD
            Wait(200);

            //Now Clear it
            LCDClear();
         }
      }


   }
}