性价比超高的U盘读写模块-PB375,兼容CH375读写操作
1. 功能
● 用于嵌入式系统/单片机读写U 盘、闪盘、闪存盘、USB 移动硬盘、USB 读卡器等。
● 支持符合USB 相关规范基于Bulk-Only 传输协议的各种U 盘/闪存盘/外置硬盘。
● 支持文件系统FAT12 和FAT16 及FAT32
● 文件操作功能:新建、删除、读写数据,打开关闭文件等。
● SPI接口,支持3.3V电平
● 兼容CH375模块的操作命令
● 单芯片解决方案,该模块只需要一个主控芯片外加少量的电容电阻便可,相对于51MCU+SL811/CH375的模块,无论模块尺寸还是成本都有着极大的优势。
● 可以是模块提供,也可以是芯片提供(只需要简单的几个外围元件就可以了)
具体参数可以下载数据手册或者联系销售人员
基本不需要占用单片机系统的存储空间,最少只需要几个字节的RAM 和几百字节的代码。
下面就是一个很通用的台湾的单片机(可以是holtek 松翰 义隆 富晶等)用汇编操作PB375实现读写操作U盘的代码:
USB_FAIL EQU 0AAH
USB_SUCC EQU 055H
TRY_TIMES EQU 0FFH
CMD_DiskReady EQU 071H
CMD_FileCreate EQU 065H
CMD_ByteWrite equ 07Ch
CMD_FileClose equ 067h
//设置SPI 端口的输入输出
spi_ini:
nop
call i2c_delay
b0bset spi_sdo_mod
b0bset spi_sck_mod
b0bset spi_cs_mod
b0bclr spi_sdi_mod
b0bset spi_cs ;not chose
b0bset spi_sck
b0bclr spi_sdo
b0bclr spi_sdi
ret
spi_send:
nop
clr epr_num
clr spi_in
b0bclr spi_sck
spi_write_bit:
b0bts0 epr_data_b
jmp spi_write_h
b0bclr spi_sdo
jmp spi_read_com
spi_write_h:
b0bset spi_sdo
spi_read_com:
call i2c_delay
b0bclr fc
rlcm spi_in
b0bts0 spi_sdi
incms spi_in
b0bclr fc
b0bset spi_sck
call i2c_delay
b0bclr spi_sck ;sck=0,data variable
call i2c_delay
rlcm epr_data ;prepare for next
incms epr_num
nop
b0bts1 epr_num_b
jmp spi_write_bit
call i2c_delay
ret
//发送命令
spi_send_command:
b0bclr spi_cs
b0mov rbank,#1 ; 写入命令码
mov a,cmd
b0mov rbank,#0
b0mov epr_data,a
call spi_send
b0mov rbank,#1 ;写入后续参数的长度
mov a,len
b0mov rbank,#0
b0mov epr_data,a
call spi_send
b0mov rbank,#1 ;写入后续参数
mov a,len
b0mov rbank,#0
b0bts0 FZ
jmp send_len_over ;len=0
b0mov temp,a
b0mov Y,#1
b0mov Z,#sendbuf
send_len_byte:
b0mov a,@YZ
b0mov rbank,#0
b0mov epr_data,a
call spi_send
b0mov rbank,#0
incms Z
nop
decms temp
jmp send_len_byte
send_len_over:
b0bset spi_cs
call i2c_delay
;check the status
mov a,#TRY_TIMES
b0mov rbank,#1
mov trytime,a
check_status:
b0mov rbank,#0
b0bclr spi_cs
mov a,#0ffh
b0mov epr_data,a
call spi_send
b0bset spi_cs
call i2c_delay
b0mov a,spi_in
sub a,#0ffh
b0bts0 FZ
jmp wait_1ms ;模块操作尚未完成
b0mov a,spi_in
sub a,#00h
b0bts0 FZ
jmp succ_quit_00 ;操作成功
fail_quit:
b0bset spi_cs
mov a,#USB_FAIL
b0mov rbank,#1
mov usb_reslt,a
b0mov rbank,#0
ret
wait_1ms: //延时
b0mov z,#0f7h
b0mov y,#0e3h ;稳定oscillator,40ms
delay_ms_02:
b0bset fwdrst
nop
nop
incms z
jmp delay_ms_02
nop
incms y
jmp delay_ms_02
b0mov rbank,#1
decms trytime
jmp check_status
jmp fail_quit
succ_quit_00:
b0mov rbank,#0
b0bclr spi_cs
mov a,#00h
b0mov epr_data,a
call spi_send
b0mov a,spi_in
b0bts0 FZ
jmp succ_quit
b0mov temp,a
b0mov Y,#1
b0mov Z,#recvbuf
read_len_byte:
mov a,#0
b0mov rbank,#0
b0mov epr_data,a
call spi_send
b0mov a,spi_in
b0mov @YZ,a
b0mov rbank,#0
incms Z
nop
decms temp
jmp read_len_byte
succ_quit:
b0bset spi_cs
mov a,#USB_succ
b0mov rbank,#1
mov usb_reslt,a
b0mov rbank,#0
ret
testusb://测试主程序
call spi_ini //初始化spi
b0mov rbank,#1 //发送U盘是否连接好命令
mov a,#CMD_DiskReady
mov cmd,a
mov a,#0
mov len,a
b0mov rbank,#0
call spi_send_command
b0mov rbank,#1
mov a,usb_reslt
b0mov rbank,#0
sub a,#USB_succ
b0bts1 FZ
ret
;ok diskread ;sendbuf is 92+”TEST.DAT” //创建文件”TEST.DAT”
b0mov rbank,#0
b0mov Y,#1
b0mov Z,#sendbuf
mov a,#92
b0mov @yz,a
incms Z
mov