51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机源码 > 详细内容
X24C44一款老的EEPROM(C语言)
发布时间:2009/6/2  阅读次数:1057  字体大小: 【】 【】【

作者:梁衍龙    来源:互联网


  

/* 版 本: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接口!!呵呵
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号