//******************************************************************
//Function: to read a single block from SD card
//Arguments: none
//return: unsigned char; will be 0 if no error,
// otherwise the response byte will be sent
//******************************************************************
unsigned char SD_readSingleBlock(unsigned long startBlock)
{
unsigned char response;
unsigned int i, retry=0;
response = SD_sendCommand(READ_SINGLE_BLOCK, startBlock<<9); //read a Block command
//block address converted to starting address of 512 byte Block
if(response != 0x00) //check for SD status: 0x00 - OK (No flags set)
return response;
PORTD = 5;
SD_CS_ASSERT;
retry = 0;
while((SPI_receive()) != 0xfe) //wait for start block token 0xfe (0x11111110)
if(retry++ > 0xfffe){SD_CS_DEASSERT; return 1;} //return if time-out
for(i=0; i<512; i++) //read 512 bytes
buffer[i] = SPI_receive();
SPI_receive(); //receive incoming CRC (16-bit), CRC is ignored here
SPI_receive();
SPI_receive(); //extra 8 clock pulses
SD_CS_DEASSERT;
return 0;
}