I2C驱动实验:编写I2C测试程序

发布于:2024-04-07 ⋅ 阅读:(109) ⋅ 点赞:(0)

一. 简介

经过前面几篇文章的学习,完成了I2C设备驱动代码的实现。这里I2C设备是 AP3216C设备,文章如下:

I2C驱动实验:读取AP3216C设备中寄存器的数据-CSDN博客

AP3216C设备是三合一设备,测试红外光(ir),接近(ps),环境光感应(als),本文通过运行应用程序,对 I2C设备驱动进行测试。测试是否可以正常读取到AP3216C设备的数据。

二. 编写应用程序

打开 17_i2c工程,应用程序为 ap3216c_app.c文件,编写完成后ap3216c_app.c文件中内容如下:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

/*
* 打开/关闭 Led灯
* 参数:
* ./ap3216c_app /dev/ap3216c  
*/
int main(int argc, char* argv[])
{
    int fd = 0,ret = 0;
    char * device_name = NULL;
    unsigned short data[3] = {0};
    unsigned short ir,ps,als;

    if(argc != 2)
    {
        printf("main's param number error!\n");
        return -1;
    }

    device_name = argv[1];
    fd = open(device_name, O_RDWR);
    if(fd < 0)
    {
        printf("open led device failed!\n");
        return -1;
    }
    
    while(1)
    {
        ret = read(fd, data, sizeof(data));
        ir = data[0];
        ps = data[1];
        als = data[2];
        printf("ir:%d  ps:%d  als:%d\n", ir,ps,als);
        sleep(1);
    }
    close(fd);
    return 0;
}

编译应用程序:

wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/17_i2c$ arm-linux-gnueabihf-gcc ap3216c_app.c -o ap3216c_app

编译完成后,生成了 应用程序 ap3216c_app。

接下来运行应用程序, 对驱动功能进行测试。


网站公告

今日签到

点亮在社区的每一天
去签到