📄 main.c
字号:
/*
main.c
CAT1021 EEPROM读写实验
*/
#include "I2C.h"
#include <reg51.h>
#include <string.h>
#include <stdlib.h>
//定义I/O端口
sbit SCL = P3^4;
sbit SDA = P3^5;
/*
函数:Delay()
功能:延时0.001~65.536s
参数:
t>0时,延时(t*0.001)s
t=0时,延时65.536s
*/
void Delay(unsigned int t)
{
do
{
TH0 = 0xFC;
TL0 = 0x66+17;
TR0 = 1;
while ( !TF0 );
TR0 = 0;
TF0 = 0;
} while ( --t != 0 );
}
/*
函数:Feed()
功能:清除CAT1021内部WDT定时器(喂狗)
说明:如果没有在1.6秒(典型值)内喂狗,则CAT1021会输出复位信号
*/
void Feed()
{
unsigned char t = 10;
I2C_SDA = 0;
while ( --t != 0 );
I2C_SDA = 1;
}
/*
函数:UartInit()
功能:串行口初始化
*/
void UartInit()
{
RXD = 1;
TXD = 1;
SCON = 0x50; //串口方式1(8位Uart),允许接收
PCON |= 0x80; //波特率加倍
TMOD &= 0x0F;
TMOD |= 0x20; //T1设置为8位自动重装定时器
TH1 = TL1 = 0xFA; //设置波特率为9600
TR1 = 1;
}
/*
函数:PutChar()
功能:从串行口输出字符c
*/
void PutChar(char c)
{
SBUF = c;
while ( !TI ) Feed();
TI = 0;
}
/*
函数:PutStr()
功能:从串行口输出字符串*s
*/
void PutStr(char *s)
{
unsigned char c;
for (;;)
{
c = *s++;
if ( c == '\0' ) break;
PutChar(c);
}
}
/*
函数:GetStr()
功能:从串行口接收一个字符串
参数:
*s:返回输入的字符串
n:最大长度限制
说明:输入的字符串以回车键结束
*/
void GetStr(char *s, unsigned char n)
{
unsigned char c;
for (;;)
{
while ( !RI ) Feed();
c = SBUF;
RI = 0;
if ( c == '\n' ) break;
if ( c == '\0' ) break;
if ( (n != 0) && (c >= ' ') )
{
*s++ = c;
n--;
}
}
*s = '\0';
}
/*
函数:SysInit()
功能:系统初始化
*/
void SysInit()
{
unsigned char c;
I2C_Init();
TMOD &= 0xF0;
TMOD |= 0x01;
UartInit();
Delay(300); //上电后必须先延时200ms以上才能对CAT1021进行操作
if ( I2C_Get(0xA0,0,&c) )
{
PutStr("\r\nCAT1021 not found!");
for (;;)
{
Feed();
Delay(800);
}
}
}
/*
函数:BytetoHex()
功能:字节c转换成16进制字符串
*/
void ByteToHex(char *s, unsigned char c)
{
code unsigned char Tab[] = "0123456789ABCDEF";
*s++ = Tab[c / 16];
*s++ = Tab[c & 0x0F];
*s = '\0';
}
/*
函数:Read()
功能:读取CAT1021内部EEPROM的数据
参数:
addr:起始地址,取值0~255
len:读取的数据长度,取值1~256
*/
void Read(unsigned char addr, unsigned char len)
{
unsigned char c;
unsigned char s[3];
PutStr("addr data ASCII\r\n");
do
{
ByteToHex(s,addr);
PutStr(s);
PutStr(" ");
I2C_Get(0xA0,addr++,&c);
ByteToHex(s,c);
PutStr(s);
if ( (c > ' ') && ( c != 0xFF ) )
{
PutStr(" ");
PutChar(c);
}
PutStr("\r\n");
} while ( --len != 0 );
}
/*
函数:Write()
功能:向CAT1021内部的EEPROM写入一个字符串
参数:
addr:起始地址,取值0~255
*s:输入的字符串
*/
void Write(unsigned char addr, char *s)
{
unsigned char c;
for (;;)
{
c = *s++;
I2C_Put(0xA0,addr++,c);
Delay(5);
if ( c == '\0' ) break;
}
}
/*
函数:Input()
功能:从串行口输入一个字符串
参数:
*s1:输入前显示的提示语
*s2:保存输入的字符串
len:最大长度限制
*/
void Input(char *s1, char *s2, unsigned char len)
{
PutStr(s1);
GetStr(s2,len);
PutStr(s2);
PutStr("\r\n");
}
void main()
{
unsigned char addr;
unsigned char len;
char s[41];
SysInit();
for (;;)
{
PutStr("\r\n");
Input("cmd>",s,40);
if ( !strcmp(s,"read") )
{
Input("address = ",s,40);
addr = atoi(s);
Input("length = ",s,40);
len = atoi(s);
Read(addr,len);
}
else if ( !strcmp(s,"write") )
{
Input("address = ",s,40);
addr = atoi(s);
Input("string = ",s,40);
Write(addr,s);
}
else
{
PutStr("error!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -