本帖最后由 geililai 于 2015-6-10 20:22 编辑
#include <STC12C5A60S2.H>
#include <intrins.h>
#include <stdio.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
/*flash引脚*/
sbit CPU_LE1=P4^1;//定义锁存器使能
sbit CPU_LE2=P4^5;
sbit CPU_LE3=P4^4;
sbit uP_RDn=P3^4;
sbit up_BLWEn_BLEn=P3^5;
uint first;
uchar idata buf4[1]; //存储所读取数据
/*1. Load Data AA (hex) into Address 05555 (hex)
2. Load Data 55 into Address 02AAA
3. Load Data A0 into Address 05555
4. Load desired sector with data
5. Pause tWC (device write cycle
time)
6. Continue with next operation.*/
void init(void)
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
PCON=0x00;
TR1=1;
SCON=0xd0;
}
void send(uchar addr)
{
TB8=1;
SBUF=addr;
while(TI==0)
{
;
}
TI=0;
}
/************延时程序************/
void delay(uchar k)
{
uchar j;
while((k--)!=0)
{
for(j=0;j<125;j++)
{;}
}
}
void delay1(unsigned int l_time)
{
unsigned int data lp;
for(lp=0;lp<l_time;lp++)
_nop_();
}
//数据保护模块
void protect()
{
uP_RDn=1;
up_BLWEn_BLEn=1;
CPU_LE1=1;
P2=0x55;
CPU_LE1=0;
CPU_LE2=1;
P2=0x55;
CPU_LE2=0;
CPU_LE3=1;
P2=0x00;
CPU_LE3=0;
up_BLWEn_BLEn=0;
P2=0xaa;
up_BLWEn_BLEn=1;
CPU_LE1=1;
P2=0xaa;
CPU_LE1=0;
CPU_LE2=1;
P2=0x2a;
CPU_LE2=0;
CPU_LE3=1;
P2=0x00;
CPU_LE3=0;
up_BLWEn_BLEn=0;
P2=0x55;
up_BLWEn_BLEn=1;
CPU_LE1=1;
P2=0x55;
CPU_LE1=0;
CPU_LE2=1;
P2=0x55;
CPU_LE2=0;
CPU_LE3=1;
P2=0x00;
CPU_LE3=0;
up_BLWEn_BLEn=0;
P2=0xa0;
up_BLWEn_BLEn=1;
}
//块选择
bit SectorChoose(uint suba)
{
suba=suba/256;
CPU_LE2=1;
P2=suba&0xff;
CPU_LE2=0;
suba=suba/256;
CPU_LE3=1;
P2=suba&0xff;
CPU_LE3=0;
return 0;
}
//后续数据写入
bit WriteData(uint addr,uint s)
{
addr=addr&0xff;
CPU_LE1=1;
P2=addr;
CPU_LE1=0;
up_BLWEn_BLEn=0;
P2=s;
up_BLWEn_BLEn=1;
return 0;
}
//读出数据,暂时不知道是否需要连续读出。
bit ReadData(uint suba)
{
CPU_LE1=1;
P2=suba&0xff;
CPU_LE1=0;
suba=suba/256;
CPU_LE2=1;
P2=suba&0xff;
CPU_LE2=0;
suba=suba/256;
CPU_LE3=1;
P2=suba&0xff;
CPU_LE3=0;
uP_RDn=0;
delay1(1);
buf4[0]=P2; //这里可能有问题
uP_RDn=1;
return 0;
}
void main()
{
int i;
uint addr=0x00200;
first=0x04;
init();
P4M1=0x00; //P4.4,P4.5作为IO口
P4M0=0x72;
P4SW=0x70;
uP_RDn=1; //读使能关闭
up_BLWEn_BLEn=1;
delay1(200);
protect();
SectorChoose(addr);
for(i=0;i<255;i++)
{
WriteData(addr,first);
addr++;
}
up_BLWEn_BLEn=0;
delay1(1000);
while(1)
{
buf4[0]=0x04;
send(buf4[0]);
delay(200);
ReadData(0x00200);
send(buf4[0]);
delay(200);
ReadData(0x00222);
send(buf4[0]);
delay(200);
ReadData(0x002ff);
send(buf4[0]);
delay(200);
}
}
原理图如下
-
一周热门 更多>