原理介紹
電路圖
程式列表
Python - pn532api.py
"""
pn532api.py contains a set of core functions
Reference: https://github.com/hoanhan101/pn532
"""
from micropython import const
from machine import I2C, Pin
import pn532register
import utime
# rest time in second betweet each read/write transaction
REST_INTERVAL = 500
# maximum block size for each read transaction
BLOCK_SIZE = 20
class PN532(object):
def __init__(self):
# i2c device address
self.address = pn532register.PN532_DEFAULT_ADDRESS
# smbus object
# initialize I2C
self.bus = I2C(0,I2C.MASTER,baudrate=400000) #Create I2C0 Master Mode, Baudrate=400kHz
def setup(self):
"""setup the device"""
self.sam_config()
def read(self):
"""return a raw reading value as an array of bytes"""
self.in_list_passive_target()
while True:
read = self.read_addr(BLOCK_SIZE)
# check the first 3 bytes to see if a card is detected or not
if read[:3] != [0x00, 0x80, 0x80]:
# TODO - the first 9 bytes are configs bytes so we're not really
# interested in getting these at the moment, though they could
# be used for validation in the future
return read[9:]
def sam_config(self):
"""send SAMConfiguration command"""
self.write_addr(
construct_frame([pn532register._COMMAND_SAMCONFIGURATION, 0x01, 0x01, 0x00])
)
self.read_addr(BLOCK_SIZE)
def in_list_passive_target(self):
"""send InListPassiveTarget command"""
self.write_addr(
construct_frame([pn532register._COMMAND_INLISTPASSIVETARGET, 0x01, 0x00])
)
self.read_addr(BLOCK_SIZE)
def write_addr(self, data):
"""write to its own address with given block data"""
utime.sleep_ms(REST_INTERVAL)
self.bus.send(data,self.address)
def read_addr(self, length):
"""read from its own address a given-length of block data"""
utime.sleep_ms(REST_INTERVAL)
buf = bytearray(length)
self.bus.recv(buf,self.address)
return buf
def construct_frame(data):
"""construct frame for communicating between host controller and pn532"""
# begin with 6-bytes frame structure
buf = bytearray(6)
buf[0] = pn532register._PREAMBLE
buf[1] = pn532register._STARTCODE1
buf[2] = pn532register._STARTCODE2
buf[3] = len(data) + 1 # number of bytes in data and frame identifier field
buf[4] = (~buf[3] & 0xFF) + 0x01 # packet length checksum
buf[5] = pn532register._HOSTTOPN532
tmp_sum = pn532register._HOSTTOPN532
for b in data:
tmp_sum += b
buf.append(b)
buf.append((~tmp_sum & 0xFF) + 0x01) # data checksum
buf.append(pn532register._POSTAMBLE)
return buf
Python - pn532register.py
"""
pn532register.py contains a set of registers addresses mapping
"""
from micropython import const
PN532_DEFAULT_ADDRESS = 0x24
# pylint: disable=bad-whitespace
_PREAMBLE = const(0x00)
_STARTCODE1 = const(0x00)
_STARTCODE2 = const(0xFF)
_POSTAMBLE = const(0x00)
# frame identifier
_HOSTTOPN532 = const(0xD4)
_PN532TOHOST = const(0xD5)
# PN532 Commands
_COMMAND_DIAGNOSE = const(0x00)
_COMMAND_GETFIRMWAREVERSION = const(0x02)
_COMMAND_GETGENERALSTATUS = const(0x04)
_COMMAND_READREGISTER = const(0x06)
_COMMAND_WRITEREGISTER = const(0x08)
_COMMAND_READGPIO = const(0x0C)
_COMMAND_WRITEGPIO = const(0x0E)
_COMMAND_SETSERIALBAUDRATE = const(0x10)
_COMMAND_SETPARAMETERS = const(0x12)
_COMMAND_SAMCONFIGURATION = const(0x14)
_COMMAND_POWERDOWN = const(0x16)
_COMMAND_RFCONFIGURATION = const(0x32)
_COMMAND_RFREGULATIONTEST = const(0x58)
_COMMAND_INJUMPFORDEP = const(0x56)
_COMMAND_INJUMPFORPSL = const(0x46)
_COMMAND_INLISTPASSIVETARGET = const(0x4A)
_COMMAND_INATR = const(0x50)
_COMMAND_INPSL = const(0x4E)
_COMMAND_INDATAEXCHANGE = const(0x40)
_COMMAND_INCOMMUNICATETHRU = const(0x42)
_COMMAND_INDESELECT = const(0x44)
_COMMAND_INRELEASE = const(0x52)
_COMMAND_INSELECT = const(0x54)
_COMMAND_INAUTOPOLL = const(0x60)
_COMMAND_TGINITASTARGET = const(0x8C)
_COMMAND_TGSETGENERALBYTES = const(0x92)
_COMMAND_TGGETDATA = const(0x86)
_COMMAND_TGSETDATA = const(0x8E)
_COMMAND_TGSETMETADATA = const(0x94)
_COMMAND_TGGETINITIATORCOMMAND = const(0x88)
_COMMAND_TGRESPONSETOINITIATOR = const(0x90)
_COMMAND_TGGETTARGETSTATUS = const(0x8A)
_RESPONSE_INDATAEXCHANGE = const(0x41)
_RESPONSE_INLISTPASSIVETARGET = const(0x4B)
_WAKEUP = const(0x55)
_MIFARE_ISO14443A = const(0x00)
# Mifare Commands
MIFARE_CMD_AUTH_A = const(0x60)
MIFARE_CMD_AUTH_B = const(0x61)
MIFARE_CMD_READ = const(0x30)
MIFARE_CMD_WRITE = const(0xA0)
MIFARE_CMD_TRANSFER = const(0xB0)
MIFARE_CMD_DECREMENT = const(0xC0)
MIFARE_CMD_INCREMENT = const(0xC1)
MIFARE_CMD_STORE = const(0xC2)
MIFARE_ULTRALIGHT_CMD_WRITE = const(0xA2)
# Prefixes for NDEF Records (to identify record type)
NDEF_URIPREFIX_NONE = const(0x00)
NDEF_URIPREFIX_HTTP_WWWDOT = const(0x01)
NDEF_URIPREFIX_HTTPS_WWWDOT = const(0x02)
NDEF_URIPREFIX_HTTP = const(0x03)
NDEF_URIPREFIX_HTTPS = const(0x04)
NDEF_URIPREFIX_TEL = const(0x05)
NDEF_URIPREFIX_MAILTO = const(0x06)
NDEF_URIPREFIX_FTP_ANONAT = const(0x07)
NDEF_URIPREFIX_FTP_FTPDOT = const(0x08)
NDEF_URIPREFIX_FTPS = const(0x09)
NDEF_URIPREFIX_SFTP = const(0x0A)
NDEF_URIPREFIX_SMB = const(0x0B)
NDEF_URIPREFIX_NFS = const(0x0C)
NDEF_URIPREFIX_FTP = const(0x0D)
NDEF_URIPREFIX_DAV = const(0x0E)
NDEF_URIPREFIX_NEWS = const(0x0F)
NDEF_URIPREFIX_TELNET = const(0x10)
NDEF_URIPREFIX_IMAP = const(0x11)
NDEF_URIPREFIX_RTSP = const(0x12)
NDEF_URIPREFIX_URN = const(0x13)
NDEF_URIPREFIX_POP = const(0x14)
NDEF_URIPREFIX_SIP = const(0x15)
NDEF_URIPREFIX_SIPS = const(0x16)
NDEF_URIPREFIX_TFTP = const(0x17)
NDEF_URIPREFIX_BTSPP = const(0x18)
NDEF_URIPREFIX_BTL2CAP = const(0x19)
NDEF_URIPREFIX_BTGOEP = const(0x1A)
NDEF_URIPREFIX_TCPOBEX = const(0x1B)
NDEF_URIPREFIX_IRDAOBEX = const(0x1C)
NDEF_URIPREFIX_FILE = const(0x1D)
NDEF_URIPREFIX_URN_EPC_ID = const(0x1E)
NDEF_URIPREFIX_URN_EPC_TAG = const(0x1F)
NDEF_URIPREFIX_URN_EPC_PAT = const(0x20)
NDEF_URIPREFIX_URN_EPC_RAW = const(0x21)
NDEF_URIPREFIX_URN_EPC = const(0x22)
NDEF_URIPREFIX_URN_NFC = const(0x23)
Python - epy-Lite_pn532.py
"""
epy-Lite_pn532.py
ePy-Lite PN532
------------
GND GND
3V3 VCC
P18 SDA
P17 SCL
"""
from machine import Switch #Get button KEY library
from pn532api import PN532
if __name__== "__main__":
KeyA = Switch('keya') #Create button A
nfc = PN532()
# setup the device
nfc.setup()
# keep reading until a value is returned
while True:
read = nfc.read()
# print(read)
print("".join("%02XH," % i for i in read))
if (KeyA.value()) == True: #Press A Key
break
# KeyA.deinit()
執行結果