c8051单片机源程序
源代码在线查看: flash1.c
////////////FLASH读写操作调试程序/////////////////
//////////flash.c////////////////////////////////////////
#include "c8051f040.h"
#include "absacc.h"
#define LEN 5
void FlashWrite(unsigned char xdata *pwrite,unsigned char *BufFrom,unsigned char len){
/*
pwrite为FLASH地址指针,采用xdata强制指明指针类型,以保证采用MOVX操作
BufFrom为数据指针
len为数据长度
*/
unsigned char i;
WDTCN = 0xde;
WDTCN = 0xad;// 禁止看门狗
EA=0;//禁止中断
SFRPAGE=0x00;
FLSCL = 0x81;
PSCTL = 0x03;
*pwrite=0; //擦除操作
PSCTL=0x01;
for(i=0;i {
*pwrite++=*BufFrom++;
}
PSCTL = 0; // disable FLASH writes
FLSCL = 0x80; // disable FLASH writes
EA=1;
}
void FlashRead1(unsigned char code *pread,unsigned char *BufTo,unsigned char len){//采用指针方法实现
/*
对FLASH读操作采用MOVC指令,故采用code指针类型
BufTo为存放读取值的缓冲指针
len为数据长度
*/
unsigned char i;
for(i=0;i BufTo[i]=*pread;
pread++;
}
}
void FlashRead2(unsigned int adr,unsigned char *BufTo,unsigned char len){//采用CBYTE[]读FLASH
unsigned i;
for(i=0;i BufTo[i]=CBYTE[adr];
adr++;
}
}
void config(){
//看门狗禁止
WDTCN = 0x07;
WDTCN = 0xDE;
WDTCN = 0xAD;
SFRPAGE = 0x0F;
//交叉开关使能,但没有进行外围设备配置
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x40;
XBR3 = 0x00;
//管脚输出配置,P0口为开漏输出,其中P0.6接上拉电阻,P0为数字输入口
SFRPAGE = 0x0F;
P0MDOUT = 0x00;
P1MDIN = 0xFF;
//晶振配置,采用内部晶振8分频
SFRPAGE = 0x0F;
CLKSEL = 0x00;
OSCXCN = 0x00;
OSCICN = 0x84;
}
void main(){
xdata unsigned char teststr1[LEN]={0x30,0x32,0x34,0xa8,0xa7};
xdata unsigned char readdata1[LEN];
xdata unsigned char readdata2[LEN];
float pi=3.14159;
idata int tt=-345;
xdata float readpi=0.0;
xdata int readtt=0;
config();
FlashWrite(512*20,teststr1,LEN);
FlashWrite(512*21,teststr1,LEN);
FlashWrite(512*21+20,&pi,sizeof(float));
FlashWrite(512*22,&tt,sizeof(int));
FlashRead1(512*20,readdata1,LEN);
FlashRead1(512*21,readdata2,LEN);
FlashRead1(512*21+20,&readpi,sizeof(float));
FlashRead2(512*22,&readtt,sizeof(int));
while(1);//可在此设断点观察写的数据与读的数据是否一致
}