RFID Click EasyAVR_V7

Post your requests and ideas for the mikroElektronika website & forums.
Post Reply
Author
Message
rajp1021
Posts: 1
Joined: 13 Sep 2013 19:18

RFID Click EasyAVR_V7

#1 Post by rajp1021 » 23 Feb 2016 04:15

Hi,

I am working on RFID Click with Easy AVR_V7.
I am following the Code provided, but I haven't had success.
Does anyone have example code that could help me?
I am using Atmel Studio to the program. Attaching my code with the Post.


#define F_CPU 8000000L
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>

#include "Sourse File/usart.h"
#include "Initialize.h"

//********** USART Intialize Variables *********//
volatile uint8_t Received_Flag_USART = 0;
volatile char ReceivedData_USART;
volatile char LoadData [20];
volatile uint8_t data;
volatile char *Decode;
volatile uint16_t Result;

//********** SPI RFID Intialize Variables *********//
unsigned short ReceivedData_RFID[32];
unsigned short SendData_RFID[32];
unsigned short Received_Flag_RFID;
unsigned short Response_RFID = 0, Data_Lan = 0;
unsigned short j = 0, tmp = 0;
char ID[10] = {0};
char txt_hex[3];
//******** End SPI RFID Intialize Variables *********//

//*****RFID CR95HF Commands Definition**************//
#define IDN 0x01
#define ProtocolSelect 0x02
#define SendRecv 0x04
#define Idle 0x07
#define RdReg 0x08
#define WrReg 0x09
#define BaudRate 0x0A
#define ECHO 0x55
#define SS_High PORTB |= (1<<4)
#define SS_Low PORTB &= ~(1<<4)
//*****End RFID CR95HF Commands Definition*********//

void DecodeReceivedata_USART();

int main(void)
{

io_init();
spi_init();
usart_init(9600); //initialize USART with 9600 baud rate

usart_puts_ln("Smart Cart");
usart_puts_ln("Welcome to Smart Cart Shopping");
sei(); //globally enable interrupts

PORTB &= ~(1 << 2); // put IRQ_IN pin at low level
_delay_ms(10);
PORTB |= (1<<2); // put IRQ_IN pin at low level
_delay_ms(10);
EchoResponse();

// Configure RFid
Calibration();
//IndexMod_Gain();
//AutoFDet();
//Select_ISO_IEC_14443_A_Protocol();

while (1)
{
if (Received_Flag_USART == 1)
{
DecodeReceivedata_USART();
}

//if (Received_Flag_USART==1)
//{
//GetNFCTag();
//}
}
}

spi_init(void)
{

DDRB |= (1<<4) |(1<<5) | (1<<7); //PB4 = SS, PB5 = MOSI, PB7 = SCLK,
SPCR |= (1<<SPE) | (1<<MSTR);//MASTER

}

void io_init()
{
DDRA |= (1<<4); // SSI_1 RFFID communication selection
PORTA &= ~(1 << 4); // Clear SSI_1
DDRB |= (1<<0); // SSI_0 RFID communication selection
PORTB |=(1<<0); // Set SSI_0


DDRB |= (1<<2)|(1<<3); //PINB2 = INT_0, IRQ_OUT PINB3 = INT_1, IRQ_IN

PORTB |= (1<<2);



}

// Get Echo response from CR95HF
void EchoResponse()
{
PORTB &= ~(1<<4);
SPDR = 0x00; // Send cmd to CR95HF
while (!(SPSR & (1<<SPIF)));
SPDR = ECHO;
while (!(SPSR & (1<<SPIF)));
PORTB |= (1<<4);

while(1)
{

PORTB &= ~(1<<4);
tmp = SPI_send_receive(0x03);
PORTB |= (1<<4);

if((tmp & 0x08) >> 3)
{
PORTB &= ~(1<<4);
tmp = SPI_send_receive(0x02);
PORTB |= (1<<4);

if(tmp == ECHO)
{
usart_putchar(tmp);
usart_putchar(ECHO);

}

}
break;
}
}


// USART Data received intrepid.
ISR(USART_RXC_vect)//receive complete
{
ReceivedData_USART = UDR; // Fetch the received byte value into the variable

LoadData[data] = (ReceivedData_USART);//store the data into correct element
++data;

if (ReceivedData_USART == 13)//reset if 13
{
Received_Flag_USART = 1;
}
}

// decode Received data from USART
DecodeReceivedata_USART()
{
//calculation for 100's/10's/1's place with ASCII correction factor
Result = (((LoadData[0]-48))); // + ((LoadData[2] - 48)*10) + ((LoadData[3] - 48)));

//The following code deciphers user input from the terminal via USART,

switch (Result)
{
case 1:
usart_puts_ln("NFC Enabled");
usart_puts_ln("please login using your NFC enabled Device.");
break;

case 2:
usart_puts_ln("RFID Enabled");
usart_puts_ln("You may begin scanning");
//EchoResponse();

break;

case 3:
usart_puts_ln("NFC Enabled");
usart_puts_ln("Place scan your NFC enable payment device");
break;
default:
{
usart_puts_ln("Invalid Entry");
}
}
//reset the variables used to 0
data = 0;
Received_Flag_USART=0;
Result =0;
}


// Write command to the RFID CR95HF
void Write_RFID_Cmd(unsigned short cmd, unsigned short Data_length)
{
unsigned short i = 0;

PORTB &= ~(1<<4);

SPI_send_receive(0x00); // Send cmd to CR95HF
SPI_send_receive(cmd);
SPI_send_receive(Data_length);
while(Data_length == 0)
{
PORTB |= (1<<4);
usart_putchar("0");
break;
}

for(i=0; i<Data_length; i++)
{
SendData_RFID = SPI_send_receive();

//SPDR = (SendData_RFID);
//while(!(SPSR & (1<<SPIF) ));
}
PORTB |= (1<<4);
}

// Get Data From the RFID CR95HF
void Read_RFID_Cmd()
{
unsigned short i = 0;

while(1)
{
SS_Low;
// Set the mode to POLL
// Get the registry Response
Response_RFID = SPI_send_receive(0x03);
// From the POLL response, see if wake up event was triggered
SS_High;

if((Response_RFID & 0x08) >> 3)
{
SS_Low;
// Set the Mode to READ
// Get Response Code
Response_RFID = SPI_send_receive(0x02);
SPDR = Data_Lan;
while(!(SPSR & (1<<SPIF) ));
// Loops through your array and save values to the array
for(i=1; i<=Data_Lan; i++)
{
SPDR = ReceivedData_RFID;
while(!(SPSR & (1<<SPIF) ));
}

break;
}
// Closes
SS_High;
// Wait before starting loop again
_delay_ms(10);
}
}
// Calibrate RFID CR95HF device
void Calibration() {
//TFT_Write_Text("Calibrating CR95HF...", 55, 100);
SendData_RFID[0] = 0x03;
SendData_RFID[1] = 0xA1;
SendData_RFID[2] = 0x00;
SendData_RFID[3] = 0xF8;
SendData_RFID[4] = 0x01;
SendData_RFID[5] = 0x18;
SendData_RFID[6] = 0x00;
SendData_RFID[7] = 0x20;
SendData_RFID[8] = 0x60;
SendData_RFID[9] = 0x60;
SendData_RFID[10] = 0x00;
SendData_RFID[11] = 0x00;
SendData_RFID[12] = 0x3F;
SendData_RFID[13] = 0x01;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();
SendData_RFID[11] = 0xFC;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x7C;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x3C;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x5C;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x6C;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x74;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();

SendData_RFID[11] = 0x70;
Write_RFID_Cmd(Idle, 0x0E);
Read_RFID_Cmd();
}
//
void Select_ISO_IEC_14443_A_Protocol()
{
SendData_RFID[0] = 0x02;
SendData_RFID[1] = 0x00;
Write_RFID_Cmd(ProtocolSelect, 2);
Read_RFID_Cmd();

// Clear read and write buffers
for(j=0; j<18; j++ )
{
ReceivedData_RFID[j] = 0;
SendData_RFID[j] = 0;
}
}

// Configure IndexMod & Gain
void IndexMod_Gain()
{
SendData_RFID[0] = 0x09;
SendData_RFID[1] = 0x04;
SendData_RFID[2] = 0x68;
SendData_RFID[3] = 0x01;
SendData_RFID[4] = 0x01;
SendData_RFID[5] = 0x50;
Write_RFID_Cmd(WrReg, 6);
Read_RFID_Cmd();
}

// Configure Auto FDet
void AutoFDet() {
SendData_RFID[0] = 0x09;
SendData_RFID[1] = 0x04;
SendData_RFID[2] = 0x0A;
SendData_RFID[3] = 0x01;
SendData_RFID[4] = 0x02;
SendData_RFID[5] = 0xA1;
Write_RFID_Cmd(WrReg, 6);
Read_RFID_Cmd();
}



// Read the tag ID
void GetNFCTag()
{
SendData_RFID[0] = 0x26;
SendData_RFID[1] = 0x07;
Write_RFID_Cmd(SendRecv , 2);
Read_RFID_Cmd();

SendData_RFID[0] = 0x93;
SendData_RFID[1] = 0x20;
SendData_RFID[2] = 0x08;
Write_RFID_Cmd(SendRecv , 3);
Read_RFID_Cmd();

if(Response_RFID == 0x80)
{

for( j = 0; j < Data_Lan; j++)
{
usart_putchar(ReceivedData_RFID[j]);
}
}

}

//Reading and Writing to SPI
volatile int SPI_send_receive(volatile int s)
{
SPDR = s;
while (!(SPSR & (1<<SPIF))); // wait for data transfer to be completed
return SPDR;
}

Post Reply

Return to “Website & Forums Wishlist”