📄 h42.c
字号:
/* 实验四十二 IIC总线24C02读写 */
/* C51 */
#include <reg51.h>
#include <intrins.h>
/* **********************************************************
24C01/02 为IIC总线EEPROM存储器, 容量为1k位(128 * 8)
************************************************************/
/* **********************************************************
EEPROM控制字节格式:
[1, 0, 1, 0, A2, A1, A0, (R/W)], 其中R/W=1读,R/W=0写;
由于实验仪上的AT24C01A(AT24C02) 的A2/A1/A0全部接地,
所以读/写控制字分别为:0xa1/0xa0
************************************************************/
#define WriteDeviceAddress 0xa0
#define ReadDeviceAddress 0xa1
/******************IIC器件驱动引脚定义**********************/
sbit SCL = P1^0;
sbit SDA = P1^1;
/***********************简单延时****************************/
void DelayMs(unsigned int number) {
unsigned char temp = 112;
while(number--!=0) {
while(temp--!=0) {
}
}
}
/*********************启动IIC总线***************************/
void Start() {
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
/*********************停止IIC总线***************************/
void Stop() {
SCL = 0;
SDA = 0;
SCL = 1;
SDA = 1;
}
/***********************请求相应****************************/
void Ack() {
SDA = 0;
SCL = 1;
SCL = 0;
SDA = 1;
}
/*******************不对IIC总线产生应答*********************/
void NoAck() {
SDA = 1;
SCL = 1;
SCL = 0;
}
/**********************检查应答位***************************/
bit TestAck() {
SDA = 1;
SCL = 1;
CY = SDA;
SCL = 0;
return(CY);
}
/********************向IIC总线写数据************************/
bit Write8Bit(unsigned char input) {
unsigned char temp;
for(temp = 8; temp != 0; temp--) {
SDA = (bit)(input&0x80);
SCL = 1;
SCL = 0;
input = input<<1;
}
return(0);
}
/****************从IIC总线上读数据子程序********************/
unsigned char Read8Bit() {
unsigned char temp, rbyte=0;
for(temp=8;temp!=0;temp--) {
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
/*******************向EEPROM中写入数据块********************/
void AT24C02WriteBlock(unsigned char *Wdata, unsigned char RomAddress, unsigned char number) {
if(number > 8)
number %= 8; /*对于24C02, 一个页为8字节,所以最大的块写操作字节数为8*/
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
for(;number!=0;number--) {
Write8Bit(*Wdata);
TestAck();
Wdata++;
}
Stop();
DelayMs(10);
}
/**************从EEPROM中读出数据块到指定RAM中**************/
void AT24C02ReadBlock(unsigned char *RamAddress, unsigned char RomAddress, unsigned char bytes) {
EA = 0;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDeviceAddress);
TestAck();
while(bytes!=1) {
*RamAddress = Read8Bit();
Ack();
RamAddress++;
bytes--;
}
*RamAddress = Read8Bit();
NoAck();
Stop();
}
/*****************向EEPROM中写入单字节数据******************/
void AT24c02WriteByte(unsigned char WriteData, unsigned char RomAddress) {
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Write8Bit(WriteData);
TestAck();
Stop();
DelayMs(10);
}
/************从EEPROM中读出单字节数据到指定RAM中************/
unsigned char AT24c02ReadByte(unsigned char RomAddress) {
unsigned char ReadData;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDeviceAddress);
TestAck();
ReadData = Read8Bit();
NoAck();
Stop();
return(ReadData);
}
void main(void)
{
unsigned char i;
unsigned char WriteBuff[8], ReadBuff[8];
/* 读写缓冲初始化 */
for(i = 0; i < 8; i++) {
WriteBuff[i] = 0x55 + i;
ReadBuff[i] = 0xff;
}
/* 从地址0开始按字节方式写入8个数据'0' */
for(i = 0; i < 8; i++) {
AT24c02WriteByte(0, i);
}
/* 按字节方式读出数据 */
for(i = 0; i < 8; i++) {
ReadBuff[i] = AT24c02ReadByte(i);
}
/* 按写Page方式从地址0开始写入WriteBuff指向的8个数据 */
AT24C02WriteBlock(WriteBuff, 0x00, 8);
/* 按连续读取方式读出从地址0开始的8个数据 */
AT24C02ReadBlock(ReadBuff, 0x00, 8);
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -