ElectricShock 2013. 1. 11. 00:10

http://www.antcircuits.com/Articles/view/39

 

DataLogger.zip(☜Hex File)

A data logger that store information on SD card.

필요한 부품

PIC18F4550

SD card socket

The PIC program was made using MikroC

 


Coding...

// 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;


  }

}