Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save macintoshplus/c4535461ea137b25e9c8759b2fd1f3ce to your computer and use it in GitHub Desktop.
Save macintoshplus/c4535461ea137b25e9c8759b2fd1f3ce to your computer and use it in GitHub Desktop.
add comment
/*
* GPLv2 Licence
* Author : Macintoshplus <[email protected]>
* Build Command Line : gcc main.c -o ds1631 -I/usr/local/include -L/usr/local/lib -lwiringPi
*/
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <unistd.h>
/*
The I2C adress found by i2cdetect command line
*/
#define DS1631_ADDR 0x48
#define ACCESS_CONFIG 0xAC //Reads or writes the 1-byte configuration register.
/*
Initiates temperature conversions. If the part is in one-shot
mode (1SHOT = 1), only one conversion is performed. In
continuous mode (1SHOT = 0), continuous temperature
conversions are performed until a Stop Convert T com-
mand is issued.
*/
#define STOP_CONVERSION 0x22
#define START_CONVERSION 0x51
#define READ_TEMP 0xAA
#define TH_CONFIG 0xA1 //Reads or writes the 2-byte T H register.
#define TL_CONFIG 0xA2 //Reads or writes the 2-byte T L register.
/*
Initiates a software power-on-reset (POR), which stops
temperature conversions and resets all registers and
logic to their power-up states
*/
#define POWER_ON_RESET 0x54
#define CONVERT_9BITS 0x00
#define CONVERT_10BITS 0x04
#define CONVERT_11BITS 0x08
#define CONVERT_12BITS 0x0C
#define ONSHOT_CONVERSION 0x01
#define CONTINUOUS_CONVERSION 0x00
int main() {
//Init du protocol pour le materiel
int fd = wiringPiI2CSetup(DS1631_ADDR);
//lecture de la config
wiringPiI2CWrite(fd, STOP_CONVERSION);
wiringPiI2CWrite(fd, ACCESS_CONFIG);
int config = wiringPiI2CRead(fd);
if (config & ONSHOT_CONVERSION) {
printf("one-shot mode is ON. !\n");
}
printf("CONFIG BEFORE : %x \n", config);
//Modification de la config
wiringPiI2CWriteReg8(fd, ACCESS_CONFIG, CONVERT_12BITS);
wiringPiI2CWrite(fd, ACCESS_CONFIG);
int config2 = wiringPiI2CRead(fd);
printf("CONFIG AFTER : %x \n", config2);
if (config2 & ONSHOT_CONVERSION) {
printf("one-shot mode is ON. !\n");
}
wiringPiI2CWrite(fd, START_CONVERSION);
while(1) {
int val = wiringPiI2CReadReg16(fd, READ_TEMP);
//Temp low and Temp High separation
int Tl = (val & 0xff00) >> 8;
int Th = val & 0x00ff;
//Display the rax value for debug
printf("TEMP VALUE : %x Th=%x Tl=%x\n", val, Th, Tl);
// T° processing
if(Th>=0x80) //if sign bit is set, then temp is negative
Th = Th - 256;
int T_dec=(10*(100*(Tl/16)))/16; // decimal part of the T°
printf("Temperature %d.%d\n", Th, Tl);
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment