⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmc_datalogger_eeprom.c

📁 C8051F340读写SD卡的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
            case 'i':                  // Init RTC;
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n Init RTC values\n");
               EA = 0;                 // Disable interrupts;
               LogInit(&LogRecord);    // Clear current time;
               EA = 1;                 // Reenable interrupts;
              }
              break;
            case 'p':                  // Stop logging;
              if(State != STOPPED)     // Only execute if not stopped already;
              {
               State = FINISHED;       // Set state to FINISHED
               printf ("\n Stop Logging\n");
               update = 1;             // Update one more time to 
                                       // clean up results;
              }
              break;
            case 's':                  // Start logging
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n Start Logging\n");
               State = RUNNING;        // Start logging data
              }
              break;
            case '?':                  // List commands;
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n List Commands\n");
               MENU_ListCommands();    // List Commands
              }
              break;
            default:                   // Indicate unknown command;
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n Unknown command: '%x'\n", key_press);
               MENU_ListCommands();    // Print Menu again;
              }
              break;
         } // switch
      } // if
      if(update)
      {
         update = 0;
         LogUpdate();
      }
   } // while
}

//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// MENU_ListCommands
//-----------------------------------------------------------------------------
// This routine prints a list of available commands.
//
void MENU_ListCommands (void)
{
   printf ("\nData logging example version %s\n", VERSION);
   printf ("Copyright 2004 Silicon Laboratories.\n\n");
   printf ("Command List\n");
   printf ("===============================================\n");
   printf (" 'c' - Clear Log\n");
   printf (" 'd' - Display Log\n");
   printf (" 'i' - Init RTC\n");
   printf (" 'p' - Stop Logging\n");
   printf (" 's' - Start Logging\n");
   printf (" '?' - List Commands\n");
   printf ("\n");
}

//-----------------------------------------------------------------------------
// Logging Subroutines
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// LogUpdate()
//-----------------------------------------------------------------------------
// This routine is called by the ADC ISR at ~1Hz if State == RUNNING or 
// FINISHED.  Here we read the decimated ADC value, convert it to temperature 
// in hundredths of a degree C, and add the log entry to the log table buffer.  
// If the buffer is full, or the user has stopped the logger, we must commit 
// the buffer to the MMC FLASH.  <State> determines if the system is logging 
// normally (State == RUNNING), or if the user has stopped the logging 
// process (State == FINISHED).  
//
//
void LogUpdate (void)
{
   static idata long temperature;      // long temperature value;

   idata int temp_int, temp_frac;      // Integer and fractional portions of
                                       // Temperature;
                                       // Count variable for number of 
                                       // Log entries in local buffer;
   static idata unsigned int lLogCount = 0;
   unsigned char* l2_pointer;
   LOG_ENTRY record_buffer;
   LOG_ENTRY* l_pointer;
   EA = 0;                             // Disable interrupts (precautionary);
   temperature = Result.l;             // Retrieve 32-bit ADC value;
   record_buffer = LogRecord;
   EA = 1;                             // Re-enable interrupts;
   
                                       // Calculate temperature in hundredths
                                       // of a degree (16-bit full scale);
   temperature = (temperature * TEMP_VREF) - TEMP_OFFSET;
   temperature = 100 * temperature / TEMP_SLOPE;
                                       
   record_buffer.wTemp = (int)temperature; // Store temp value in temporary log
                                       // entry;

   if(uLogCount == 0)                  // If the FLASH table has been cleared,
   {                                   // The local buffer is reset;
      lLogCount = 0;                   // Reset number of local table entries;
                                       // Reset local buffer pointer;
      pLogTable = LOCAL_BLOCK;

   }
   if(State == RUNNING)                // Execute the following if the logger
   {                                   // is logging normally;
                                       // Check to see if the log table is
                                       // full;
      if ((uLogCount*LOG_ENTRY_SIZE) < LOG_SIZE)
      {
         l_pointer = &record_buffer;
         l2_pointer = (unsigned char*)(l_pointer);
                                       // Write the current log entry to the
                                       // EEPROM buffer;
         EEPROM_WriteArray(pLogTable, l2_pointer,
                           LOG_ENTRY_SIZE);
         pLogTable+=LOG_ENTRY_SIZE;    // Increment buffer pointer;
         lLogCount++;                  // Increment local log entry count;
         uLogCount++;                  // Increment global log entry count;
                                       // If the buffer is full, it must be
                                       // written to FLASH;
         if(lLogCount == (unsigned int)(BUFFER_SIZE / LOG_ENTRY_SIZE))
         {
                                       // Call FLASH Write function;  Write to
                                       // address pointed at by the global
                                       // entry count less the EEPROM buffer
                                       // count;
            MMC_FLASH_Write((uLogCount - 
                            (unsigned long)lLogCount)*LOG_ENTRY_SIZE, 
                            SCRATCH_BLOCK, LOCAL_BLOCK, BUFFER_SIZE);
            lLogCount = 0;             // Reset the EEPROM buffer size
                                       // and pointer;
            pLogTable = LOCAL_BLOCK;
         }
                                       // Update display;
         temp_int = record_buffer.wTemp / 100;
         temp_frac = record_buffer.wTemp - ((long) temp_int * 100L);

   	   printf (" %08lu\t%04u: %02u:%02u:%02u ", uLogCount,
            (unsigned)record_buffer.uDay, (unsigned) record_buffer.bHour,
            (unsigned) record_buffer.bMin, (unsigned) record_buffer.bSec);

         printf ("%+02d.%02d\n", temp_int, temp_frac);
        
      }

      else                             // If the FLASH table is full, stop
      {                                // logging data and print the full
         State = STOPPED;              // message;
         printf ("Log is full\n");
      }
   }
   else if(State == FINISHED)          // If the data logger has been stopped
   {                                   // by the user, write the local buffer
                                       // to FLASH;
      MMC_FLASH_Write((uLogCount - (unsigned long)lLogCount)*LOG_ENTRY_SIZE,
                                   SCRATCH_BLOCK, LOCAL_BLOCK, 
                                   lLogCount*LOG_ENTRY_SIZE);
      lLogCount = 0;                   // Reset the local buffer size;
                                       // and pointer;
      pLogTable = LOCAL_BLOCK;
      State = STOPPED;                 // Set the state to STOPPED;
   }
}

//-----------------------------------------------------------------------------
// LogFindCount()
//-----------------------------------------------------------------------------
// This function finds the number of entries already stored in the MMC log;
//
unsigned long LogFindCount()
{
   unsigned long Count = 0;            // Count variable, incremented as table
                                       // entries are read;
   unsigned long i = 0;                // Address variable, used to read table
                                       // table entries from FLASH;
   LOG_ENTRY Entry;
   LOG_ENTRY *TempEntry;               // Temporary log entry space;

                                       // Initialize temp space in 
                                       // SCRATCH_BLOCK of external memory;

                                       // Loop through the table looking for a
                                       // blank entry;
   TempEntry = &Entry;
   for (i=LOG_ADDR;i<LOG_SIZE;i += LOG_ENTRY_SIZE)
   {
                                       // Read one entry from address i of
                                       // FLASH;
      MMC_FLASH_Read((unsigned long)(i), SCRATCH_BLOCK,
         (unsigned int)LOG_ENTRY_SIZE);
                                       // Move the entry from EEPROM space to 
                                       // local memory for testing;
      EEPROM_ReadArray((unsigned char*)TempEntry, SCRATCH_BLOCK, LOG_ENTRY_SIZE);      
                                       // Check if entry is blank;
      if ((TempEntry->bSec == 0x00)&&(TempEntry->bMin == 0x00) 
           && (TempEntry->bHour == 0x00))
      {
                                       // If entry is blank, set Count;
         Count = (i/LOG_ENTRY_SIZE) - LOG_ADDR;
         break;                        // Break out of loop;
      }
   }
   return Count;                       // Return entry count;
}

//-----------------------------------------------------------------------------
// LogErase
//-----------------------------------------------------------------------------
// This function clears the log table using the FLASH Mass Erase capability.
//
void LogErase (void)
{
                                       // Call Mass Erase function with start
                                       // of table as address and log size as
                                       // length;
   MMC_FLASH_MassErase(LOG_ADDR, LOG_SIZE);
   uLogCount = 0;                      // Reset global count;
}

//-----------------------------------------------------------------------------
// LogPrint
//-----------------------------------------------------------------------------
// This function prints the log table.  Entries are read one at a time, temp
// is broken into the integer and fractional portions, and the log entry is
// displayed on the PC through UART.
//
void LogPrint (void)
{
   idata long temp_int, temp_frac;     // Integer and fractional portions of
                                       // temperature;
   idata unsigned long i;              // Log index;
   LOG_ENTRY Entry;
   LOG_ENTRY *TempEntry;

   TempEntry = &Entry;
   printf ("Entry#\tTime\t\tResult\n");// Print display column headers;
                                       // Assign pointers to local block;
                                       // FLASHRead function stores incoming
                                       // data at pchar, and then that data can
                                       // be accessed as log entries through
                                       // TempEntry;

   for (i = 0; i < uLogCount; i++)     // For each entry in the table,
   {                                   // do the following;
                                       // Read the entry from FLASH;
      MMC_FLASH_Read((unsigned long)(LOG_ADDR + i*LOG_ENTRY_SIZE), SCRATCH_BLOCK,
         (unsigned int)LOG_ENTRY_SIZE);
                                       // Move entry from EEPROM space to local
                                       // memory;
      EEPROM_ReadArray((unsigned char*)TempEntry, SCRATCH_BLOCK, LOG_ENTRY_SIZE);
      // break temperature into integer and fractional components
      temp_int = (long) (TempEntry->wTemp) / 100L;
      temp_frac = (long) (TempEntry->wTemp) - ((long) temp_int * 100L);

      // display log entry
      printf (" %lu\t%03u: %02u:%02u:%02u ", (i + 1),
         TempEntry->uDay, (unsigned) TempEntry->bHour, 
         (unsigned) TempEntry->bMin,
         (unsigned) TempEntry->bSec);
      printf ("%+02ld.%02ld\n", temp_int, temp_frac);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -