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

📄 f00x_spi0_eeprom_polled_mode.c

📁 芯科原厂所有c8051fxx程序的例子。
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// F00x_SPI0_EEPROM_Polled_Mode.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program accesses a SPI EEPROM using polled mode access. The 'F00x MCU 
// is configured in 4-wire Single Master Mode, and the EEPROM is the only 
// slave device connected to the SPI bus. The read/write operations are 
// tailored to access a Microchip 4 kB EEPROM 25LC320. The relevant hardware 
// connections of the 'F00x MCU are shown here:
// 
// P0.0 - SPI SCK    (digital output, push-pull) (J2 - pin 17)
// P0.1 - SPI MISO   (digital input, open-drain) (J2 - pin 18)
// P0.2 - SPI MOSI   (digital output, push-pull) (J2 - pin 15)
// P0.3 - SPI NSS    (digital input, open-drain) (unused in single master mode)
//                   (J2 - pin 16)
// P0.7 - GPIO (NSS) (digital output, push-pull) (used as slave select output)
//                   (J2 - pin 12)
// P1.6 - LED        (digital output, push-pull) (J2 - pin 3)
//
//
// How To Test:
//
// 1) Download the code to a 'F00x device that is connected as above.
// 2) Run the code. The LED will blink fast during the write/read/verify 
//    operations.
// 3) If the verification passes, the LED will blink slowly. If it fails,
//    the LED will be OFF.
//
//
// Target:         C8051F00x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (PKC)
//    -31 MAY 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F000.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for the 'F00x
//-----------------------------------------------------------------------------
sfr16 TMR2     = 0xCC;                 // Timer2 low and high bytes together

//-----------------------------------------------------------------------------
// User-defined types, structures, unions etc
//-----------------------------------------------------------------------------
#ifndef BYTE
#define BYTE unsigned char
#endif

#ifndef UINT
#define UINT unsigned int
#endif

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK             16000000    // Internal oscillator frequency in Hz

// Microchip 25AA320 Slave EEPROM Parameters
#define  F_SCK_MAX         2000000     // Max SCK freq (Hz)
#define  T_NSS_DISABLE_MIN 500         // Min NSS disable time (ns)
#define  EEPROM_CAPACITY   4096        // EEPROM capacity (bytes)

// EEPROM Instruction Set
#define  EEPROM_CMD_READ   0x03        // Read Command
#define  EEPROM_CMD_WRITE  0x02        // Write Command
#define  EEPROM_CMD_WRDI   0x04        // Reset Write Enable Latch Command
#define  EEPROM_CMD_WREN   0x06        // Set Write Enable Latch Command
#define  EEPROM_CMD_RDSR   0x05        // Read Status Register Command
#define  EEPROM_CMD_WRSR   0x01        // Write Status Register Command

sbit LED = P1^6;                       // LED='1' means ON
sbit EEPROM_CS = P0^7;                 // Active low chip select for EEPROM

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void Reset_Sources_Init (void);
void OSCILLATOR_Init (void);
void PORT_Init (void);
void TIMER2_Init (void);
void SPI0_Init (void);
void Init_Device (void);

void Delay_us (BYTE time_us);
void Delay_ms (BYTE time_ms);
void EEPROM_Write (UINT address, BYTE value);
BYTE EEPROM_Read (UINT address);

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
   UINT  address;                      // EEPROM address
   BYTE  test_byte;                    // Used as a temporary variable
   
   Init_Device ();                     // Initializes hardware peripherals
   
   
   // The following code will test the EEPROM by performing write/read/verify
   // operations. The first test will write 0xFFs to the EEPROM, and the 
   // second test will write the LSBs of the EEPROM addresses.

   // Fill EEPROM with 0xFF's
   LED = 1;
   for (address = 0; address < EEPROM_CAPACITY; address++)
   {
      test_byte = 0xFF;
      EEPROM_Write (address, test_byte);

      // Print status to UART0
      if ((address % 32) == 0)
         LED = ~LED;
   }

   // Verify EEPROM with 0xFF's
   for (address = 0; address < EEPROM_CAPACITY; address++)
   {
      test_byte = EEPROM_Read (address);

      // Print status to UART0
      if ((address % 32) == 0)
         LED = ~LED;
      if (test_byte != 0xFF)
      {
         LED = 0;
         while (1);                    // Stop here on error (for debugging)
      }
   }

   // Fill EEPROM with LSB of EEPROM addresses
   for (address = 0; address < EEPROM_CAPACITY; address++)
   {
      test_byte = address & 0xFF;
      EEPROM_Write (address, test_byte);

      // Print status to UART0
      if ((address % 32) == 0)
         LED = ~LED;
   }

   // Verify EEPROM with LSB of EEPROM addresses
   for (address = 0; address < EEPROM_CAPACITY; address++)
   {
      test_byte = EEPROM_Read (address);

      // print status to UART0
      if ((address % 32) == 0)
         LED = ~LED;
      
      if (test_byte != (address & 0xFF))
      {
         LED = 0;
         while (1);                    // Stop here on error (for debugging)
      }
   }

   while (1)                           // Loop forever
   {                                   
      LED = ~LED;                      // Flash LED when done (all verified)
      Delay_ms (200);
   }
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function disables the watchdog timer.
//
//-----------------------------------------------------------------------------
void Reset_Sources_Init (void)
{
   WDTCN = 0xDE;                       // Disable WDT
   WDTCN = 0xAD;
}

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal oscillator
// at 16 MHz (nominal). See the device datasheet for oscillator specifications.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
   OSCICN = 0x07;
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// Port 0
// P0.0 = SPI Bus SCK     (Push-Pull Output)
// P0.1 = SPI Bus MISO    (Open-Drain Output/Input)
// P0.2 = SPI Bus MOSI    (Push-Pull Output)
// P0.3 = SPI Bus NSS     (Open-Drain Output/Input; unused-single master mode)
// P0.4 = GP I/O          (Open-Drain Output/Input)
// P0.5 = GP I/O          (Open-Drain Output/Input)
// P0.6 = GP I/O          (Open-Drain Output/Input)
// P0.7 = GP I/O          (Push-Pull Output) (Used as NSS slave select output)

// Port 1
// P1.0 = GP I/O          (Open-Drain Output/Input)
// P1.1 = GP I/O          (Open-Drain Output/Input)
// P1.2 = GP I/O          (Open-Drain Output/Input)
// P1.3 = GP I/O          (Open-Drain Output/Input)
// P1.4 = GP I/O          (Open-Drain Output/Input)

⌨️ 快捷键说明

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