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 Clock 2. 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 " ); } unsigned char ch = 0; // 通过测试秒数来判断访问芯片DS1337没问题 // terminal output: get data from ds1337 0x00: 54 i2c_data_read_byte( fd, 0x68, 0x00, &ch ); printf( "get data from ds1337 0x00: %x ", ch ); i2c_data_read_byte( fd, 0x68, 0x0e, &ch ); // terminal output: get data from ds1337 0x0e: 18 printf( "get data from ds1337 0x0e: %x ", 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 ", ch ); i2c_data_read_byte( fd, 0x68, 0x0F, &ch ); // terminal output: get data from ds1337 0x0f: 0 printf( "get data from ds1337 0x0f: %x ", 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 test int eepromDemo ( int argc, char **argv ) { int fd,ret; fd = open("/dev/i2c-3",O_RDWR); if ( fd < 0 ) { perror("open error "); } unsigned char buf[] = {'x', 'x', 'x', '0', 'y', 'm', '