Google

Jul 28, 2008

Interfacing Multimedia card with ATMega 128

I am interfacing Multimedia card with ATMega 128 through SPI protocol. For that while reading data from Multimedia Card, I use to send dummy bytes through MOSI and receive data through MISO in SPDR.
hears some routine. Take a Look on them

void SPI_init(void)

{

DDRB |= (1 << SPICS); // 0 set port B SPI chip select to output

DDRB |= (1 << SPICLK); // 1 set port B SPI clock to output

DDRB |= (1 << SPIDO); // 2 set port B SPI data out to output

DDRB &= ~(1 << SPIDI); // 3 set port B SPI data input to input

SPCR = (1 << SPE) | (1 << MSTR); //pg#168(391)

SPICS_ON();

}





char Command(char befF, int AdrH, int AdrL, char CRC )

{ // sends a command to the MMC

SPI(0xFF);

SPI(befF);

SPI((AdrH >> 8));

SPI(AdrH);

SPI((AdrL >> 8));

SPI(AdrL);

SPI(CRC);

SPI(0xFF);

return SPI(0xFF); // return the last received character

}



unsigned char SPI(unsigned char mydata)//transmit through SPI data out
line(MOSI)

{

//unsigned char Rx_SPDR=0xDC, Rx;

SPDR = mydata; //1 Byte data

while(!(SPSR & (1<<SPIF))) // wait for complete transmission

;

//printf("\r\nRx_SPDR = 0x%X", Rx_SPDR);

//Rx=SPDR;

//printf("\r\nRx_SPDR = 0x%X", Rx_SPDR);

return SPDR; //return the received value of SPDR

}





int Read_from_MMC(void) { // send 512 bytes from the MMC via the serial port

int i;

// 512 byte-read-mode

if (Command(0x51,0,512,0xFF) != 0)

{

printf("MMC: read error 1 ");

return 0;

}

// wait for 0xFE - start of any transmission

// ATT: typecast (char)0xFE is a must!

while(SPI(0xFF) != (char)0xFE);



for(i=0; i < 512; i++)

{

while(!(UCSR0A & (1 << UDRE0))); // wait for serial port

/*Here we are transmitting 0xFF through MOSI and then assigning the received
character to USART Data Register*/

UDR0 = SPI(0xFF); // send character

}





// at the end, send 2 dummy bytes

SPI(0xFF); // actually this returns the CRC/checksum byte

SPI(0xFF);

return 1;

}

You might be also interested in:

:: Assembly Language Programs for Multiplication and Division
:: Assembly Language Programs on array of Hexadecimal numbers
:: Assembly Language Programs on strings




No comments: