sukhdeepmankoo
Member level 2
Hi,
I am trying to interface SD Card with Atmega8. I have made the following connections on hardware side.
SD Card Atmega8
Pins Pins
1)CS 16(SS)
2)DI 17(MOSI)
3)GND
4)3.24V
5)SCK 19(SCK)
6)GND
7)DO 18(MISO)
Through MicroSD Adaptor.
On software Side, I have made Atmega8's software. Where Atmega8 is operating at 8Mhz.
void spi_init(void)
{ SPI_DDR|=(1<<MOSI)|(1<<SS_LOW)|(1<<SCK);
SPI_DDR&=~(1<<MISO);
SPI_PORT|=(1<<MOSI)|(1<<SS_LOW)|(1<<SCK)|(1<<MISO);
SPI_PORT&=~(1<<SS_LOW);
SPSR|=(1<<SPI2X);
SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR1);
}
unsigned char spi_transmit(unsigned char data)
{ SPDR=data;
while(!(SPSR & (1<<SPIF)));
data=SPDR;
return data;
}
void sd_spi_init(void)
{ unsigned char count,response=0;
unsigned int retry;
/***********wait for 74 clock cycles**********/
lcd_command_register(0x01);
delay(1500);
lcd_char_display("Initialization",LINE1+3);
SPI_PORT|=(1<<SS_LOW);
for(count=0;count<10;count++)
spi_transmit(0xff);
SPI_PORT&=~(1<<SS_LOW);
spi_transmit(0x40);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0x95);
for(retry=0;retry<0xfffe;retry++)
{ response=spi_transmit(0xff);
if(response==1)
{ SPI_PORT|=(1<<SS_LOW);
break;
}
}
lcd_value_display(response,LINE4);
SPI_PORT|=(1<<SS_LOW);
spi_transmit(0xff);
SPI_PORT&=~(1<<SS_LOW);
lcd_value_display(response,LINE4);
for(retry=0;retry<0xfffe;retry++)
{ spi_transmit(0x41);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0xff);
response=spi_transmit(0xff);
if(response==0)
{ SPI_PORT|=(1<<SS_LOW);
break;
}
}
lcd_value_display(response,LINE4+10);
SPI_PORT|=(1<<SS_LOW);
spi_transmit(0xff);
}
I am getting 255 on LCD Screen. why SD is not able to Initialize? Kindly tell me to find where i have done mistake.
Thanks & Regards
I am trying to interface SD Card with Atmega8. I have made the following connections on hardware side.
SD Card Atmega8
Pins Pins
1)CS 16(SS)
2)DI 17(MOSI)
3)GND
4)3.24V
5)SCK 19(SCK)
6)GND
7)DO 18(MISO)
Through MicroSD Adaptor.
On software Side, I have made Atmega8's software. Where Atmega8 is operating at 8Mhz.
void spi_init(void)
{ SPI_DDR|=(1<<MOSI)|(1<<SS_LOW)|(1<<SCK);
SPI_DDR&=~(1<<MISO);
SPI_PORT|=(1<<MOSI)|(1<<SS_LOW)|(1<<SCK)|(1<<MISO);
SPI_PORT&=~(1<<SS_LOW);
SPSR|=(1<<SPI2X);
SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR1);
}
unsigned char spi_transmit(unsigned char data)
{ SPDR=data;
while(!(SPSR & (1<<SPIF)));
data=SPDR;
return data;
}
void sd_spi_init(void)
{ unsigned char count,response=0;
unsigned int retry;
/***********wait for 74 clock cycles**********/
lcd_command_register(0x01);
delay(1500);
lcd_char_display("Initialization",LINE1+3);
SPI_PORT|=(1<<SS_LOW);
for(count=0;count<10;count++)
spi_transmit(0xff);
SPI_PORT&=~(1<<SS_LOW);
spi_transmit(0x40);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0x95);
for(retry=0;retry<0xfffe;retry++)
{ response=spi_transmit(0xff);
if(response==1)
{ SPI_PORT|=(1<<SS_LOW);
break;
}
}
lcd_value_display(response,LINE4);
SPI_PORT|=(1<<SS_LOW);
spi_transmit(0xff);
SPI_PORT&=~(1<<SS_LOW);
lcd_value_display(response,LINE4);
for(retry=0;retry<0xfffe;retry++)
{ spi_transmit(0x41);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0);
spi_transmit(0xff);
response=spi_transmit(0xff);
if(response==0)
{ SPI_PORT|=(1<<SS_LOW);
break;
}
}
lcd_value_display(response,LINE4+10);
SPI_PORT|=(1<<SS_LOW);
spi_transmit(0xff);
}
I am getting 255 on LCD Screen. why SD is not able to Initialize? Kindly tell me to find where i have done mistake.
Thanks & Regards