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:
Test circuit diagram:
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);
}
Previous article:Electronic perpetual calendar system based on STC89S52 microcontroller
Next article:Interface circuit design between AT89S52 microcontroller and CF card
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Could you please tell me what circuit is generally used to implement the 0/4-20mA drive circuit?
- Application of FRAM in automobile driving recorder 2
- Principle and application of diaphragm pressure gauge
- Interpretation of Mobile DreamNet charges for SMS, MMS and mobile Internet access
- Application of SP2338 serial port expansion chip in automobile driving recorder
- Showing off your products (6) - silicon lab
- EEWORLD University - How to draw a high-end PCB ruler with Altium20
- A very good article, those who learn Verilog can take a good look at it
- Atmel introduces first product in ARM9 microcontroller family
- Agitek case sharing - Metrology and testing demonstration case of automotive electronic modules