怎样构建自己的网站,万网 网站建设方案书,wordpress静态生成页面,营销型网站建设规划书在嵌入式开发中#xff0c;I2C#xff08;Inter-Integrated Circuit#xff09;是一种常用的串行通信协议#xff0c;广泛应用于与外设#xff08;如 EEPROM、传感器、显示屏等#xff09;进行数据交换。AT24C02 是一种常见的 I2C EEPROM 存储器#xff0c;它提供 2Kbit… 在嵌入式开发中I2CInter-Integrated Circuit是一种常用的串行通信协议广泛应用于与外设如 EEPROM、传感器、显示屏等进行数据交换。AT24C02 是一种常见的 I2C EEPROM 存储器它提供 2Kbit 的存储空间适合存储少量数据如配置参数或校准数据等。通过编写 I2C 驱动程序和应用程序APP我们可以方便地与 AT24C02 EEPROM 进行读写操作。 本指南将介绍如何开发一个 I2C 驱动编写应用程序来访问 AT24C02 EEPROM并在 Ubuntu 环境下配置交叉编译工具链来开发和部署该程序。 目录
AT24C02访问方法
在Ubuntu设置交叉编译工具链
使用I2C app代码 AT24C02访问方法
在编写APP之前我们首先要知道用哪个IIC控制器这个IIC设备的设备地址是多少芯片手册如下所示就是芯片手册中的截取片段了。因为我们的A2-A0都是拉低的位于PCB图片中可以看到因此呢我们的地址就是0x50了。 同时就是我们也查到了这个设备地址的内容了。 在Ubuntu设置交叉编译工具链
export ARCHarm
export CROSS_COMPILEarm-linux-gnueabihf-
export PATH$PATH:/home/book/imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin
export ARCHarm
export CROSS_COMPILEarm-buildroot-linux-gnueabihf-
export PATH$PATH:/home/book/stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin 使用I2C app代码
相关代码如下 #include sys/ioctl.h
#include errno.h
#include string.h
#include stdio.h
#include stdlib.h
#include unistd.h
#include linux/i2c.h
#include linux/i2c-dev.h
#include i2c/smbus.h
#include i2cbusses.h
#include time.h/* ./at24c02 i2c_bus_number w xxxxx* ./at24c02 i2c_bus_number r*/int main(int argc, char **argv)
{unsigned char dev_addr 0x50;unsigned char mem_addr 0;unsigned char buf[32];int file;char filename[20];unsigned char *str;int ret;struct timespec req;if (argc ! 3 argc ! 4){printf(Usage:\n);printf(write eeprom: %s i2c_bus_number w string\n, argv[0]);printf(read eeprom: %s i2c_bus_number r\n, argv[0]);return -1;}file open_i2c_dev(argv[1][0]-0, filename, sizeof(filename), 0);if (file 0){printf(cant open %s\n, filename);return -1;}if (set_slave_addr(file, dev_addr, 1)){printf(cant set_slave_addr\n);return -1;}if (argv[2][0] w){// write str: argv[3]str argv[3];req.tv_sec 0;req.tv_nsec 20000000; /* 20ms */while (*str){// mem_addr, *str// mem_addr, strret i2c_smbus_write_byte_data(file, mem_addr, *str);if (ret){printf(i2c_smbus_write_byte_data err\n);return -1;}// wait tWR(10ms)nanosleep(req, NULL);mem_addr;str;}ret i2c_smbus_write_byte_data(file, mem_addr, 0); // string end charif (ret){printf(i2c_smbus_write_byte_data err\n);return -1;}}else{// readret i2c_smbus_read_i2c_block_data(file, mem_addr, sizeof(buf), buf);if (ret 0){printf(i2c_smbus_read_i2c_block_data err\n);return -1;}buf[31] \0;printf(get data: %s\n, buf);}return 0;}
i2c_bus_number: 目标设备所在的 I2C 总线号。操作类型w 表示写数据r 表示读数据。如果是写操作传入一个要写入 EEPROM 的字符串。 open_i2c_dev 是一个自定义函数用于打开 I2C 设备文件例如 /dev/i2c-1。它根据参数指定的 I2C 总线号来选择设备文件。
set_slave_addr 是另一个自定义函数用于设置 I2C 从设备的地址。在这里dev_addr 0x50 是 AT24C02 EEPROM 的默认设备地址。
如果第二个参数是 w程序会将字符串写入 EEPROM。
nanosleep(req, NULL) 在每次写入后暂停 20 毫秒确保数据能够稳定写入 EEPROM。
最终写入一个 0 字符表示字符串结束。
如果第二个参数是 r程序会从 EEPROM 中读取数据。
读取的数据存储在 buf 数组中最后会打印出来。
例子如下所示
./at24c02 1 w xxxxx这会将字符串 xxxxx 写入 EEPROM。
如果你要读取 EEPROM 中存储的数据可以使用以下命令
./at24c02 1 r这会从 EEPROM 中读取数据并打印出来。
如下为Makefile的相关命令
all:$(CROSS_COMPILE)gcc -I ./include -o at24c02_test at24c02_test.c i2cbusses.c smbus.c
文章转载自: http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.frmmp.cn.gov.cn.frmmp.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn