Created
August 3, 2012 12:24
-
-
Save micahscopes/3247105 to your computer and use it in GitHub Desktop.
Sd2Card.init() is successful vs. not successful
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SD Card Library | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <SdFat.h> | |
#include <algorithm> | |
uint16 bufferA[40]; | |
uint16 bufferB[40]; | |
uint16 * incomingBuffer = bufferA; | |
uint16 * outgoingBuffer = bufferB; | |
int bufferIndex = 0; | |
//SD Card Variable Initialization | |
Sd2Card card; | |
SdVolume volume; | |
SdFile root; | |
SdFile file; | |
// Analog input pin. | |
const int adcPin = 1; | |
void setup(){ | |
// Declare adcPin as INPUT_ANALOG: | |
pinMode(adcPin, INPUT_ANALOG); | |
pinMode(BOARD_BUTTON_PIN, INPUT); | |
pinMode(BOARD_LED_PIN, OUTPUT); | |
//Being SD Card File-Write Initialization | |
SerialUSB.begin(); | |
SerialUSB.println("type any char to start"); | |
while (!SerialUSB.available()); | |
SerialUSB.println(); | |
delay(500); | |
if (!card.init()) | |
SerialUSB.println("card.init failed"); | |
else | |
SerialUSB.println("card.init passed"); | |
// initialize a FAT volume | |
if (!volume.init(&card)) | |
SerialUSB.println("volume.init failed"); | |
else | |
SerialUSB.println("volume.init passed"); | |
// open the root directory | |
if (!root.openRoot(&volume)) | |
SerialUSB.println("openRoot failed"); | |
else | |
SerialUSB.println("openRoot passed"); | |
char name[] = "data.raw"; | |
file.open(&root, name, O_CREAT | O_WRITE | O_APPEND); //open file | |
if(!file.isOpen()) { | |
SerialUSB.println("Could not open file!"); //Error if file isn't open. | |
while (1) ; | |
} else { | |
SerialUSB.println("Writing from adc in one second... press button (\"BUT\") to close the file! LED says it's safe to eject."); | |
} | |
} | |
void loop(){ | |
uint16 adcValue = analogRead(adcPin); | |
incomingBuffer[bufferIndex%40] = adcValue; | |
if(bufferIndex%40 == 39) { | |
// swap the buffers around | |
std::swap(incomingBuffer,outgoingBuffer); | |
file.write((uint8_t *)outgoingBuffer,80); | |
} | |
bufferIndex++; | |
//Close File when the button gets pressed. | |
if (isButtonPressed()) { | |
file.close(); | |
toggleLED(); | |
while (1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SD Card Library | |
#include <stdlib.h> | |
#include <SdFat.h> | |
#include <HardwareSPI.h> | |
#include <stdint.h> | |
//16.16 fixed point Library | |
#include <fix16.h> | |
#include <fixmath.h> | |
#include <fract32.h> | |
#include <uint32.h> | |
//These variables used for ADC conversion to voltage | |
float conv = 0.000806; //Conversion constant = 3.3v/4096 | |
fix16_t v_conv; | |
//SD Card Variable Initialization | |
Sd2Card card; | |
SdVolume volume; | |
SdFile root; | |
SdFile file; | |
// Analog input pin. | |
const int analogInputPin = 1; | |
/* | |
* Write CR LF to a file | |
*/ | |
void writeCRLF(SdFile& f) { | |
f.write((uint8_t*)"\r\n", 2); | |
} | |
/* | |
* Write a string to a file | |
*/ | |
void writeString(SdFile& f, char *str) { | |
uint8_t n; | |
for (n = 0; str[n]; n++); | |
f.write((uint8_t *)str, n); | |
} | |
void setup(){ | |
// Declare analogInputPin as INPUT_ANALOG: | |
pinMode(analogInputPin, INPUT_ANALOG); | |
//Convert floating conversion to fix16 conversion | |
v_conv = fix16_from_float(conv); | |
//Being SD Card File-Write Initialization | |
SerialUSB.begin(); | |
SerialUSB.println("type any char to start"); | |
while (!SerialUSB.available()); | |
SerialUSB.println(); | |
if (!card.init()) | |
SerialUSB.println("card.init failed"); | |
else | |
SerialUSB.println("card.init passed"); | |
// spi.end(); | |
// spi.begin(SPI_1_125MHZ, MSBFIRST, 0); | |
// initialize a FAT volume | |
if (!volume.init(&card)) | |
SerialUSB.println("volume.init failed"); | |
else | |
SerialUSB.println("volume.init passed"); | |
// open the root directory | |
if (!root.openRoot(&volume)) | |
SerialUSB.println("openRoot failed"); | |
else | |
SerialUSB.println("openRoot passed"); | |
} | |
void loop(){ | |
// Read the analog input into a variable: | |
int analogValue = analogRead(analogInputPin); | |
fix16_t voltage = fix16_mul(fix16_from_int(analogValue), v_conv); | |
conv = fix16_to_float(voltage); | |
char floatstr[10]; | |
//sprintf(floatstr, "%f", conv); | |
gcvt(conv,5,floatstr); | |
// print the result: | |
SerialUSB.println(floatstr); | |
// create a new file | |
char name[] = "data.txt"; | |
file.open(&root, name, O_CREAT | O_WRITE | O_APPEND); //open file | |
if(!file.isOpen()) { | |
SerialUSB.print("Could not open file!"); //Error if file isn't open. | |
while (1) ; | |
} | |
SerialUSB.print("Writing to: "); | |
SerialUSB.print(name); | |
SerialUSB.print("\n"); | |
//Write data to file | |
writeString(file, "Voltage: "); | |
writeString(file, floatstr); | |
writeCRLF(file); | |
//Close File | |
file.close(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment