微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > avr的SD卡基本读写程序(二)

avr的SD卡基本读写程序(二)

时间:11-13 来源:互联网 点击:

interrupts
return 0;

}

unsigned char SD_read_sector(unsigned long addr,unsigned char *Buffer,unsigned int Bytes)
{

unsigned int a;
unsigned char retry;
//Command to read a block;
char CMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};//cmd17
//CLI(); //disable all interrupts
//addr = addr < 9; //addr = addr * 512
CMD[1]=((addr&0xFF000000)>>24);
CMD[2]=((addr&0x00FF0000)>>16);
CMD[3]=((addr&0x0000FF00)>>8);
CMD[4]=(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();//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 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==xx
SD_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 bytes
return 0;
/////////////////
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top