hi all,
Good day. I did interfacing SD card with Atmega32 via SPI. i gone through with these links http://elm-chan.org/fsw/ff/00index_e.htmlIn this i could not create or open a existing file. And my code is
http://elm-chan.org/fsw/ff/en/open.html
/***************************************************** This program was produced by the CodeWizardAVR V2.05.3 Standard Automatic Program Generator © Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 11/3/2014 Author : Dev6 Company : sass Comments: Chip type : ATmega32L Program type : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 512 *****************************************************/ #include <mega32.h> #include <stdio.h> #include <stdlib.h> #include <spi.h> #include <sdcard.h> #include <ff.h> #include <delay.h> void main(void) { unsigned char wr_buf[7] = "sriram"; unsigned int i=0; FRESULT res; FATFS fs; FIL fp; unsigned char buffer[10]; unsigned int br, bw; // File read/write count // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0xB0; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud Rate: 9600 UCSRA=0x00; UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x33; // SPI initialization // SPI Type: Master // SPI Clock Rate: 2000.000 kHz // SPI Clock Phase: Cycle Start // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0x50; SPSR=0x00; puts("\nstart_sd_card"); res = f_mount(0, &fs); if (res == FR_OK) { printf("\nres_mount: %d", res); } if (res == FR_OK) { puts("\nin create_new"); res = f_open(&fp,"0:sd0.txt",FA_CREATE_NEW); //create a new file } else puts("\nfail create_new"); f_close(&fp); if (res == FR_OK) { puts("\nin write"); res = f_open(&fp,"0:sd0.txt",FA_WRITE); //res = f_write(&fp,buffer,sizeof buffer,&br); res = f_write(&fp,wr_buf,sizeof(wr_buf),&bw); } else puts("\nfail write"); f_close(&fp); if (res == FR_OK) { puts("\nin read"); res = f_open(&fp,"0:sd.txt",FA_READ); if(res) printf("res: %d",res); //delay_ms(2000); //res = f_read(&fp,buffer,br,&bw); res = f_read(&fp,buffer,10,&bw); if(res) printf("res_read: %d",res); printf("\nbuffer:%s",buffer); } else puts("\nfail read"); f_close(&fp); puts("\nend"); while (1) { // Place your code here } }
and my result is
start_sd_card
res_mount: 0
in create_new