📄 i2csample.c
字号:
/******************************************************************************************
* Functions
*******************************************************************************************/
void i2c_init(void)
{
unsigned int value;
value = TLG1100_SDA| TLG1100_SCL /* | BUSENABLE*/;
*(volatile unsigned int *)(BASE_GPIO_TIMER+GPIO_DIR) |= value;
*(volatile unsigned int *)(BASE_GPIO_TIMER+GPIO_OUT) |= value;
}
static void i2c_delay(unsigned int time)
{
while(time--)
{
;
}
}
static void set_i2c_pin_dir(unsigned char Dir)
{
if(Dir == TLG_IIC_OUTPUT)
{
//set pin as output
}
else
{
//set pin as input
}
}
static void set_i2c_pin(unsigned int pin)
{
// set pin high
//*(volatile unsigned int *)(BASE_GPIO_TIMER+GPIO_OUT) |= pin;
}
static void clr_i2c_pin(unsigned int pin)
{
// set pin low
*(volatile unsigned int *)(BASE_GPIO_TIMER+GPIO_OUT) &= (~pin);
}
static unsigned char get_i2c_pin(unsigned int pin)
{
unsigned char ret;
unsigned int value;
//read gpio
value = *(volatile unsigned int *)(BASE_GPIO_TIMER+GPIO_IN);
if ((value & pin) == 0)
ret = 0;
else
ret = 1;
return ret;
}
static void i2c_begin(void)
{
set_i2c_pin_dir(TLG_IIC_OUTPUT);//set gpio for output
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SDA);
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SDA);
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
}
static void i2c_end(void)
{
set_i2c_pin_dir(TLG_IIC_OUTPUT);//set gpio for output
i2c_delay(I2C_DELAY_UNIT << 2);
clr_i2c_pin(TLG1100_SDA);
i2c_delay(I2C_DELAY_UNIT << 2);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 3);
set_i2c_pin(TLG1100_SDA);
i2c_delay(I2C_DELAY_UNIT << 4);
}
static void i2c_write_ask(unsigned char flag)
{
set_i2c_pin_dir(TLG_IIC_OUTPUT);//set gpio for output
if(flag)
set_i2c_pin(TLG1100_SDA);
else
clr_i2c_pin(TLG1100_SDA);
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
}
static unsigned char i2c_read_ack(void)
{
unsigned char ret;
set_i2c_pin_dir(TLG_IIC_INPUT);//set gpio for input
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
if (!get_i2c_pin(TLG1100_SDA))
{
ret = 1;
}
else
{
ret = 0;
}
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
return ret;
}
static unsigned char i2c_read_byte(void)
{
unsigned char i;
unsigned char ret;
ret = 0;
set_i2c_pin_dir(TLG_IIC_INPUT);//set gpio for input
for (i=0; i<8; i++)
{
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
ret = ret<<1;
if (get_i2c_pin(TLG1100_SDA))
ret |= 1;
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SCL);
}
return ret;
}
static unsigned char i2c_write_byte(unsigned char data)
{
unsigned char i;
set_i2c_pin_dir(TLG_IIC_OUTPUT);//set gpio for output
for (i=0; i<8; i++)
{
i2c_delay(I2C_DELAY_UNIT << 0);
if (data & 0x80)
set_i2c_pin(TLG1100_SDA);
else
clr_i2c_pin(TLG1100_SDA);
data <<= 1;
i2c_delay(I2C_DELAY_UNIT << 0);
set_i2c_pin(TLG1100_SCL);
i2c_delay(I2C_DELAY_UNIT << 0);
clr_i2c_pin(TLG1100_SCL);
}
return i2c_read_ack();
}
/***************************************************************************
*
*
* IIC Write data for Tlg1100
* dab:device Id =0x2c
* rab: register address
* data: dump into register
*
****************************************************************************/
unsigned char I2C_WriteReg(unsigned char dab, unsigned short rab, unsigned short data)
{
unsigned char tmpData = 0;
i2c_begin();
if (!i2c_write_byte(dab<<1))
{
i2c_end();
return 0;
}
/* write 16bits register */
tmpData = ((rab & 0x7f00) >> 8);
if (!i2c_write_byte(tmpData))
{
i2c_end();
return 0;
}
tmpData = (rab & 0x00ff);
if (!i2c_write_byte(tmpData))
{
i2c_end();
return 0;
}
/* write 16bits data */
tmpData = ((data& 0xff00) >> 8);
if (!i2c_write_byte(tmpData))
{
i2c_end();
return 0;
}
tmpData = ((data) & 0x00ff);
if (!i2c_write_byte(tmpData))
{
i2c_end();
return 0;
}
i2c_end();
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -