linux I2C DS1337 disable square-wave output

                        \\\\\\\\\\\-*- 目录 -*-//////////
| 一、DS1337访问寄存器说明:
| 二、cat main.c
| 三、cat i2c_data.c
| 四、cat i2c_data.h
| 五、cat Android.mk
-------------------------------- 一、DS1337控制寄存器说明:
. reference:
. DS1337 I2C Serial Real-Time Clock
. How to: wire the DS1337 RTC?
http://forum.arduino.cc/index.php?topic=20937.0. SPECIAL-PURPOSE REGISTERS
The DS1337 has two additional registers (control and status) that control the RTC, alarms, and square-wave output.
. Control Register (0Eh)
+-------+-------+-------+-------+-------+-------+-------+-------+
| Bit | Bit | Bit | Bit | Bit | Bit | Bit | Bit |
+-------+-------+-------+-------+-------+-------+-------+-------+
| EOSC | | | RS2 | RS1 | INTCN | A2IE | A1IE |
+-------+-------+-------+-------+-------+-------+-------+-------+
. Bit : Enable Oscillator (EOSC). This active-low bit when set to logic starts the oscillator. When this bit is set to logic , the oscillator is stopped. This bit is enabled (logic ) when power is first applied.
. Bits and : 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 (32kHz) when power is first applied.
. SQW/INTB Output:
+------+-----+-----+-----------+------+
| NTCN | RS2 | RS1 | SQW/INTB | A2IE |
| | | | OUTPUT | |
+------+-----+-----+-----------+------+
| | | | 1Hz | X |
+------+-----+-----+-----------+------+
| | | | .096kHz | X |
+------+-----+-----+-----------+------+
| | | | .192kHz | X |
+------+-----+-----+-----------+------+
| | | | .768kHz | X |
+------+-----+-----+-----------+------+
| | X | X | A2F | |
+------+-----+-----+-----------+------+
. Bit : 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 , a match between the timekeeping registers and the alarm registers l activates the INTA pin (provided that the alarm is enabled) and a match between the timekeeping registers and the alarm registers activates the SQW/INTB pin (provided that the alarm is enabled). When the INTCN bit is set to logic , a square wave is output on the SQW/INTB pin. This bit is set to logic when power is first applied.
. Bit : Alarm Interrupt Enable (A2IE). When set to logic , this bit permits the alarm flag (A2F) bit in the status register to assert INTA (when INTCN = ) or to assert SQW/INTB (when INTCN = ). When the A2IE bit is set to logic , the A2F bit does not initiate an interrupt signal. The A2IE bit is disabled (logic ) when power is first applied.
. Bit : Alarm Interrupt Enable (A1IE). When set to logic , this bit permits the alarm flag (A1F) bit in the status register to assert INTA. When the A1IE bit is set to logic , the A1F bit does not initiate the INTA signal. The A1IE bit is disabled (logic ) 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 < ) {
perror( "open error\n" );
} unsigned char ch = ;
// 通过测试秒数来判断访问芯片DS1337没问题
// terminal output: get data from ds1337 0x00: 54
i2c_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: 18
printf( "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: 98
printf( "get data from ds1337 0x0e: %x\n", ch ); i2c_data_read_byte( fd, 0x68, 0x0F, &ch );
// terminal output: get data from ds1337 0x0f: 0
printf( "get data from ds1337 0x0f: %x\n", ch ); close( fd ); return ;
} 三、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 test
int eepromDemo ( int argc, char **argv ) {
int fd,ret; fd = open("/dev/i2c-3",O_RDWR);
if ( fd < ) {
perror("open error\n");
} unsigned char buf[] = {'x', 'x', 'x', '', 'y', 'm', '\0'};
i2c_data_write_byte( fd, 0x50, , 'z' );
i2c_data_write_byte( fd, 0x50, , 'j' );
i2c_data_write_byte( fd, 0x50, , '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, , &ch); close(fd); return ;
} /*****************************************************************************
* 函数说明:
* 往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 = ;
struct i2c_rdwr_ioctl_data i2c_data; i2c_data.nmsgs = ; /* 每次只写一个字节 */
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 -;
} ioctl(fd, I2C_TIMEOUT, ); /*超时时间*/
ioctl(fd, I2C_RETRIES, ); /*重复次数*/ (i2c_data.msgs[]).buf = (unsigned char*)malloc(); int i = ;
for (i = ; i < count; i++) { (i2c_data.msgs[]).len = ;
(i2c_data.msgs[]).addr = addr; //i2c 设备地址
(i2c_data.msgs[]).flags = ; //write
(i2c_data.msgs[]).buf[] = (unsigned char)offset+i; // i2c 写入目标的地址
(i2c_data.msgs[]).buf[] = (unsigned char)buf[i]; //the data to write ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
if(ret < ) {
perror("i2c_data_write_str ioctl error");
return ret;
}
} free((i2c_data.msgs[]).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 = ;
struct i2c_rdwr_ioctl_data i2c_data; i2c_data.nmsgs = ; /* 每次只写一个字节 */
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 -;
} ioctl(fd,I2C_TIMEOUT,); /*超时时间*/
ioctl(fd,I2C_RETRIES,); /*重复次数*/ (i2c_data.msgs[]).buf = (unsigned char*)malloc(); (i2c_data.msgs[]).len = ;
(i2c_data.msgs[]).addr = addr; //i2c 设备地址
(i2c_data.msgs[]).flags = ; //write
(i2c_data.msgs[]).buf[] = (unsigned char)offset; // i2c 写入目标的地址
(i2c_data.msgs[]).buf[] = (unsigned char)buf; //the data to write ret = ioctl(fd, 0x0707, (unsigned long)&i2c_data);
if(ret < ) {
perror("i2c_data_write_byte ioctl error");
return ret;
} free((i2c_data.msgs[]).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个开始信号*/
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();
} ioctl(fd,I2C_TIMEOUT,); /*超时时间*/
ioctl(fd,I2C_RETRIES,); /*重复次数*/ (i2c_data.msgs[]).len = ; //i2c 目标数据的地址
(i2c_data.msgs[]).addr = addr; // i2c 设备地址
(i2c_data.msgs[]).flags = ; //write
(i2c_data.msgs[]).buf = (unsigned char*)malloc();
(i2c_data.msgs[]).buf[] = offset; //i2c 数据地址 (i2c_data.msgs[]).len = ; //读出的数据
(i2c_data.msgs[]).addr = addr; // i2c 设备地址
(i2c_data.msgs[]).flags = I2C_M_RD; //read
(i2c_data.msgs[]).buf = (unsigned char*)malloc();//存放返回值的地址。
(i2c_data.msgs[]).buf[] = ; //初始化读缓冲 ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
if(ret<) {
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[].buf[]; free((i2c_data.msgs[]).buf);
free((i2c_data.msgs[]).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 = ;
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();
} ioctl(fd, I2C_TIMEOUT, ); /*超时时间*/
ioctl(fd, I2C_RETRIES, ); /*重复次数*/ (i2c_data.msgs[]).len = ; //i2c 目标数据的地址
(i2c_data.msgs[]).addr = addr; // i2c 设备地址
(i2c_data.msgs[]).flags = ; //write
(i2c_data.msgs[]).buf = (unsigned char*)malloc();
(i2c_data.msgs[]).buf[] = offset; //i2c 数据地址 (i2c_data.msgs[]).len = count; //读出的数据
(i2c_data.msgs[]).addr = addr; // i2c 设备地址
(i2c_data.msgs[]).flags = I2C_M_RD; //read
(i2c_data.msgs[]).buf = buf; //存放返回值的地址。 ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
if(ret<) {
perror("i2c_data_read_byte ioctl error.\n");
return ret;
}
//buf[count] = 0;
//printf("buf = %s\n", buf); free((i2c_data.msgs[]).buf);
free(i2c_data.msgs); return ;
} 四、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.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := linuxTest
LOCAL_SRC_FILES := linuxTest.c i2c_data.c include $(BUILD_EXECUTABLE)

I.MX6 I2C DS1337 disable square-wave output的更多相关文章

  1. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

  2. RFID Reader 线路图收集

    This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...

  3. linux时钟管理

    ref https://access.redhat.com/solutions/18627 在el5中 如何查看系统现在使用的clock source是什么? 答: 方式1:需要说明的是不能保证这个两 ...

  4. RTC(x86)

    RTC 原创,转载请写明出处. 一直以来想写一篇关于RTC的总结,可是人太懒,在读完John Z. Sonmez大伽的<软技能代码之外的生存技能>后,终于下定决心,完成这项早已计划中的任务 ...

  5. POJ 2362:Square 觉得这才算深度搜索

    Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21821   Accepted: 7624 Descripti ...

  6. HDU1518 Square(DFS,剪枝是关键呀)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  7. HDU1518 Square(DFS)

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. HDOJ 1518 Square

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. Square

    Square TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1638   Accepted: 440 Description ...

随机推荐

  1. Mac下配置Hadoop环境

    下载Hadoop(我下载的是2.8.0版本) 点击此处下载 下载后,使用 tar -zxvf tar包名 解压tar包,解压完成后有这样一个文件出现 修改Hadoop的配置文件 需要修改的配置文件在H ...

  2. 解题报告:hdu1248寒冰王座 - 完全背包模板

    2017-09-03 16:16:38 writer:pprp 完全背包问题:从左向右进行扫描,用一维阵列进行分析 代码如下: /* @theme:hdu1248 寒冰王座 @writer:pprp ...

  3. 使用 Python 在 Caché 和 Sql Server 之间同步数据

    任务目标:抽取 Caché 中的数据,导入 Sql Server 中. 遇到的问题: 1.UnicodeEncodeError: ‘ascii’ codec can’t encode characte ...

  4. ELK 6.x 部署

    Elasticsearch版本:6.3.2 Kibana版本:6.3.2 1.es安装 按照官方提示操作即可. 通过yum安装或者下载tar包解压. 安装完成之后,需要修改一些配置 ①修改文件 /et ...

  5. Redis可以做哪些事儿?

    Redis可以作为数据库,提供高速缓存,消息队列等功能,这里介绍Redis可以做的其中两件事: 1.提供缓存功能,作为缓存服务器; 2.轻量级的消息队列(MQ)进行使用. /// <summar ...

  6. 用svg实现不规则形状

    像这种弧形,用纯html和css很难写,但是用svg就简单多了. 可以用作图工具画出一个弧形,然后导成svg格式.在页面中,下面的白块就是div+svg构成 mixin svgCard(...cont ...

  7. [Vue]组件——通过$emit为组件自定义事件

    1.在定义组件时调用内建的 $emit 方法并传入事件的名字,来向父级组件触发一个事件enlarge-text: Vue.component('blog-post', { props: ['post' ...

  8. 数据库原理及应用-SQL数据操纵语言(Data Manipulation Language)和嵌入式SQL&存储过程

    2018-02-19 18:03:54 一.数据操纵语言(Data Manipulation Language) 数据操纵语言是指插入,删除和更新语言. 二.视图(View) 数据库三级模式,两级映射 ...

  9. APP Inventor 基于网络微服务器的即时通信APP

    APP Inventor 基于网络微服务器的即时通信APP 一.总结 一句话总结:(超低配版的QQ,逃~) 1.APP Inventor是什么? google 傻瓜式 编程 手机 app App In ...

  10. Windows中使用wget整站下载

    weget wget安装 Windows下载 点击下载   https://eternallybored.org/misc/wget/ 会跳转到wget的下载页,根据自己电脑选择下载的文件,我下载的版 ...