#includew25q80.h//×¢£ºW25Q80ÓÉ256BYTE×é³ÉÒ»¸öPAGE£¬²»¿ÉPGAE²Á³ý£¬¿ÉÒÔ½øÐÐBYTEPROGRAM»òÕßPAGEPROGRAM//ÓÉ16PAGE×é³ÉÒ»¸öSECTOR£¬¿ÉSECTOR²Á³ý//ÓÉ16SECTOR×é³ÉÒ»¸öBLOCK£¬¿ÉBLOCK²Á³ý//ÓÉ16BLOCK×é³ÉÒ»¸öFULLMEMEORY£¬¿ÉFULLMEMORY²Á³ý//ËùÒÔ£¬×ÜÈÝÁ¿ÊÇ1Mbytes//W25Q80Ö÷ÒªÃüÁî×Ö#defineREAD_ARRAY0x03#defineSECTOR_ERASE0x20#defineBYTE_OR_PAGE_PROGRAM0x02#defineWRITE_ENABLE0x06#defineWRITE_DISABLE0x04#defineREAD_STATUS_REGISTER0x05#defineManufacturer_DeviceID0x9F//¶¨ÒåW25Q80µÄCS½Å¶ÔÓ¦MCUµÄIO#defineW25Q80_CSP1_2//SPIÓ²¼þ³õʼ»¯voidSpi_Init(void){PERCFG|=0x02;//SPI1Ó³Éäµ½P1¿ÚP1SEL|=0xE0;//P15~P17×÷¸´Óù¦ÄÜ(clkmosimiso)P1SEL&=~0x04;//P12×÷GPIOP1DIR|=0x04;//P12×÷Êä³öP1_2=1;//P12Êä³ö¸ßµçƽU1CSR&=~0xA0;//SPIÖ÷·½Ê½U1GCR&=~0xC0;//CPOL=0CPHA=0U1GCR|=0x20;//MSBU1BAUD=0;//²¨ÌØÂÊÉèΪsysclk/8U1GCR|=0x11;}//SPI·¢ËÍÓë½ÓÊÕ×Ö½Ústaticu8Spi_ReadWriteByte(u8TxData){U1DBUF=TxData;//·¢ËÍÊý¾Ýwhile(!(U1CSR&0x02));//µÈ´ý·¢ËÍÍê³ÉU1CSR&=0xFD;//Çå³ý·¢ËÍÍê³É±êÖ¾returnU1DBUF;}//CSÏßÀµÍ£¬Ê¹ÄÜоƬstaticvoidW25Q80_Enable(void){volatileu8i;W25Q80_CS=0;for(i=5;i0;i--);//ÑÓʱ}//CSÏßÀ¸ß£¬½ûÄÜоƬ#defineW25Q80_Disable()(W25Q80_CS=1)//ÉèÖÃFLASHоƬдʹÄÜstaticvoidSetW25Q80WriteEnable(void){W25Q80_Enable();Spi_ReadWriteByte(WRITE_ENABLE);W25Q80_Disable();}//ÉèÖÃFLASHоƬд½ûÄÜ//staticvoidClearW25Q80WriteEnable(void)//{//W25Q80_Enable();//Spi_ReadWriteByte(WRITE_DISABLE);//W25Q80_Disable();//}//¶ÁÈ¡FLASHоƬµÄ״̬×Ö½Ú£¬¿ÉÅжÏоƬÊÇ·ñbusystaticu8ReadW25Q80StatusRegister(void){u8temp;W25Q80_Enable();Spi_ReadWriteByte(READ_STATUS_REGISTER);temp=Spi_ReadWriteByte(0xF0);W25Q80_Disable();returntemp;}//¶ÁÈ¡FLASHµÄÄÚÈÝ£¬¶ÁÈ¡µÄ×Ö½ÚÊýûÓÐÏÞÖÆvoidReadW25Q80Operation(u32addr,u8*databuf,u32len){u32i=0;u8temp;temp=ReadW25Q80StatusRegister();while(temp&0x01)//µÈ´ýFLASHоƬ½áÊøBUSY״̬{temp=ReadW25Q80StatusRegister();if(++i1000000)return;}W25Q80_Enable();Spi_ReadWriteByte(READ_ARRAY);Spi_ReadWriteByte((u8)(addr16));Spi_ReadWriteByte((u8)(addr8));Spi_ReadWriteByte((u8)addr);for(i=0;ilen;i++){databuf[i]=Spi_ReadWriteByte(0xF0);}W25Q80_Disable();}//дÈëFLASH£¬Ò»´Î×î¶àд256×Ö½Ú£¬×¢Òâ²»ÒªÔ½½ç£¨²»Òª³¬¹ýÒ³±ß½ç£©voidWriteW25Q80Operation(u32addr,u8*databuf,u16len){u32i=0;u8temp;temp=ReadW25Q80StatusRegister();while(temp&0x01)//µÈ´ýFLASHоƬ½áÊøBUSY״̬{temp=ReadW25Q80StatusRegister();if(++i1000000)return;}SetW25Q80WriteEnable();//ÉèÖÃоƬдʹÄÜW25Q80_Enable();Spi_ReadWriteByte(BYTE_OR_PAGE_PROGRAM);Spi_ReadWriteByte((u8)(addr16));Spi_ReadWriteByte((u8)(addr8));Spi_ReadWriteByte((u8)addr);for(i=0;ilen;i++){Spi_ReadWriteByte(databuf[i]);}W25Q80_Disable();}//¶ÔFLASH½øÐÐSECTOR²Á³ývoidEraseW25Q80Operation(u32addr){u32i=0;u8temp;temp=ReadW25Q80StatusRegister();while(temp&0x01)//µÈ´ýFLASHоƬ½áÊøBUSY״̬{temp=ReadW25Q80StatusRegister();if(++i1000000)return;}SetW25Q80WriteEnable();//ÉèÖÃоƬдʹÄÜW25Q80_Enable();Spi_ReadWriteByte(SECTOR_ERASE);Spi_ReadWriteByte((u8)(addr16));Spi_ReadWriteByte((u8)(addr8));Spi_ReadWriteByte((u8)addr);W25Q80_Disable();}//¶ÁÈ¡FLASHоƬµÄJEDECIDÐÅÏ¢voidReadW25Q80DeviceID(u8*buf){W25Q80_Enable();Spi_ReadWriteByte(Manufacturer_DeviceID);buf[0]=Spi_ReadWriteByte(0xF0);buf[1]=Spi_ReadWriteByte(0xF0);buf[2]=Spi_ReadWriteByte(0xF0);W25Q80_Disable();}