收藏 分销(赏)

SD卡程序设计、流程及操作系统.doc

上传人:xrp****65 文档编号:7666621 上传时间:2025-01-11 格式:DOC 页数:8 大小:101KB
下载 相关 举报
SD卡程序设计、流程及操作系统.doc_第1页
第1页 / 共8页
SD卡程序设计、流程及操作系统.doc_第2页
第2页 / 共8页
SD卡程序设计、流程及操作系统.doc_第3页
第3页 / 共8页
SD卡程序设计、流程及操作系统.doc_第4页
第4页 / 共8页
SD卡程序设计、流程及操作系统.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、对SD卡进行操作首先要对SD卡进行初始化,初始化的过程中设置SD卡工作在SPI模式,其流程图如图3所示。在复位成功之后可以通过CMD55和ACMD41判断当前电压是否在工作范围内。主机还可以继续通过CMD10读取SD卡的CID寄存器,通过CMD16设置数据Block长度,通过CMD9读取卡的CSD寄存器。从CSD寄存器中,主机可获知卡容量,支持的命令集等重要参数。SD卡初始化的C语言程序如下:unsigned char SD_Init(void) unsigned char retry,temp;unsigned char i;for (i=0;i0x0f;i+) SPI_TransferBy

2、te(0xff); /延迟74个以上的时钟SD_Enable(); /开片选SPI_TransferByte(SD_RESET); /发送复位命令SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x95);SPI_TransferByte(0xff);SPI_TransferByte(0xff);retry=0;do temp=Write_Command_SD(SD_INIT,0); /发送初始化命令retry+;if(ret

3、ry=100) /重试100次SD_Disable(); /关片选return(INIT_CMD1_ERROR); /如果重试100次失败返回错误号while(temp!=0);SD_Disable(); /关片选return(TRUE); /返回成功数据块的读写完成SD卡的初始化之后即可进行它的读写操作。SD卡的读写操作都是通过发送SD卡命令完成的。SPI总线模式支持单块(CMD24)和多块(CMD25)写操作,多块操作是指从指定位置开始写下去,直到SD卡收到一个停止命令CMD12才停止。单块写操作的数据块长度只能是512字节。单块写入时,命令为CMD24,当应答为0时说明可以写入数据,大小

4、为512字节。SD卡对每个发送给自己的数据块都通过一个应答命令确认,它为1个字节长,当低5位为00101时,表明数据块被正确写入SD卡。在需要读取SD卡中数据的时候,读SD卡的命令字为CMD17,接收正确的第一个响应命令字节为0xFE,随后是512个字节的用户数据块,最后为2个字节的CRC验证码。可见,读写SD卡的操作都是在初始化后基于SD卡命令和响应完成操作的,写、读SD卡的程序流程图如图4和图5所示。结束语实验结果表明单片机使用12MHz的晶体振荡器时,读写速度和功耗都基本令人满意,可以应用于对读写速度要求不高的情况下。本文详细阐述了用AT89C52单片机对SD卡进行操作的过程,提出了一种

5、不带SD卡控制器,MCU读写SD卡的方法,实现了SD卡在电能监测及无功补偿数据采集系统中的用途。 本文的实现程序把SPI总线读写功能集成在一起,传递的val变量既是向SPI写的数据,也是从SPI读取的数据。具体程序如下:(程序是在Keil uVision2的编译环境下编写)sbit CS=P35; sbit CLK= P15;sbit DataI=P17; sbit DataO=P16;#define SD_Disable() CS=1 /片选关 #define SD_Enable() CS=0 /片选开 unsigned char SPI_TransferByte(unsigned char

6、 val) unsigned char BitCounter;for(BitCounter=8; BiCounter!=0; BitCounter-) CLK=0;DataI=0; / writeif(val&0x80) DataI=1;val1; 2.io口模拟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 by

7、te_write_operation4.硬spi读数据unsigned 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_operation 7.初始化unsigned char SDInit(void) unsigned char a,b,retry,erroetoken; unsi

8、gned char CMD=0x40,0x00,0x00,0x00,0x00,0x95;/cmd0/ Set certain pins to inputs and others to outputs / Only SPI_DI (data in) is an input /SD_Direction_REG=ddrbSD_Direction_REG&=(1200) /fail and returnreturn 1; /return 0;/Send the 2nd command retry=0; CMD0=0x41; CMD5=0xFF; while(erroetoken=iocmd(CMD)!

9、=0x00) WDR(); if (retry+200) return 2; /Set the SPI bus to full speed SPCR=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; /cmd16 CMD3=(length

10、&0xFF00)8);/ CMD4= (length&0x00FF); while(Write_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; unsi

11、gned char retry,temp; /Command to read a block; char CMD=0x58,0x00,0x00,0x00,0x00,0xFF;/cmd24CMD1=(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 byte Wr

12、ite_Byte_SD(0xfe); /Read off all the bytes in the block for(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 dog if (retry+200) return 1; /Send the start byte while(Read_Byte_SD()!=0xfe) WDR(

13、);/feed the dog /Read off all the bytes in the block for(a=0;a WDR();/feed the dog*Buffer=Read_Byte_SD(); /serial(*Buffer); Buffer+; /Read CRC byte Read_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 misalig

14、nment!*/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=0x0000)return 0;/addr_temp+=length;SD_Enable();/SD_set_length(_length); SD_Enable();/while(SD_read_sector(addr_temp,p_buffer,_length)SD_Enable();SD_Enable();/SD_set_length(length+_length);/to read the rest bytes of the xx bytesreturn 0;

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服