CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

10 bit Analog CSV into buffer properly to write to SDCARD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gomond



Joined: 05 Nov 2008
Posts: 18

View user's profile Send private message

10 bit Analog CSV into buffer properly to write to SDCARD
PostPosted: Sat Sep 26, 2009 6:27 pm     Reply with quote

I am attempting to write AN0-->AN4 into a buffer and then out to an SD_CARD using f_write() (BE sd card library).

Is it possible to use fprintf() with the BE_SD library as I noticed that in the ELM version f_printf() is implimented.

This is what I have tried so far with mixed results and various methods.
Code:

/*****************************************************************/
//Analog functions
/*****************************************************************/
//
//
//
//
//
//char * anabuf; 
unsigned long anabuf[12];
struct {
    unsigned long zero;
    unsigned long one;
    unsigned long two;
    unsigned long three;
    unsigned long four;
   }analoginput;
   
void Service_Analog_Inputs(void)
{
FIL fdst;           // file structures
FRESULT result;     // FatFs function common result code
WORD btw, bw;       // File R/W count
char mesg[64];


set_adc_channel(0);
delay_us(10);
//analoginput.zero = read_adc();
itoa(read_adc(),10,anabuf[0]);

set_adc_channel(1);
delay_us(10);
//analoginput.one = read_adc();
itoa(read_adc(),10,anabuf[2]);

set_adc_channel(2);
delay_us(10);
//analoginput.two = read_adc();
itoa(read_adc(),10,anabuf[4]);

set_adc_channel(3);
delay_us(10);
//analoginput.three = read_adc();
itoa(read_adc(),10,anabuf[6]);

set_adc_channel(4);
delay_us(10);
//analoginput.four = read_adc();
itoa(read_adc(),10,anabuf[8]);

anabuf[1]=",";anabuf[3]=",";anabuf[5]=",";anabuf[7]=",";anabuf[9]=",";
anabuf[10]="\r";anabuf[11]="\n";
//sprintf(anabuf,"%uL,%uL,%uL,%uL,%uL\r\n",analoginput.zero,analoginput.one,analoginput.two,analoginput.three,analoginput.four); // used with *anabuf
printf(anabuf);
   if (logging)
      {
      btw = strlen(anabuf);
      result = f_write(&fdata, anabuf, btw, &bw);
      f_sync(&fdata);
      //printf("%s\n\r",anabuf);
      lcd_gotoxy(1,2);
      printf(lcd_putc,"%d\n",passes);
      passes ++;
      if (result)
         {
         f_get_error_mesg(result,mesg);
         printf("Error writing to the data file\r\n");
         printf(lcd_putc,"\nFILE SYSTEM ERROR - %s",mesg);
         //return (result);
         }
      }
    else printf("Error dataloging not enabled\r\n");
   //return(FR_OK);
   

//  printf("Analog values are: 0=%Lu, 1=%Lu, 2=%Lu, 3=%Lu, 4=%Lu\n\r",\
//  analoginput.zero,analoginput.one,analoginput.two,analoginput.three,analoginput.four);


}


/*****************************************************************/

Its probably real simple to do correctly, but if I don't ask I will never know.
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Sep 26, 2009 8:44 pm     Reply with quote

The driver does not implement f_printf directly. Here is one way you would do this using sprintf to build your formatted string and f_write to output it.

Code:
sprintf(mesg,"Analog values are: 0=%Lu, 1=%Lu, 2=%Lu, 3=%Lu, 4=%Lu\r\n",\
          analoginput.zero,analoginput.one,analoginput.two,analoginput.three,analoginput.four);
f_write(&fdata, (void*)mesg, strlen(mesg), &bw);

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
gomond



Joined: 05 Nov 2008
Posts: 18

View user's profile Send private message

PostPosted: Sat Sep 26, 2009 9:59 pm     Reply with quote

Thanks Andrew that worked, though at the start of the file there is a series of chars that pad the start for some obscure reason.
in HexEdit They are:
(0x0C 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00)

In wordpad:(blank line +11 spaces)
Blank Line
___________Analog values are: 0=1023, 1=5, 2=1, 3=5, 4=1020
Analog values are: 0=1023, 1=5, 2=1, 3=5, 4=1020
.
.
.
Analog values are: 0=1023, 1=5, 2=1, 3=5, 4=1020


Code:

/******************************************************************************/
//Analog functions
/******************************************************************************/
//
//
//
//
//

struct {
    unsigned long zero;
    unsigned long one;
    unsigned long two;
    unsigned long three;
    unsigned long four;
   }analoginput;
   
BYTE Service_Analog_Inputs(void)
{
//FIL fdst;           // file structures
FRESULT result;     // FatFs function common result code
WORD btw, bw;       // File R/W count
char mesg[64];
char anabuf[64];

set_adc_channel(0);
delay_us(10);
analoginput.zero = read_adc();

set_adc_channel(1);
delay_us(10);
analoginput.one = read_adc();

set_adc_channel(2);
delay_us(10);
analoginput.two = read_adc();

set_adc_channel(3);
delay_us(10);
analoginput.three = read_adc();

set_adc_channel(4);
delay_us(10);
analoginput.four = read_adc();

sprintf(anabuf,"Analog values are: 0=%Lu, 1=%Lu, 2=%Lu, 3=%Lu, 4=%Lu\r\n",\
analoginput.zero,analoginput.one,analoginput.two,analoginput.three,analoginput.four);

//printf("DEBUG: %s",anabuf);
   if (logging)
      {
      result = f_write(&fdata, (void*)anabuf, strlen(anabuf), &bw);
      f_sync(&fdata);
      lcd_gotoxy(1,2);
      printf(lcd_putc,"Samples: %d\n",passes);
      passes ++;
      if (result)
         {
         f_get_error_mesg(result,mesg);
         printf("Error writing to the data file\r\n");
         printf(lcd_putc,"\nFILE SYSTEM ERROR - %s",mesg);
         return (result);
         }
      }
    else printf("Error dataloging not enabled\r\n");
return(FR_OK);
   
}

Andrew how difficult is it to timestamp the start of each line using the Software RTC?

Thanks

Greg O.


Last edited by gomond on Sat Sep 26, 2009 10:57 pm; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Sep 26, 2009 10:50 pm     Reply with quote

Try this.

use sprintf to build your string.

Once this is done use memcpy to copy the newly built string to another string (call it dummy).

Then use f_write operating on dummy.

Let me know how you get on with it.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
gomond



Joined: 05 Nov 2008
Posts: 18

View user's profile Send private message

PostPosted: Sat Sep 26, 2009 11:27 pm     Reply with quote

Hi Andrew same result.

Quote:

blank line (0c)
_11 NULLS_1023,1,4,2,21
1023,1,4,2,21
1023,1,4,1,21
1023,1,4,1,21
1023,1,4,2,21
1023,1,4,2,21
1023,1,4,1,21
1023,1,4,2,21
1023,1,4,1,21
1023,1,4,2,21


Code:

Snippets...
*************************************
char anabuf[26];
char dummy[26];

************************************
sprintf(anabuf,"%Lu,%Lu,%Lu,%Lu,%Lu\r\n",\
analoginput.zero,analoginput.one,analoginput.two,analoginput.three,analoginput.four);
memcpy(dummy,anabuf,26);
******************************
 result = f_write(&fdata, (void*)dummy, strlen(dummy), &bw);
      f_sync(&fdata);

asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Sep 26, 2009 11:40 pm     Reply with quote

Where ever you open the file, immediately after opening the file write a hello world string to the file.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
gomond



Joined: 05 Nov 2008
Posts: 18

View user's profile Send private message

PostPosted: Sat Sep 26, 2009 11:54 pm     Reply with quote

I have copied the logging_start(), logging_stop() and OpenDataFile() functions over to the Sample FAT.c example.
and I am using logger_start(); to open the file and leaving it open until logger_stop();

I have emailed you the file as I dont know if you wanted of your IP posted.

Thanks again

Greg.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group