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

📄 spi_ee_poll1.c

📁 c8051f020开发板的所有参考程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// SPI_EE_Poll1.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: BW
// DATE: 14 SEP 01
//
// This program shows an example of how to interface to a SPI EEPROM using
// the SPI0 interface in polled-mode. The SPI EEPROM used here is a Microchip
// 25LC320 (4k bytes).  The hardware connections are as follows:
//
// P0.0 - TX -- UART used for display/testing purposes
// P0.1 - RX
//
// P0.2 - SCK  (connected to SCK on EEPROM)
// P0.3 - MISO (connected to SI on EEPROM)
// P0.4 - MOSI (connected to SO on EEPROM)
// P0.5 - NSS  (unconnected, but pulled high by on-chip pull-up resistor)
//
// P1.7 - EE_CS (connected to /CS on EEPROM)
//
// Assumes an 22.1184MHz crystal is attached between XTAL1 and XTAL2.
//
// In this example, the attached SPI device is loaded with a test pattern.
// The EEPROM contents are then verified with the test pattern.  If the test
// pattern is verified with no errors, the LED blinks on operation complete.
// Otherwise, the LED stays off.  Progress can also be monitored by a terminal
// connected to UART0 operating at 115.2kbps.
//
// Target: C8051F02x 
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f020.h>                 // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbe;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
sfr16 T4       = 0xf4;                 // Timer4
sfr16 DAC0     = 0xd2;                 // DAC0 data
sfr16 DAC1     = 0xd5;                 // DAC1 data

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define     SYSCLK   22118400          // SYSCLK frequency in Hz
#define     BAUDRATE 115200            // Baud rate of UART in bps

sbit        LED = P1^6;                // LED='1' means ON
sbit        EE_CS = P1^7;              // EEPROM CS signal

#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

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void SPI0_Init (void);
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;

   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;

   SYSCLK_Init ();                     // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   UART0_Init ();                      // initialize UART0


   SPI0_Init ();                       // initialize SPI0

   // 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
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an 22.1184 MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184 MHz crystal

   for (i=0; i < 256; i++) ;           // Wait for osc. to start up

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
   XBR0   |= 0x06;                     // Enable SPI0 and UART0
   XBR1    = 0x00;
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P0MDOUT |= 0x15;                    // enable P0.0 (TX), P0.2 (SCK), and 

⌨️ 快捷键说明

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