?? spi_ee_f30x.c
字號:
//-----------------------------------------------------------------------------
// SPI_EE_F30x.c
//-----------------------------------------------------------------------------
// Copyright 2001 Cygnal Integrated Products, Inc.
//
// AUTH: BD
// DATE: 14 DEC 01
//
// This program demonstrates how a collection of SPI master routines for the
// 8051F30x devices can be used in a C program.
//
// In this example, a Microchip 25LC320 4k X 8 Serial EEPROM is interfaced to a
// SPI master device implemented in the C8051F30x. The EEPROM is written with
// two test patterns: 1) all locations are 0xFF and 2) each location is written
// with the LSB of the corresponding address.
// The EEPROM contents are then verified with the test patterns. If the test
// patterns are verified with no errors, the LED blinks on operation completion.
// Otherwise, the LED stays off. Progress can also be monitored by a terminal
// connected to UART0 operating at 115.2kbps.
//
// For this code to be functional, *one* of the following files should also be
// compiled or assembled, and the resulting object file must be linked to the
// object file produced from this code:
//
// SPI_MODE0.c Mode 0 SPI Master Implementation in C
// SPI_MODE0.asm Mode 0 SPI Master Implementation in Assembly
// SPI_MODE3.c Mode 3 SPI Master Implementation in C
// SPI_MODE3.asm Mode 3 SPI Master Implementation in Assembly
//
// This EEPROM's serial port will only operate with a Mode 0 or Mode 3
// SPI configuration.
//
// Target: C8051F30x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f300.h> // SFR declarations
#include <stdio.h> // Standard I/O
#include "SPI_defs.h" // SPI port definitions
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define EE_SIZE 4096 // EEPROM size in bytes
#define EE_READ 0x03 // EEPROM Read command
#define EE_WRITE 0x02 // EEPROM Write command
#define EE_WRDI 0x04 // EEPROM Write disable command
#define EE_WREN 0x06 // EEPROM Write enable command
#define EE_RDSR 0x05 // EEPROM Read status register
#define EE_WRSR 0x01 // EEPROM Write status register
sbit LED = P0^6; // LED Indicator
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void); // Port I/O configuration
void SYSCLK_Init (void); // SYSCLK Initialization
void UART0_Init (void); // UART0 Initialization
extern char SPI_Transfer (char); // SPI Transfer routine
void Timer0_ms (unsigned ms);
void Timer0_us (unsigned us);
unsigned char EE_Read (unsigned Addr);
void EE_Write (unsigned Addr, unsigned char value);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
unsigned EE_Addr; // address of EEPROM byte
unsigned char test_byte;
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize ports and GPIO
UART0_Init (); // initialize UART0
EA = 1; // enable global interrupts
SCK = 0;
// fill EEPROM with 0xFF's
LED = 1;
for (EE_Addr = 0; EE_Addr < EE_SIZE; EE_Addr++)
{
test_byte = 0xff;
EE_Write (EE_Addr, test_byte);
// print status to UART0
if ((EE_Addr % 16) == 0)
{
printf ("\nwriting 0x%04x: %02x ", EE_Addr, (unsigned) test_byte);
}
else
{
printf ("%02x ", (unsigned) test_byte);
}
}
// verify EEPROM with 0xFF's
LED = 0;
for (EE_Addr = 0; EE_Addr < EE_SIZE; EE_Addr++)
{
test_byte = EE_Read (EE_Addr);
// print status to UART0
if ((EE_Addr % 16) == 0)
{
printf ("\nverifying 0x%04x: %02x ", EE_Addr, (unsigned) test_byte);
}
else
{
printf ("%02x ", (unsigned) test_byte);
}
if (test_byte != 0xFF)
{
printf ("Error at %u\n", EE_Addr);
while (1); // stop here on error
}
}
// fill EEPROM memory with LSB of EEPROM address.
LED = 1;
for (EE_Addr = 0; EE_Addr < EE_SIZE; EE_Addr++)
{
test_byte = EE_Addr & 0xff;
EE_Write (EE_Addr, test_byte);
// print status to UART0
if ((EE_Addr % 16) == 0)
{
printf ("\nwriting 0x%04x: %02x ", EE_Addr, (unsigned) test_byte);
}
else
{
printf ("%02x ", (unsigned) test_byte);
}
}
// verify EEPROM memory with LSB of EEPROM address
LED = 0;
for (EE_Addr = 0; EE_Addr < EE_SIZE; EE_Addr++)
{
test_byte = EE_Read (EE_Addr);
// print status to UART0
if ((EE_Addr % 16) == 0)
{
printf ("\nverifying 0x%04x: %02x ", EE_Addr, (unsigned) test_byte);
}
else
{
printf ("%02x ", (unsigned) test_byte);
}
if (test_byte != (EE_Addr & 0xFF))
{
printf ("Error at %u\n", EE_Addr);
while (1); // stop here on error
}
}
while (1)
{ // Flash LED when done
Timer0_ms (100);
LED = ~LED;
}
}
//-----------------------------------------------------------------------------
// Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.0 - MOSI (push-pull)
// P0.1 - MISO
// P0.2 - SCK (push-pull)
// P0.3 - NSS (push-pull)
// P0.4 - UART TX (push-pull)
// P0.5 - UART RX
// P0.6 - LED
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -