Contiki 3.x
tmp102-ds3231-test.c
1 /* test both temp sensors on a feshie node - tmp103 and RTC one
2 * rtc returned as 100 * C
3 * Kirk June 2015
4 */
5 #include <stdio.h>
6 #include "contiki.h"
7 #include "dev/i2cmaster.h" // Include IC driver
8 #include "dev/tmp102.h" // Include sensor driver
9 #include "dev/ds3231-sensor.h" // RTC contains a T sensor
10  // Poll the sensor second
11 #define READ_INTERVAL (CLOCK_SECOND)
12 
13 float floor(float x){
14  if(x>=0.0f) return (float) ((int)x);
15  else return (float) ((int)x-1);
16 }
17 
18 PROCESS (temp_process, "Test Temperature process");
19 AUTOSTART_PROCESSES (&temp_process);
20 /*---------------------------------------------------------------------------*/
21 static struct etimer et;
22 
23 PROCESS_THREAD (temp_process, ev, data)
24 {
25  PROCESS_BEGIN ();
26 
27  {
28  int16_t tempint;
29  uint16_t tempfrac;
30  int16_t raw;
31  uint16_t absraw;
32  int16_t sign;
33  char minus = ' ';
34  int rtctemp;
35 
36  tmp102_init();
37 
38  while (1)
39  {
40  etimer_set(&et, READ_INTERVAL); // Set the timer
41  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); // wait for its expiration
42 // actually that timer seems to do nothing at the moment wierdly
43 
44  sign = 1;
45 
46  raw = tmp102_read_temp_raw(); // Reading from the sensor
47 
48  absraw = raw;
49  if (raw < 0) { // Perform 2C's if sensor returned negative data
50  absraw = (raw ^ 0xFFFF) + 1;
51  sign = -1;
52  }
53  tempint = (absraw >> 8) * sign;
54  tempfrac = ((absraw>>4) % 16) * 625; // Info in 1/10000 of degree
55  minus = ((tempint == 0) & (sign == -1)) ? '-' : ' ' ;
56  rtctemp = ds3231_temperature();
57 
58  printf ("Temp102 = %c%d.%04d rtc %d\n", minus, tempint, tempfrac,rtctemp);
59 
60  //printf ("Temp102 = %c%d.%04d rtc %d.%d\n", minus, tempint, tempfrac,
61 //(int)(rtctemp/100), (rtctemp - floor(rtctemp))*100 );
62 
63  }
64  }
65  PROCESS_END ();
66 }
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
static struct etimer et
NIC receiver thread.
Definition: lanc111.c:1133
A timer.
Definition: etimer.h:76
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
Sensors for DS3231 (RTC with Temperature Sensor).
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
int ds3231_temperature(void)
ds3231_temperature
void tmp102_init(void)
Initialiser for the TMP102 sensor driver.
Definition: tmp102.c:59
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120