发表于:2009/3/21 10:51:13
#0楼
pb375 u盘读写汇编程序
性价比超高的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发一个字节,同时读一个字节
*************************/
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 a,#116
b0mov @yz,a
incms z
mov a,#101
b0mov @yz,a
incms z
mov a,#115
b0mov @yz,a
incms z
mov a,#116
b0mov @yz,a
incms z
mov a,#46
b0mov @yz,a
incms z
mov a,#100
b0mov @yz,a
incms z
mov a,#97
b0mov @yz,a
incms z
mov a,#116
b0mov @yz,a
incms z
b0mov rbank,#1
mov a,#cmd_filecreate //创建文件命令
mov cmd,a
mov a,#10
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
b0mov rbank,#0
b0mov y,#1
b0mov z,#sendbuf
mov a,#19
b0mov @yz,a
incms z
mov a,#33
b0mov @yz,a
incms z
mov a,#34
b0mov @yz,a
incms z
mov a,#35
b0mov @yz,a
incms z
mov a,#36
b0mov @yz,a
incms z
mov a,#37
b0mov @yz,a
incms z
mov a,#38
b0mov @yz,a
incms z
mov a,#39
b0mov @yz,a
incms z
mov a,#40
b0mov @yz,a
incms z
mov a,#41
b0mov @yz,a
b0mov rbank,#1
mov a,#cmd_bytewrite //写入数据
mov cmd,a
mov a,#10
mov len,a
b0mov rbank,#0
call spi_send_command
b0mov rbank,#1
mov a,#cmd_fileclose //关闭文件
mov cmd,a
mov a,#1
mov len,a
b0mov rbank,#0
call spi_send_command
ret
欢迎垂询
电话:075529739852
地址:深圳市南山区高新中四道30号龙泰利大厦306
http://www.chinamcu.com
----------------------------------------------
此篇文章从博客转发
原文地址: Http://blog.gkong.com/more.asp?id=81186&Name=lanxunlanya
深圳蓝色飞舞