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

📄 f34x_spi0_master_to_eeprom3.c

📁 与SPI Eeprom(AT25040)通信的测试代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// F34x_SPI0_Master.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program configures a C8051F34x as a 4-wire SPI Single Master.
//
// The SPI clock in this example is limited to 250 kHz when used with the
// SPI0_Slave code example.  During a SPI_Read, the slave needs some time to
// interpret the command and write the appropriate data to the SPI0DAT
// register, and the slave no longer has enough time to complete the
// SPI_READ_BUFFER command with a clock greater than 250 kHz.  For faster SPI
// clocks, a dummy byte between the command and the first byte of Read data
// will be required.
//
// This example is intended to be used with the SPI0_Slave example.
//
// Pinout:
//
// P0.0 - SPI SCK    (digital output, push-pull)
// P0.1 - SPI MISO   (digital input, open-drain)
// P0.2 - SPI MOSI   (digital output, push-pull)
// P0.3 - SPI NSS    (digital output, push-pull)
//
// P2.2 - LED        (digital output, push-pull)
//
// all other port pins unused.
//
//
// How To Test:
//
// 1) Download the code to a F340-TB that is connected as above to
//    another device running the SPI0_Slave code.
// 2) Verify that the LED pins on jumper J12 are populated.
// 3) Run the code.
// 4) If the communication passes, the LEDs on both the Master and Slave
//    boards will blink slowly. If it fails, the LEDs will be OFF.
//
//
// Target:         C8051F34x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (TP)
//    -14 DEC 2006
//

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

#include <C8051F340.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK             12000000    // Internal oscillator frequency in Hz

#define SPI_CLOCK          3000000      // Maximum SPI clock
                                       // The SPI clock is a maximum of 250 kHz
                                       // when this example is used with
                                       // the SPI0_Slave code example.

#define MAX_BUFFER_SIZE    10           // Maximum buffer Master will send

// Instruction Set
//#define  SLAVE_LED_ON      0x01        // Turn the Slave LED on
//#define  SLAVE_LED_OFF     0x02        // Turn the Slave LED off
//#define  SPI_WRITE         0x04        // Send a byte from the Master to the
                                       // Slave
//#define  SPI_READ          0x08        // Send a byte from the Slave to the
                                       // Master
//#define  SPI_WRITE_BUFFER  0x10        // Send a series of bytes from the
                                       // Master to the Slave
//#define  SPI_READ_BUFFER   0x20        // Send a series of bytes from the Slave
                                       // to the Master
#define  ERROR_OCCURRED    0x40        // Indicator for the Slave to tell the
                                       // Master an error occurred

//sbit LED = P2^2;                       // LED='1' means ON
sbit CS  = 0xFA;

#define  WREN      0x06
#define  WRDI      0x04
#define  RDSR      0x05
#define  WRSR      0x01
#define  READ      0x03
#define  WRITE      0x02
#define Write_And_Read 0x10

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

unsigned char SPI_Data = 0xA5;
unsigned char Byteaddr = 0x01;

unsigned char SPI_Data_Array[MAX_BUFFER_SIZE] = {0};

bit Error_Flag = 0;

unsigned char Command = 0x00;

unsigned char xdata * pSPIWord ;                      /*uint8 data point for write or read*/
unsigned char  sendlength ;
unsigned char  readlength ;


//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void PCA0_Init (void);
void Oscillator_Init (void);
void Port_Init (void);
void SPI0_Init (void);
void Init_Device (void);

void WREN_Byte_Write (void);
void WRDI_Byte_Write (void);
void WRSR_Byte_Write (void);
void RDSR_Byte_Read (void);
void WRITE_Byte_Write (void);
void READ_Byte_Read (void);
void SPI_Byte_Write (void);
void SPI_Array_Write (void);
void SPI_Array_Write_And_Read (void);

void Delay(void);

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
   unsigned char test_value = 0x55;
   //unsigned char test_array[MAX_BUFFER_SIZE] = {4};
//   unsigned char i;

	pSPIWord = SPI_Data_Array;

   Init_Device ();                     // Initializes hardware peripherals

   EA = 1;                             // Enable global interrupts

   //LED = 0;

   // TEST BEGIN --------------------------------------------------------------

  pSPIWord[0] = 0x04;
	sendlength = 1;
	SPI_Array_Write();

	pSPIWord[0] = 0x06;
	sendlength = 1;
	SPI_Array_Write();

	pSPIWord[0] = RDSR;
	sendlength = 1;
    readlength = 1;
	SPI_Array_Write_And_Read();

   while (!CS);                    // Wait until the Write transfer has
 	                                   // finished

   if (pSPIWord[0] != 0x02)
   {
      Error_Flag = 1;
   }
 
	pSPIWord[0] = 0x02;
	pSPIWord[1] = 0x00;
	pSPIWord[2] = 0x11;
	sendlength = 3;
	SPI_Array_Write();


   while (!CS);                    // Wait until the Write transfer has
                                       // finished
   while (pSPIWord[0] != 0)
   {

	   //RDSR_Byte_Read ();
	
	   //while (!CS);                    // Wait until the Read transfer has
	                                       // finished

	pSPIWord[0] = RDSR;
	sendlength = 1;
    readlength = 1;
	SPI_Array_Write_And_Read();

    while (!CS);                    // Wait until the Write transfer has
 	                                   // finished

   }

    Byteaddr = 0x00;


	pSPIWord[0] = 0x03;
	pSPIWord[1] = 0x00;
	sendlength = 2;
    readlength = 1;
	SPI_Array_Write_And_Read();

   while (!CS);                    // Wait until the Write transfer has
 	                                   // finished

   if (pSPIWord[0] != test_value)
   //if (SPI_Data != test_value)
   {
      Error_Flag = 1;
   }


 
   // END OF TEST -------------------------------------------------------------

   while (1)
   {
      // If no error has occurred, blink the LEDs on the Master and Slave
      // boards
      if (Error_Flag == 0)
      {
         Delay ();
      }
   };
}

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

//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function disables the watchdog timer.
//
//-----------------------------------------------------------------------------
void PCA0_Init (void)
{
   PCA0MD &= ~0x40;                    // Disable the Watchdog Timer
   PCA0MD = 0x00;
}

//-----------------------------------------------------------------------------
// Oscillator_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal oscillator
// at 12 MHz.
//
//-----------------------------------------------------------------------------
void Oscillator_Init (void)
{
   OSCICN = 0x83;                      // Set the internal oscillator to
                                       // 12 MHz
}

//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0  -  SCK  (SPI0), Push-Pull,  Digital
// P0.1  -  MISO (SPI0), Open-Drain, Digital
// P0.2  -  MOSI (SPI0), Push-Pull,  Digital
// P0.3  -  NSS  (SPI0), Push-Pull,  Digital
//
// P2.2  -  Skipped,     Push-Pull,  Digital (LED D4 on Target Board)
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   P0MDOUT = 0x0D;                     // Make SCK, MOSI, and NSS push-pull
   P2MDOUT = 0x04;                     // Make the LED push-pull

   P2SKIP = 0x04;                      // Skip the LED (P2.2)

   XBR0 = 0x02;                        // Enable the SPI on the XBAR
   XBR1 = 0x40;                        // Enable the XBAR and weak pull-ups
}

//-----------------------------------------------------------------------------
// SPI0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configures SPI0 to use 4-wire Single Master mode. The SPI timing is
// configured for Mode 0,0 (data centered on first edge of clock phase and
// SCK line low in idle state).
//
//-----------------------------------------------------------------------------
void SPI0_Init()
{
   SPI0CFG   = 0x40;                   // Enable the SPI as a Master
                                       // CKPHA = '0', CKPOL = '0'
   SPI0CN    = 0x0D;                   // 4-wire Single Master, SPI enabled

   // SPI clock frequency equation from the datasheet
   SPI0CKR   = (SYSCLK/(2*SPI_CLOCK))-1;

   ESPI0 = 1;                          // Enable SPI interrupts
}

//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Calls all device initialization functions.
//
//-----------------------------------------------------------------------------
void Init_Device (void)
{
   PCA0_Init ();                       // Disable the Watchdog Timer first
   Oscillator_Init ();
   Port_Init ();
   SPI0_Init ();
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SPI_ISR
//-----------------------------------------------------------------------------
//
// Handles all error checks and single-byte writes.
//
// Note: SPI_WRITE_ARRAY is not handled by this ISR in order to take
// advantage of double-buffering (checking the TXBMT flag) using polling.
//
//
// Typical Write:
//
//              | 1st sent | 2nd sent | 3rd sent |   ...    | last sent |
//              ---------------------------------------------------------
//  Master NSSv | Command  |   Data1  |   Data2  |   ...    |   DataN   |  NSS^
//  Slave       |   N/A    |    N/A   |    N/A   |   ...    |    N/A    |
//
// Typical Read:
//
//              | 1st sent | 2nd sent | 3rd sent |   ...    | last sent |
//              ---------------------------------------------------------
//  Master NSSv | Command  |   dummy  |   dummy  |   ...    |   dummy   |  NSS^
//  Slave       |   N/A    |   Data1  |   Data2  |   ...    |   DataN   |
//-----------------------------------------------------------------------------
void SPI_ISR (void) interrupt 6
{
   static unsigned char array_index = 0;
   static char state = 0;

   if (WCOL == 1)
   {
      // Write collision occurred
      WCOL = 0;                        // Clear the write collision flag

      //Error_Flag = 1;
   }
   else
   {
      //if (SPI0DAT == ERROR_OCCURRED)
      //{

⌨️ 快捷键说明

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