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

📄 f330_w25p16_flashmemory.ver.1.01.c

📁 利用SPI讀取Flash Memory W25P16的過程
💻 C
字号:
//
// File Name:F330_W25P16_FlashMemory.ver.1.01.c
//----------------------------------------------------------------------------
// Project Name: W25P16
// Module Name: 16MBytes Flash Memory
//----------------------------------------------------------------------------
//
// Company:Semiconductor Device Solution, Inc 
//         http://www.sdsi.com.tw/
//
// Engineer: Owen Chen
// Create Date: 13:30:00 10/03/2007 
// Revision: 1.01
//
// Description:
//             . Winbond W25P16 16MBytes Flash Memory
//             . Read / Write Status Register
//             . Read / Write Page Program
//             . Erase
//              
//                                           
// 
// Target Devcies: C8051F330
//
// Tool Chain: Tool chain: KEIL Eval 'c'
// Copyright Semiconductor Device Solution, Inc .All Rights Reserved
//
//-----------------------------------------------------------------------------

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

#include <c8051f330.h>                 // SFR declarations
#define High    1
#define Low     0

typedef union {                        
      unsigned int i;
      unsigned char b[2];
              } UINT;

//-----------------------------------------------------------------------------
// Initialation PROTOTYPES
//-----------------------------------------------------------------------------

void Port_IO_Init();
void SPI_Init();

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

void Read_Manu();      // Read Manufacturer & Device ID
void Write_Instruction(unsigned char COMMAND, unsigned long DATA);
void W25P16_Check_BUSY();   // Check Busy

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

sbit CS  = P0^6;
sbit LED = P1^3; 

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

unsigned char counter;
idata UINT ManID;        // Manufacturer & Device ID 2 Bytes
idata unsigned char Status_REG;   // Status Register
xdata unsigned char FlashDATA[10];
unsigned char temp[3];

//=============================================================================
// MAIN Routine
//=============================================================================
//start main

void main()
{    

     PCA0MD &= ~0x40;    // disable WDT temporarily 
     OSCICN    = 0x83;   // SYSCLK = 24.5MHz  
	 
	 Port_IO_Init();	
	 SPI_Init();
	 
	 Read_Manu();        // Read Manufacturer & Device ID

     Write_Instruction(0x06, 0xFF);       // Write Enable
	 Write_Instruction(0x01, 0x00);       // Write Status Register

	 W25P16_Check_BUSY();	              // Check Busy

     //Write_Instruction(0x06, 0xFF);     // Write Enable
	 //Write_Instruction(0x02, 0x123456); // Page Program 

	 //W25P16_Check_BUSY();	              // Check Busy

     Write_Instruction(0x03, 0x123454);   // Read Data

	 W25P16_Check_BUSY();	              // Check Busy
     
	 while(1)
	 {
        
			 
	 }
	 
}


//=============================================================================
// Initialization Subroutines
//=============================================================================
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------

void Port_IO_Init()
{
    // P0.0  -  CLK  (SPI0), Push-Pull,  Digital
    // P0.1  -  DO (SPI0), Open-Drain, Digital
    // P0.2  -  DI (SPI0), Push-Pull,  Digital
    // P0.3  -  NSS  (SPI0), Open-Drain, Digital
    // P0.4  -  TX0 (UART0), Push-Pull,  Digital
    // P0.5  -  RX0 (UART0), Open-Drain, Digital
    // P0.6  -  CS,  Push-Pull,  Digital
    // P0.7  -  Unassigned,  Open-Drain, Digital

    // P1.3  -  LED,  Push-Pull,  Digital

    P0MDOUT   = 0x55;
    P1MDOUT   = 0x08;
    XBR0      = 0x03;
    XBR1      = 0x40;

}

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------

void SPI_Init()
{
    SPI0CFG   = 0x60;
    SPI0CN    = 0x05;
}

//=============================================================================
// Function Service Routines
//=============================================================================
//-----------------------------------------------------------------------------
// Read_Manu()
//-----------------------------------------------------------------------------
// Read Manufacturer & Device ID (90h)
//-----------------------------------------------------------------------------

void Read_Manu()
{
     unsigned char i;

	 CS = Low;
	 
	 SPI0DAT = 0x90;                // Instruction(90h)
     while(!SPIF){}                 // Wait for ready
     SPIF = 0;

	 for(counter= 0; counter<3; counter++) 
     {                              // followed by 24-bit address(A23-A0)
      SPI0DAT = 0x00;               // of 000000h
      while(!SPIF){}                // Wait for ready
      SPIF = 0;
     }  

     for(i=0;i<2;i++)             
     {                               
         SPI0DAT = 0xFF;               
         while(!SPIF){}            // Wait for ready
         SPIF = 0;
         ManID.b[i] = SPI0DAT;     // Manufacturer ID : NexFlash(EFh)
		                           // Device ID : W25P80(13h) W25P16(14h)
     } 

	 CS = High;

}

//-----------------------------------------------------------------------------
// Write_Instruction(unsigned char COMMAND, unsigned char ADDR)
//-----------------------------------------------------------------------------
// Write Instruction:
//          . Write Enable          (06h)
//          . Write Disable         (04h)
//          . Read Status Register  (05h)
//          . Write Status Register (01h) 
//          . Read Data             (03h)
//          . Page Program          (02h)
//          . Sector Erase          (D8h)
//          . Bulk Erase            (C7h)
//          . Read Parameter Page   (53h)
//          . Erase Parameter Page  (D5h)
//          
//-----------------------------------------------------------------------------

void Write_Instruction(unsigned char COMMAND, unsigned long DATA)
{
     unsigned char i;	 

	 for(i= 0; i<2; i++) 
     {                              // followed by 24-bit address(A23-A0)
        SPI0DAT = 0xFF;             // of 000000h
        while(!SPIF){}              // Wait for ready
        SPIF = 0;
     }  

	 CS = Low;
	 
	 SPI0DAT = COMMAND;             // Instruction
     while(!SPIF){}                 // Wait for ready
     SPIF = 0;

	 if(COMMAND==0x05)              // Read Status Register
	 {                       
         SPI0DAT = 0xFF;               
         while(!SPIF){}             // Wait for ready
         SPIF = 0;
         Status_REG = SPI0DAT;      // Status Register Out
	 }
	 else if(COMMAND==0x01)         // Write Status Register
	 {
         SPI0DAT = DATA;               
         while(!SPIF){}             // Wait for ready
         SPIF = 0; 
	 }
	 else if(COMMAND==0x03)         // Read Data
	 {
         
		 for(i=0;i<3;i++)               // 24-Bits Address
	     {
             temp[i] = DATA>>(8*(2-i));
			 SPI0DAT = temp[i];               
             while(!SPIF){}             // Wait for ready
             SPIF = 0;			  
		 }

		 for(i=0;i<10;i++)              // Data Byte
		 {  
             SPI0DAT = 0xFF;               
             while(!SPIF){}             // Wait for ready
             SPIF = 0;
             FlashDATA[i]= SPI0DAT;     // Status Register Out
		 }
	 
	 }
	 else if(COMMAND==0x02)             // Page Program
	 {
         
		 for(i=0;i<3;i++)               // 24-Bits Address
	     {
             temp[i] = DATA>>(8*(2-i));
			 SPI0DAT = temp[i];               
             while(!SPIF){}             // Wait for ready
             SPIF = 0;			  
		 }

		 for(i=0;i<10;i++)              // Data Byte
		 {  
             SPI0DAT = FlashDATA[i];               
             while(!SPIF){}             // Wait for ready
             SPIF = 0;
		 }
	 
	 }

	 CS = High;

	 for(i= 0; i<2; i++) 
     {                              
        SPI0DAT = 0xFF;               
        while(!SPIF){}            // Wait for ready
        SPIF = 0;
     }  

}

//-----------------------------------------------------------------------------
// W25P16_Check_BUSY()
//-----------------------------------------------------------------------------
// Check Status Register S0(Busy):
//       Symbol   Description            Typ   Max   Unit
//         tw     Write Status            17    30    ms
//                Register Cycle Time   
//         tpp    Page Program Cycle      
//                3.0V~3.6V              3.5    7      s
//                2.7V~3.6V               4     8      s
//         tse    Sector Erase           0.6   1.5     s
//                Cycle Time         
//         tbe    Bulk Erase Cycle Time   12    40     s
//         tpe    Parameter Page         100   200    ms
//                Erase Cycle Time       
//-----------------------------------------------------------------------------

void W25P16_Check_BUSY()
{
     Write_Instruction(0x05, 0xFF);      // Read Status Register(05h)
	 
	 while((Status_REG &= 0x01))
     {
         LED = 0;
         Write_Instruction(0x05, 0xFF);  // Read Status Register(05h)
     }

	 Write_Instruction(0x05, 0xFF);      // Read Status Register(05h)

	 LED = 1;

}

//=============================================================================
// Interrupt Service Routines
//=============================================================================
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// smaRTClock interrupt ISR
//-----------------------------------------------------------------------------
//
//This routine changes the state of the LED whenever Timer2 overflows.
//
//-----------------------------------------------------------------------------





//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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