📄 i2c.c
字号:
/****************************************************************************
* *
* I2C Driving Routines *
* *
****************************************************************************/
#include "define.h"
#include "global.h"
#include <regX52.H>
#include <intrins.h>
void I2C_BitDelay(void)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
/****************************************************************************
* Name: Send_I2C_Start
* Description: This function assumes that SCCLK and SDA are both high upon entrance,
and generates the I2C start condition required for initiating
communication
Return value: NONE
****************************************************************************/
void Send_I2C_Start(void)
{
PIN_SDA=1;I2C_BitDelay(); PIN_SCL=1;
I2C_BitDelay();
PIN_SDA=0; // drive SDA low while SCCLK is high for I2C start
I2C_BitDelay();
PIN_SCL=0; // drive SCCLK low to prepare for data transfer
I2C_BitDelay();
}
/****************************************************************************
* Name: Send_I2C_Stop
* Description: This function generates the I2C stop condition required for
proper termination of I2C communication
Return value: NONE
****************************************************************************/
void Send_I2C_Stop(void)
{
PIN_SDA=0; //ensure that SDA is low
I2C_BitDelay();
PIN_SCL=1; //drive SCCLK high to prepare for I2C stop
I2C_BitDelay();
PIN_SDA=1; //SDA rising edge while SCCLK high = I2C stop
I2C_BitDelay();
}
/***************************************************************************/
//法送确认信号
void SendAcknowledge(bit ack)
{
PIN_SDA=ack;
I2C_BitDelay();
PIN_SCL=1;
I2C_BitDelay();
PIN_SCL=0;
}
/****************************************************************************
* Name: Write_I2C_Byte
* Description:
This function will write one byte through the parallel port in I2C serial
communication format. The SCCLK is assumed LOW upon entering this function,
and is left LOW upon exiting.
****************************************************************************/
bit Write_I2C_Byte(uchar mybyte){
register uchar i,t; // bit counter
bit ack;
for (i=0;i<8;i++){
if(mybyte & 0x80)
{PIN_SDA=1;P2_3=0;}
else {PIN_SDA=0;P2_3=1;}
I2C_BitDelay();
PIN_SCL=1; // clock out
for(t=0;t<12;t++)
{
I2C_BitDelay();
}
P2_2=0;
for(t=0;t<1;t++)
{
I2C_BitDelay();
}
P2_2=1;
for(t=0;t<12;t++)
{
I2C_BitDelay();
}
PIN_SCL=0;
mybyte<<=1;
}
PIN_SDA=1; // release SDA for ACK
I2C_BitDelay();
// ************* to get ACK ***************
PIN_SCL=1;P2_2=0;
I2C_BitDelay();
P2_2=1;
PIN_SCL=0;
I2C_BitDelay();if(PIN_SDA==0)ack=0;else ack=1;if(ack==1)P2_3=0;
return ack; // Received ACK, return 0
}
/****************************************************************************
; Name: Read_I2C_Byte
; Description:
; This function will read one byte through I2C serial
; communication format. The SCCLK is assumed LOW upon entering this function,
; and is left LOW upon exiting.
;****************************************************************************/
uchar Read_I2C_Byte(void){
register uchar i;
uchar Byte;
for(i=0;i<8;i++){
PIN_SCL=1;
I2C_BitDelay();
Byte<<=1;
if(PIN_SDA)Byte=Byte | 0x01;
I2C_BitDelay();
PIN_SCL=0;
I2C_BitDelay();
}
return Byte;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -