📄 c8051f330_flash_spi_20071213.c
字号:
//***********************************Begain of Code*********************
//**********************c8051f330--sst25vf080**********************************
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F330.h"
#include "intrins.h"
sbit WP=P0^3;
sbit CE=P0^4;
#define Select_Serial_Memory CE=0
#define Deselect_Serial_Memory CE=1
#define U16 unsigned int
#define U8 unsigned char
#define SPIFLASH_CMD_AAIP 0xAD
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void PCA_Init()
{
PCA0MD &= ~0x40;
PCA0MD = 0x00;
}
void SPI_Init()
{
SPI0CFG = 0x40;
SPI0CN = 0x01;
SPI0CKR = 0x79;
}
void Port_IO_Init()
{
// P0.0 - SCK (SPI0), Open-Drain, Digital
// P0.1 - MISO (SPI0), Open-Drain, Digital
// P0.2 - MOSI (SPI0), Push-Pull, Digital
// P0.3 - Unassigned, Push-Pull, Digital
// P0.4 - Unassigned, Push-Pull, Digital
// P0.5 - Unassigned, Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Open-Drain, Digital
// P1.1 - Unassigned, Open-Drain, Digital
// P1.2 - Unassigned, Open-Drain, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Open-Drain, Digital
// P1.6 - Unassigned, Open-Drain, Digital
// P1.7 - Unassigned, Open-Drain, Digital
P0MDOUT = 0x1C;
XBR0 = 0x02;
XBR1 = 0x40;
}
void Oscillator_Init()
{
OSCICN = 0x83;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
PCA_Init();
SPI_Init();
Port_IO_Init();
Oscillator_Init();
}
/*********************************************************************************/
/* SPI---seng one byte*/
void SendSPIByte(unsigned char ch)
{
SPIF = 0;
SPI0DAT = ch;
while (SPIF == 0); // 等待写结束
}
/*********************************************************************************/
/* SPI---receive one byte*/
unsigned char GetSPIByte(void)
{
SPIF = 0;
SPI0DAT = 0;
while (SPIF == 0);
return SPI0DAT; // 等待读结束
}
/*******************************************************************************/
/* PROCEDURE: Read_Status_Register */
/* */
/* This procedure reads from Read_Status_Register. */
/* */
/* Input: None */
/* */
/* Returns: status byte */
/* */
/*******************************************************************************/
unsigned char Read_Status_Register(void)
{
unsigned char byte = 0;
Select_Serial_Memory;
SendSPIByte(0x05); /* send RDSR command */
byte = GetSPIByte(); /* receive byte */
Deselect_Serial_Memory; /* disable device */
return byte;
}
/*******************************************************************************/
/* PROCEDURE: Wait_Busy */
/* */
/* This procedure waits until device is no longer busy. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Wait_Busy()
{
while ((Read_Status_Register() & 0x03) == 0x03)
Read_Status_Register(); /* waste time until not busy */
}
/*******************************************************************************/
/* PROCEDURE: EWSR */
/* */
/* This procedure enables the Write Status Register. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void EWSR(void )
{
Select_Serial_Memory;
SendSPIByte(0x50); /* enuable writing to the stats register */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRSR */
/* */
/* This procedure writes a byte to the Status Register. */
/* */
/* Input: data byte */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRSR(unsigned char byte)
{
Select_Serial_Memory;
SendSPIByte(0x01); /* select write to status register */
SendSPIByte(byte); /* data that will change the status of BPx or BPL(only bits 2,3,7 can be written) */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: WREN */
/* */
/* This procedure enables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WREN(void)
{
Select_Serial_Memory;
SendSPIByte(0x06); /* send WREN command */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRDI */
/* */
/* This procedure disables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRDI(void)
{
Select_Serial_Memory;
SendSPIByte(0x04); /* send WRDI command */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: Read_ID */
/* */
/* This procedure reads the manufacturer’s ID and device ID. */
/* It will use 90h as the command to read the ID. It is up to */
/* the user to give the last byte ID_addr to determine whether */
/* the device outputs manufacturer’s ID first, or device ID first. */
/* Review the data sheets for details. */
/* Returns ID in variable byte. */
/* */
/* Input: ID_addr */
/* */
/* Returns: byte: ID1 */
/* */
/*******************************************************************************/
unsigned char Read_ID(unsigned char ID_addr)
{
unsigned char byte;
Select_Serial_Memory;
SendSPIByte(0x90); /* send read ID command */
SendSPIByte(0x00); /* send address */
SendSPIByte(0x00); /* send address */
SendSPIByte(ID_addr); /* send address - either 00H or 01H */
byte = GetSPIByte(); /* receive byte */
Deselect_Serial_Memory; /* disable device */
return byte;
}
/*******************************************************************************/
/* PROCEDURE: Read */
/* */
/* This procedure reads one address of the device. */
/* It will return the byte read in variable byte. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: byte */
/* */
/*******************************************************************************/
unsigned char Read(unsigned long Dst)
{
unsigned char byte = 0;
Select_Serial_Memory;
SendSPIByte(0x03); /* read command */
SendSPIByte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst & 0xFFFF) >> 8));
SendSPIByte(Dst & 0xFF);
byte = GetSPIByte();
Deselect_Serial_Memory; /* disable device */
return byte; /* return one byte read */
}
/*******************************************************************************/
/* PROCEDURE: Read_Cont */
/* */
/* This procedure reads multiple consecutive addresses of */
/* the device and stores the data into DataArray. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* no_bytes Number of bytes to read */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -