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
2、;for (i=0;i1; 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 byte_write_operation 4.硬 spi 读数据unsigned char Read_Byte_SD(void) char Byte;/SD_Enable();SPDR=0xff; while(!(SPSR /fo
3、r 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; unsigned char CMD=0x40,0x00,0x00,0x00,0x00,0x95;/cmd0/ Set certain pins to inputs and others to outputs / Only SPI_DI (data in) i
4、s an input /SD_Direction_REG=ddrbSD_Direction_REG /return 0;/Send the 2nd command retry=0; CMD0=0x41; CMD5=0xFF; while(erroetoken=iocmd(CMD)!=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_le
5、ngth(unsigned int length) unsigned char retry; /Command to set the block length; char CMD=0x50,0x00,0x00,0x00,0x00,0xFF; /cmd16 CMD3=(length/ CMD4= (length 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_lo
6、ng 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 CMD=0x58,0x00,0x00,0x00,0x00,0xFF;/cmd24CMD1=(addr CMD2=(addr CMD3=(addr CMD4=(addr /Send the write command while(Write_Command
7、_SD(CMD)!=0) if (retry+50) return 1; /Send the start byte Write_Byte_SD(0xfe); /Read off all the bytes in the block for(a=0;a24); CMD2=(addr CMD3=(addr CMD4=(addr /Send the read command while(Write_Command_SD(CMD)!=0) WDR();/feed the dogif (retry+200) return 1; /Send the start byte while(Read_Byte_S
8、D()!=0xfe) WDR();/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 ma
9、tter 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=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;
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。