- /*
- 数据存储空间从地址0x0000到0x08ff;
- 扇区是512字节,擦除操作是整扇区擦除,所以Flash_Erase(0x0000)的擦除范围是0x0000到0x0200;
- 值得注意的是Flash_Erase(0x01ff)的擦除范围也是0x0000到0x0200;
- */
- #include "c8051f410.h"
- void Flash_Write(unsigned char source,unsigned int address);
- unsigned char Flash_Read(unsigned int addr);
- void Flash_Erase(unsigned int address);
- int i,j;
- unsigned char result;
- main()
- {
- OSCICN = 0x87;
- PCA0MD &= ~0x40;
- XBR0= 0x01;
- XBR1= 0x40;
-
- //Flash_Erase(0x410);
- //Flash_Write(11,0x410);
- result=Flash_Read(0x410);
- i=0;
- }
- void Flash_Write(unsigned char source,unsigned int address)
- {
- unsigned char EA_save; // saves the current interrupt state
- unsigned char xdata * data pwrite;
- EA_save = EA;
- EA = 0; // disable interrupts
- pwrite = (unsigned char xdata *) address;
- FLKEY = 0xA5; // FLASH lock and key sequence 1
- FLKEY = 0xF1; // FLASH lock and key sequence 2
- PSCTL = 0x01; // Sets PSWE bit, Clears PSEE bit
- *pwrite = source; // Move the data to C8051F326 flash
- PSCTL &= ~0x01; // Clear PSWE so MOVX target XRAM
- EA = EA_save; // re-enable interrupts
- }
- void Flash_Erase(unsigned int address)
- {
- unsigned char EA_save; // saves the current interrupt state
- unsigned char xdata * data pwrite;
- EA_save = EA;
- EA = 0; // disable interrupts
- pwrite = (unsigned char xdata *) address;
- FLKEY = 0xA5; // FLASH lock and key sequence 1
- FLKEY = 0xF1; // FLASH lock and key sequence 2
- PSCTL = 0x03; // Sets PSWE bit, Sets PSEE bit
- *pwrite = 0xff; //擦除整个扇区
- PSCTL &= ~0x03; // Clear PSWE and PSEE so MOVX target XRAM
- EA = EA_save; // re-enable interrupts
- }
- unsigned char Flash_Read(unsigned int addr)
- {
- unsigned char EA_save,Tmp_data; // saves the current interrupt state
- unsigned char code * data pread;
- EA_save = EA;
- EA = 0; // disable interrupts
- pread = (unsigned char code *) addr;
- Tmp_data = *pread; //读指定flash地址数据
- EA = EA_save; // re-enable interrupts
- return Tmp_data;
- }
|