?? 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 + -