Using single chip microcomputer to realize reading and writing of universal memory IC card

Publisher:沭阳小黄同志Latest update time:2012-02-25 Source: 国外电子元器件 Keywords:E2PROM  FLASH  ROM  AT24C01 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Overview

Universal memory IC card is made of universal memory chip package. It has been widely used in various fields because of its simple structure and function, low production cost and convenient use. At present, most universal memory chips used in IC cards are E2PROM. The commonly used protocols are mainly two-wire serial connection protocol (I2C) and three-wire serial link protocol. Among them, the more commonly used are AT24 series chips produced by ATMEL. Take AT24C01 in this series as an example. It has a storage capacity of 1k, is suitable for low voltage/standard voltage operation of 2V~5V, and has the advantages of low power consumption and high reliability. Although AT89C2051 is a low-end model of ATMEL's 89 series microcontroller, it has 2k FLASH ROM (reprogrammable flash memory), 128×8-bit internal RAM and full static operation mode, and also has low power consumption and strong functions. Taking AT24C2051 as an example, the working principle and basic circuit connection of the universal memory IC card are introduced below. The circuit is simple, flexible to use, and can reliably read and write universal memory IC cards.

2 Hardware Features

2.1 Characteristics of AT24 Series Memory

AT24 series memory chips are manufactured using CMOS technology and have a built-in high-voltage pump, which can work under single voltage supply conditions. Its standard package is an 8-pin DIP package, and the functions of each pin are as follows:

SCL: Serial clock. At the rising edge of this pin, the system inputs data to each EEPROM device and outputs it at the falling edge.

SDA: Serial Data. This pin is open drain driven and can transmit data in both directions.

A0, A1, A2: Device/page addressing. Device address input terminals. In AT24C01/02, this pin is hard-wired.

Vcc: Generally input +5V operating voltage.

Figure 1 is a contact diagram of an IC card that complies with the ISO7816-2 standard. For the AT24 series universal memory IC card, only four contacts are usually used. The internal configuration of the AT24C01 is 128 8-bit bytes, and a 7-bit address is required for random word addressing.

2.2 Bus Status and Timing

The SCL and SDA buses of A24C01 can be pulled up to a high level through a resistor, and the data on SDA can only be changed periodically when SCL is at a low level. When SCL is at a high level, the change of SDA indicates the "start" and "stop" states. At this time, all addresses and data words are input and output from the EEPROM in 8-bit serial code.

Start state: When SCL is high, SDA changes from high to low. This command must be executed before other commands.

Stop state: When SCL is high, SDA changes from low to high. This command terminates all communications.

Confirmation: After receiving the data, the devices on the same bus confirm it by setting SDA to a low level.

2.3 Device Addressing

The AT24 series EEPROM requires an 8-bit device address immediately after the start state to perform read and write operations. The upper 4 bits of the device address code are 1, 0, 1, 0. For AT24C01/02, the three bits after the upper 4 bits of the address code are the device address code, corresponding to their hard-wired pins. The lowest bit should be the read and write selection bit, which can trigger a read operation when set to 0.

The specific format is as follows:

1 0 1 0 A2 A1 A0 R/W

2.4 AT89C2051 chip

AT89C2051 is compatible with MCS-51 products. It has 2k FLASH ROM, 128 bytes ROM, 15 I/O pins, two 16-bit timers/counters, a five-vector two-level interrupt structure, a full-duplex serial port, a precision analog comparator, and an on-chip oscillator circuit and clock circuit. Its P1 and P3 ports are bidirectional I/O ports, of which P1.2~P1.7, P3.0~P3.5 and P3.7 have internal pull-up resistors. When AT89C2051 is used as an input terminal, "1" will be written to the pin first to turn off the internal MOS tube so that the pin is in a suspended state, thereby obtaining a high-impedance input. [page]

FIG. 2 is a basic circuit connection diagram of a general-purpose memory IC card.

3 Read and write operation software

When the system uses a 6MHz crystal oscillator, the defined I/O lines and device addresses are as follows:

SCL BIT P1.7

SDA BIT P1.6

DEVICEAD_W DATA 10100000B ; Write card device address

DEVICEAD_R DATA 10100001B ; Card reader device address

3.1 Start Condition (START_IC)

In the start condition, when SCL is high, SDA changes from high to low. The procedure is as follows:

START_IC: CLR SCL; SDA is allowed to change only when SCL is low

NOP; add a null instruction delay to ensure signal reliability

NOP

SETB SDA

NOP

NOP

SETB SCL

NOP

NOP

CLR SDA

NOP

NOP

CLR SCL

NOP

RET

3.2 Stop Condition (STOP_C)

In the stop condition, when SCL is high, SDA changes from low to high. The procedure is as follows:

STOP_IC:CLR SCL

NOP

NOP

CLR SDA

NOP

NOP

SETB SCL

NOP

NOP

SETB SDA

NOP

NOP

CLR SCL

NOP

NOP

CLR SDA

RET

3.3 Acknowledgement signal (ACK_IC)

When the receiver responds, the SDA level is pulled low after each byte is received. The procedure is as follows:

ACK_IC:CLR SCL

NOP

NOP

CLR SDA

NOP

NOP

SEIB SCL

NOP

NOP

CLR SCL[page]

NOP

SETB SDA

NOP

RET

3.4 Write one byte of data to the IC card (WR_BYTE)

In the following program, parameter A represents the source data and R5 represents the number of bytes.

WR_BYTE: MOV R5, #08; one byte of 8-bit data

CLR SCL

NOP

NOP

WR_BYTE1: RLC A; left shift with carry, A.8->C

MOV SDA, C; Change the data on SDA when SCL is low

NOP

SETB SCL; pull up SCL to send data out

NOP

NOP

CLR SCL

NOP

NOP

DJNZ R5, WR_BYTE1; send 8 bits of data in A in sequence

SETB SDA

SETB SCL

JB SDA, $; Waiting for IC card confirmation signal

CLR SCL

NOP

RET

The main function of this subroutine is to sequentially shift the 8-bit data of one byte in A to the left according to the defined timing and transmit it through the pin. When one byte is sent, wait for the confirmation signal sent back by the IC card.

3.5 Read one byte from IC card (RD+BYTE)

The source program to read one byte from the IC card is as follows:

RD_BYTE: MOV R5, #08

SETB SDA; device SDA is in read state

CLR A ; Clear the A register

RD_BTYE1: MOV C, SDA; read one bit of data to the carry bit

RLC A; Left shift data to A.0

SETB SCL

NOP

NOP

CLR SCL

NOP

NOP

DJNZ R5, RD_BYTE1; read out 8 bits of data into A in sequence

RET ; No response signal

This program can be used to store the read data in A. It should be noted that the device that reads the data does not respond by confirming the status, but generates a stop status afterwards. [page]

3.6 Byte write mode write data (WRITE_BYTE)

The parameters in the following program are: R6= =destination address, A= =data; the data is as follows:

WRITE_BYTE: PUSH ACC; save the data in A

LCALL START_IC ; send start signal

MOV A, #DEVICEAD_W; write device address

LCAL WR_BYTE

MOV A, R6 ; write byte address

LCALL WR_BYTE

POP ACC; Restore data in A

LCALL WR_BYTE ;Write data

LCALL STOP_IC

RET

After receiving 8 bits of data, the EEPROM will send back a confirmation signal through SDA, and the transmitting device must terminate the write operation with a stop state. At this time, the EEPROM will enter a write cycle of the internal fixed memory and prohibit all inputs during this period, and will not respond to the communication until the write operation is completed. The write cycle can be customized, with a maximum of 10ms.

3.7 Write data in page write mode (WRITE_PAGE)

The parameters in the following program are P0= = source pointer, R6= = destination address, R7= = page length.

WRITE_PAGE: LCALL START_IC

MOV A, #DEVICEAD_W

LCALL WR_BYTE

MOV A, R6

LCALL WR_BYTE

WRITE_PAGE1: MOV A, @R0

LCALL WR_BYTE

INC R0

DJNZ R7, WRITE_PAGE1

LCALL STOP_IC

RET

AT24C01/02 can use the above program to write 8-byte pages. Its operation is similar to writing bytes. The difference is that it does not need to stop after the first byte is sent. After receiving the confirmation signal, it sends 7 bytes of data code and finally stops the page write sequence. The page of AT24C04/08/16 is 16 bytes.

3.8 Immediate Address Read Mode (READ_BYTEC)

The procedure for reading one byte of data in immediate address read mode is as follows: READ_BYTEC: LCALL START_IC

MOV A, #DEVICEAD_R

LCALL WR_BYTE

LCALL RD_BYTE ;Read the default address data

LCALL STOP_IC ;Send stop status response

RET

After the program is executed, its internal data word address pointer will remain at the last address accessed by the last read or write operation, and will increment by 1 and remain valid during the chip power-on period. Only when the address is at the end of the page will the next access scroll to the first address of the page.

3.9 Random Address Read Mode (READ_BYTER)

In the following program, R6 = = source address, the program is as follows:

READ_BYTER: LCALL START_IC

MOV A, #DEVICEAD_W ; Execute a null byte write sequence

LCALL WR_BYTE ; load data address

MOV A, R6

LCALL WR_BYTE

LCALL START_IC

MOV A, #DEVICEAD_R; immediate address read

LCALL WR_BYTE

LCALL DR_BYTE

LCALL STOP_IC

RET

The read mode requires a byte write sequence to load the data address. After the device and data address are written and acknowledged, another start condition is generated and the address of the read device is sent, stimulating an immediate address read.

3.10 Sequential Address Read (READ_BYTES)

In the following program: R0 = = destination pointer; R7 = = data length, the program is as follows:

READ+BYTES:LCALL START_IC

MOV A, #DEVICEAD_R

LCALL WR_BYTE

READ_BYTES2: LCALL RD_BYTE

MOV @R0, A; store data to the destination address

INC R0

DJNZ R7, READ_BYTES1

LCALL STOP_IC ;Stop after reading or writing the specified length

RET

READ_BYTES1: LCALL ACK_IC; send confirmation signal SJMP after receiving data READ_BYTES2

The sequential read is triggered by an immediate address read or a random address read, and responds with a confirmation signal after receiving a byte of data. When the reading device responds with a stop state, the operation is terminated.


4 Conclusion

The system has a simple structure and high reliability in actual operation. It also has certain scalability and can be directly connected to the computer through the serial port of the microcontroller after level conversion for data communication. In addition, it can also be connected to other pins or card holder contacts as needed. If the circuit and program are properly improved, encryption cards and CPU cards can also be read and written.

Keywords:E2PROM  FLASH  ROM  AT24C01 Reference address:Using single chip microcomputer to realize reading and writing of universal memory IC card

Previous article:Development of a wide range and high precision optical fiber temperature sensor based on single chip microcomputer
Next article:Design Method of Realizing Digital Phase Converter Using Single Chip Microcomputer

Recommended ReadingLatest update time:2024-11-16 19:52

[ARM Notes] Access to device IO ports and IO memory
Devices usually provide a set of registers for controlling devices, reading and writing devices, and obtaining device status, namely control registers, data registers, and status registers. These registers may be located in IO space or in memory space. When located in IO space, they are usually called IO ports, and wh
[Microcontroller]
An intelligent password lock based on single chip microcomputer and serial EEPROM
1. Introduction The rapid development of electronic technology has brought great changes to traditional mechanical locks. The combination of modern electronic technology and mechanical technology has produced a large number of advanced locks such as voice-controlled locks, electronic password locks, r
[Microcontroller]
An intelligent password lock based on single chip microcomputer and serial EEPROM
Why does the microcontroller have so little memory?
Many friends have this question: Nowadays, solid-state drives are measured in T units, and the memory of mobile phones and computers is 8G or even 32G. Why is the RAM of microcontrollers less than 1M? The classic AT89C51 microcontroller has only 128 bytes of RAM (memory) inside, and the similarly classic MSP430F149 ha
[Microcontroller]
STM32F103 Flash operation code analysis
        /****************************************************** *************************/         // !!! Remember!!! Flash life is limited, don't keep erasing while the program is running!! Erase it when you need it         //!!!Everyone has the responsibility to protect Flash         // Flash must be unlocked first
[Microcontroller]
STM8 internal flash
Example typedef enum {     FLASH_MEMTYPE_PROG      = (u8)0x00, /*! Program memory */     FLASH_MEMTYPE_DATA      = (u8)0x01 /*! Data EEPROM memory */ } FLASH_MemType_TypeDef; int main() {     u32 addr = 0x4000;     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);     while(1)     {         FLASH_Unlock(FLASH_MEMT
[Microcontroller]
STM8 internal flash
STM32 Flash Balanced Storage Algorithm
In practical applications, it is often necessary to save or read some data during program execution. This data often changes during work and cannot be lost after power failure, so it needs to be stored in a timely manner. The commonly used memory for storing this data is EEPROM. Although STM32 does not have EEPROM i
[Microcontroller]
Summary of several memory types of STM32F407 development board, SRAM, FLASH, EEPROM
Common memory concepts: RAM, SRAM, SDRAM, ROM, EPROM, EEPROM, Flash memory can be divided into many categories. According to whether the data is lost when power is off, it can be divided into RAM (random access memory) and ROM (read-only memory). RAM has a faster access speed, but the data will be lost after power off
[Microcontroller]
Redmi K30S Extreme Edition is not only equipped with Snapdragon 865: LPDDR5 memory + UFS 3.1
      Today, Redmi preheats the Redmi K30S Extreme Edition.   The official emphasized that the Redmi K30S Extreme Edition not only has the Snapdragon 865 flagship processor, but also has LPDDR5 memory, UFS 3.1 flash memory, and supports WiFi 6. It is definitely not a "naked version" of the Snapdragon 865 mobile phone.
[Mobile phone portable]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号