DataLogger K64F codigos

CÓDIGO


main.c

#include "Cpu.h"
#include "Events.h"
#include "FAT1.h"
#include "TmDt1.h"
#include "SD1.h"
#include "SS1.h"
#include "CD1.h"
#include "SM1.h"
#include "WAIT1.h"
#include "TMOUT1.h"
#include "TI1.h"
#include "TimerIntLdd1.h"
#include "TU1.h"
#include "CS1.h"
#include "FX1.h"
#include "GI2C1.h"
#include "CI2C1.h"
#include "UTIL1.h"
#include "RED.h"
#include "LEDpin1.h"
#include "BitIoLdd2.h"
#include "GREEN.h"
#include "LEDpin2.h"
#include "BitIoLdd3.h"
#include "BLUE.h"
#include "LEDpin3.h"
#include "BitIoLdd4.h"
#include "SW3.h"
#include "BitIoLdd5.h"
#include "SW2.h"
#include "BitIoLdd6.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "PDD_Includes.h"
#include "Init_Config.h"
#include "Application.h"


/* User includes (#include below this line is not maintained by Processor Expert) */

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here
  for(;;) { } */
   APP_Run();

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/



Application.c

#include "Application.h"
#include "WAIT1.h"
#include "FX1.h"
#include "FAT1.h"
#include "UTIL1.h"
#include "PORT_PDD.h"
#include "RED.h"
#include "GREEN.h"
#include "BLUE.h"
#include "SW2.h"
#include "SW3.h"

uint32 time_delay;
static FIL fp;
static FATFS fs;
TIMEREC time;
DATEREC date;
uint8 sec_prev,
                  min_prev,
                  hr_prev;

char name_folder[22],
                 name_file[40];


void APP_Run(void) {
  int16_t x,y,z;
  uint8_t res;
  
  /* SD card detection: PTE5 y PTE6 with pull-down! */
  PORT_PDD_SetPinPullSelect(PORTE_BASE_PTR, 6, PORT_PDD_PULL_DOWN);
  PORT_PDD_SetPinPullEnable(PORTE_BASE_PTR, 6, PORT_PDD_PULL_ENABLE);
  
  res = FX1_Enable(); /* enable accelerometer (just in case) */
  if (res!=ERR_OK) {
    Err();
  }
  if (FAT1_Init()!=ERR_OK) { /* initialize FAT driver */
    Err();
  }
  if (FAT1_mount(0, &fs) != FR_OK) { /* mount file system */
    Err();
  }
 
  /* get time */
                 if (TmDt1_GetTime(&time)!=ERR_OK) {
                                                 Err();
                                  }
 
      hr_prev=time.Hour;
      min_prev=time.Min;
     
      Folder_Name();                                        /*           it's necesSary make the NAME of the FOLDER that will be used                */            
      FAT1_mkdir(name_folder); /*           and make the DIRECTORY           */
                  File_Name();                                                  /*           besides it's necesSary make the NAME of the first FILE that will be used               */
 
    for(;;) {
                sec_prev=time.Sec;

    /* get accelerometer values */
    x = FX1_GetX();
    y = FX1_GetY();
    z = FX1_GetZ();
    /* log it to the file on the SD card */
   
   
    BLUE_On();GREEN_Off();RED_Off();
    LogToFile(x, y, z);
    while(sec_prev==time.Sec){
                 /* get time */
                 if (TmDt1_GetTime(&time)!=ERR_OK) {
                                                 Err();
                                  }
                Check_SWs();
      }
   }
    /* do this every second */
}

/*check SW2 to pause the DATALOGER and unmount the SD,
 * after that check SW3 to mount again the SD and keep writing*/
void Check_SWs(void) {
                int expulsar;
                expulsar=0;
                 
                GREEN_On(); RED_Off();BLUE_Off();
                if(!SW2_GetVal())
                {
                               BLUE_On(); RED_On(); GREEN_Off();
                                                               WAIT1_Waitms(500);
                                                               expulsar=1;                                                                                                                    
                                               if (FAT1_mount(0, NULL) != FR_OK) { /* UNmount file system */
                                                 Err();
                                                }
                                               while(expulsar==1){
                                                                              if(!SW3_GetVal())
                                                                                                              expulsar=0;                                                      
                                                                              if (FAT1_mount(0, &fs) != FR_OK) { /* mount file system again */
                                                                                              Err();
                                                                              //file_name();
                                                                              }
                                               }
                }
}

 void Err(void) {
  for(;;)
  {
                  RED_On();BLUE_Off();GREEN_Off();
  }
}

void LogToFile(int16_t x, int16_t y, int16_t z) {
  uint8_t write_buf[48];
  UINT bw;

  if(hr_prev!=time.Hour){                           /*every hour (or day, or month, etc) will be created a new folder*/
                  Folder_Name();                                            /*           so it's necessary make the NAME of the new FOLDER   */
                               hr_prev=time.Hour;
                               FAT1_mkdir(name_folder);
  }
 
  if(min_prev!=time.Min){                          /*every minute (or day, or month, etc) will be created a new file*/
                  File_Name();                                                  /*           so it's necessary make the NAME of the new FILE          */
                               min_prev=time.Min;
  }
 
  /* open file */
  if (FAT1_open(&fp, name_file, FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) {
    Err();
  }
  /* move to the end of the file */
  if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) {
    Err();
  }

 
  /* make the sentence that will be used in every line, type:     "hr:min:sec  accX  accY  accZ"
   * example: "11:58:40  -48         -208  16592"  */
 
  write_buf[0] = '\0';
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Hour);
  UTIL1_chcat(write_buf, sizeof(write_buf), ':');
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Min);
  UTIL1_chcat(write_buf, sizeof(write_buf), ':');
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Sec);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');

  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), x);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), y);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), z);
  UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\r\n");
 
  /* write data */
  if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
    (void)FAT1_close(&fp);
    Err();
  }
  /* closing file */
  (void)FAT1_close(&fp);
}


void Folder_Name(void){
                  /* make the sentence that will be used as name in every new FOLDER created,
                   * type:  "DatFiles\Data_day_month_hourh"   example: "DatFiles\Data_2_6_11h"  */
               
                if (TmDt1_GetDate(&date)!=ERR_OK) {
                                                                 Err();
                                                  }
               
                name_folder[0] = '\0';
                UTIL1_strcat(name_folder, sizeof(name_folder), (unsigned char*)"Data_");
                UTIL1_strcatNum8u(name_folder, sizeof(name_folder), date.Day);
                UTIL1_chcat(name_folder, sizeof(name_folder), '_');
                UTIL1_strcatNum8u(name_folder, sizeof(name_folder), date.Month);
                UTIL1_chcat(name_folder, sizeof(name_folder), '_');
                UTIL1_strcatNum8u(name_folder, sizeof(name_folder), time.Hour);
                UTIL1_chcat(name_folder, sizeof(name_folder), 'h');
               
}

void File_Name(void){
                  /* make the sentence that will be used as name in every new FILE created, and have to write the directory
                   * type:  "DatFiles\Data_day_month_hourh\LOG_hrh_minm.txt"   example: "DatFiles\Data_2_6_11h\LOG_12h_0m.txt"  */
               
                name_file[0] = '\0';
                UTIL1_strcat(name_file, sizeof(name_file), (unsigned char*)"./");
                UTIL1_strcat(name_file, sizeof(name_file), name_folder);
                UTIL1_strcat(name_file, sizeof(name_file), (unsigned char*)"/LOG_");
                UTIL1_strcatNum8u(name_file, sizeof(name_file), time.Hour);
                UTIL1_strcat(name_file, sizeof(name_file), (unsigned char*)"h_");
                UTIL1_strcatNum8u(name_file, sizeof(name_file), time.Min);
                UTIL1_strcat(name_file, sizeof(name_file), (unsigned char*)"m.txt");              
}


Events.c

#include "Cpu.h"
#include "Events.h"
#include "Init_Config.h"
#include "PDD_Includes.h"

extern uint32 time_delay;

#ifdef __cplusplus
extern "C" {
#endif


/* User includes (#include below this line is not maintained by Processor Expert) */

/*
** ===================================================================
**     Event       :  Cpu_OnNMI (module Events)
**
**     Component   :  Cpu [MK64FN1M0LQ12]
*/
/*!
**     @brief
**         This event is called when the Non maskable interrupt had
**         occurred. This event is automatically enabled when the [NMI
**         interrupt] property is set to 'Enabled'.
*/
/* ===================================================================*/
void Cpu_OnNMI(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  TI1_OnInterrupt (module Events)
**
**     Component   :  TI1 [TimerInt]
**     Description :
**         When a timer interrupt occurs this event is called (only
**         when the component is enabled - <Enable> and the events are
**         enabled - <EnableEvent>). This event is enabled only if a
**         <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void TI1_OnInterrupt(void)
{
 TmDt1_AddTick();
}

/*
** ===================================================================
**     Event       :  SM1_OnBlockSent (module Events)
**
**     Component   :  SM1 [SPIMaster_LDD]
*/
/*!
**     @brief
**         This event is called after the last character from the
**         output buffer is moved to the transmitter. This event is
**         available only if the SendBlock method is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer is passed
**                           as the parameter of Init method.
*/
/* ===================================================================*/
void SM1_OnBlockSent(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
}

/* END Events */

#ifdef __cplusplus
}  /* extern "C" */
#endif

1 comentario: