本帖最后由 a250871207 于 2013-1-28 15:55 编辑
- #include "reg52.h"
- #include "main.h"
- sbit FLASH_CLK = P2^5;
- sbit FLASH_DI = P2^6;
- sbit FLASH_DO = P2^7;
- sbit FLASH_CS = P2^4;
- #define WREN 0x06
- #define WRDI 0x04
- #define RDID 0x9f
- #define RDSR 0x05
- #define READ 0x03
- #define FAST_READ 0x0B
- #define PAGE_WRITE 0x0A
- #define PAGE_PROGRAM 0x02
- #define PAGE_ERASE 0xDB
- #define SECOTR_ERASE 0xD8
- void FLASH_WriteByte(unsigned char inData);
- unsigned char FLASH_ReadByte(void);
- void FLASH_Init(void);
- void FLASH_Write(unsigned long addr, unsigned char* p_value, unsigned char writebytes );
- void FLASH_WriteByte(unsigned char inData)
- {
- unsigned char i;
- for(i = 0x80; i > 0; i >>= 1)
- {
- FLASH_CLK = 0;
- if( inData & i ) FLASH_DI = 1;
- else FLASH_DI = 0;
- FLASH_CLK = 1;
- }
- }
- unsigned char FLASH_ReadByte(void)
- {
- unsigned char i;
- unsigned char outData = 0;
- for(i = 0x80; i > 0; i >>= 1)
- {
- FLASH_CLK = 1;
- FLASH_CLK = 0;
- if( FLASH_DO ) outData |= i;
- }
- return outData;
- }
- void FLASH_Init(void)
- {
- FLASH_CS = 1;
- FLASH_CS = 0;
- }
- unsigned char FLASH_Register(void)
- {
- unsigned char result;
- FLASH_CS = 0;
- FLASH_WriteByte(RDSR);
- result = FLASH_ReadByte();
- FLASH_CS = 1;
- return result;
- }
- void FLASH_WriteEnable(void)
- {
- FLASH_CS = 0;
- FLASH_WriteByte(WREN);
- FLASH_CS = 1;
- }
- void FLASH_WriteDisEnable(void)
- {
- FLASH_CS = 0;
- FLASH_WriteByte(WRDI);
- FLASH_CS = 1;
- }
- void FLASH_PageErase(unsigned long addr)
- {
- while(!( FLASH_Register() & 0x02 ))FLASH_WriteEnable();
- FLASH_CS = 0;
- FLASH_WriteByte(PAGE_ERASE);
- FLASH_WriteByte((addr>>16)&0xff);
- FLASH_WriteByte((addr>>8)&0xff);
- FLASH_WriteByte(0);
- FLASH_CS = 1;
- while( FLASH_Register() & 0x01 );
- FLASH_WriteDisEnable();
- }
- void FLASH_Write(unsigned long addr, unsigned char* p_value, unsigned char writebytes )
- {
- unsigned char i;
- unsigned char frontPageCnt = 0;
- unsigned char pageIndex = addr&0xff;
- while(!( FLASH_Register() & 0x02 ))FLASH_WriteEnable();
- FLASH_CS = 0;
- FLASH_WriteByte(PAGE_WRITE);
- FLASH_WriteByte((addr>>16)&0xff);
- FLASH_WriteByte((addr>>8)&0xff);
- FLASH_WriteByte(pageIndex);
- if( writebytes > (256-pageIndex) )
- {
- frontPageCnt = 256-pageIndex;
- }
- else
- {
- frontPageCnt = writebytes;
- }
- for( i = 0; i < frontPageCnt; i++ )
- {
- FLASH_WriteByte(*p_value++);
- }
- FLASH_CS = 1;
- while( FLASH_Register() & 0x01 );
- FLASH_WriteDisEnable();
- if( frontPageCnt == writebytes ) return;
- addr++;
- frontPageCnt = writebytes - frontPageCnt;
- FLASH_WriteEnable();
- FLASH_CS = 0;
- FLASH_WriteByte(PAGE_WRITE);
- FLASH_WriteByte((addr>>16)&0xff);
- FLASH_WriteByte((addr>>8)&0xff);
- FLASH_WriteByte(0);
- for( i = 0; i < frontPageCnt; i++ )
- {
- FLASH_WriteByte(*p_value++);
- }
- FLASH_CS = 1;
- while( FLASH_Register() & 0x01 );
- FLASH_WriteDisEnable();
- }
- void FLASH_Read(unsigned long addr, unsigned char* p_value, unsigned char readbytes )
- {
- unsigned char i;
- FLASH_CS = 0;
- FLASH_WriteByte(READ);
- FLASH_WriteByte((addr>>16)&0xff);
- FLASH_WriteByte((addr>>8)&0xff);
- FLASH_WriteByte(addr&0xff);
- for(i = 0; i < readbytes; i++)
- {
- *p_value++ = FLASH_ReadByte();
- }
- FLASH_CS = 1;
- }
- void main(void)
- {
- unsigned int i;
- unsigned char failCnt = 0;
- unsigned char test[101];
- unsigned char result = 0;
- for(i=0;i<10000;i++);
- FLASH_Init();
- while(1){
- result++;
- FLASH_PageErase(0);
- for( i = 0; i < 101; i++ )
- {
- test[i] = result;
- }
- FLASH_Write( 0, test, 101 );
- FLASH_Read( 0, test, 101 );
- if( result != test[0] )
- {
- failCnt++;
- }
- if( result > 100 )
- {
- result = 0;
- }
- }
- }
上程序
|