//send new file command
sprintf(cmdBuf,”new
microsd%1d.csv\r”,filenum);
sendStrUart2(cmdBuf);
//wait for command mode prompt
do{
if(CharInQueue2())
{
bitein recvchar2();
}
}while(bitein ‘>’);
}
//***********************************************
//* LIST File
//***********************************************
void listFile(void)
{
char cmdBuf[32];
WORD timeoutms;
WORD i;
BYTE bitein;
U2TXREG (*buffer++);
}
}
//***********************************************
//* CREATE NEW FILE
//***********************************************
void newFile(BYTE filenum)
{
BYTE bitein;
char cmdBuf[32];
The maximum length of our filenames is eight
characters plus three characters behind the dot delineator
(12345678.123). So, we can create up to 10 files with our
filename of microsdX.csv, where X represents the filenum
argument that can range from 0 through 9. The .csv
extension allows us to read the file in a formatted manner
using Microsoft Excel. The new command is issued from
our PIC32MX575F512H firmware just as it would be
manually (reference Screenshot 1).
//send list file command
sprintf(cmdBuf,”ls\r”);
sendStrUart2(cmdBuf);
//wait for card to respond to ls
i 0;
timeoutms 5000;
do{
if(CharInQueue2())
{
do{
bitein recvchar2();
sendBiteUart1(bitein);
}while(CharInQueue2());
}
tdelayms(1);
}while(timeoutms— && (bitein ‘>’));
}
The sprintf function writes the new command string
into the cmdBuf array and appends a null character (0x00)
to the command string. The null character is important as
our sendStrUart2 function is looking for a null character to
indicate the end of the string. Once the new command is
transmitted to the OpenLog firmware, we wait for a >
prompt following the completion of the command.
Again, we use the sprintf function to load and format
our command string. And again, we send the ls command
string using the sendStrUart2 function. If the OpenLog
firmware doesn’t return a result in five seconds, we’ve got
big problems to solve and probably would not have even
gotten this far in our coding. So, we’ll wait for the file
listing to flow to the PIC32MX575F512H’s UART2. Every
character that is received by UART2 is forwarded to the
transmit pin of UART1 (the FTDI USB portal). As you can
see in Screenshot 3, everything is okay. There are
currently two files on the microSD card we can list and
both of them show up in our Screenshot 3 listing.
We can instantly verify that the file was created by
utilizing the onboard FTDI USB portal. All we need is a
simple piece of code targeting the ls command:
Write to Our New File
We have successfully created a file called
Original OpenLog Module
SparkFun
www.sparkfun.com
PIC32MX575F512H
XC32 C Compiler
Microchip
www.microchip.com
Serial Input/Output Monitor
CCS
www.ccsinfo.com
ATmega328P
Atmel
www.atmel.com
PICBASIC Pro
■ Screenshot 3. This is exactly what we should have expected. The
CONFIG. TXT file — which was created by the OpenLog firmware — and a
newly created and empty MICROSD1.CSV file.
M.E. Labs
www.PBP3.com
72 October 2015