امتیاز موضوع:
  • 1 رأی - میانگین امتیازات: 2
  • 1
  • 2
  • 3
  • 4
  • 5
mmc
نویسنده پیام
alfa.1000 آفلاین
تازه وارد

ارسال‌ها: 17
موضوع‌ها: 4
تاریخ عضویت: تير ۱۳۹۱

تشکرها : 7
( 4 تشکر در 3 ارسال )
ارسال: #1
mmc
سلام
چطوری میشه یه متغیر رو در فایل text ذخیره کرد
اگه میشه با C
این برنامه ای که من نوشتم اما کار نمیکنه
کد:
#include <mega128.h>

#include <ff.h>
#include <stdio.h>
#include <string.h>
#include <delay.h>
#include <stdlib.h>
/* Timer1 overflow interrupt frequency [Hz] */
#define T1_OVF_FREQ 100
/* Timer1 clock prescaler value */
#define T1_PRESC 1024L
/* Timer1 initialization value after overflow */
#define T1_INIT (0x10000L-(_MCU_CLOCK_FREQUENCY_/(T1_PRESC*T1_OVF_FREQ)))


/* USART Baud rate */
#define BAUD_RATE 19200
#define BAUD_INIT (_MCU_CLOCK_FREQUENCY_/(BAUD_RATE*16L)-1)

unsigned int nbytes;

FIL file;

unsigned char s_text[];
char path3[]="0:/Temp.txt";


/* 100Hz timer interrupt generated by ATmega128 Timer1 overflow */
interrupt [TIM1_OVF] void timer_comp_isr(void)
{
/* re-initialize Timer1 */
TCNT1H=T1_INIT>>8;
TCNT1L=T1_INIT&0xFF;
/* MMC/SD/SD HC card access low level timing function */
disk_timerproc();
}


/* error message list */
flash char * flash error_msg[]=
{
"", /* not used */
"FR_DISK_ERR",
"FR_INT_ERR",
"FR_INT_ERR",
"FR_NOT_READY",
"FR_NO_FILE",
"FR_NO_PATH",
"FR_INVALID_NAME",
"FR_DENIED",
"FR_EXIST",
"FR_INVALID_OBJECT",
"FR_WRITE_PROTECTED",
"FR_INVALID_DRIVE",
"FR_NOT_ENABLED",
"FR_NO_FILESYSTEM",
"FR_MKFS_ABORTED",
"FR_TIMEOUT"
};


/* display error message and stop */
void error(FRESULT res)
{
if ((res>=FR_DISK_ERR) && (res<=FR_TIMEOUT))
   printf("ERROR: %p\r\n",error_msg[res]);
/* stop here */
while(1);
}


/* will hold file/directory information returned by f_readdir*/
FILINFO file_info;


/* recursively scan directory entries and display them */
FRESULT directory_scan(char *path)
{
/* will hold the directory information */
DIR directory;
/* FAT function result */
FRESULT res;
int i;


if ((res=f_opendir(&directory,path))==FR_OK)
   {
   while (((res=f_readdir(&directory,&file_info))==FR_OK) &&
         file_info.fname[0])
         {
         /* display file/directory name and associated information */
         printf("%c%c%c%c%c %02u/%02u/%u %02u:%02u:%02u %9lu"                 "  %s/%s\r\n",
                (file_info.fattrib & AM_DIR) ? 'D' : '-',
                (file_info.fattrib & AM_RDO) ? 'R' : '-',
                (file_info.fattrib & AM_HID) ? 'H' : '-',
                (file_info.fattrib & AM_SYS) ? 'S' : '-',
                (file_info.fattrib & AM_ARC) ? 'A' : '-',
                file_info.fdate & 0x1F,(file_info.fdate >> 5) & 0xF,
                (file_info.fdate >> 9)+1980,
                file_info.ftime >> 11,(file_info.ftime >> 5) & 0x3F,
                (file_info.ftime & 0xF) << 1,
                file_info.fsize,path,file_info.fname);
         if (file_info.fattrib & AM_DIR)
            {
            /* its a subdirectory */
            /* make sure to skip past "." and ".." when recursing */
            if (file_info.fname[0]!='.')
               {
               i=strlen(path);
               /* append the subdirectory name to the path */
               if (path[i-1]!='/') strcatf(path,"/");
               strcat(path,file_info.fname);
               /* scan subdirectory */
               res=directory_scan(path);
               /* restore the old path name */
               path[i]=0;
               /* remove any eventual '/' from the end of the path */
               --i;
               if (path[i]=='/') path[i]=0;
               /* stop if an error occured */
               if (res!=FR_OK) break;
               }
            }
          }
   }
return res;
}


void main(void)
{
/* FAT function result */
FRESULT res;
/* will hold the information for logical drive 0: */
FATFS drive;

/* pointer to the FATFS type structure */
FATFS *pfat;

/* root directory path for logical drive 0: */
char path[256]="0:/";

/* number of free clusters on logical drive 0:*/
unsigned long free_clusters;
/* number of free kbytes on logical drive 0: */
unsigned long free_kbytes;

unsigned int a;
unsigned int d;
/* initialize Timer1 overflow interrupts in Mode 0 (Normal) */
TCCR1A=0x00;
/* clkio/1024 */
TCCR1B=(1<<CS12)|(1<<CS10);
/* timer overflow interrupts will occur with 100Hz frequency */
TCNT1H=T1_INIT>>8;
TCNT1L=T1_INIT&0xFF;
/* enable Timer1 overflow interrupt */
TIMSK=1<<TOIE1;


/* initialize the USART0 TX, 8N1, Baud rate: 19200 */
UCSR0A=0;
UCSR0B=1<<TXEN0;
UCSR0C=(1<<UCSZ01)|(1<<UCSZ00);
UBRR0H=BAUD_INIT>>8;
UBRR0L=BAUD_INIT&0xFF;


/* globally enable interrupts */
#asm("sei")


printf("Directory listing for root of logical drive 0:\r\n");


/* mount logical drive 0: */
if ((res=f_mount(0,&drive))==FR_OK)
   printf("Logical drive 0: mounted OK\r\n");
else
   /* an error occured, display it and stop */
   error(res);


/* repeateadly read directory entries and display them */
if ((res=directory_scan(path))!=FR_OK)
   /* if an error occured, display it and stop */
   error(res);


// new program

/* get the number of free clusters */
if ((res=f_getfree(path,&free_clusters,&pfat))==FR_OK)
   {
   /* calculate the number of free bytes */
   free_kbytes=free_clusters *
               /* cluster size in sectors */
               pfat->csize
               /* divide by 2 to obtain the sector size in kbytes
               512 (sector size in bytes)/1024 = 1/2 kbytes
               we need to do the division by 2 directly,
               in order to prevent unsigned long multiplication
               overflow for 8GB+ SD HC cards */
               /2;
   /* display the number of free kbytes */
   printf("Free space on logical drive 0: %lu kbytes\r\n",free_kbytes);
   }
else
   /* an error occured, display it and stop */
   error(res);  
  



/* delete an eventual old file with the same name */
f_unlink(path3);

/* create a new file in the root of drive 0:
and set write access mode */
if ((res=f_open(&file,path3,FA_CREATE_ALWAYS | FA_WRITE))==FR_OK)
   printf("File %s created OK\r\n",path3);
else
   /* an error occured, display it and stop */
   error(res);





a=20;

while(1)
    {    
      
      
        ltoa(a,s_text);
        
  
        if ((res=f_write(&file,s_text,16,&nbytes))==FR_OK)
            printf("Writing Data\r\n");
        else error(res);
                  
        // pas az neveshtan file ra mibandim
        if ((res=f_close(&file))==FR_OK)
               printf("File %s closed \r\n",path3);
        else error(res);
        
        
        
        
        delay_ms(5000);
        a=a+1;  
        
        
         if ((res=f_open(&file,path3,FA_WRITE))==FR_OK)
            printf("File opened test\r\n");  
            else error(res);
    }

}

فقط می خوام یه متغیر رو سیو بشه + اینتر خط بعد و این روند تکرار بشه
(آخرین ویرایش در این ارسال: ۰۷-شهریور-۱۳۹۲, ۲۰:۲۸:۲۷، توسط alfa.1000.)
۰۷-شهریور-۱۳۹۲, ۲۰:۲۷:۵۱
ارسال‌ها
پاسخ


پرش به انجمن:


کاربرانِ درحال بازدید از این موضوع: 2 مهمان

صفحه‌ی تماس | IranVig | بازگشت به بالا | | بایگانی | پیوند سایتی RSS