?? write_read.c
字號:
#include "reg52.h"
// #include "def.h"
#include "absacc.h"
#include "eep16.h"
#define uchar unsigned char
sbit a16sda=P1^6;
sbit a16scl=P1^7;
#define CAT24C16Slave 0xA0
/****************************************\
|微秒延時 |
\****************************************/
void delaytime_10us(uchar times)
{
int i;
do{
for(i=0;i<5;i++);}
while(times--);
}
/********************************************************\
| 函數名 AT24C16Start |
| 函數功能 向I2C總線發送一個開始信號 |
\********************************************************/
void CAT24C16Start(void)
{
a16sda=1;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
a16sda=0;
delaytime_10us(1);
a16scl=0 ;
delaytime_10us(1);
}
/********************************************************\
| 函數名 AT24C16Stop |
| 函數功能 向I2C總線發送一個停止信號 |
\********************************************************/
void CAT24C16Stop(void)
{
a16sda=0;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
a16sda=1;
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
}
/********************************************************\
| 函數名 AT24C02Clock |
| 函數功能 從I2C總線接受一BIT數據 |
| 參數 無 |
| 返回值 unsigned char :從I2C總線接受的數據 |
\********************************************************/
unsigned char CAT24C16Clock(void)
{
unsigned char SDA_value;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
SDA_value=a16sda ;
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
return(SDA_value);
}
/********************************************************\
| 函數名 AT24C02Ack |
| 函數功能 向I2C總線發送一個數據接受完成信號 |
\********************************************************/
void CAT24C16Ack()
{
a16scl=1;
delaytime_10us(1);
a16sda=0;
delaytime_10us(1);
a16scl=0;
}
/*******************************************************\
| 函數名 AT24C02Nack |
| 函數功能 從器件應答信號是否產生 |
\*******************************************************/
bit CAT24C16Nack()
{
a16sda=1;
if (CAT24C16Clock()==0)
{
return 1;
}
else
{
return 0;
}
}
/*******************************************************\
| 函數名 AT24C16OutByte |
| 函數功能 向I2C總線發送一個字節的數據 |
\*******************************************************/
void CAT24C16OutByte(uchar byte)
{
char count;
for (count=0; count<8; count++)
{
if ((byte & 0x80)==0)
{
a16sda=0;
}
else
{
a16sda=1;
}
byte <<=1;
a16scl=1;
delaytime_10us(1);
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
}
}
/*******************************************************\
| 函數名 AT24C16GetByte |
| 函數功能 從I2C總線接受一個字節的數據 |
\*******************************************************/
uchar CAT24C16GetByte()
{
uchar byte=0,temp,count;
for (count=0; count<8; count++)
{
byte <<=1;
a16sda=1;
temp = CAT24C16Clock();
if (temp == 1)
{
byte=byte|0x01;
}
}
return(byte);
}
/*******************************************************\
| 函數名 CAT24C16DummyWrite |
| 函數功能 向I2C總線發送開始信號和從地址 |
\*******************************************************/
bit CAT24C16DummyWrite(uchar addr)
{
CAT24C16Start();
CAT24C16OutByte(CAT24C16Slave);
if (CAT24C16Nack()==0)
{
return 0;
}
CAT24C16OutByte(addr);
if (CAT24C16Nack()==0)
{
return 0;
}
return 1;
}
/*******************************************************\
| 函數名 AT24C02Read |
| 函數功能 從AT24C02讀取數據 |
| 參數 data : 讀取數據的緩沖區 |
| addr : 準備讀去的AT24C02扁移地址 |
| leng : 準備讀去的數據長度(字節) |
| 返回值 0 : 讀數據成功 |
| 1 : 讀數據失敗 |
\*******************************************************/
char CAT24C16Read(uchar * _data,uchar addr,uchar leng)
{
unsigned char n;
if (CAT24C16DummyWrite(addr)==0)
{
return 0;
}
CAT24C16Start();
CAT24C16OutByte(CAT24C16Slave|0x01);
if (CAT24C16Nack()==0)
{
return 0;
}
for (n=0;n<leng-1; n++)
{
_data[n] = CAT24C16GetByte();
CAT24C16Ack();
}
_data[leng - 1]=CAT24C16GetByte();
CAT24C16Stop();
delaytime_10us(20);
return 1;
}
/*******************************************************\
| 函數名 AT24C02Write |
| 函數功能 從AT24C02讀取數據 |
| 參數 data : 待寫數據的緩沖區 |
| addr : 待寫的AT24C02扁移地址 |
| leng : 待寫數據長度(字節
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -