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

Saving struct data in SD card?

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



Joined: 21 Sep 2012
Posts: 10

View user's profile Send private message

Saving struct data in SD card?
PostPosted: Sat Feb 09, 2019 10:24 am     Reply with quote

hi..
I've started to work with SD card, still i have some problems. Before i want to tell you that i used libraries from CCS (mmcsd.c & fat_m).

How i can save that?:

Code:
struct formas_sonidales{
int8 senoidal_1[48];
int8 senoidal_2[48];};


I want save this data in a file on SD card FAT32 but i don't know make it.


Can someone help me ?
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 09, 2019 10:41 am     Reply with quote

I personally have had little luck with the CCS libraries. You can check the
sticky thread in the Code Library forum regarding the fat.c and mmcsd.c
files. There are several changes that you need to make.

I was never able to get fat.c working myself, but I was able to make
mmcsd.c work just writing bytes to specific locations.

You also need to make sure that you are using the correct voltage level
(3V3) and that you have the proper pullups on the pins.

It will also be helpful if you tell us what PIC you are using, along with
compiler version. Also, paste the code for your SPI setup.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 11:56 am     Reply with quote

You do also realise that to write this to SD, you need to create/open a file
with write permission, write the data, then close the file.
You also need to ensure the WP switch on the SD is not on.
As others have asked 'what PIC', but also 'what SD (particularly size)'. You need the modifications in the code librarly if it is over 2GB.
If the PIC is not 3.3v, then there is the complexity of buffering required.
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 12:08 pm     Reply with quote

A while ago I needed to make a logger that saves data on a SD card. I tried to make it by interfacing the card directly to the PIC, but I soon got lost with all the drivers, voltage problems and other things. So I just bought an Open Log which interfaces with the PIC via serial communication. It does everything for you, you just send the data via uart and it stores it on SD. I didn't need any reads from the card. Just a simple: something happened ---> log it.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 09, 2019 12:14 pm     Reply with quote

Theoretically, chico, you can write to the SD card as if it was some generic
memory using the mmcsd drivers :D But doing that requires a lot of
keeping track of memory locations on your own part. It would be much
easier to work with a file system.

You can try the code modifications in the library, but at least for me, they
still did not work. After a long time of messing around, I only managed to
get the mmcsd library working.

I bought the Brush Electronics FAT drivers and was talking to it reading
and writing within a couple of hours. The openlog will definitely be a
cheaper option than buying the driver source code if you are making only
one. Another user (temtronic) also talked about the Vinculum modules
that can talk to USB keys via serial uart.

The advantage of buying an openlog or Vinculum is that you don't need to
worry about any low-level stuff or electronics. They are just expensive if
you are making a bunch.
empty



Joined: 13 Jan 2018
Posts: 14

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 2:57 pm     Reply with quote

As Ttelmah said, The original fat library and sd card driver only support small
size SD cards (I think < 1GB), try with the modified versions at the following link:
https://simple-circuit.com/ccs-c-fat-library-for-mmc-sd-cards/

If you've the 'F' version (5V) then you should use a 5V to 3.3V converter or at least
3 proper voltage dividers for CLK, MOSI and CS lines.
The MISO line may be connected directly between the SD card and the microcontroller,
but if you want to use hardware SPI module, then it's highly recommended
to add another converter that converts the 3.3V levels into 5V levels.
Keep in mind that many unoriginal SD cards may not work in SPI mode.

The original CCS fat example (ex_fat.c) was tested with PIC18F4550 microcontroller using the modified versions:
https://simple-circuit.com/ccs-c-ex_fat-example-pic18f4550/

and this shows a temperature and humidity data logger (with date and time) using the PIC18F4550 again:
https://simple-circuit.com/pic18f4550-data-logger-sd-ds3231-dht22/
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 3:01 pm     Reply with quote

Thanks 'empty'. I know the standard drivers do work (with the mods), used them some time ago, but it is nice to have someone else confirm this.
Also the comments on the voltage handling are vital....
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 09, 2019 3:08 pm     Reply with quote

This thread has a post and link to a reference schematic for interfacing 5V
PIC with 3v3 SD card:
http://www.ccsinfo.com/forum/viewtopic.php?t=53223

Instead of resistor networks for level translation, I suggest using something
like this:
https://www.sparkfun.com/products/12009

Since you are going to have to translate the MISO line anyway, might as well
translate all of them through this bidirectional level shifter. It also has built in
pullups. So if you are trying to build all this on a breadboard, this makes
things a little cleaner.

Either that or just straight up use a 3v3 PIC.
chico_pic



Joined: 21 Sep 2012
Posts: 10

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 5:01 pm     Reply with quote

tnks to all for answer

i use 18f4550, Nokia 5110, SDHC 8 Gb for interface i use 74lcv125 so i convert the voltages... and i edit my libraries as say in the forum.

i can
read txt files (makes in pc & own pic)
make files txt (makes in pc& own pic)
edit files txt (makes in pc & own pic)


so work with fat32

i can't:
read files bmp to display in Nokia
save 2 signal sine on file (signals read from ADC pin)


but i haven't idea for save a struct in some file in SDHC


this is other problem:
http://www.todopic.com.ar/foros/index.php?topic=49294.0
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 5:49 pm     Reply with quote

re:
save 2 signal sine on file (signals read from ADC pin)

That PIC cannot read a sine wave( AC) signal. It will ONLY read a voltage from 0 to +5 VDD. It's the the 'datasheet', in the ADC section as well as the 'electrical specs' section.
To read a sine wave or AC signal you need to 'offset' the analog signal by 1/2 of the VDD and ensure that neither +ve max or -ve max exceeds the limits( +5 and gnd). A simple rail2rail opamp and a few resistors can make the required circuit. You should use a very stable Vref and proper filtering to make the circuit stable and have accurate readings. Pay a LOT of attention to grounds, bypass caps,layout, etc.

as for the files..... 'technically' there's no difference in a BMP file or a TXT file, they're both just '1's and '0's. I suspect it's the actual FORMAT that's the problem. A device reading in a BMP file requires the data in the official BMP format. I'm sure if you Goggle 'BMP format' there will be several 'hits' as how those files are laid out or formatted.

Jay
chico_pic



Joined: 21 Sep 2012
Posts: 10

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 6:02 pm     Reply with quote

yes!! my sine wave is 0-5v DC

but now i just want save my 2 signals as struct
and i donĀ“t know how make it. (inside that txt file, inside that .hex file, inside .ABC file, inside .123 file. etc etc)
or own file format.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 09, 2019 7:22 pm     Reply with quote

I just need to make sure....
Your sine wave has a 'zero cross' at +2.50 volts ? So 0 volts represents 'lowest' or most -ve part of the sine wave and +5 volts represents the 'highest' or most +ve of the sine wave.

It sounds like you're trying to record an analog source (an offset sine wave) to a file. If so, how do you expect to play it back ? Take the file, download into a PC and 'play it' ? For that you would need to see how .wav files are created and stored. I'd use google for that search. It may not b epossible to run that PIC fast enough to capture the 'sine wave' and store into the SD card. I'm sure others will know.
A better description of your project would help.
I know the translation into English can be hard, but the more details you give us, the easier it will be for us to guide you.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 11, 2019 1:40 am     Reply with quote

.wav is actually quite an easy format for a PIC to generate.
However, there is a 'caveat', that your data needs to be sampled at
a rate and resolution that it supports. Though wav actually supports
any clock rate you want, standard players won't accept formats in
'non standard' rates.
The standard supported formats that a PIC can probably do, which are
supported by standard players, are 8KHz and
11025Hz sampling using either 8 or 16bit.
Key is though that you are going to have to write the correct .wav header
and format header, before the data, and the data assumes that you know how
large it is before writing the headers (the size has to be in the header). So
if doing unknown duration sampling, the code is going to have to go back
after the sample is finished, and update the header size to match.
MSDN has a full description of the RIFF format (wav is a subset of this),
and the wav version.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 11, 2019 5:59 am     Reply with quote

I reread this thread...
saw this...
i can't:
read files bmp to display in nokia
...
so I looked up how a BMP is created.
Yeesh..there sure is a LOT of code required to store a couple of pixels!!
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