STC单片机上用TLV320ADC3101请写出i2c驱动
时间: 2024-06-13 11:04:53 浏览: 21
```c
#include <reg52.h>
#include <intrins.h>
#define SDA P1_1
#define SCL P1_0
void delay_us(unsigned int us)
{
while(us--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void i2c_start()
{
SDA = 1;
SCL = 1;
delay_us(5);
SDA = 0;
delay_us(5);
SCL = 0;
}
void i2c_stop()
{
SDA = 0;
SCL = 1;
delay_us(5);
SDA = 1;
delay_us(5);
}
void i2c_send_byte(unsigned char dat)
{
unsigned char i;
for(i=0; i<8; i++)
{
SDA = dat & 0x80;
SCL = 1;
delay_us(5);
SCL = 0;
dat <<= 1;
}
SDA = 1;
SCL = 1;
delay_us(5);
SCL = 0;
}
unsigned char i2c_read_byte()
{
unsigned char i, dat = 0;
SDA = 1;
for(i=0; i<8; i++)
{
SCL = 1;
delay_us(5);
dat <<= 1;
dat |= SDA;
SCL = 0;
}
return dat;
}
void i2c_ack()
{
SDA = 0;
SCL = 1;
delay_us(5);
SCL = 0;
SDA = 1;
}
void i2c_nack()
{
SDA = 1;
SCL = 1;
delay_us(5);
SCL = 0;
}
void i2c_write_reg(unsigned char addr, unsigned char reg, unsigned char dat)
{
i2c_start();
i2c_send_byte(addr);
i2c_ack();
i2c_send_byte(reg);
i2c_ack();
i2c_send_byte(dat);
i2c_ack();
i2c_stop();
}
unsigned char i2c_read_reg(unsigned char addr, unsigned char reg)
{
unsigned char dat;
i2c_start();
i2c_send_byte(addr);
i2c_ack();
i2c_send_byte(reg);
i2c_ack();
i2c_start();
i2c_send_byte(addr | 0x01);
i2c_ack();
dat = i2c_read_byte();
i2c_nack();
i2c_stop();
return dat;
}
```
以上是一个简单的I2C驱动程序,可以用于STC单片机上的TLV320ADC3101。在使用时,需要根据具体的硬件连接情况修改SDA和SCL的引脚定义。同时,需要根据TLV320ADC3101的寄存器地址和寄存器值,调用i2c_write_reg和i2c_read_reg函数来进行寄存器的写入和读取。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)