HARDWARE
SOFTWARE
Please refer to “Getting Started with CIKU” tutorial.
SUGGESTED READING
- Interface MiFare Reader/Writer with Arduino
- Demonstration on GUI Software of the MiFare Reader/Writer CR038
HARDWARE SETUP
- CR038 Pin 1 is connect to 5V and CR038 pin 4 is connect to GND
- CR038 TX pin is connect to CIKU RX pin and CR038 RX pin is connect to CIKU TX pin.
CIKU MIFARE READER/WRITER CR038 LIBRARY FUNCTION
Below is a list for Mifare Reader/Writer CR038 function that you can used with CIKU. You also can refer to the CR038.h under CIKU.X folder.
[code lang=”c” highlight=””]
/*
* Manufacturer Block : 0
* Trailer Block : 3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,63
* Data Block : 1,2,4,5,6,8,9,10,12,13,14,16,17,18,20,21,22,24,
* 25,26, 28,29,30,32,33,34,36,37,38,40,41,42,44,45,46,48,49,50,
* 52,53,54,56,57,58,60,61,62
*
* Note : Do not write in Manufacturer Block and Trailer Block.
* only write in Data Block
*/
//Public – To be use by normal user
void CR038_begin(void);
void CR038_ledOn(void);
void CR038_ledOff(void);
void CR038_readDev(void); //Read MiFare device number subroutine
void CR038_read(char block); //Read data from Block 0 (manufaturer block) in memory of MiFare card
void CR038_write(char block); //Write data into from Block 4 in memory of MiFare card subroutine
void CR038_getNuid(void); //Get NUID of MiFare card subroutine
int CR038_checkError(void); //LED on CIKU will blinking
//Private – To be use by advance user
void CR038_authenSec(char block); //Authenticate Sector in MiFare card subroutine
void CR038_antennaOn(void); //Turn on antenna of MiFare module subroutine
void CR038_cardReq(void); //Request MiFare card type subroutine
void CR038_antiCol(void); //Perform Anti-Collison subroutine
void CR038_cardSel(void); //Select MiFare card subroutine
void CR038_serialRead(char length);
[/code]
USER CODE
This example code shows related function that can be used in order to read and write Mifare Card. You also can see this code in User-CR038.c under CIKU.X folder.
[code lang=”c” highlight=””]
#include “Arduino.h”
#include “HardwareSerial.h”
#include “LiquidCrystal.h”
#include “CR038.h”
//16Byte data to be stored in the MiFare Card Data Block
unsigned char BLOCK1[16] = “Name:Cytron 01”;
unsigned char BLOCK4[16] = “Name:Cytron 04”;
unsigned char BLOCK8[16] = “Name:Cytron 08”;
unsigned char BLOCK12[16] = “Name:Cytron 12”;
unsigned char BLOCK16[16] = “Name:Cytron 16”;
unsigned char BLOCK20[16] = “Name:Cytron 20”;
unsigned char BLOCK24[16] = “Name:Cytron 24”;
unsigned char BLOCK28[16] = “Name:Cytron 28”;
unsigned char BLOCK32[16] = “Name:Cytron 32”;
unsigned char BLOCK36[16] = “Name:Cytron 36”;
unsigned char BLOCK40[16] = “Name:Cytron 40”;
unsigned char BLOCK44[16] = “Name:Cytron 44”;
unsigned char BLOCK48[16] = “Name:Cytron 48”;
unsigned char BLOCK52[16] = “Name:Cytron 52”;
unsigned char BLOCK56[16] = “Name:Cytron 56”;
unsigned char BLOCK60[16] = “Name:Cytron 60”;
#define MREAD 0
#define MWRITE 1
char mode = MREAD;
void setup()
{
pinMode(LED, OUTPUT); //onboard LED as output
pinMode(SW, INPUT); //onboard SW as input
CR038_begin(); //Initialize CR038
//if succesfully initialize, CR038 onboard Led will blinking 3 times
CR038_checkError(); //if error in initialize CR038, CIKU onboard LED will blinking 3 times
lcd_4bit(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
lcd_begin(16, 2);
lcd_clear();
}
void loop()
{
//Select mode using SW
if(digitalRead(SW) == 0)
{ //SW press
if(mode == MREAD)
{
mode = MWRITE;
lcd_setCursor(0, 0);
lcd_printString(“Mode: WRITE”);
}
else if(mode == MWRITE)
{
mode = MREAD;
lcd_setCursor(0, 0);
lcd_printString(“Mode: READ “);
}
}
//Get NUID of MiFare card subroutine
CR038_getNuid(); //NUID of MiFare card is stored in NUID[] array after execution of this subroutine
if(CR038_checkError() == 0) //NUID of Mifare card is succecfully read
{
lcd_setCursor(0, 2);
for(int i = 3; i >= 0; i–)
{
lcd_printNumber(NUID[i], 16);
}
lcd_printString(” “);
delay(500);
if(mode == MREAD)
{
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Read”);
delay(1000);
for(int i = 0; i < 16; i++)
{
if(i == 0) CR038_read(i + 1);
else CR038_read(i * 4);
if(CR038_checkError() != 0)
{
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Error Read”);
delay(1000);
break;
}
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Block “);
if(i == 0) lcd_printNumber(i + 1, 10);
else lcd_printNumber(i * 4, 10);
delay(500);
lcd_clear();
lcd_setCursor(0, 0);
for(int i = 9; i < 25; i++)
{
lcd_write(reply_buffer[i]);
}
delay(500);
}
}
else if(mode == MWRITE)
{
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Write”);
delay(1000);
for(int i = 0; i < 16; i++)
{
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Block “);
if(i == 0) lcd_printNumber(i + 1, 10);
else lcd_printNumber(i * 4, 10);
delay(100);
if(i == 0)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK1[j];
}
}
else if(i == 1)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK4[j];
}
}
else if(i == 2)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK8[j];
}
}
else if(i == 3)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK12[j];
}
}
else if(i == 4)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK16[j];
}
}
else if(i == 5)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK20[j];
}
}
else if(i == 6)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK24[j];
}
}
else if(i == 7)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK28[j];
}
}
else if(i == 8)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK32[j];
}
}
else if(i == 9)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK36[j];
}
}
else if(i == 10)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK40[j];
}
}
else if(i == 11)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK44[j];
}
}
else if(i == 12)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK48[j];
}
}
else if(i == 13)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK52[j];
}
}
else if(i == 14)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK56[j];
}
}
else if(i == 15)
{
for(int j = 0; j < 16; j++)
{
WRITE[9 + j] = BLOCK60[j];
}
}
if(i == 0) CR038_write(i + 1);
else CR038_write(i * 4);
if(CR038_checkError() != 0)
{
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Error Write”);
delay(1000);
break;
}
}
}
lcd_clear();
lcd_setCursor(0, 0);
lcd_printString(“Done”);
delay(2000);
lcd_clear();
}
else
{
lcd_setCursor(0, 2);
lcd_printString(“Card Not Found”);
}
}
[/code]
NOTE
If you want to compile User-User-CR038.c, under MPLAB X IDE, first please remove the User-Template.c (CIKU – Source Files – user). Right click on User-Template.c and select Remove From Project. Then, right click on user folder (CIKU – Source Files), choose Add Existing Item…, open User-User-CR038.c, and proceed with Build Project.