Go Down

Topic: SD card datalogging files library (Read 5 times) previous topic - next topic

carl47

Mar 02, 2011, 05:58 am Last Edit: Mar 02, 2011, 06:08 am by carl47 Reason: 1
This class was written for the Arduino library.

It creates text files and appends data to them.
It's use is for mass storage on the arduino platform.
This data is then loaded onto and used on a PC.

Paste the folder SDCARDFILES into the library then Import:

Quote

#include <SDCARDFILES.h>



It has only two functions:

Quote

SDCARDFILES.createfile();  //create a text file with 0 length
SDCARDFILES.fileappend();  //append 512 bytes to the open file



The 512 bytes are written from a buffer which must be declared as global:

Quote

unsigned char buffer[512] ; //contains the 512 bytes to write to file



Operational notes:

This library is based on the SDCARD library :

http://arduino.cc/forum/index.php/topic,52871.0.html

It works on standard SD cards from 256MB to 2GB made between 2007-2011.

Start with a newly formatted(FAT16) card.


The first call of createfile will create a text file in the first root directory location:

DATA0000.TXT         it will have length = 0

Further calls will create sequential number files up to the maximum allowed in
the root directory(usually 512):

DATA00511.TXT         it will have length = 0

Calls to fileappend will write the 512 byte buffer to the end of the currently
opened file.
Further calls will append until the data sectors are used up.

Both createfile and fileappend use the 512 byte buffer.
It will be changed after the call.

You should use multiples of the cluster size to use the card efficiently.


To help with this and other decisions about your program use the example
SDCARDbootrecord to get this information(2Gb card):

bootsector =  251
partitionsectors =  3962629
bytespersector =  512
sectorspercluster =  64
reservedsectors =  1
fatcopies =  2
rootdirnumber =  512
sectorsperFAT =  242
totalfilesectors =  3962629

FAT1start =  252
FAT2start =  494
rootdirectorystart =  736
datastartsector =  768
clusterbytes =  32768
maximumdataclusters =  61904
maximum clusters per file spread over all files =  120

The class uses 70 bytes of RAM, 2960 bytes of program FLASH.
Of course you must use 512 bytes of RAM for the buffer.

On average a file append takes 60 ms.

Other example programs  in the library are:

SDCARDdump: prints one sector. 32 bytes per line


These programs write to the SD card.


SDCARDwritesector:   writes one sector on the SD card

Create512files:  create 512 text files   DATA0000.TXT - DATA0511.TXT
                         each file is one sector(512 bytes) long.



Download SDCARDFILESpack.zip from:

http://code.google.com/p/mysudoku/downloads/list

balajitenetchat

Hi

I am Bala From Tenet Technetronics, Bangalore.
I worked in the similar sd card interface with Arduino
I have altered the code to create the files generated based on the input from analog.
I am using FAT 16 Library.



Regards,
Bala
Tenet Technetronics

carl47

balajitenetchat

Yes I know that the FAT 16 library works just fine.

I wrote my library to use a very small memory size.

I have only incorporated the functions which I need in my current project.

Your program is very good and could be used in many general applications.

Cheers.

jirka766

Hello,
thank you very much for simple library.
But I am beginner and I have a problem with it.
I use AVR Studio 4.18 and WinAVR. I give the library there and I press the button build and it writes
../SDCARD.h:47: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SDCARDclass'
(it doesn´t like or does´t know "class SDCARDclass")

What can I do ? Thank you for your answer.

carl47

#4
Mar 13, 2011, 06:33 am Last Edit: Mar 13, 2011, 06:45 am by carl47 Reason: 1
I'm afraid I will be just shooting in the dark because I do not have and have never
used the  AVR Studio.

Both the AVR studio and Arduino use the C compiler avr-gcc.

http://www.nongnu.org/avr-libc/user-manual/

But it Cautions there are caveats on using C++. See the avr-libc FAQ.

The arduino can handle class SDCARDclass which I use so that I can:

Preinstantiate SDCARD with  line .cpp:234 SDCARDclass SDCARD;

This means that in the calling program I can just use:

SDCARD.readblock();

I dont have to in the program say:

SDCARD SF; //instantiate SDCARDFILES

then use:

SF.readblock();

My best guess is that your compiler version in AVR studio does not accept Preinstantiation.

In this case the .h and .cpp will have to be rewritten.

The class TVout is a good example of a class that does not use Preinstantiation:

http://arduino.cc/forum/index.php/topic,51867.0.html

Use that as a template to rewrite the SDCARD class.

If I had the AVR studio I would try it out for you.

But I'm very happy with just using the arduino IDE.


jirka766

AVR Studio doesn´t know C++, and I think that it is the problem.

Would you be so kind and rewrite it for me ? I am trying to rewrite it, but there are still some errors and it doesn´t run.

carl47

There are two reasons why I will not rewrite the library.

1.I do not know the environment  for which I am coding.

2.It is to much work to ask of an "adviser".

I looked at the AVR 4 studio and it can handle C++.

Quote
For larger code projects, assembler may not be nirvana. C/C++ may be. There is a number of good C compilers available for the AVR family of microcontrollers including the compilers from IAR and Imagecraft which can be purchased, or the AVR GCC compiler that can be downloaded from a number of sites, including www.avrfreaks.com.

Using these compilers, you typically program in an environment set up by the compiler vendor. You maintain your code project in a 3rd party tool. You typically set the compiler to produce code in DEBUG mode, which means that references to the high level code will be added to the compiled source. When you compile and link, you create an object file, and that object file can be loaded directly in AVR Studio 4.


You might be best to try that route but I think you will have trouble with:
#include "WProgram.h"
This is necessary and only available in the arduino files.

Why not concider the arduino route.
You will find it a very easy to proform many complex operations with very little knowledge.

jirka766

Yes, I tried C++ with AVR Studio, but it doesn´t run.

And how can I convert all my files from AVR Studio to Arduino ? I have .c and .h files. If I open it in arduino, it writes "it can only open its own sketches or files in .pde"

And how can i test your SD datalogger ? I open it in arduino, I press scetch/verify and where do I have .hex file to my ATmega 168 ?

Thank you for your answer.

carl47

First start with the simple case. Download SDCARDsectors.zip ,unzip and paste the folder
SDCARD into:

C:\arduino-0021\arduino-0021\libraries

Run the arduino IDE and under file-examples-SDCARD choose readwritesector
What you will have is the sketch.

It always has two subroutines.
setup   runs once
loop    is called in a contineous loop

Save the file as "tutorial"

Then look in:

C:\Users\carl\Documents\Arduino\tutorial

you will see tutorial.pde

This is just a text file, open with wordpad to see the program sketch.

You can copy this and paste it into Visual C++ or into AVR studio as a .cpp file

Now delete SDCARD folder in the arduino library(keep the .h and .cpp somewhere else)

Close the IDE and reopen tutorial. A compile will now show no SDCARD.h

We can add as many .h .c or .cpp files as we like.

Click the righthand button with an arrow. Select new tab.

Name the file SDCARD.h and copy the .h file into the window.
Repeat for SDCARD.cpp.

Change:

#include <SDCARD.h>    shows its in library

to

#include "SDCARD.h"    shows in this folder

Now when you select the compile(verify) hold the shift key down.

You will see the location of the HEX(and many other usefull) file.

Mine was:

C:\Users\carl\AppData\Local\Temp\build3053465379500411531.tmp

If you save this program the folder will now contain the .h and .cpp files.

When you close the IDE the build is deleted.

carl47

#9
Mar 30, 2011, 03:38 am Last Edit: Mar 30, 2011, 03:41 am by carl47 Reason: 1
After the questions from jirka766 I deceided to have a look at the AVR studio 4.

I really like the debug facilities.

To run my files class worked out to be a very simple affair.

As long as you dont use the arduino functions like digitalWrite ect it all works OK.

I tested it out using the AVR studio and a standalone 328 and it all runs as for the arduino.

I ported it to avrfreaks:

http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_type=project&item_id=2955

soccer_hooligan

#10
May 18, 2011, 09:18 pm Last Edit: May 18, 2011, 11:31 pm by soccer_hooligan Reason: 1
I have a simple question. Is the sd card chip select pin defined in the SDCARD.h library in these lines?:

#define selectSDCARD PORTB &= ~0x04;  //set the SS to 0 to select the sd card
#define deselectSDCARD PORTB |= 0x04;  //set the SS to 1 to deselect the sd card

It looks like CS is mapped to pin 4 right?

By the way, a big thanks to carl47 this is great!

carl47

#11
May 20, 2011, 10:42 pm Last Edit: May 20, 2011, 10:44 pm by carl47 Reason: 1
Thanks  soccer_hooligan.

Yes you are right one bit of PORTB is used for the CS line. You also have to select The direction register:

Quote

#define setupDDRB DDRB |= 0x2c;  //set SS as output for cs, MOSI and SCK
#define selectSDCARD PORTB &= ~0x04;  //set the SS to 0 to select the sd card
#define deselectSDCARD PORTB |= 0x04;  //set the SS to 1 to deselect the sd card



In binary 0x04 = 00000100  This is the 8 bits from b7 to b0.

So it is b2 of PORTB that is used.

This is pin 16 of the 328.
Its PB2 which is the arduino digital pin 10.

soccer_hooligan

#12
May 23, 2011, 08:59 pm Last Edit: May 23, 2011, 10:00 pm by soccer_hooligan Reason: 1
Thanks carl, I have aother question when you get a chance. How do I reduce the spi clock rate more than the x/128 and x/4 that you have for setup and data writing? I experimented with some values but none of them worked.  

carl47

Three bits are used to control the clock frequency see Table 18-5 of the data sheet.

The SPCR - SPI Control Register has the last 2 bits and SPSR - SPI Status Register has the last bit.

The ratios are system clock /2 down to to /128. You can use any value in that range for setup or data read.

I dont know of any other ratio available.

soccer_hooligan

thanks Carl, after looking at the datasheet clock / 128 is as slow as I can go.

Go Up