📄 f36x_spi0_eeprom_polled_mode.c
字号:
//-----------------------------------------------------------------------------
// F36x_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 'F36x 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 'F36x MCU are shown here:
//
// P0.0 - SPI SCK (digital output, push-pull)
// P0.1 - UART TXD (digital output, push-pull)
// P0.2 - UART RXD (digital input, open-drain)
// P0.3 - SPI MISO (digital input, open-drain)
// P0.4 - SPI MOSI (digital output, push-pull)
// P0.5 - SPI NSS (digital output, push-pull)
//
// P3.2 - LED (digital output, push-pull)
//
//
// How To Test:
//
// Method1:
// 1) Download the code to a 'F36x device that is connected as above.
// 2) Verify J15 and J16 are NOT populated on the Target Board.
// 3) Run the code. The LED will blink fast during the write/read/verify
// operations.
// 4) If the verification passes, the LED will blink slowly. If it fails,
// the LED will be OFF.
//
// Method2 (optional):
// 1) Download code to a 'F36x device that is connected as above.
// 2) Verify J15 and J16 are NOT populated on the Target Board.
// 3) Connect a USB cable from the CP210x USB-to-UART bridge to a PC.
// 3) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the COM port at <BAUDRATE> and 8-N-1 (CP210x VCP drivers must be
// installed)
// 4) HyperTerminal will print the progress of the write/read operation, and in
// the end will print the test result as pass or fail. Additionally, if the
// verification passes, the LED will blink slowly. If it fails, the LED will
// be OFF.
//
// FID: 36X000033
// Target: C8051F36x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (PKC / TP)
// -16 OCT 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F360.h> // SFR declarations
#include <stdio.h> // printf is declared here
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for the 'F36x
//-----------------------------------------------------------------------------
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 BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 24500000 // 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 = P3^2; // LED='1' means ON
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void PCA0_Init (void);
void Oscillator_Init (void);
void Port_Init (void);
void Timer2_Init (void);
void UART0_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
SFRPAGE = LEGACY_PAGE;
// 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;
printf("Filling with 0xFF's...\n");
for (address = 0; address < EEPROM_CAPACITY; address++)
{
test_byte = 0xFF;
EEPROM_Write (address, test_byte);
// Print status to UART0
if ((address % 16) == 0)
{
printf ("\nWriting 0x%04x: %02x ", address, (UINT)test_byte);
LED = ~LED;
}
else
printf ("%02x ", (UINT)test_byte);
}
// Verify EEPROM with 0xFF's
printf("\n\nVerifying 0xFF's...\n");
for (address = 0; address < EEPROM_CAPACITY; address++)
{
test_byte = EEPROM_Read (address);
// Print status to UART0
if ((address % 16) == 0)
{
printf ("\nVerifying 0x%04x: %02x ", address, (UINT)test_byte);
LED = ~LED;
}
else
printf ("%02x ", (UINT)test_byte);
if (test_byte != 0xFF)
{
LED = 0;
printf ("Error at %u\n", address);
while (1); // Stop here on error (for debugging)
}
}
// Fill EEPROM with LSB of EEPROM addresses
printf("\n\nFilling with LSB of EEPROM addresses...\n");
for (address = 0; address < EEPROM_CAPACITY; address++)
{
test_byte = address & 0xFF;
EEPROM_Write (address, test_byte);
// Print status to UART0
if ((address % 16) == 0)
{
printf ("\nWriting 0x%04x: %02x ", address, (UINT)test_byte);
LED = ~LED;
}
else
printf ("%02x ", (UINT)test_byte);
}
// Verify EEPROM with LSB of EEPROM addresses
printf("\n\nVerifying LSB of EEPROM addresses...\n");
for (address = 0; address < EEPROM_CAPACITY; address++)
{
test_byte = EEPROM_Read (address);
// print status to UART0
if ((address % 16) == 0)
{
printf ("\nVerifying 0x%04x: %02x ", address, (UINT)test_byte);
LED = ~LED;
Delay_ms(1000);
}
else
printf ("%02x ", (UINT)test_byte);
if (test_byte != (address & 0xFF))
{
LED = 0;
printf ("Error at %u\n", address);
while (1); // Stop here on error (for debugging)
}
}
printf ("\n\nVerification success!\n");
while (1) // Loop forever
{
LED = ~LED; // Flash LED when done (all verified)
Delay_ms (150);
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function disables the watchdog timer.
//
//-----------------------------------------------------------------------------
void PCA0_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
PCA0MD &= ~0x40;
PCA0MD = 0x00;
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// Oscillator_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use the internal oscillator
// at 24.5 MHz.
//
//-----------------------------------------------------------------------------
void Oscillator_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
OSCICN = 0x83;
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 - SCK (SPI0), Push-Pull, Digital
// P0.1 - TX0 (UART0), Push-Pull, Digital
// P0.2 - RX0 (UART0), Open-Drain, Digital
// P0.3 - MISO (SPI0), Open-Drain, Digital
// P0.4 - MOSI (SPI0), Push-Pull, Digital
// P0.5 - NSS (SPI0), Push-Pull, Digital
//
// P3.2 - Skipped, Push-Pull, Digital (LED on Target Board)
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -