整理SD卡程序设计流程及操作系统_第1页
整理SD卡程序设计流程及操作系统_第2页
整理SD卡程序设计流程及操作系统_第3页
整理SD卡程序设计流程及操作系统_第4页
整理SD卡程序设计流程及操作系统_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、对SD卡进行操作首先要对 式,其流程图如图3所示。SD卡进行初始化,初始化的过程中设置SD卡工作在SPI模发送74个(H钟周期I发送M位命令CMDO发送激活命令CMD1接收sS卡响应f *4t0=0?一*二rczzzzoCMD55和ACMD41判断当前电压是否在工作范围内。主机在复位成功之后可以通过还可以继续通过 CMD10读取SD卡的CID寄存器,通过 CMD16设置数据Block长度,通 过CMD9读取卡的CSD寄存器。从CSD寄存器中,主机可获知卡容量,支持的命令集等 重要参数。SD卡初始化的C语言程序如下:un sig ned char SD_ln it(void) un sig ne

2、d char retry,te mp;un sig ned char i;for (i=0;i0x0f;i+) SPI_TransferByte(0xff); / 延迟 74 个以上的时钟SD_E nableO; / 开片选SPI_TransferByte(SD_RESET); / 发送复位命令SP I_Tra nsferByte(0x00);SP I_Tra nsferByte(0x00);SP I_Tra nsferByte(0x00);SP I_Tra nsferByte(0x00);SP I_Tra nsferByte(0x95);SP I_Tra nsferByte(0xff);SP

3、 I_Tra nsferByte(0xff);retry=0;do temp=Write_Comma nd_SD(SD_INIT,0);发送初始化命令retry+; if(retry=100) / 重试 100 次SD_Disable(); / 关片选return(INIT_CMD1_ERR0R);/如果重试100次失败返回错误号while(te mp !=0);SD_Disable(); / 关片选return(TRUE); / 返回成功数据块的读写完成SD卡的初始化之后即可进行它的读写操作。SD卡的读写操作都是通过发送SD卡命令完成的。SPI总线模式支持单块(CMD24 )和多块(CMD2

4、5 )写操作,多块操作是 指从指定位置开始写下去,直到SD卡收到一个停止命令 CMD12才停止。单块写操作的数据块长度只能是512字节。单块写入时,命令为CMD24,当应答为0时说明可以写入数据, 大小为512字节。SD卡对每个发送给自己的数据块都通过一个应答命令确认,它为1个字SD卡。节长,当低5位为00101时,表明数据块被正确写入在需要读取SD卡中数据的时候,读 SD卡的命令字为 CMD17,接收正确的第一个响 应命令字节为0xFE,随后是512个字节的用户数据块,最后为2个字节的CRC验证码。可见,读写SD卡的操作都是在初始化后基于SD卡命令和响应完成操作的,写、读SD卡的程序流程图如

5、图 4和图5所示。:7T 始 J 发送得命令CMD24T按收M)卡响应发i512?-节数据横收5D卡响陶十fN心壇务命令&加11$?二主J J 一弓,Y遞冋、VJ t三门丄小口牝SD背初始化发送耳命令CMD17J-MJ毎祚is?令存貢二亠y七-工厂“-Y接收2个字ijCRC码返冋结束语实验结果表明单片机使用 12MHz的晶体振荡器时,图n汶卡注殺国读写速度和功耗都基本令人满意,可以应用于对读写速度要求不高的情况下。本文详细阐述了用AT89C52单片机对SD卡进行操作的过程,提出了一种不带 SD卡控制器,MCU读写SD卡的方法,实现了 SD卡在电 能监测及无功补偿数据采集系统中的用途。本文的实现

6、程序把 SPI总线读写功能集成在一起,传递的 val变量既是向SPI写的数据,也 是从SPI读取的数据。具体程序如下:(程序是在Keil uVision2的编译环境下编写) sbit CS= P3A5;sbit CLK= P 1人5;sbit DataI=P 1人7;sbit DataO=卩1人6;#define SD_Disable() CS=1 / 片选关#define SD_Enable() CS=O / 片选开un sig ned char SP I_Tra nsferByte( un sig ned char val) un sig ned char BitCo un ter;for

7、(BitCou nter=8; BiCou nter!=0; BitCou nter-) CLK=O;DataI=0; / write if(val&0x80) DataI=1; val1;2.10 口模拟spi,实现数据读取,在初始化时使用 unsigned char iodataread(void)unsigned char data,temp,b;data=0;temp=0;for (b=0;b100) break;WDR();/feed the dogreturn(temp);/the respone of the byte_write_operation4.硬 spi 读数据 uns

8、igned char Read_Byte_SD(void) char Byte;/SD_Enable();SPDR=0xff; while(!(SPSR&(1100)break;/for some reason we need to delay here /delay_1ms();return(tmp);/the respone of the byte_write_operation7. 初始化 unsigned char SDInit(void) unsigned char a,b,retry,erroetoken;unsigned char CMD=0x40,0x00,0x00,0x00,

9、0x00,0x95;/cmd0 / Set certain pins to inputs and others to outputs/ Only SPI_DI (data in) is an input /SD_Direction_REG=ddrb SD_Direction_REG&=(1200) /fail and return return 1;/return 0;/Send the 2nd command retry=0;CMD0=0x41;CMD5=0xFF; while(erroetoken=iocmd(CMD)!=0x00)WDR();if (retry+200)return 2;

10、/Set the SPI bus to full speedSPCR=0x50;SPSR|=0x01; /Raise Chip Select SD_Disable(); return 0;8. 设置每次读的字节数char SD_set_length(unsigned int length)unsigned char retry; /Command to set the block length; char CMD=0x50,0x00,0x00,0x00,0x00,0xFF; /cmd16CMD3=(length&0xFF00)8);/CMD4= (length&0x00FF);while(Wr

11、ite_Command_SD(CMD)!=0)/WDR();if (retry+200)return 1;SD_Disable();return 0;9. write 512 bytes to a given sector from a Byte_byte_long Bufferunsigned char SD_write_sector(unsigned long addr,unsigned char *Buffer,unsigned int Bytes) unsigned int a;unsigned char retry,temp;/Command to read a block;char

12、 CMD=0x58,0x00,0x00,0x00,0x00,0xFF;/cmd24 CMD1=(addr&0xFF000000)24);CMD2=(addr&0x00FF0000)16);CMD3=(addr&0x0000FF00)8); CMD4=(addr&0x000000FF);/Send the write command while(Write_Command_SD(CMD)!=0)if (retry+50)return 1;/Send the start byteWrite_Byte_SD(0xfe);/Read off all the bytes in the block for

13、(a=0;a24);CMD2=(addr&0x00FF0000)16);CMD3=(addr&0x0000FF00)8);CMD4=(addr&0x000000FF);/Send the read command while(Write_Command_SD(CMD)!=0)WDR();/feed the dogif (retry+200)return 1;/Send the start bytewhile(Read_Byte_SD()!=0xfe)WDR();/feed the dog/Read off all the bytes in the blockfor(a=0;aWDR();/fe

14、ed the dog*Buffer=Read_Byte_SD();/serial(*Buffer);Buffer+;/Read CRC byteRead_Byte_SD();Read_Byte_SD();/ Set SD_Chip_Select to high SD_Disable();/SEI(); /re-enable interrupts return 0;/*/read xx bytes no matter of misalignment!*/unsigned char read_antimisaliment(unsigned long addr_temp,unsigned char *p_buffer, unsigned int length)unsigned int _length=0x0000;SD_Enable(); while(SD_read_sector(addr_temp,p_buffer,length)SD_Enable();/length-=0x0001;/to find a suuitable length to avoid misalignment_length+=0x0001;/ _length+length=xxSD_set_length(length);/if(_length=0x00

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论