TQ2440 学习笔记—— 24、IIC 接口

(韦东山——嵌入式Linux 应用开发完全手册) 

IIC 接口

IIC (Inter-Integrated Circuit)总线是一种由PHILIPS 公司开发的串行总线,用于连接微控制器及其外围设备,它具有如下特点:

1、只有两条总线线路:一条串行数据线(SDA),一条串行时钟线(SCL)。

2、每个连接到总线的器件都可以使用软件根据它的唯一的地址来识别。

3、传输数据的设备间是简单的主从关系。

4、主机可以用主机发送器或主机接收器。

5、它是一个真正的多主机总线,两个或多个主机同时发起数据传输时,可以通过冲突检验和仲裁来防止数据被破坏。

6、串行的 8 位双向数据传输,位速率在标准模式下可达100 kbit/s,在快速模式下可达 400 kbit/s,在高速模式下可达3.4Mbit/s。

7、片上的滤波器可以增强抗干扰功能,保证数据的完整。

8、连接到同一总线上的 IC 数量只受总线的最大电容400 pF 的限制。


IIC 总线术语的定义




I2C在Linux中是Bus下的一个子系统. 它由客户驱动(client driver),i2c-core核心,i2c适配器驱动(adapter driver) ,算法aglorithm组成。s3c2440中有两个i2c现适配器.作为platform_device设备在系统启动先时被注册和添加。下面我们分析i2c(设备,驱动,总线)的实现过程.

//填充设备资源
//struct resource结构体描述了挂接在cpu总线上的设备实体资源
//.start:i2c寄存器起始地址; .end:i2c寄存器结束地址; .flag:描述设备实体的共性和特性标志
[cpp]  view plain copy
  1. static struct resource s3c_i2c_resource[] = {  
  2. [0] = { //i2c-0  
  3. .start = S3C_PA_IIC,  
  4. .end   = S3C_PA_IIC + SZ_4K - 1,  
  5. .flags = IORESOURCE_MEM,  
  6. },  
  7. [1] = { //i2c-1   
  8. .start = IRQ_IIC,  
  9. .end   = IRQ_IIC,  
  10. .flags = IORESOURCE_IRQ,  
  11. },  
  12. };  
/i2c适配器初始化时数据
[cpp]  view plain copy
  1. static struct s3c2410_platform_i2c default_i2c_data0 __initdata = {  
  2. .flags = 0,  
  3. .slave_addr = 0x10,  
  4. .frequency = 100*1000,  
  5. .sda_delay = 100,   
  6. };  

//声明i2c适配器为platform_device

[cpp]  view plain
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
24C04是一种IIC接口的串行EEPROM芯片,其容量为4Kbit,即512字节。在C语言中,可以使用以下代码示例来实现对24C04芯片的读写操作。 读取数据: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> int main() { int device; char *filename = "/dev/i2c-1"; // IIC设备文件路径,根据实际情况修改 int address = 0x50; // 24C04芯片的IIC地址,根据硬件连接情况修改 unsigned char buffer[2]; // 打开IIC设备 if ((device = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus"); return 1; } // 设置IIC设备的从设备地址 if (ioctl(device, I2C_SLAVE, address) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; } // 读取数据 if (read(device, buffer, sizeof(buffer)) != sizeof(buffer)) { perror("Failed to read from the i2c bus"); return 1; } // 打印读取的数据 printf("Read data: 0x%X, 0x%X\n", buffer[0], buffer[1]); close(device); return 0; } ``` 写入数据: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> int main() { int device; char *filename = "/dev/i2c-1"; // IIC设备文件路径,根据实际情况修改 int address = 0x50; // 24C04芯片的IIC地址,根据硬件连接情况修改 unsigned char buffer[2] = {0x01, 0x02}; // 要写入的数据,根据实际情况修改 // 打开IIC设备 if ((device = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus"); return 1; } // 设置IIC设备的从设备地址 if (ioctl(device, I2C_SLAVE, address) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; } // 写入数据 if (write(device, buffer, sizeof(buffer)) != sizeof(buffer)) { perror("Failed to write to the i2c bus"); return 1; } printf("Data written successfully.\n"); close(device); return 0; } ``` 这两个示例代码可以通过打开IIC设备文件,设置从设备地址,使用`read`和`write`函数来进行读写操作。注意,程序中的`filename`和`address`需要根据实际的硬件连接情况进行修改。另外,`buffer`数组是需要读取或写入的数据,也需要根据实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值