📄 i2c.c
字号:
/*
********************************************************************************
*
* (c) Copyright 2002, Vineyard Technologies, Inc.
*
* Filename : i2c.c
* Programmer(s): Steve KyeongHyeon Lee
* Created : 2003/05/07
* Modified :
*
* Description :
********************************************************************************
*/
#include "types.h"
#include "8052reg.h"
#include "gio.h"
#include "i2c.h"
// try to make I2C clock around 100KHz when using 40MHz MCU clock
//------------------------------------------------------------------------------
// I2C Peripheral Function Prototypes
//------------------------------------------------------------------------------
void i2c_delay(void)
{
idata u8 m=1;
return;
}
void i2c_delay2(u8 delay_duration)
{
xdata u8 i;
for(i=0;i<delay_duration;i++);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void i2c_pkt_write(u8 ipw_channel, u8 ipw_regidx, u8 ipw_regval)
{
i2c_start();
i2c_write(ipw_channel);
i2c_write(ipw_regidx);
i2c_write(ipw_regval);
i2c_stop();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
u8 i2c_pkt_read(u8 ipr_channel, u8 ipr_regidx)
{
xdata u8 ipr_data_in;
i2c_start();
i2c_write(ipr_channel);
i2c_write(ipr_regidx);
i2c_stop();
i2c_start();
i2c_write(ipr_channel+1);
ipr_data_in = i2c_read();
i2c_stop();
return ipr_data_in;
}
//------------------------------------------------------------------------------
// I2C Functions - Bit Banged
//------------------------------------------------------------------------------
void i2c_start(void)
{
SDATA = HIGH; // Set data line high
i2c_delay(); //(10);
SCLK = HIGH; // Set clock line high
i2c_delay(); //(10);
SDATA = LOW; // Set data line low (START SIGNAL)
i2c_delay(); //(4);
SCLK = LOW; // Set clock line low
i2c_delay(); //(4);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void i2c_stop (void)
{
xdata u8 is_input_var;
SCLK = LOW; // Set clock line low .. need more delay
i2c_delay2(1);
SDATA = LOW; // Set data line low
i2c_delay(); //(4);
SCLK = HIGH; // Set clock line high
i2c_delay(); //(4);
SDATA = HIGH; // Set data line high (STOP SIGNAL)
is_input_var = SDATA; // Put port pin into HiZ
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void i2c_write (u8 iw_output_data)
{
u8 iw_index;
for(iw_index = 0; iw_index < 8; iw_index++) // Send 8 bits to the I2C Bus
{
SDATA = ((iw_output_data & 0x80) ? 1 : 0);
iw_output_data <<= 1; // Shift the byte by one bit
i2c_delay(); //(3);
SCLK = HIGH; // Clock the data into the I2C Bus
i2c_delay(); //(4);
SCLK = LOW;
i2c_delay(); //(2);
}
// need more delay
i2c_delay2(1);
iw_index = SDATA; // Put data pin into read mode1
SCLK = HIGH; // Clock the ACK from the I2C Bus
i2c_delay(); //(7);
SCLK = LOW;
i2c_delay(); //(4);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
u8 i2c_read (void)
{
u8 ir_index;
u8 ir_input_data;
ir_index = SDATA; // Put data pin into read mode
ir_input_data = 0x00;
for(ir_index = 0; ir_index < 8; ir_index++) // Send 8 bits to the I2C Bus
{
ir_input_data <<= 1; // Shift the byte by one bit
ir_input_data |= SDATA; // Input the data from the I2C Bus
SCLK = HIGH; // Clock the data into the I2C Bus
i2c_delay(); //(4);
SCLK = LOW;
i2c_delay(); //(3);
}
// need more delay
i2c_delay2(1);
ir_index = SDATA; // Put data pin into read mode1
SCLK = HIGH; // Clock the ACK from the I2C Bus
i2c_delay(); //(7);
SCLK = LOW;
i2c_delay(); //(4);
return ir_input_data;
}
u8 I2C_RxByte(u8 bSLA, u8 bREG)// 20051020 add for BIT1601
{
xdata u8 ipr_data_in;
i2c_start();
i2c_write(bSLA);
i2c_write(bREG);
i2c_start();
i2c_write(bSLA | 0x01);
ipr_data_in = i2c_read();
i2c_stop();
return ipr_data_in;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -