我自己写了一段代码如下,但有问题,我检查了好久没能修正。前辈们能给我一份代码参考吗?
#include<reg51.h>
#include<intrins.h>
sbit CS=P0^0;
sbit SK=P0^1;
sbit DI=P0^2;
sbit DO=P0^3;
//写允许、写禁止、擦除全部、写满存储区等指令的操作步骤
//address=100aaXXX X(aa=11 写允许 aa=00 写禁止 aa=10 擦除全部 aa=01 写满存储区)
void SysCommand(uchar address)
{
uchar temp;
CS=0;
SK=0;
CS=1;
for(temp=0;temp<=8;temp++) //打入9位地址,最低位任意
{
DI=address & 0x80;
SK=1; SK=0;
address<<=1;
}
CS=0;
}
//写允许
void EWEN()
{
SysCommand(0x98);
}
//写禁止
void EWDS()
{
SysCommand(0x80);
}
//擦除全部
void ERAL()
{
SysCommand(0x90);
}
//写满存储区
void WRAL()
{
SysCommand(0x88);
}
//写数据
//address=0~0x3f,Indata(H/L) 写入的数据高8位和低8位
void WRITE(uchar address,uchar IndataH,uchar IndataL)
{
uint temp;
EWEN(); //写允许
address=address&0x3f|0x40; //使address=01XXXXXX
CS=0;SK=0;
CS=1;
DI=1; //打入开始位1
SK=1;SK=0;
for(temp=0;temp<=7;temp++) //打入地址
{
DI=address & 0x80;
SK=1; SK=0;
address<<=1;
}
for(temp=0;temp<=7;temp++) //写入高8位
{
DI=IndataH & 0x80;
SK=1; SK=0;
IndataH<<=1;
}
for(temp=0;temp<=7;temp++) //写入低8位
{
DI=IndataL & 0x80;
SK=1; SK=0;
IndataL<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(!DO)
{SK=0;SK=1;} //判忙
SK=0;CS=0;
EWDS(); //写禁止
}
//读数据
//address=0~0x3f,Redata(H/L) 储存读出数据高8位和低8位的变量地址
void READ(uchar address,uchar *ReDataH,uchar *ReDataL)
{
uint temp;
// EWEN();
ReDataH=0;ReDataL=0;
CS=0;SK=0;
address=address&0x3f|0x80; //使address=10XXXXXX
CS=1;
DI=1; //打入开始位1
SK=1;SK=0;
for(temp=0;temp<=7;temp++) //打入地址
{
DI=address & 0x80;
SK=1; SK=0;
address<<=1;
}
DO=1;
for(temp=0;temp<=7;temp++) //打入地址
{
SK=1;
if(DO) *ReDataH|=0x01;
*ReDataH<<=1;
SK=0;
}
for(temp=0;temp<=7;temp++) //打入地址
{
SK=1;
if(DO) *ReDataL|=0x01;
*ReDataL<<=1;
SK=0;
}
CS=0;
// EWEDS();
}
/////////////////////////////////
// 主函数 //
/////////////////////////////////
main()
{
uchar ReDataH;
uchar ReDataL;
ReDataH=0;
ReDataL=0;
WRITE(0,0xAA,0xAA);
READ(0,&ReDataH,&ReDataL);
P1=ReDataH;
} |