用C8051F120单片机驱动FLASH芯片 AT45DB161D

更新时间:2023-10-13 09:52:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

用C8051F120单片机驱动FLASH芯片 AT45DB161D 2011-11-26 08:51 //

// AT45DB161D 驱动 //

// Pinout: //

// P0.2 - SPI SCK (digital output, push-pull) // P0.3 - SPI MISO (digital input, open-drain) // P0.4 - SPI MOSI (digital output, push-pull) // P0.5- NSSMD0

// P1.6 - LED (digital output, push-pull) //

// all other port pins unused. //

// #define SYSCLK 22118400 // Internal oscillator frequency in Hz // #define SPI_CLOCK 11059200 // Maximum SPI clock //----------------------------------------------------------------------------- // Includes

//----------------------------------------------------------------------------- #include #include \

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

//-----------------------------------------------------------------------------

#define FLASH_CHREAD 0x0B #define FLASH_CLREAD 0x03

#define FLASH_PREAD 0xD2 //Main Memory Page Read #define FLASH_BUFWRITE1 0x84 #define FLASH_BUFWRITE2 0x87

#define B1_TO_MM_PAGE_PROG_WITH_ERASE 0x83 // 将第一缓冲区的数据写入主存储器(擦除模式)

#define B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86 // 将第二缓冲区的数据写入主存储器(擦除模式)

#define FLASH_IDREAD 0x9F #define FLASH_STATUS 0xD7 #define PAGE_ERASE 0x81 #define PAGE_READ 0xD2

#define MM_PAGE_TO_B1_XFER 0x53 // 将主存储器的指定页数据加载到第一缓冲区 #define Dummy_Byte 0xA5

/* Select SPI FLASH: ChipSelect pin low */ #define Select_Flash() NSSMD0=0

/* Deselect SPI FLASH: ChipSelect pin high */

#define NotSelect_Flash() NSSMD0=1 U8 xdata Wr_buffer[528]; U8 xdata Rr_buffer[528]; U8 Pid[4];

void SPI_Flash_Init(void); //SPI初始化

U8 SPI_Flash_ReadByte(void); //flash操作基本函数,读一个字节

U8 SPI_Flash_SendByte(U8 byte); // FLASH操作基本函数,发送一个字节 void FlashPageEarse(U16 page); //擦除指定的页,页范围0-4095

void FlashPageRead(U16 page,U16 StartAddr,U16 ReadCount);写多个字节(页内) void FlashPageWrite(U16 page); //写一整页,页范围0-4095 void FlashWaitBusy(void); //Flash忙检测

void FlashReadID(void); //读取flashID四个字节 U8 xdata ReadAddr[3]; U8 xdata state_reg=0x00;

U8 SPI_Flash_SendByte(U8 byte); // FLASH操作基本函数,发送一个字节 void FlashPageRead(U16 page,U16 StartAddr,U16 ReadCount,U16 Soffset); void FlashWaitBusy(void); //Function

void FlashReadID(void)//读芯片ID {

U8 i;

U8 SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

SFRPAGE = SPI0_PAGE; // Switch to the necessary SFRPAGE Select_Flash();

SPI_Flash_SendByte(0x9F); for(i = 0; i < 4; i++) {

Pid[i] = SPI_Flash_ReadByte(); }

NotSelect_Flash();

SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE } //

U8 SPI_Flash_ReadByte(void) {

return (SPI_Flash_SendByte(Dummy_Byte)); }

//--------------------------------------------------------------------------------------------- U8 SPI_Flash_SendByte(U8 dat) {

SPI0DAT = dat;

while (!SPIF); // Step1.3: Wait for end of transfer SPIF = 0; // Step1.4: Clear the SPI intr. flag

dat=SPI0DAT; return dat; }

//--------------------------------------------------------------------------------------------- void FlashPageEarse(U16 page) {

U8 SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

SFRPAGE = SPI0_PAGE; // Switch to the necessary SFRPAGE FlashWaitBusy(); Select_Flash();

SPI_Flash_SendByte(PAGE_ERASE); SPI_Flash_SendByte((U8)(page >> 6)); SPI_Flash_SendByte((U8)(page << 2)); SPI_Flash_SendByte(Dummy_Byte); NotSelect_Flash();

SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE }

//--------------------------------------------------------------------------------------------- void FlashPageRead(U16 page,U16 StartAddr,U16 ReadCount) {

U16 i;

U8 saddr=0;

U8 SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

SFRPAGE = SPI0_PAGE; // Switch to the necessary SFRPAGE ReadAddr[0]= (U8)(page >> 7) ; ReadAddr[2]=(U8)(StartAddr);

saddr=(((U8)(StartAddr >> 8)) & 0x01); saddr=(saddr | (U8)(page << 1)); FlashWaitBusy(); Select_Flash();

SPI_Flash_SendByte(PAGE_READ); SPI_Flash_SendByte((U8)(page >> 7) ); SPI_Flash_SendByte(saddr);

SPI_Flash_SendByte((U8)(StartAddr));//3个字节 SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); for (i = 0;i

Rr_buffer[i] = SPI_Flash_ReadByte(); }

NotSelect_Flash();

SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE }

void FlashPageWrite(U16 page) //写一整页,页范围0-4095 {

U16 i;

U8 SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

SFRPAGE = SPI0_PAGE; // Switch to the necessary SFRPAGE Select_Flash();

SPI_Flash_SendByte(FLASH_BUFWRITE2); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); SPI_Flash_SendByte(0x00); for (i = 0;i < 528; i++) {

SPI_Flash_SendByte(Wr_buffer[i]); }

NotSelect_Flash(); FlashWaitBusy(); if ( page < 4096) {

Select_Flash();

SPI_Flash_SendByte(B2_TO_MM_PAGE_PROG_WITH_ERASE); SPI_Flash_SendByte((U8)(page>>6)); SPI_Flash_SendByte((U8)(page<<2)); SPI_Flash_SendByte(0x00); NotSelect_Flash(); FlashWaitBusy(); }

SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE }

void FlashWaitBusy(void) {

state_reg=0x00; Select_Flash();

SPI_Flash_SendByte(FLASH_STATUS); while((state_reg&0x80) == 0) {

state_reg = SPI_Flash_ReadByte(); }

NotSelect_Flash(); }

//-------------------------------------------------------------------------------------------------

本文来源:https://www.bwwdw.com/article/dq4f.html

Top