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

📄 mmc_datalogger_eeprom.c

📁 C8051F340读写SD卡的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
} UINT;

typedef struct LOG_ENTRY               // (7 bytes per entry)
{
   int wTemp;                          // temperature in hundredths of a
                                       // degree
   unsigned int uDay;                  // day of entry
   unsigned char bHour;                // hour of entry
   unsigned char bMin;                 // minute of entry
   unsigned char bSec;                 // second of entry
} LOG_ENTRY;
     
// The states listed below represent various phases of 
// operation;
typedef enum STATE 
{
   RESET,                              // Device reset has occurred;
   RUNNING,                            // Data is being logged normally;
   FINISHED,                           // Logging stopped, store buffer;
   STOPPED                             // Logging completed, buffer stored;
} STATE;

// This structure defines entries into the command table;
typedef struct 
{
      unsigned char command_byte;      // OpCode;
      unsigned char arg_required;      // Indicates argument requirement;
      unsigned char CRC;               // Holds CRC for command if necessary;
      unsigned char trans_type;        // Indicates command transfer type;
      unsigned char response;          // Indicates expected response;
      unsigned char var_length;        // Indicates varialble length transfer;
} command;

// Command table for MMC.  This table contains all commands available in SPI
// mode;  Format of command entries is described above in command structure
// definition;
code command commandlist[25] = {
      { 0,NO ,0x95,CMD,R1 ,NO },    // CMD0;  GO_IDLE_STATE: reset card;
      { 1,NO ,0xFF,CMD,R1 ,NO },    // CMD1;  SEND_OP_COND: initialize card;
      { 9,NO ,0xFF,RD ,R1 ,NO },    // CMD9;  SEND_CSD: get card specific data;
      {10,NO ,0xFF,RD ,R1 ,NO },    // CMD10; SEND_CID: get card identifier;
      {12,NO ,0xFF,CMD,R1 ,NO },    // CMD12; STOP_TRANSMISSION: end read;
      {13,NO ,0xFF,CMD,R2 ,NO },    // CMD13; SEND_STATUS: read card status;
      {16,YES,0xFF,CMD,R1 ,NO },    // CMD16; SET_BLOCKLEN: set block size;
      {17,YES,0xFF,RD ,R1 ,NO },    // CMD17; READ_SINGLE_BLOCK: read 1 block;
      {18,YES,0xFF,RD ,R1 ,YES},    // CMD18; READ_MULTIPLE_BLOCK: read > 1;
      {24,YES,0xFF,WR ,R1 ,NO },    // CMD24; WRITE_BLOCK: write 1 block;
      {25,YES,0xFF,WR ,R1 ,YES},    // CMD25; WRITE_MULTIPLE_BLOCK: write > 1;
      {27,NO ,0xFF,CMD,R1 ,NO },    // CMD27; PROGRAM_CSD: program CSD;
      {28,YES,0xFF,CMD,R1b,NO },    // CMD28; SET_WRITE_PROT: set wp for group;
      {29,YES,0xFF,CMD,R1b,NO },    // CMD29; CLR_WRITE_PROT: clear group wp;
      {30,YES,0xFF,CMD,R1 ,NO },    // CMD30; SEND_WRITE_PROT: check wp status;
      {32,YES,0xFF,CMD,R1 ,NO },    // CMD32; TAG_SECTOR_START: tag 1st erase;
      {33,YES,0xFF,CMD,R1 ,NO },    // CMD33; TAG_SECTOR_END: tag end(single);
      {34,YES,0xFF,CMD,R1 ,NO },    // CMD34; UNTAG_SECTOR: deselect for erase;
      {35,YES,0xFF,CMD,R1 ,NO },    // CMD35; TAG_ERASE_GROUP_START;
      {36,YES,0xFF,CMD,R1 ,NO },    // CMD36; TAG_ERASE_GROUP_END;
      {37,YES,0xFF,CMD,R1 ,NO },    // CMD37; UNTAG_ERASE_GROUP;
      {38,YES,0xFF,CMD,R1b,NO },    // CMD38; ERASE: erase all tagged sectors;
      {42,YES,0xFF,CMD,R1b,NO },    // CMD42; LOCK_UNLOCK;
      {58,NO ,0xFF,CMD,R3 ,NO },    // CMD58; READ_OCR: read OCR register;
      {59,YES,0xFF,CMD,R1 ,NO },    // CMD59; CRC_ON_OFF: toggles CRC checking;
                              };

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

xdata LONG Result = {0L};              // ADC0 decimated value

xdata LOG_ENTRY LogRecord;             // Memory space for each log entry
xdata unsigned long uLogCount;         // Current number of table entries
xdata unsigned int pLogTable;          // Pointer to buffer for table entries
xdata STATE State = RESET;             // System state variable;  Determines
                                       // how log update function will exec;
xdata unsigned long PHYSICAL_SIZE;     // MMC size variable;  Set during
                                       // initialization;
xdata unsigned long PHYSICAL_BLOCKS;   // MMC block number;  Computed during
                                       // initialization;
xdata unsigned long LOG_SIZE;          // Available number of bytes for log 
                                       // table

xdata unsigned char EEPROM_PageBuffer[64];

xdata unsigned char* pSMB_DATA_IN;     // Global pointer for SMBus data
                                       // All receive data is written here

xdata unsigned char SMB_SINGLEBYTE_OUT;// Global holder for single byte writes.

xdata unsigned char* pSMB_DATA_OUT;    // Global pointer for SMBus data.
                                       // All transmit data is read from here

xdata unsigned char SMB_DATA_LEN;      // Global holder for number of bytes
                                       // to send or receive in the current
                                       // SMBus transfer.

xdata UINT WORD_ADDR;                  // Global holder for the EEPROM word
                                       // address that will be accessed in
                                       // the next transfer

xdata unsigned char TARGET;            // Target SMBus slave address

bit SMB_BUSY = 0;                      // Software flag to indicate when the
                                       // EEPROM_ReadByte() or 
                                       // EEPROM_WriteByte()
                                       // functions have claimed the SMBus

bit SMB_RW;                            // Software flag to indicate the
                                       // direction of the current transfer

bit SMB_SENDWORDADDR;                  // When set, this flag causes the ISR
                                       // to send the 8-bit <WORD_ADDR>
                                       // after sending the slave address.

bit SMB_HIGHBYTENOTSENT;


bit SMB_RANDOMREAD;                    // When set, this flag causes the ISR
                                       // to send a START signal after sending
                                       // the word address.

bit SMB_ACKPOLL;                       // When set, this flag causes the ISR
                                       // to send a repeated START until the
                                       // slave has acknowledged its address

bit update;
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void main (void);

// Support Subroutines
void MENU_ListCommands (void);         // Outputs user menu choices via UART


// Logging Subroutines
void LogUpdate (void);                 // Builds MMC log table
unsigned long LogFindCount();          // Returns current number of log entries
void LogErase (void);                  // Erases entire log table
void LogPrint (void);                  // Prints log through UART
void LogInit (LOG_ENTRY *pEntry);      // Initializes area for building entries

// High Level MMC_FLASH Functions

void MMC_FLASH_Init (void);            // Initializes MMC and configures it to 
                                       // accept SPI commands;

                                       // Reads <length> bytes starting at 
                                       // <address> and stores them at <pchar>;
unsigned char MMC_FLASH_Read (unsigned long address, unsigned int pchar,
                         unsigned int length);

                                       // Writes <length> bytes of data at
                                       // <wdata> to <address> in MMC;
                                       // <scratch> provides temporary storage;
unsigned char MMC_FLASH_Write (unsigned long address, unsigned int scratch,
                          unsigned int wdata, unsigned int length);

                                       // Clears <length> bytes of FLASH 
                                       // starting at <address1>; Requires that
                                       // desired erase area be sector aligned;
unsigned char MMC_FLASH_MassErase (unsigned long address1, 
                                   unsigned long length);

// Low Level MMC_FLASH_ Functions

                                       // Decodes and executes MMC commands;  
                                       // <cmd> is an index into the command
                                       // table and <argument> contains a 
                                       // 32-bit argument if necessary;  If a 
                                       // data operation is taking place, the
                                       // data will be stored to or read from
                                       // the location pointed to by <pchar>;
unsigned int MMC_Command_Exec (unsigned char cmd, unsigned long argument,
                           unsigned int pchar);

// SMBus Routines
                                       // Send a block of data of size
                                       // <len> bytes to address <addr> in the
                                       // EEPROM;
void EEPROM_WriteArray(unsigned int addr, char* SrcAddr,unsigned char len);
                                       // Read a block of data of size <len>
                                       // from address <src_addr> in the EEPROM
                                       // and store it at <dest_addr> in local
                                       // memory;
void EEPROM_ReadArray (unsigned char* dest_addr, unsigned int src_addr,
                       unsigned char len);
                                       // Writes <dat> to address <addr> in the
                                       // EEPROM;
void EEPROM_WriteByte( unsigned int addr, unsigned char dat);
                                       // Returns the value at address <addr>
                                       // in the EEPROM;
unsigned char EEPROM_ReadByte( unsigned int addr);
                                       // Clear <length> bytes of data at
                                       // address <addr> in the EEPROM;
void EEPROM_Clear (unsigned int addr, unsigned int length);
// Initialization Subroutines

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer0_Init (void);
void Timer2_Init (int counts);
void SPI_Init (void);
void SMBus_Init (void);

// Interrupt Service Routines

void ADC0_ISR (void);
void Soft_ISR (void);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) 
{
   idata char key_press;               // Input character from UART;
   // Disable Watchdog timer
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable);

   PORT_Init ();                       // Initialize crossbar and GPIO;
   SYSCLK_Init ();                     // Initialize oscillator;
   UART0_Init ();                      // Initialize UART0;
   SPI_Init ();                        // Initialize SPI0;
   Timer0_Init ();
   Timer2_Init (SYSCLK/SAMPLE_RATE);   // Init Timer2 for 16-bit autoreload;
   ADC0_Init ();                       // Init ADC0;

   AD0EN = 1;                          // enable ADC0;

   State = RESET;                      // Set global state machine to reset
                                       // state;
   SMBus_Init();                       // Configure and enable SMBus

   State = STOPPED;                    // Global state is STOPPED; no data
                                       // is being logged;
   EA = 1;                             // Enable global interrupts;
                                       // Clear space in EEPROM for buffers
   EEPROM_Clear((unsigned int)LOCAL_BLOCK,
                (unsigned int)PHYSICAL_BLOCK_SIZE);
   EEPROM_Clear((unsigned int)SCRATCH_BLOCK,
                (unsigned int)PHYSICAL_BLOCK_SIZE);
   MMC_FLASH_Init();                   // Initialize MMC card;
                                       // Initialize log table buffer pointer
   pLogTable = LOCAL_BLOCK;
   uLogCount = LogFindCount();         // Find current number of log table
                                       // entries;
   update = 0;
   printf ("\n");                      // Print list of commands;
   MENU_ListCommands ();
	while (1)                           // Serial port command decoder;
   {
      if(RI0 == 1)
      {
         key_press = getchar();        // Get command character;
                                       // Convert to lower case;
         key_press = tolower(key_press);  

         switch (key_press) {
            case 'c':                  // Clear log;
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n Clear Log\n");
               LogErase();             // erase log entries;
                                       // update global log entry count;
               uLogCount = LogFindCount();
              }
               break;
            case 'd':                  // Display log;
              if(State == STOPPED)     // Only execute if not logging;
              {
               printf ("\n Display Log\n");
               LogPrint();             // Print the log entries;
              }
              break;

⌨️ 快捷键说明

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