?? iic.c.bak
字號:
#include <reg52.h>
#include "delay.h"
#include "iic.h"
/*啟動IIC設備*/
void start(void)
{
DelayMs(5);
SDA = 1 ;
Delay(5);
SCL = 1 ;
Delay(5);
SDA = 0 ;
Delay(5);
SCL = 0 ;
Delay(5);
}
/*停止IIC設備*/
void stop(void)
{
Delay(5);
SCL = 0 ;
Delay(5);
SDA = 0 ;
Delay(5);
SCL = 1 ;
Delay(5);
SDA = 1 ;
Delay(5);
DelayMs(15);
}
/*檢測ack*/
bit testack(void)
{
bit AckFlag;
Delay(5);
SDA = 1;
Delay(5);
SCL = 1;
Delay(5);
AckFlag = SDA;
SCL = 0;
Delay(5);
return(AckFlag);
}
/*發送應答信號*/
void sendack(void)
{
Delay(5);
SDA = 0;
Delay(5);
SCL = 1;
Delay(5);
SCL = 0;
Delay(5);
SDA = 1;
Delay(5);
}
/*發送非應答信號*/
void sendNack(void)
{
Delay(5);
SDA = 1 ;
Delay(5);
SCL = 1 ;
Delay(5);
SCL = 0 ;
Delay(5);
}
/*寫1個字節的數據*/
void write8bit(unsigned char T_Data)
{
unsigned char i ;
for(i = 8; i != 0; i--)
{
SDA = (bit)(T_Data & 0x80);
Delay(5);
SCL = 1 ;
Delay(5);
SCL = 0 ;
Delay(5);
T_Data = T_Data << 1 ;
Delay(5);
}
}
/*讀1個字節的數據*/
unsigned char read8bit(void)
{
unsigned char i = 0 ;
unsigned char R_Data = 0x00 ;
for(i = 8; i != 0; i--)
{
SDA = 1;
Delay(5);
SCL = 0 ;
Delay(5);
Delay(5);
SCL = 1 ;
R_Data = R_Data << 1 ;
R_Data = R_Data |((unsigned char)(SDA)) ;
SCL = 0 ;
Delay(5);
}
return(R_Data);
}
/*寫1個字節的數據到指定的地址*/
void write_I2C(unsigned char RomAddress,unsigned char T_Data)
{
start() ;
write8bit(WrDevAddr) ;
testack() ;
write8bit(RomAddress) ;
testack() ;
write8bit(T_Data) ;
testack() ;
stop() ;
}
/*讀1個字節的數據*/
unsigned char read_I2C(unsigned char RomAddress)
{
unsigned char R_Data = 0x00 ;
start() ;
write8bit(WrDevAddr) ;
testack() ;
write8bit(RomAddress) ;
testack() ;
start() ;
write8bit(RdDevAddr) ;
testack() ;
R_Data = read8bit() ;
sendNack() ;
stop() ;
return(R_Data) ;
}
/*指定的地址寫頁-8字節數據*/
void WriteOnePage (unsigned char addr,unsigned char *thedata) //寫入一頁8個字節到指定開始地址,可以自動翻頁
{
unsigned char i; //計數器
start(); //開始總線
write8bit(WrDevAddr); //發送控制數據
testack(); //檢查應答信息
write8bit(addr); //寫入地址
testack();
for(i=0;i<8;i++) //循環寫入第一頁的數據
{
write8bit(*thedata); //寫入數據
testack();
thedata++; //數據指針加1
}
stop(); //停止總線
}
/*指定的地址寫N(N<=8)字節數據*/
void WriteNByte (unsigned char addr,unsigned char *thedata,unsigned char n) //寫入N個字到指定開始地址
{
unsigned char i; //計數器
start(); //開始總線
write8bit(WrDevAddr); //發送控制數據
testack(); //檢查應答信息
write8bit(addr); //寫入地址
testack();
for(i=0;i<n;i++) //循環寫入第一頁的數據
{
write8bit(*thedata); //寫入數據
testack();
thedata++; //數據指針加1
}
stop(); //停止總線
}
/*指定地址,頁寫n個數據*/
void WritePages(unsigned char addr,unsigned char *thedata,unsigned char n)
{
unsigned char page=n/8;
unsigned char leave=n%8;
unsigned char i;
for(i=0;i<page;i++)
{
WriteOnePage((addr+8*i),thedata);
thedata=thedata+8;
}
WriteNByte((addr+8*page),thedata,leave);
}
/*指定地址,讀取8字節,頁讀*/
void ReadoOnePage(unsigned char addr,unsigned char *thedata) //連續讀取8個字節 頁
{
unsigned char i;
start();
write8bit(WrDevAddr);
testack();
write8bit(addr);
testack();
start();
write8bit(RdDevAddr) ;
testack();
for(i=0;i<7;i++) //8字節
{
*thedata=read8bit(); //讀取數據
sendack(); //發送應答,表示繼續讀取
thedata++; //數據指針加1
}
*thedata=read8bit(); //讀取最后一個數據
sendNack(); //發送非應答,結束讀取
stop(); //停止總線
}
/*連續讀取N個字節*/
void ReadNByte (unsigned char addr,unsigned char *thedata,unsigned char n)
{
unsigned char i;
start();
write8bit(WrDevAddr);
testack();
write8bit(addr);
testack();
start();
write8bit(RdDevAddr) ;
testack();
for(i=0;i<n-1;i++) //8字節
{
*thedata=read8bit(); //讀取數據
sendack(); //發送應答,表示繼續讀取
thedata++; //數據指針加1
}
*thedata=read8bit(); //讀取最后一個數據
sendNack(); //發送非應答,結束讀取
stop();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -