📄 datalogger.c
字号:
/*********************************************************************
*
* Microchip USB C18 Firmware Version 1.0
*
*********************************************************************
* FileName: Datalogger.c
* Dependencies: See INCLUDES section below
* Processor: PIC18
* Compiler: C18 3.0
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the 揅ompany? for its PICmicro?Microcontroller is intended and
* supplied to you, the Company抯 customer, for use solely and
* exclusively on Microchip PICmicro Microcontroller products. The
* software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN 揂S IS?CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Gurinder Singh 10/28/05 Original.
********************************************************************/
/** I N C L U D E S *************************************************/
#include "system/fat16/fileio.h"
#include "system/sdcard/sdcard.h"
#include "stdlib.h"
#include "string.h"
/** G L O B A L B U F F E R & V A R I A B L E S ******************/
char writeBffr[513];
char ReadBuffer[4][10] ;
char RBuf[8] ;
int index=0;
/** S D C A R D D A T A R E P R E N S T A T I O N **************/
DISK myDisk;
DISK * diskPtr = &myDisk;
FILE * myFile;
CARDSTATUS cs;
char name[] = "DATALOG TXT"; // File names, in 8.3 format
CETYPE error; // The error from functions
/********************************************************************
* Function: void InitFAT(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Mounts the Disk and intilize the CARD
*
* Note: None
********************************************************************/
void InitFAT (void)
{
SocketInitialize(); //Initializes the socket
// Start the card
if ( StartupCard ( diskPtr, &cs) == CE_GOOD )
{
;
}//~Card initialized, disk mounted
else
{
// Your card could not be initialized correctly.
// Try removing it from the connector and then
//reinserting it, then running the code again
while(1);
}
}
void ReadFile(void)
{
int Index ;
int Loop , ReadLine ;
if ( ( myFile = fopen (name, diskPtr, READ) ) == NULL )
{
while(1); //File Could not be created; infinate loop
}
for ( Loop = 0 ; Loop < 4 ; Loop++ )
{
ReadLine = 1 ;
Index = 0 ;
while(ReadLine)
{
fread (myFile, RBuf, 1) ;
ReadBuffer[Loop][Index] = RBuf[0] ;
Index ++ ;
if (RBuf[0] == 0x0a)
ReadLine = 0 ;
}
}
if ( fclose ( myFile ) != CE_GOOD) // closes the file and updates the size
{
while(1);
}
}
/******************************************************************
* Function: void DoADC(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Check the Index value if its "zero" create a new file
* DATALOG.TXT and write the A/D results. If the Index is
* not zero it will open the file in append mode and add
* append the result into the file.
*
* Note: None
*******************************************************************/
void DoADC ()
{
const char tn[]="\r\n";
const char ts[]=" ";
char test[10];
int result;
if (index==0) {
// Create a new file: DATALOG.TXT
if ( ( myFile = fopen (name, diskPtr, WRITE) ) == NULL )
{
while(1); //File Could not be created; infinate loop
}
if ( fclose ( myFile ) != CE_GOOD) // closes the file and updates the size
{
while(1);
}
}
if ( ( myFile = fopen (name, diskPtr, APPEND) ) == NULL )
{
while(1); //Infinate loop if file not created
} else {
index++;
itoa(index,writeBffr);
strcat(writeBffr,ts);
ADCON0bits.GO = 1; // Start AD conversion
while(ADCON0bits.NOT_DONE); // Wait for conversion
result=ADRESH*256+ADRESL;
itoa(result,test);
strcat(writeBffr,test);
strcat(writeBffr,tn);
if((error = fwrite (myFile, writeBffr, strlen(writeBffr))) != CE_GOOD) {
while(1);
}
if ( fclose ( myFile ) != CE_GOOD) // closes the file and updates the size
{
while(1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -