/* 版 本:V1.2
作 者:梁衍龙
创作时间:2002年8月
版 权:www.mcuzb.com
联系方式:slzsw_lyl_2@163.com
*/
#include "reg51.h"
#include "intrins.h"
#define X24C44_WRDS 0x80 //RESET WRITE ENABLE LATCH
#define X24C44_STO 0x81 //TRANSFERS FROM RAM TO EEPROM
#define X24C44_WRITE 0x83 //RAM WRITE
#define X24C44_WREN 0x84 //SET WRITE ENABLE LATCH
#define X24C44_RCL 0x85 //TRANSFERS FROM EEPROM TO RAM, RESETS WRITE ENABLE LATCH
#define X24C44_READ 0x86 //RAM READ</P><P>
//#define NOP() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }
//对从芯片而言
sbit SPI24C44_CS =P1^1;
sbit SPI24C44_SK =P1^0;
sbit SPI24C44_DI =P1^2;
sbit SPI24C44_DO =P1^3;
lag(unsigned int Time){while(Time--);}
void delayms(){ unsigned char i=255; while(i--);}
//写一字节// SPI_SK上升沿数据有效 _/-
void SPI24C44_WriteByte(unsigned char dat)
{ unsigned char count;
for(count=0;count<8;count++)
{
dat<<=1;
SPI24C44_DI=CY; NOP();
SPI24C44_SK=1; ///上升
SPI24C44_SK=0; //开始上升 NOP();
}
}
//读一字// SPI_SK上升沿数据有效 _/—
unsigned int SPI24C44_ReadWord()
{ unsigned char count;
unsigned int dat=0;
for(count=0;count<16;count++)
{
dat<<=1;
if(SPI24C44_DO) dat++; //为"0"读出一个字
SPI24C44_SK=1;
SPI24C44_SK=0;
NOP();
} return(dat);
}
//从24C44(EEPROM)里面读取一字
//address: 0-15
unsigned int SPI24C44_Read(unsigned char address)
{
unsigned char temp;
unsigned int dat;
SPI24C44_SK=0;
NOP();
SPI24C44_CS=0;
NOP();
SPI24C44_DI=0;
NOP();
SPI24C44_DO=0;
NOP();
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_RCL);
//PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WREN);
//PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
address=address<<3;
temp= X24C44_READ | address;
SPI24C44_CS=1;
SPI24C44_WriteByte(temp); //Read Data from RAM Address
dat=SPI24C44_ReadWord();
SPI24C44_CS=0;
return(dat);}//向24C44里(EEPROM)写一字
SPI24C44_Write(unsigned char address,unsigned int dat)
{ unsigned char temp;
unsigned int moon;
SPI24C44_SK=0;
NOP();
SPI24C44_CS=0;
NOP();
SPI24C44_DI=0;
NOP();
SPI24C44_DO=0;
NOP();
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_RCL); //PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WREN); //PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
address=address<<3;
temp=X24C44_WRITE | address;
SPI24C44_CS=1;
SPI24C44_WriteByte(temp); //WRITE(1AAAA011): Write Data into RAM Address AAAA
SPI24C44_WriteByte( (unsigned char)(dat>>8) );
//send data
SPI24C44_WriteByte( (unsigned char)dat );
//send data
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_STO );
//STO(1XXXX001):Store RAM Data in E2PROM
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WRDS );
//STO(1XXXX001):Store RAM Data in E2PROM
SPI24C44_CS=0;
lag(500);
moon=SPI24C44_Read(address>>3);
if(moon==dat) SystemError=0;
else SystemError=1;}//非标准SPI口驱动,比较难调注意芯片与CPU接口!!呵呵
作 者:梁衍龙
创作时间:2002年8月
版 权:www.mcuzb.com
联系方式:slzsw_lyl_2@163.com
*/
#include "reg51.h"
#include "intrins.h"
#define X24C44_WRDS 0x80 //RESET WRITE ENABLE LATCH
#define X24C44_STO 0x81 //TRANSFERS FROM RAM TO EEPROM
#define X24C44_WRITE 0x83 //RAM WRITE
#define X24C44_WREN 0x84 //SET WRITE ENABLE LATCH
#define X24C44_RCL 0x85 //TRANSFERS FROM EEPROM TO RAM, RESETS WRITE ENABLE LATCH
#define X24C44_READ 0x86 //RAM READ</P><P>
//#define NOP() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }
//对从芯片而言
sbit SPI24C44_CS =P1^1;
sbit SPI24C44_SK =P1^0;
sbit SPI24C44_DI =P1^2;
sbit SPI24C44_DO =P1^3;
lag(unsigned int Time){while(Time--);}
void delayms(){ unsigned char i=255; while(i--);}
//写一字节// SPI_SK上升沿数据有效 _/-
void SPI24C44_WriteByte(unsigned char dat)
{ unsigned char count;
for(count=0;count<8;count++)
{
dat<<=1;
SPI24C44_DI=CY; NOP();
SPI24C44_SK=1; ///上升
SPI24C44_SK=0; //开始上升 NOP();
}
}
//读一字// SPI_SK上升沿数据有效 _/—
unsigned int SPI24C44_ReadWord()
{ unsigned char count;
unsigned int dat=0;
for(count=0;count<16;count++)
{
dat<<=1;
if(SPI24C44_DO) dat++; //为"0"读出一个字
SPI24C44_SK=1;
SPI24C44_SK=0;
NOP();
} return(dat);
}
//从24C44(EEPROM)里面读取一字
//address: 0-15
unsigned int SPI24C44_Read(unsigned char address)
{
unsigned char temp;
unsigned int dat;
SPI24C44_SK=0;
NOP();
SPI24C44_CS=0;
NOP();
SPI24C44_DI=0;
NOP();
SPI24C44_DO=0;
NOP();
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_RCL);
//PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WREN);
//PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
address=address<<3;
temp= X24C44_READ | address;
SPI24C44_CS=1;
SPI24C44_WriteByte(temp); //Read Data from RAM Address
dat=SPI24C44_ReadWord();
SPI24C44_CS=0;
return(dat);}//向24C44里(EEPROM)写一字
SPI24C44_Write(unsigned char address,unsigned int dat)
{ unsigned char temp;
unsigned int moon;
SPI24C44_SK=0;
NOP();
SPI24C44_CS=0;
NOP();
SPI24C44_DI=0;
NOP();
SPI24C44_DO=0;
NOP();
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_RCL); //PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WREN); //PERFORM A RECALL TO SET THE RECALL LATCH
SPI24C44_CS=0;
address=address<<3;
temp=X24C44_WRITE | address;
SPI24C44_CS=1;
SPI24C44_WriteByte(temp); //WRITE(1AAAA011): Write Data into RAM Address AAAA
SPI24C44_WriteByte( (unsigned char)(dat>>8) );
//send data
SPI24C44_WriteByte( (unsigned char)dat );
//send data
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_STO );
//STO(1XXXX001):Store RAM Data in E2PROM
SPI24C44_CS=0;
SPI24C44_CS=1;
SPI24C44_WriteByte(X24C44_WRDS );
//STO(1XXXX001):Store RAM Data in E2PROM
SPI24C44_CS=0;
lag(500);
moon=SPI24C44_Read(address>>3);
if(moon==dat) SystemError=0;
else SystemError=1;}//非标准SPI口驱动,比较难调注意芯片与CPU接口!!呵呵