PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Devices
Memory
SD with Tests.c
1
/*
2
SD with Tests.side
3
4
SD Minimal modified so that it tests for drive and file before performing
5
any read/write operations.
6
7
http://learn.parallax.com/propeller-c-simple-devices/sd-card-data
8
*/
9
10
#include "
simpletools.h
"
// Include simpletools header
11
12
int
DO = 22, CLK = 23, DI = 24, CS = 25;
// SD card pins on Activity Board
13
14
int
main
(
void
)
// main function
15
{
16
int
erc =
sd_mount
(DO, CLK, DI, CS);
// Mount SD card
17
if
(!erc)
// Error code = 0, good, continue
18
{
19
FILE
* fp = fopen(
"test.txt"
,
"w"
);
// Open a file for writing
20
21
if
(fp)
// Nonzero file pointer?
22
{
// Good, continue
23
fwrite(
"Test message, hello!\n"
, 1, 21, fp);
// Add contents to the file
24
}
25
else
// Zero file pinter?
26
{
// Bad, error message.
27
print
(
"File did not open.\n"
);
28
}
29
fclose(fp);
// Close the file
30
31
fp = fopen(
"test.txt"
,
"r"
);
// Reopen file for reading.
32
33
char
s[80];
// Buffer for characters.
34
35
if
(fp)
// Nonzero file pinter?
36
{
// Good, continue.
37
print
(
"First 21 chars in test.txt:\n"
);
// Display heading
38
fread(s, 1, 21, fp);
// Read 21 characters
39
print
(
"%s"
, s);
// Display them
40
print
(
"\n"
);
// With a newline at the end.
41
}
42
else
// Zero file pointer?
43
{
// Bad, print error.
44
print
(
"File did not open.\n"
);
45
print
(
"\n"
);
46
}
47
fclose(fp);
// Close the file.
48
}
49
else
// Mount error code not zero?
50
{
// Bad, display code
51
print
(
"Error opening card."
);
52
print
(
"error code = %d\n"
, erc);
53
}
54
}
55
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
main
int main(void)
Definition:
GraphicsTest.c:20
sd_mount
int sd_mount(int doPin, int clkPin, int diPin, int csPin)
Mount an SD card with the minimal 4-pin interface. For Parallax Learn Site examples,...
Definition:
sddriverconfig.c:7
_FILE
Definition:
stdio.h:62
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17