📄 eerom.c
字号:
#include <AT89X51.h>
#include <string.h>
// 对AT24C08的读、写
// extern void DelayMs(unsigned int);
// extern void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
// extern void WriteI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
extern unsigned char data recieve[15];//21 ="(244)6.5211N(1210)0.1536E";
/***************************************************************************/
#define WriteDeviceAddress 0xa0 //写器件地址
#define ReadDviceAddress 0xa1 //读器件地址
#define SCL P2_7 //I2C时钟线SCL
#define SDA P2_6 //I2C数据线SDA
#define DOG P3_7 //程序运行标志及数据读写正确标志
#define N 15 //一次存储数组长度
/***************************************************************************/
void DelayMs(unsigned int number) //延时子程序,延时大小取决于工作频率和number值
{
unsigned char temp;
for(;number!=0;number--) //循环,DOG用于输出状态信号
{
for(temp=112;temp!=0;temp--) //空循环
{
}
}
}
/***************************************************************************/
void Start() //起始信号子程序
{
SDA=1;
DelayMs(1); //延时
SCL=1;
DelayMs(1);
SDA=0;
DelayMs(1);
SCL=0;
DelayMs(1);
}
/***************************************************************************/
void Stop() //终止信号子程序
{
SCL=0;
DelayMs(1);
SDA=0;
DelayMs(1);
SCL=1;
DelayMs(1);
SDA=1;
DelayMs(1);
}
/***************************************************************************/
void Ack() //发送应答位子程序
{
SDA=0;
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
SDA=1;
DelayMs(1);
}
/***************************************************************************/
void NoAck() //发送非应答位子程序
{
SDA=1;
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
}
/***************************************************************************/
bit TestAck() //应答位检查子程序
{
bit ErrorBit;
SDA=1;
DelayMs(1);
SCL=1;
DelayMs(1);
ErrorBit=SDA; //读入数据线上的应答状态
DelayMs(1);
SCL=0;
DelayMs(1);
return(ErrorBit); //返回应答状态,0为正常应答信号,1为非应答信号
}
/***************************************************************************/
bit Write8Bit(unsigned char input) //写一个字节数据子程序
{ //input为待发送的数据
unsigned char temp;
for(temp=8;temp!=0;temp--) //循环移位,逐位发送数据
{
SDA=(bit)(input&0x80); //取数据的最高位
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
input=input<<1; //左移一位
}
return 1;
}
/***************************************************************************/
void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
{ //写n个字节数据子程序
Start(); //启动
Write8Bit(WriteDeviceAddress); //写写器件的寻址地址
TestAck(); //应答检查
Write8Bit(RomAddress); //写入I2C器件内部的数据存储首地址
TestAck(); //应答检查
for(;number!=0;number--) //循环,逐个字节发送
{
Write8Bit(*Wdata); //写一个字节
TestAck(); //应答检查
Wdata++; //指针增加,指向下一个数据
}
Stop(); //停止
DelayMs(10);
}
/***************************************************************************/
unsigned char Read8Bit() //读一个字节数据子程序
{
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--) //循环,逐位读入字节数据
{
SCL=1;
DelayMs(1);
rbyte=rbyte<<1; //左移一位
DelayMs(1);
rbyte=rbyte|((unsigned char)(SDA)); //数据线SDA上的数据存入rbyte的最低位
SCL=0;
DelayMs(1);
}
return(rbyte); //返回读入的字节数据
}
/***************************************************************************/
void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)
{ //读n个字节数据子程序
Start(); //启动
Write8Bit(WriteDeviceAddress); //写写器件的寻址地址
TestAck(); //应答检查
Write8Bit(RomAddress); //写I2C器件内部数据的读取首地址
TestAck(); //应答检查
Start(); //重新启动
Write8Bit(ReadDviceAddress); //写读器件的寻址地址
TestAck(); //应答检查
while(bytes!=1) //循环读入字节数据
{
*RamAddress=Read8Bit(); //读入一个字节
Ack(); //应答
RamAddress++; //地址指针递增
bytes--; //待读入数据个数递减
}
*RamAddress=Read8Bit(); //读入最后一个字节数据
NoAck(); //非应答
Stop(); //停止
}
/* store the message & read it out at once to check out if right, the result of time show it */
unsigned char store(unsigned char *addw,unsigned char time)
{
unsigned char readByte[N]; //用于存读入的N个字节数据
unsigned char *addr;
unsigned int store_address; //读数据指针操作
addr = readByte; //读地址映射
store_address = time * N;
WriteI2C(addw,store_address,N); //写数据
ReadI2C(addr,store_address,N); //读数据
if(strcmp(recieve,readByte) == 0) ;
{
time ++;
}
return time;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -