我没有用看门狗功能,只做存储器用。读出的数总是不对,看看是不是我时序不对呢。
#include <AT89X051.H>
#include<stdio.h>
#include<intrins.h>
#include<math.h>
#include<absacc.h>
#define uint unsigned int
#define uchar unsigned char
//X5045
sbit CS1 = P1^7;
sbit SCK = P1^6;
sbit SO = P1^5;
sbit SI = P1^4;
#define WREN 0x06
#define WRDI 0x04
#define RDSR 0x05
#define WRSR 0x01
#define READ0 0x03
#define READ1 0x0b
#define WRITE0 0x02
#define WRITE1 0x0a
void RESWDI(void)
{
CS1=0;
CS1=1;
}
//Write Byte to spi/X5045
void write_byte(uchar edata)
{
uchar i;
SCK=0;
_nop_();
_nop_();
_nop_();
for(i=0;i<8;i++)
{
if(edata & 0x80==0)SI=0;
else SI=1;
SCK=1;
_nop_();
_nop_();
_nop_();
edata=edata<<1;
SCK=0;
_nop_();
_nop_();
_nop_();
}
}
//Read Byte from Spi/X5045
uchar read_byte(void)
{
uchar i,result;
result=0;
for(i=0;i<8;i++)
{
SCK=1;
_nop_();
_nop_();
_nop_();
result=result<<1;
if(SO==1) result|=0x01;
SCK=0;
_nop_();
_nop_();
_nop_();
}
return(result);
}
//Write enable
void WriteEN(void)
{
SCK=0;
CS1=0;
write_byte(WREN);
SCK=0;
CS1=1;
}
//reset Write enable (Disable write operations)
void WriteDI(void)
{
SCK=0;
CS1=0;
write_byte(WRDI);
SCK=0;
CS1=1;
}
//READ status register
uchar ReadSR(void)
{
uchar Temp;
WriteEN();
CS1=1;
SCK=0;
CS1=0;
write_byte(RDSR);
Temp=read_byte();
SCK=0;
CS1=1;
return Temp;
}
//check WIP 判断是否写入完成
void WIPCHK(void)
{
uchar Temp,i;
for(i=0;i<50;i++)
{
Temp=ReadSR();
if(Temp&0x01==0)
i=50;
}
}
//write status register
void WriteSR(uchar cData)
{
WriteEN();
CS1=1;
SCK=0;
CS1=0;
write_byte(WRSR);
write_byte(cData);//0x02//发送寄存器值BL0,BL1为0没写保护,WD0=0 WD1=1看门狗复位时间为200ms
SCK=0;
CS1=1;
WIPCHK();
}
//Read byte from x5045读地址中的数据,address 为读入地址//0x00-0xff
uchar R5045(uint address)
{
uchar result;
CS1=1;
SCK=0;
CS1=0;
write_byte((char)(address>255?READ1:READ0));
write_byte((char)(address&0x0ff));
result=read_byte();
SCK=0;
CS1=1;
return(result);
}
//write byte to 5045 向地址中写入数据,只能写00-FFH
void w5045(uint address,uchar udata)
{
CS1=0;
write_byte(WREN);
CS1=1;
SCK=0;
CS1=0;
write_byte((char)(address>255?WRITE1:WRITE0));
write_byte((char)(address&0x0ff));
write_byte(udata);
SCK=0;
CS1=1;
WIPCHK();
}
void main()
{
uchar s=0;
WriteEN();
_nop_();
_nop_();
WriteSR(0x30);
w5045(0x10,0x08);
// WriteDI();
ReadSR();
s=R5045(0x10);
}