SD karta veri yazma problemi

Başlatan berkay_91, 25 Temmuz 2015, 13:29:16

berkay_91

mrb, amacım ADC den aldığım verileri SD karta .txt uzantılı bir dosyada altalta yazmak ve butona bastığımda yazmayı durdurmak bidaki basışımda tekrar yazmaya devam etmesini sağlamak, ancak bu kütüphaneyi kullanmasını çok bilmediğin için yapamadım daha önceden bu kütüphaneyi kullanmış arkadaşlardan yardım istiyorum... işlemci Atmega328, kütüphane Elm by chan Fatfs, lütfen kod yazarak veya ne yazıcağımı söyleyerek yardım edin
http://s2.dosya.tc/server2/sau0g2/SD_kart_ve_ADC.rar.html
/*
 * SD_kart_ve_ADC.c
 *
 * Created: 25.07.2015 10:57:10
 *  Author: BERKAY
 */ 


#define F_CPU 1000000UL
#include <util/delay.h>
#include <avr/io.h>	/* Device specific declarations */
#include "ff.h"		/* Declarations of FatFs API */

int Read_adc();
void adc_init();
void write_data();

volatile int temp=0,x=0;

FATFS FatFs;		/* FatFs work area needed for each volume */
FIL Fil;			/* File object needed for each open file */
UINT bw;

int Read_adc(){
	
	ADCSRA |= 0x40;			// start the adc conversion
	while(ADCSRA & 0x40);
	return ADCH;         // ADCW = adc low bits + adc high bits
	
}


void adc_init(){
	
	ADMUX= 0x65;  // adc pin is selected ADC5, 8 bit using is actived, referrance voltage is AVCC
	ADCSRA = 0x83;  // prescalar is used 8

}

void write_data(){
	
	    PORTB |=(1<<0);
	
	    x++;
	
		f_mount(&FatFs, "", 0);		/* Give a work area to the default drive */

		if (f_open(&Fil, "ADC_data.txt", FA_WRITE | FA_OPEN_EXISTING) == FR_OK) { /* open existing a file */
		
		f_write(&Fil, "ADC VALUES", 11, &bw);
		f_printf(&Fil, "\r\nData[%d]: %d \r\n",x ,temp);
		
		if(bit_is_set(PINB,1)){
		f_close(&Fil);		/* Close the file */
        PORTB &=~(1<<0);	// LED off
		}		
		
        }
	
}

int main (void){
	
	DDRB|=(1<<0); // for LED
	
	disk_initialize();
	adc_init();
	
	while(1){
		
		temp=Read_adc();
		
		_delay_ms(1000);
		
		write_data();
			
    }

return 0;

}


berkay_91

sorun çözülmüştür f_lseek fonksiyonunun kullanılması gerekiyormuş

parametre

aynı problemi yasayan diğer arkadaslarınızında problemi çozebilmeleri amacıyla cozdugunuz seklinide paylasırsanız yeni arkadaslarınıza ısık olmus olursunuz kolay gelsin

berkay_91

#3
butona basarak ADC verilerini bir saniyede bir SD kartta .txt uzantılı dosyda kaydını başlatmış oluyorsunuz bu ara potu çevirerek farklı değerleri kaydedebilirsiniz butona bidaki basışınızda led sönüyor yani kayıt bitiyor...

/*
 * SD_kart_ve_ADC.c
 *
 * Created: 25.07.2015 10:57:10
 *  Author: BERKAY
 */ 


#define F_CPU 1000000UL
#include <util/delay.h>
#include <avr/io.h>	/* Device specific declarations */
#include "ff.h"		/* Declarations of FatFs API */

int Read_adc();
void adc_init();
void write_data();

volatile int temp=0,x=0,kontrol=0;

FATFS fs;		/* FatFs work area needed for each volume */
FIL fil;			/* File object needed for each open file */
FRESULT fr;

int Read_adc(){
	
	ADCSRA |= 0x40;			// start the adc conversion
	while(ADCSRA & 0x40);
	return ADCH;         // ADCW = adc low bits + adc high bits
	
}


void adc_init(){
	
	ADMUX= 0x65;  // adc pin is selected ADC5, 8 bit using is actived, referrance voltage is AVCC
	ADCSRA = 0x83;  // prescalar is used 8

}

FRESULT open_append (FIL* fp, const char* path){
	
	FRESULT fr;

	/* Opens an existing file. If not exist, creates a new file. */
	fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
	if (fr == FR_OK) {
		/* Seek to end of the file to append data */
		fr = f_lseek(fp, f_size(fp));
		if (fr != FR_OK)
		f_close(fp);
	}
	return fr;
}


void write_data(){
	
	    PORTB |=(1<<0); // led on
	
	    x++;
		
		f_mount(&fs, "", 0); // Give a work area to the default drive 
		
		fr = open_append(&fil, "ADC_data.txt"); /* Open or create a log file and ready to append */
		
		if (fr != FR_OK) 
		return 1;

		f_printf(&fil, "Data[%d]: %d \n",x ,temp); /* Append a line */
		
		f_close(&fil); /* Close the file */

}

int main (void){
	
	DDRB|=(1<<0); // for LED
	
	disk_initialize();
	adc_init();
	
	while(1){
		
		temp=Read_adc();
		
		_delay_ms(1000);
		
		if(bit_is_set(PINB,1)){
			
			if(kontrol==0){
			kontrol=1;
			PORTB|=(1<<0); // led on
			}			
			
			else if(kontrol==1){
			kontrol=0;
			PORTB &=~(1<<0);	// LED off
			}			
		}
		
		if(kontrol==1)
		write_data();
			
    }

return 0;

}