《2022年用CF单片机驱动FLASH芯片ATDBD知识 .pdf》由会员分享,可在线阅读,更多相关《2022年用CF单片机驱动FLASH芯片ATDBD知识 .pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、用 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
2、 oscillator frequency in Hz/#define SPI_CLOCK 11059200/Maximum SPI clock/-/Includes/-#include#include C8051F120.h/-/Global Constants/-#define FLASH_CHREAD 0 x0B#define FLASH_CLREAD 0 x03#define FLASH_PREAD 0 xD2/Main Memory Page Read#define FLASH_BUFWRITE1 0 x84#define FLASH_BUFWRITE2 0 x87#define B
3、1_TO_MM_PAGE_PROG_WITH_ERASE 0 x83/将第一缓冲区的数据写入主存储器(擦除模式)#define B2_TO_MM_PAGE_PROG_WITH_ERASE 0 x86/将第二缓冲区的数据写入主存储器(擦除模式)#define FLASH_IDREAD 0 x9F#define FLASH_STA TUS 0 xD7#define PAGE_ERASE 0 x81#define PAGE_READ 0 xD2#define MM_PAGE_TO_B1_XFER 0 x53/将主存储器的指定页数据加载到第一缓冲区#define Dummy_Byte 0 xA5/*S
4、elect SPI FLASH:ChipSelect pin low*/#define Select_Flash()NSSMD0=0/*Deselect SPI FLASH:ChipSelect pin high*/名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 4 页 -#define NotSelect_Flash()NSSMD0=1 U8 xdata Wr_buffer528;U8 xdata Rr_buffer528;U8 Pid4;void SPI_Flash_Init(void);/SPI初始化U8 SPI_Flash_ReadByte(void);/flash 操作
5、基本函数,读一个字节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 x
6、data ReadAddr3;U8 xdata state_reg=0 x00;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;/S
7、witch to the necessary SFRPAGE Select_Flash();SPI_Flash_SendByte(0 x9F);for(i=0;i 6);SPI_Flash_SendByte(U8)(page 7);ReadAddr2=(U8)(StartAddr);saddr=(U8)(StartAddr 8)&0 x01);saddr=(saddr|(U8)(page 7);SPI_Flash_SendByte(saddr);SPI_Flash_SendByte(U8)(StartAddr);/3个字节SPI_Flash_SendByte(0 x00);SPI_Flash_
8、SendByte(0 x00);SPI_Flash_SendByte(0 x00);SPI_Flash_SendByte(0 x00);for(i=0;i ReadCount;i+)Rr_bufferi=SPI_Flash_ReadByte();NotSelect_Flash();名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 4 页 -SFRPAGE=SFRPAGE_save;/Restore the SFRPAGE void FlashPageWrite(U16 page)/写一整页,页范围0-4095 U16 i;U8 SFRPAGE_save=SFRPAGE;/Save
9、the current SFRPAGE SFRPAGE=SPI0_PAGE;/Switch to the necessary SFRPAGE Select_Flash();SPI_Flash_SendByte(FLASH_BUFWRITE2);SPI_Flash_SendByte(0 x00);SPI_Flash_SendByte(0 x00);SPI_Flash_SendByte(0 x00);for(i=0;i 528;i+)SPI_Flash_SendByte(Wr_bufferi);NotSelect_Flash();FlashWaitBusy();if(page 6);SPI_Fla
10、sh_SendByte(U8)(page2);SPI_Flash_SendByte(0 x00);NotSelect_Flash();FlashWaitBusy();SFRPAGE=SFRPAGE_save;/Restore the SFRPAGE void FlashWaitBusy(void)state_reg=0 x00;Select_Flash();SPI_Flash_SendByte(FLASH_STATUS);while(state_reg&0 x80)=0)state_reg=SPI_Flash_ReadByte();NotSelect_Flash();/-名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 4 页 -