51汇编实现的spi代码(模式0)

本文提供了一种使用SPI模式0的汇编语言实现代码,该代码已在指纹模组上验证通过。文中详细展示了SPI模式0的初始化过程及读写操作,适用于需要使用SPI接口进行数据交换的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

鉴于很多单片机书上是多是使用C51实现的spi模式1,用于读取SD卡,实际上spi模式0应该更普遍才对。故这里附上汇编实现的spi代码,已在指纹模组上验证过,功能ok。

/*CopyRight@piaoxiang.zhang****/

CSn EQU P0.4
MOSI EQU P0.5
MISO EQU P0.6
CLK EQU P0.7
ORG 0000H
   LJMP start
ORG 0100H
start:
        LCALL spi_init
        MOV R2,#40H
read_reg_all:
        MOV40H,#0BFH;
        LCALL spi_enable
        MOV R0,#10H
        MOV A,@R1
        ADD A,R2
        LCALL spi_write
        LCALL spi_disable
        DJNZ R2, read_reg_all
        SJMP start
spi_read:
       CPL CLK
       NOP
       NOP
       MOV C,MISO
       RLC A
       CPL CLK
       DJNZ R0,spi_read    
       RET
spi_write:

       RLC A
       MOV MOSI,C
       CPL CLK;CLK为低,发送数据
       NOP
       NOP
       NOP
       CPL CLK;CLK 为高
       DJNZ R0,spi_write
       SETB MOSI
       RET
spi_enable:
       CLR CSn
       ;MOV R0,#01H
       ;LCALL delay
       RET
spi_disable:
       SETB CSn
       ;MOV R0,#01H
       ;LCALL delay
       RET
spi_init:
    CLR CLK ;CPOL=0,CLK 空闲低电平
    SETB CSn ;片选低电平有效
    SETB MOSI
    SETB MISO
    MOV R1,#40H
    MOV 40H,#5FH
    CLR P0.2;在取上拉电阻时
    CLR P0.1;P01 与 P02 短路了
    RET

仿真图这里写图片描述

myasm51,小型的51单片机汇编器源码。 基于Linux环境下编写的小型的51单片机汇编器,源码开放,采用lex和yacc两个扫描和分析工具创建,代码小巧,易于研读和分析。对汇编源程序2遍扫描完成汇编,可以生成列表文件,Intel的Hex格式的文件及.bin格式的映像文件,后两种文件可以直接下载到单片机上运行。源码程序包内包含若干示例汇编源程序(.asm),proteus的格式的数字种的仿真文件,用以测试编译结果,另有编译后的dos下的可执行文件myasm51.exe,可以在windows的命令窗口下运行。另外提供一个简明的用户手册以供参考。以下为程序包的README: What is Myasm51 =============== Myasm51 is an open source mini-assembler for the Intel MCS-51 family of microcontrollers or the compatible ones, distributed under the GPL license. By scanning the source file in two pass, Myasm51 translates a symbolic code in text file (assembly language source) into a machine executable object file. During the first pass, the assembler builds a symbol table from the symbols and labels used in the source file. In the second pass, the assembler maps the source file into machine code and generates the listing file through what it receives in the first pass. Myasm51 is an absolute assembler and only generates absolute object files in the plain binary file (with .bin extension) or the Intel Hex file (with .Hex extension) which can be read by any ROM programmer to burn the object code into the ROM space of microcontrollers. How to make =========== We assume that the UNIX utilities yacc and lex have been installed in you system, and following these steps to build Myasm51 by the super user 'root' in the Linux or the UNIX cloned system. # tar zxf myasm51-gk-20151208_121306.tar.gz # cd myasm51 # make # cp myasm51 /usr/local/bin done. How to use ========== [root@rh9 myasm51]# cd examples [root@rh9 examples]# myasm51 Myasm51 Assembler. Ver 0.01 Release 1, (20151231_165818) Snallie@tom.com, Wed Sep 30 17:28:09 CST 2015 built: Dec 31 2015 - 17:04:44 Usage: myasm51 [-o] [-F] [-C] [-d] in.asm where -ob to output binary file 'in.bin' -oh to output hex file 'in.hx' (default format) -oH to output Intel Hex file 'in.Hex' -F to fill free bit with 0 or 1, (default 0) -C to t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值