Design of AT89S8252 microcontroller to realize contact IC card reading and writing control

Publisher:tony520Latest update time:2023-08-01 Source: elecfansKeywords:AT89S8252 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Since I was in high school, the meal cards used in our school have been contact-type IC cards. There are also many IC card phones distributed on campus. After going to college, the campus cards used by the school are non-contact and radio frequency IC cards. Therefore, I became interested in IC cards. In the process of learning microcontrollers, I learned that microcontrollers can realize reading and writing control of IC cards. Based on referring to relevant materials, I learned to use microcontrollers to realize contact IC card reading and writing control.


Main components:

1. AT89S8252 microcontroller chip. This chip has an SPI interface and can be used to read and write IC card chips.

2. The IC card chip AT45D041A, which uses a serial data interface compatible with the SPI interface, supports in-system reprogramming and can be used for the storage of digital voice, images and data.

Test flow chart:

Design of AT89S8252 microcontroller to realize contact IC card reading and writing control

Test circuit diagram:

Design of AT89S8252 microcontroller to realize contact IC card reading and writing control

Test program code:

//ICRdWr.h program

#ifndef _ICRDWR_H // Prevent ICRdWr.h from being referenced repeatedly

#define _ICRDWR_H

#include //Important header file references

#define uchar unsigned char

#define uint unsigned int

/* Instruction macro definition*/

#define BUFFER_1_WRITE 0x84 // buffer1 write instruction code

#define B1_TO_MM_PAGE_NO_ERA 0x88 // Buffer1 write main memory page instruction code without online erasure

#define MM_PAGE_READ 0xD2 // Main memory page read instruction code

#define STAT_REG_READ 0xD7 // Status register read instruction code

#define DATA_IN_MAX_LEN 8

#define DATA_OUT_MAX_LEN 8

uint page_start_addr; // Starting byte address in the page

uint page_addr; // Page address, the lower 9 bits of the 16 bits are valid bits

uint buf_start_addr; //The starting byte address in the buffer, the lower 11 bits of the 16 bits are valid bits

uchar data_in[DATA_IN_MAX_LEN]; //Data to be written to the IC card

uchar data_out[DATA_OUT_MAX_LEN]; //The data to be read from the IC card

#endif

//ICRdWr.c program

#include “ICRdWr.h”

/* Delay t milliseconds*/

void delay(uint t)

{

uint i;

while(t--)

{

/* For 11.0592M clock, the delay is about 1ms */

for (i=0;i<125;i++)

{}

}

}

/* Get the function that needs to be stored in the IC card data */

void getdata()

{

// This function is simplified as follows:

uchar i;

for (i=0;i«8;i++)

data_in[i]=i+1;

}

/* Write the SPDR register of the microcontroller AT89S8252, and the data is serially output to the IC card chip through the SPI port */

void write_spi(uchar dat)

{

SPDR = dat;

while (!(SPSR & 0x80)); // Wait for a transfer to complete

}

/* Get IC card chip status function */

uchar IC_stat(void)

{

P1_1 = 0; // Enable IC card chip;/cs=0

write_spi(STAT_REG_READ); // Write read IC card chip status command

write_spi(0x00); // Write don't care bits

P1_1 = 1; //Disable IC card chip;/cs=1

return SPDR; // Return IC card chip status byte

}

/* Write IC card chip function: write data into the buffer, if the buffer is full,

Then write the data in the buffer to the main memory page*/

void write_to_IC(uchar dat)

{

uchar stat;

/* Check whether the IC card chip is busy*/

stat = IC_stat();

while ((stat&0x80)==0x00);

/* Write data to buffer */

P1_1 = 0; // Enable IC card chip;/cs=0

write_spi(BUFFER_1_WRITE); // buffer1 write instruction code

write_spi(0x00); //Write 8 don't care bits

write_spi((uchar)(buf_start_addr》》8)); //Write 7-bit irrelevant bits plus the 1st bit of the 9-bit buffer start byte address

write_spi((uchar)buf_start_addr); //Write the last 8 bits of the 9-bit buffer start byte address

write_spi(dat); //Write data

P1_1 = 1; // Disable IC card chip; end buffer write command

buf_start_addr++; // Next buffer starting byte address

/* If the buffer is full, write the data in the buffer to the main memory page */

if (buf_start_addr》263)

{

buf_start_addr = 0; //buffer start byte address reset to 0

if (page_addr "2047") // If the main memory page is not full

{

/* Write buffer data to main memory page*/

P1_1 = 0; // Enable IC card chip;/cs=0

write_spi(B1_TO_MM_PAGE_NO_ERA); // Write buffer1 write main memory page instruction code without online erasure

write_spi((uchar)(page_addr》》7)); // Write 4 reserved bits plus the high 4 bits of the 11-bit page address

write_spi((uchar)(page_addr《》1)); //Write the lower 7 bits and 1 don't care bit of the 11-bit page address

write_spi(0x00); //Write 8 more irrelevant bits

P1_1 = 1; // Disable the IC card chip; end the buffer write main memory page instruction without online erasure

page_addr++; // Next page address

}

}

}

/* Read IC card chip function, if one page is read, read the next page */

uchar read_from_IC()

{

uchar stat;

uchar tmp;

/* Check whether the IC card chip is busy*/

stat = IC_stat();

while ((stat&0x80)==0x00);

/* Read data from the main memory page */

P1_1 = 0; // Enable IC card chip;/cs=0

write_spi(MM_PAGE_READ); //Write the main memory page read instruction code

tmp = (uchar)(page_addr》》7);

write_spi(tmp); // Write 4 reserved bits plus the high 4 bits of the 11-bit page address

tmp = (uchar)(page_addr《》1)|((uchar)(page_start_addr》》8)&0x01);

write_spi(tmp); //Write the lower 7 bits of the 11-bit page address and the highest bit of the 9-bit page starting byte address

tmp = (uchar)(page_start_addr);

write_spi(tmp); //Write the lower 8 bits of the 9-bit page starting byte address

write_spi(0x00); //Write 8 don't care bits

write_spi(0x00); //Write 8 don't care bits

write_spi(0x00); //Write 8 don't care bits

write_spi(0x00); //Write another 8 bits of don't care bits, totaling 32 bits of don't care bits.

write_spi(0xff); //Write 8-bit meaningless value to ensure completion of reading out one byte of data

P1_1 = 1; // Disable IC card chip; end main memory page read command

page_start_addr++; // Starting byte address in the next page

/* If one page is read, read the next page*/

if (page_start_addr》263)

{

page_start_addr = 0; //Reset the page start byte address to 0

if (page_addr "2047") // If the main memory page has not been read

page_addr++; // Next page address

}

return SPDR; // Return the read data

}

Continuing from the previous program:

/* Main function */

void main()

{

uchar i;

P1_0 = 1; // /RST pin is set high

/* SPIE=0, SPE=1, DORD=0, MSTR=1, CPOL=CPHA=1, SPR1=0, SPR0=1*/

SPCR=0x5d;

buf_start_addr = 0;

page_start_addr = 0;

page_addr = 0;

/* Get the data that needs to be written to the IC card and store it in data_in[]*/

getdata();

/* Write the data stored in data_in[] to the IC card*/

for (i=0;i

{

write_to_IC(data_in[i]);

delay(2); //delay 2ms

}

delay(10); // Delay 10ms

buf_start_addr = 0;

page_start_addr = 0;

page_addr = 0;

/* Read the data from the IC card and store it in data_out[]*/

for (i=0;i

{

data_out[i] = read_from_IC();

delay(2); //delay 2ms

}

while(1);

}


Keywords:AT89S8252 Reference address:Design of AT89S8252 microcontroller to realize contact IC card reading and writing control

Previous article:Electronic perpetual calendar system based on STC89S52 microcontroller
Next article:Interface circuit design between AT89S52 microcontroller and CF card

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号