⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c.bak

📁 怎么样操作flash
💻 BAK
字号:

#include <reg51.h>
#include "mcu_def.h"
#include "sd_reg.h"
#define  FL_ALE_SET()   DPL = FL_CTRL0; DPH = 0x42; PIOP = 0x87;
#define  FL_ALE_CLR()   DPL = FL_CTRL0; DPH = 0x42; PIOP = 0x47;
sfr PIOP = 0x98;
sbit psw1 = PSW^1;
void wait_rb(void);
void fl_cmd(unsigned char cmd);


unsigned char xdata fl_data  _at_ 0x7f00;
unsigned char xdata ff_data  _at_ 0x8f00;

//flash reg
unsigned char xdata fl_CTRL0  _at_ 0x4200;
unsigned char xdata fl_CTRL1  _at_ 0x4201;
//dma2
unsigned char xdata dma2_LEN0  _at_ 0x4010;
unsigned char xdata dma2_LEN1  _at_ 0x4011;
unsigned char xdata dma2_A0  _at_ 0x4012;
unsigned char xdata dma2_A1  _at_ 0x4013;

unsigned char xdata *p;
int i;
main()
{
 unsigned char n1,n2,n3,n4,n5,status,ram_data;
 
 for (i=0,p = 0; i<512; i++,p++)
 *p = i + 1;
 
 for (i=0,p = 0; i<512; i++,p++)
 {
 ram_data = *p;
 i = i;
}
 
 fl_CTRL1 = 0x0c;
 fl_CTRL0 = 0x3e;
 wait_rb();
 
 //read ID
 fl_cmd(0x90);
 FL_ALE_SET();
 fl_data = 0X00;
 FL_ALE_CLR();
 
 ff_data = 0x00;
 
 n1 = fl_data;
 ff_data = 0x00;
 n2 = fl_data;
 ff_data = 0x00;
 n3 = fl_data;
 ff_data = 0x00;
 n4 = fl_data;
 ff_data = 0x00;
 n5 = fl_data;
 
 //ERASE FLASH
 fl_cmd(0x60);
 
 FL_ALE_SET();
 fl_data = 0X00; //ROW ADDR1
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR2
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR3
 ff_data = 0x00;
 FL_ALE_CLR();
 
 fl_cmd(0xD0);
 
 wait_rb();
 
 //read status
 fl_cmd(0x70);
 status = fl_data;
 
 //write flash 
 fl_cmd(0x80);
 
 FL_ALE_SET();
 fl_data = 0X00; //column ADDR1
 ff_data = 0x00; 
 fl_data = 0X00; //column ADDR2
 ff_data = 0x00;  
 fl_data = 0X00; //ROW ADDR1
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR2
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR3
 ff_data = 0x00;
 FL_ALE_CLR();
 
 dma2_LEN0 = 0x80;
 dma2_LEN1 = 0;
 dma2_A0 = 0;
 dma2_A1 = 0;
 
 DPL = DMA2CTRL; 
 DPH = 0x40; 
 PIOP = DMA_SEND_set;
 
 DPL = DMA2CTRL; 
 DPH = 0x40; 
 PIOP = DMA_START_set;
 //start flash write
 DPL = FL_CTRL0; 
 DPH = 0x42; 
 PIOP = FL_WRN_clr;
 
 //wait for DMA done
 DPL = DMA2STATUS;
 DPH = 0x40;
 PIOP = DMA_DONE_test;
 while (!psw1)
 PIOP = DMA_DONE_test;
 //wait for flash-write done
 DPL = FL_STATUS;
 DPH = 0x42;
 PIOP = RW_DONE_test;
 while (!psw1)
 PIOP = RW_DONE_test;
 //stop flash write
 DPL = FL_CTRL0; 
 DPH = 0x42; 
 PIOP = FL_WRN_set;
 
 fl_cmd(0x10);
 
 wait_rb();
 
 fl_cmd(0x70);
 status = fl_data;
 
 for (i=0; i<512; i++);
 
 //read flash
 fl_cmd(0x00);
 
 FL_ALE_SET();
 fl_data = 0X00; //column ADDR1
 ff_data = 0x00; 
 fl_data = 0X00; //column ADDR2
 ff_data = 0x00;  
 fl_data = 0X00; //ROW ADDR1
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR2
 ff_data = 0x00; 
 fl_data = 0X00; //ROW ADDR3
 ff_data = 0x00;
 FL_ALE_CLR();
 
 fl_cmd(0x30);
 wait_rb();
 
 dma2_LEN0 = 0x80;
 dma2_LEN1 = 0;
 dma2_A0 = 0x90;
 dma2_A1 = 0;
 
 DPL = DMA2CTRL; 
 DPH = 0x40; 
 PIOP = DMA_SEND_clr;
 
 DPL = DMA2CTRL; 
 DPH = 0x40; 
 PIOP = DMA_START_set;
 //start flash write
 DPL = FL_CTRL0; 
 DPH = 0x42; 
 PIOP = FL_RDN_clr;
 
 //wait for DMA done
 DPL = DMA2STATUS;
 DPH = 0x40;
 PIOP = DMA_DONE_test;
 while (!psw1)
 PIOP = DMA_DONE_test;
 //wait for flash-write done
 DPL = FL_STATUS;
 DPH = 0x42;
 PIOP = RW_DONE_test;
 while (!psw1)
 PIOP = RW_DONE_test;
 //stop flash write
 DPL = FL_CTRL0; 
 DPH = 0x42; 
 PIOP = FL_RDN_set;
 
 
 
 
 
 
 
 
 
 
 


}

void wait_rb(void)
{
 DPL = FL_STATUS;
 DPH = 0x42;
 PIOP = FL_RB0_test;
 while (!psw1)
 PIOP = FL_RB0_test;	
	
}

void fl_cmd(unsigned char cmd)
{
 DPL = FL_CTRL0;
 DPH = 0x42;
 PIOP = FL_CLE_set;
 fl_data = cmd;
 DPL = FL_CTRL0;
 DPH = 0x42;
 PIOP = FL_CLE_clr;
	
}
/*
void fl_ale_set(void)
{
 DPL = FL_CTRL0;
 DPH = 0x42;
 PIOP = FL_CLE_set;	
}

void fl_ale_clr(void)
{
 DPL = FL_CTRL0;
 DPH = 0x42;
 PIOP = FL_ALE_clr;	
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -