linux I2C DS1337 disable square-wave output\\\\\\\\\\\-*- 目录 -*-//|   一、DS1337访问寄存器说明:    |   二、cat main.c|   三、cat i2c_data.c|   四、cat i2c_data.h|   五、cat Android.mk--------------------------------一、DS1337控制寄存器说明:    1. reference: 1. DS1337 I2C Serial Real-Time Clock2. How to: wire the DS1337 RTC?http://forum.arduino.cc/index.php?topic=20937.02. SPECIAL-PURPOSE REGISTERS The DS1337 has two additional registers (control and status) that control the RTC, alarms, and square-wave output. 3. Control Register (0Eh) +-------+-------+-------+-------+-------+-------+-------+-------+| Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 | +-------+-------+-------+-------+-------+-------+-------+-------+| EOSC  | 0     | 0     | RS2   | RS1   | INTCN | A2IE  | A1IE  | +-------+-------+-------+-------+-------+-------+-------+-------+1. Bit 7: Enable Oscillator (EOSC). This active-low bit when set to logic 0 starts the oscillator. When this bit is set to logic 1, the oscillator is stopped. This bit is enabled (logic 0) when power is first applied.2. Bits 4 and 3: Rate Select (RS2 and RS1). These bits control the frequency of the square-wave output when the square wave has been enabled. The table below shows the square-wave frequencies that can be selected with the RS bits. These bits are both set to logic 1 (32kHz) when power is first applied.3. SQW/INTB Output:+------+-----+-----+-----------+------+| NTCN | RS2 | RS1 | SQW/INTB  | A2IE | |      |     |     |  OUTPUT   |      | +------+-----+-----+-----------+------+| 0    | 0   | 0   | 1Hz       | X    | +------+-----+-----+-----------+------+| 0    | 0   | 1   | 4.096kHz  | X    | +------+-----+-----+-----------+------+| 0    | 1   | 0   | 8.192kHz  | X    | +------+-----+-----+-----------+------+| 0    | 1   | 1   | 32.768kHz | X    | +------+-----+-----+-----------+------+| 1    | X   | X   | A2F       | 1    | +------+-----+-----+-----------+------+4. Bit 2: Interrupt Control (INTCN). This bit controls the relationship between the two alarms and the interrupt output  pins. When the INTCN bit is set to logic 1, a match between the timekeeping registers and the alarm 1 registers l activates the INTA pin (provided that the alarm is enabled) and a match between the timekeeping registers and the alarm 2 registers activates the SQW/INTB pin (provided that the alarm is enabled). When the INTCN bit is set to logic 0, a square wave is output on the SQW/INTB pin. This bit is set to logic 0 when power is first applied. 5. Bit 1: Alarm 2 Interrupt Enable (A2IE). When set to logic 1, this bit permits the alarm 2 flag (A2F) bit in the status register to assert INTA (when INTCN = 0) or to assert SQW/INTB (when INTCN = 1). When the A2IE bit is set to logic 0, the A2F bit does not initiate an interrupt signal. The A2IE bit is disabled (logic 0) when power is first applied. 6. Bit 0: Alarm 1 Interrupt Enable (A1IE). When set to logic 1, this bit permits the alarm 1 flag (A1F) bit in the status register to assert INTA. When the A1IE bit is set to logic 0, the A1F bit does not initiate the INTA signal. The A1IE bit is disabled (logic 0) when power is first applied. 二、cat main.c#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include "i2c-dev.h"#include "i2c.h"#include "i2c_data.h"int main ( int argc, char ** argv ) {int fd,ret;fd = open( "/dev/i2c-2", O_RDWR );if ( fd < 0 ) {perror( "open error\n" );}unsigned char ch = 0;// 通过测试秒数来判断访问芯片DS1337没问题// terminal output: get data from ds1337 0x00: 54i2c_data_read_byte( fd, 0x68, 0x00, &ch );printf( "get data from ds1337 0x00: %x\n", ch );i2c_data_read_byte( fd, 0x68, 0x0e, &ch );// terminal output: get data from ds1337 0x0e: 18printf( "get data from ds1337 0x0e: %x\n", ch );// 关闭方波输出ch = 0x98;i2c_data_write_byte( fd, 0x68, 0x0e, ch );i2c_data_read_byte( fd, 0x68, 0x0e, &ch );// terminal output: get data from ds1337 0x0e: 98printf( "get data from ds1337 0x0e: %x\n", ch );i2c_data_read_byte( fd, 0x68, 0x0F, &ch );// terminal output: get data from ds1337 0x0f: 0printf( "get data from ds1337 0x0f: %x\n", ch );close( fd );return 0;}三、cat i2c_data.c#include <stdio.h>#include <string.h>#include <linux/types.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <sys/types.h>#include <sys/ioctl.h>#include <errno.h>#include "i2c-dev.h"#include "i2c.h"#include "i2c_data.h"// A demo for testint eepromDemo ( int argc, char **argv ) {int fd,ret;fd = open("/dev/i2c-3",O_RDWR);if ( fd < 0 ) {perror("open error\n");}unsigned char buf[] = {'x', 'x', 'x', '0', 'y', 'm', '\0'};i2c_data_write_byte( fd, 0x50, 0, 'z' );i2c_data_write_byte( fd, 0x50, 1, 'j' );i2c_data_write_byte( fd, 0x50, 2, 'f' );//printf("i2c function test: %s\n", buf);//i2c_data_write_byte (fd, 0x50, 3, buf[1]);//i2c_data_read_str (fd, 0x50, 0, buf, array_size(buf)-1);//unsigned char ch = 0;i2c_data_read_byte(fd, 0x50, 1, &ch);close(fd);return 0;}/****************************************************************************** 函数说明:*     往i2c设备写数据,写一串字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    字节数组*     5. count:  字节数组长度* 返回值:*     正常返回写入的个数,如果出错,返回错误码****************************************************************************/int i2c_data_write_str (int fd, int addr, int offset, unsigned char *buf, int count) {int ret = 0;struct i2c_rdwr_ioctl_data i2c_data;i2c_data.nmsgs = 1;     /* 每次只写一个字节 */i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));if(!i2c_data.msgs){perror("i2c_data_write_str function malloc error.\n");return -1;}ioctl(fd, I2C_TIMEOUT, 1); /*超时时间*/ioctl(fd, I2C_RETRIES, 2); /*重复次数*/(i2c_data.msgs[0]).buf = (unsigned char*)malloc(2);int i = 0;for (i = 0; i < count; i++) {(i2c_data.msgs[0]).len      = 2;(i2c_data.msgs[0]).addr     = addr;     //i2c 设备地址(i2c_data.msgs[0]).flags    = 0;        //write(i2c_data.msgs[0]).buf[0]   = (unsigned char)offset+i;   // i2c 写入目标的地址(i2c_data.msgs[0]).buf[1]   = (unsigned char)buf[i];     //the data to write
    ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);if(ret < 0) {perror("i2c_data_write_str ioctl error");return ret;}   }   free((i2c_data.msgs[0]).buf);free(i2c_data.msgs);return i;}/************************************************************************* 函数说明:*     往i2c设备写数据,写一个字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    要写入的字节* 返回值:*     正常返回0,出错,返回错误码;************************************************************************/int i2c_data_write_byte (int fd, int addr, int offset, unsigned char buf) {int ret = 0;struct i2c_rdwr_ioctl_data i2c_data;i2c_data.nmsgs = 1; /* 每次只写一个字节 */i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));if(!i2c_data.msgs){perror("i2c_data_write_byte function malloc error.\n");return -1;}ioctl(fd,I2C_TIMEOUT,4); /*超时时间*/ioctl(fd,I2C_RETRIES,8); /*重复次数*/(i2c_data.msgs[0]).buf      = (unsigned char*)malloc(2);(i2c_data.msgs[0]).len      = 2;(i2c_data.msgs[0]).addr     = addr;     //i2c 设备地址(i2c_data.msgs[0]).flags    = 0;        //write(i2c_data.msgs[0]).buf[0]   = (unsigned char)offset;    // i2c 写入目标的地址(i2c_data.msgs[0]).buf[1]   = (unsigned char)buf;       //the data to write
    ret = ioctl(fd, 0x0707, (unsigned long)&i2c_data);if(ret < 0) {perror("i2c_data_write_byte ioctl error");return ret;}   free((i2c_data.msgs[0]).buf);free(i2c_data.msgs);return ret;}/************************************************************************** 函数说明:*     从i2c设备读数据,读一个字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    保存读出的字节指针* 返回值:*     正常返回0,出错,返回错误码;************************************************************************/int i2c_data_read_byte (int fd, int addr, int offset, unsigned char *buf) {int ret;struct i2c_rdwr_ioctl_data i2c_data;i2c_data.nmsgs = 2; /*读时序为2个开始信号*/i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));if(!i2c_data.msgs) {perror("i2c_data_read_byte functionmalloc error");exit(1);}ioctl(fd,I2C_TIMEOUT,1); /*超时时间*/ioctl(fd,I2C_RETRIES,2); /*重复次数*/(i2c_data.msgs[0]).len      = 1;        //i2c 目标数据的地址(i2c_data.msgs[0]).addr     = addr;     // i2c 设备地址(i2c_data.msgs[0]).flags    = 0;        //write(i2c_data.msgs[0]).buf      = (unsigned char*)malloc(1);(i2c_data.msgs[0]).buf[0]   = offset;   //i2c 数据地址
    (i2c_data.msgs[1]).len      = 1;        //读出的数据(i2c_data.msgs[1]).addr     = addr;     // i2c 设备地址(i2c_data.msgs[1]).flags    = I2C_M_RD; //read(i2c_data.msgs[1]).buf      = (unsigned char*)malloc(1);//存放返回值的地址。(i2c_data.msgs[1]).buf[0]   = 0;        //初始化读缓冲
    ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);if(ret<0) {perror("i2c_data_read_byte ioctl error.\n");return ret;}//printf("buff[0] = %x\n",i2c_data.msgs[1].buf[0]);*buf = i2c_data.msgs[1].buf[0];free((i2c_data.msgs[0]).buf);free((i2c_data.msgs[1]).buf);free(i2c_data.msgs);return ret;}/*********************************************************************************** 函数说明:*     从i2c设备读数据,读一串字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    保存从i2c设备读出数据的字节数组指针*     5. count:  要读的字节个数* 返回值:*     正常返回0,如果出错,返回错误码**********************************************************************************/int i2c_data_read_str (int fd, int addr, int offset, unsigned char *buf, int count) {int ret;struct i2c_rdwr_ioctl_data i2c_data;i2c_data.nmsgs = 2; i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));if(!i2c_data.msgs) {perror("i2c_data_read_byte functionmalloc error");exit(1);}ioctl(fd, I2C_TIMEOUT, 1); /*超时时间*/ioctl(fd, I2C_RETRIES, 2); /*重复次数*/(i2c_data.msgs[0]).len      = 1;        //i2c 目标数据的地址(i2c_data.msgs[0]).addr     = addr;     // i2c 设备地址(i2c_data.msgs[0]).flags    = 0;        //write(i2c_data.msgs[0]).buf      = (unsigned char*)malloc(1);(i2c_data.msgs[0]).buf[0]   = offset;   //i2c 数据地址
    (i2c_data.msgs[1]).len      = count;    //读出的数据(i2c_data.msgs[1]).addr     = addr;     // i2c 设备地址(i2c_data.msgs[1]).flags    = I2C_M_RD; //read(i2c_data.msgs[1]).buf      = buf;      //存放返回值的地址。
    ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);if(ret<0) {perror("i2c_data_read_byte ioctl error.\n");return ret;}//buf[count] = 0;//printf("buf = %s\n", buf);free((i2c_data.msgs[0]).buf);free(i2c_data.msgs);return 0;}四、cat i2c_data.h/*************************************************************************************** 声明:*     这个函数库主要用于读写i2c设备,提供了以下功能:*         1. 读一串字节:i2c_data_read_str()*         2. 读一个字节:i2c_data_read_byte()*         3. 写一串字节:i2c_data_write_str()*         4. 写一个字节:i2c_data_write_byte()***                                      write by zengjf     2015-4-28**************************************************************************************/#ifndef __I2C_DATA_H__#define __I2C_DATA_H__#define array_size(array_buffer) (sizeof(array_buffer)/sizeof(*array_buffer))/*********************************************************************************** 函数说明:*     往i2c设备写数据,写一串字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    字节数组*     5. count:  字节数组长度* 返回值:*     正常返回写入的个数,如果出错,返回错误码*********************************************************************************/int i2c_data_write_str (int fd, int addr, int offset, unsigned char *buf, int count);/*********************************************************************************** 函数说明:*     从i2c设备读数据,读一串字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    保存从i2c设备读出数据的字节数组指针*     5. count:  要读的字节个数* 返回值:*     正常返回0,如果出错,返回错误码**********************************************************************************/int i2c_data_read_str (int fd, int addr, int offset, unsigned char *buf, int count);/*********************************************************************************** 函数说明:*     往i2c设备写数据,写一个字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    要写入的字节* 返回值:*     正常返回0,出错,返回错误码;**********************************************************************************/int i2c_data_write_byte (int fd, int addr, int offset, unsigned char buf);/*********************************************************************************** 函数说明:*     从i2c设备读数据,读一个字节* 参数说明:*     1. fd:     文件描述符*     2. addr:   i2c设备地址*     3. offset: i2c设备内寄存器偏移地址*     4. buf:    保存读出的字节指针* 返回值:*     正常返回0,出错,返回错误码;**********************************************************************************/int i2c_data_read_byte (int fd, int addr, int offset, unsigned char *buf);#endif //__I2C_DATA_H__五、cat Android.mkLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := linuxTestLOCAL_SRC_FILES := linuxTest.c i2c_data.cinclude $(BUILD_EXECUTABLE)

转载于:https://www.cnblogs.com/zengjfgit/p/4981089.html

I.MX6 I2C DS1337 disable square-wave output相关推荐

  1. 如何获取BIOS序列号

    http://blog.sina.com.cn/s/blog_57dff12f0100d8do.html typedef struct _UNICODE_STRING {     USHORT  Le ...

  2. 每秒递减的方波扫频信号怎么生成_逆变器是怎么工作的?

    点击上方蓝色字关注? 导语 逆变器,作为新能源汽车动力总成的大脑,为电动汽车提供理想的正弦交流电力.简短的六分钟视频,从为什么需要逆变器.直流变交流的实现方式--全桥逆变电路.正弦交流电产生的原理等几 ...

  3. TI - MCU - MSP430使用指南14 - I2C通信(eUSCI)

    说到MSP430 MCU的I2C资源,那么首先就得先看一下USCI. 如果你了解过多款MSP430 MCU你会发现,内部资源里有USCI和eUSCI,那么他们什么意思呢? USCI (Universa ...

  4. 【 MATLAB 】信号处理工具箱的信号产生函数之 square 函数简记

    因为案例需要,所以这里先看一下linspace这个函数的用法: y = linspace(x1,x2): 均匀产生位于x1 到 x2 之间的100个点: y = linspace(x1,x2,n): ...

  5. 【HDU - 1518】Square (经典的dfs + 剪枝)

    题干: Given a set of sticks of various lengths, is it possible to join them end-to-end to form a squar ...

  6. DSP sawtooth锯齿波与square方波matlab产生(M2.2)

    x = sawtooth(t,xmax)产生锯齿波序列,有两个参数,其中第二个参数xmax可省略. 该函数周期为2π2\pi2π,t是时间刻度序列,xmax是刻度伸缩系数,介于0到1之间,默认为1,默 ...

  7. 【FPGA】十一、I2C通信回环

    文章目录 前言 一.I2C简介 二.I2C原理 2.1.I2C物理层 2.2.I2C协议层 2.2.1.I2C协议 2.2.2.I2C数据传输格式 2.2.3.I2C写操作 2.2.4.I2C读操作 ...

  8. FPGA进阶(2):基于I2C协议的EEPROM驱动控制

    文章目录 第49讲:基于I2C协议的EEPROM驱动控制 理论部分 设计与实现 i2c_ctrl i2c_rw_data eeprom_byte_rd_wr tb_eeprom_byte_rd_wr ...

  9. matlab中0.1见方,square,square怎么读?

    square怎么读? square的读音为英[2113skweə(r)]5261美[skwer].具体释义4102如下: square英[skweə(r)]美[skwer] n.平方:广场:正方165 ...

最新文章

  1. java面试-死锁产生、定位、修复
  2. AI大牛纷纷离职!2021大厂AI Lab现状盘点,网友:名存实亡
  3. 网页截图工具CutyCapt
  4. JavaScript编程:文档对象模型DOM
  5. PostgreSQL 9.4版本的物化视图更新
  6. java鼠标改变窗口大小监听事件_java – 当用户拖动鼠标时,监听JFrame调整事件大小?...
  7. OK335xS dhcpcd porting
  8. Java与微信不得不说的故事——消息的接收与发送
  9. MAC编译OpenJDK8:ld: library not found for -lstdc++(独家解决办法)
  10. [2018.10.17 T2] 最优路线
  11. 电子海图通信态势软件设计与实现
  12. excel转置怎么操作_EXCEL转置的方法介绍,这种函数80%的人没用过,教你如何转置996...
  13. 内容协商 Spring ContentNegotiation
  14. Android神笔之Paint
  15. Mysql笔记之 数据类型
  16. IM推送Android客户端SDK之智能心跳
  17. IntelliJ IDEA 如何下载安装插件
  18. 如何使用大智慧条件选股
  19. Ubuntu 安装并切换lightdm图形界面
  20. 行业研究-全球与中国牙科电子病历软件市场现状及未来发展趋势

热门文章

  1. 准备您的虚拟桌面--WinXp模板
  2. java的编译及运行
  3. C# WinForm开发系列 - DataGrid
  4. 解决Chrome浏览器打开虾米音乐网页播放器时的排版问题
  5. Bitdefender Internet Security 2013 – 免费3个月
  6. 使用repeater,遍历数据,不规则排序,不同的样式之间切换
  7. 泛型技巧系列:简单类型选择器
  8. python 字符串拼接_面试官让用 3 种 python 方法实现字符串拼接 ?对不起我有8种……...
  9. python实现表格_零基础小白怎么用Python做表格?
  10. Linux Kernel TCP/IP Stack — L1 Layer — 多队列网卡