Data Logger
After many trials, finally I was able to complete this small project which had been in my mind for a long time.
A data logger that store information on SD card. Using PIC18F4550, and one SD card socket I was able finally to complete this project. One Analogue input was added to the board where any analogue sensor can be placed over there. (As of now I am using a temperature sensor (NTC).
The PIC program was made using MikroC. (This made programming PIC much simpler, and saved a lot of time with its gerat libraries)
Below the final circuit picture.
My plan now is to write a small application that would get the logged data, and based on scaling information entered to the software; correct values would be calculated and plotted.
Please let me know if you were trying to build a similar project, and I’ll help you as much as possible.
Code:
// MMC module connections
sbit Mmc_Chip_Select at LATA5_bit; // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISA5_bit;
// eof MMC module connections
long int temp_res;
const LINE_LEN = 16; ///012345678901234
char file_contents[LINE_LEN] = "XX,XXXXXXXXXXXX";
int temp;
char filename[] = "TempLogF.TXT";
int i;
unsigned short loop, loop2;
char txt[12];
void M_Create_New_File() {
Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
Mmc_Fat_Assign(&filename, 0xA0); // Find existing file or create a new one
Mmc_Fat_Rewrite();
PORTD=0xFF; // To clear file and start with new data
for(loop = 1; loop <= 9999; loop++) {
PORTD=0b10100000;
Delay_1sec();
Delay_1sec();
Delay_1sec();
PORTD=0b01000000;
file_contents[0] = loop / 10 + 48;
file_contents[1] = loop % 10 + 48;
temp_res = ADC_Read(2);
LongIntToStrWithZeros(temp_res, txt);
file_contents[3] = (temp_res / 1000) + 48;
for(i=4;i<=14;i++)
file_contents[i] = txt[i-4];
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
Mmc_Fat_Write("\r\n", 2); // write data to the assigned file
// Mmc_Fat_Write('\n', 1); // write data to the assigned file
Delay_1sec();
Delay_1sec();
}
}
// Main.
void main() {
ADCON1 = 12; // Configure AN pins as digital
CMCON |= 7; // Turn off comparators
TRISD=0x00;
TRISA=0b00001111;
PORTD=0b10100000;
Delay_1sec();
Delay_1sec();
PORTD=0b01100000;
Delay_1sec();
Delay_1sec();
// Initialize SPI1 module
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Delay_ms(10);
if (Mmc_Fat_Init() == 0) // reinitialize spi at higher speed
{
PORTD=0b11100000;
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
M_Create_New_File();
PORTD=0b11100000;
while(1)
{
PORTD=0b11100000;
Delay_1sec();
Delay_1sec();
PORTD=0x0;
Delay_1sec();
Delay_1sec();
}
}
else {
PORTD=0b01000000;
}
}