ESP32C3 LuatOS RC522①写入数据并读取M1卡

LuatOS RC522官方示例

官方示例没有针对具体开发板,现以ESP32C3开发板为例。

 选用的RC522模块

 ESP32C3-CORE开发板

 

注意ESP32C3的 SPI引脚位置,SPI的id=2

示例代码

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "helloworld"
VERSION = "1.0.0"

-- 引入必要的库文件(lua编写), 内部库不需要require
sys = require("sys")
local rc522 = require "rc522"
log.info("main", "hello world")

print(_VERSION)

sys.taskInit(function()
    spi_rc522 = spi.setup(2,nil,0,0,8,100000,spi.MSB,1,1)
    -- spi_rc522 = spi.setup(0,nil,0,0,8,100000)
    rc522.init(2,6,7)
    wdata={0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
    while 1 do
        rc522.write_datablock(8,wdata)
        for i=0,63 do
            local a,b = rc522.read_datablock(i)
             if a then
                 print("read",i,b:toHex())
             end
         end
     sys.wait(500)
     

    end
end)
 
-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

项目文件

 点击"重启串口"运行程序。程序串口输出。

 程序在数据块8中写入数据{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}

以上代码用于验证模块连接及代码可用性。

猜你喜欢

转载自blog.csdn.net/armcsdn/article/details/132638530