avr的SD卡基本读写程序(二)
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;
/////////////////
}
avrSD卡读写程 相关文章:
- avr的SD卡基本读写程序(一)(11-13)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)