13 Ekim 2017 Cuma

Temperature Logger

We will combine thermometer and SD card examples to create a temperature logger.

Proteus: SD card is connected to SPI pins and we will use RA0 for ADC to read temperature from LM35.


MCC: Add MSSP and ADC peripherals. no interrupts. Select RA0 at pin manager for ADC. 







XC8:  add headers of SD card as in previous example.  First read the value convert  and save the result "t" string. Writing to SD card is the same as in SD card example. Just be careful on number of bytes, it must be same to the length of string "Measurement 000:000.0\r\n" which is 23 bytes, otherwise you will get garbage characters in your "temp_log.txt" file.  We use the same 2Mbytes image file "sdcard.ima" that we created in SD card example. "temp_log.txt" will be created if you run the simulation in Proteus. Dont forget to select "sdcard.ima" file in SD card properties.







#include "mcc_generated_files/mcc.h"
#include <stdio.h>
#include "ff.h"


FATFS FatFs; /* FatFs work area needed for each volume */
FIL Fil;  /* File object needed for each open file */


...


    char t[20];
    uint16_t cV = 0,i=0;
    float  tm;

    while (1)
    {
        // Add your application code
        i++;
        ADC_StartConversion(channel_AN0);
        while(!ADC_IsConversionDone());
        cV = ADC_GetConversionResult();
        tm=cV*500.0/65536;
        sprintf(t, "Measurement %3d:%3.1f \r\n", i,tm);
        
    UINT bw;

    if (f_mount(&FatFs, "", 1) == FR_OK) { /* Mount SD */

    if (f_open(&Fil, "temp_log.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) { /* Open or create a file */

    if ((Fil.fsize != 0) && (f_lseek(&Fil, Fil.fsize) != FR_OK))  goto endSD; /* Jump to the end of the file */

        f_write(&Fil, t, 23, &bw); /* Write data to the file */
           
     endSD: f_close(&Fil);        /* Close the file */
    }
    }
    
    

    __delay_ms(1000);
    }
}

download the example

Hiç yorum yok:

Yorum Gönder