tsic.c


1
/*
2
 Library for TSIC digital Temperature Sensor Type 206/306 and may more.
3
 using ZACwire™ Communication Protocol for the TSic™
4
 Manufatorer:    IST AG, Hygrosens, ZMD
5
 rewritten by:    Rolf Wagner
6
 Date:        2013.05.09
7
 Original Code by Rogger (http://www.mikrocontroller.net/topic/159774)
8
       ________
9
GND     ---| TSIC  |
10
Signal  ---|206/306  |
11
Vcc-Pin  ---|________|
12
13
 Der Anschluss erfolgt mit GND and GND, die Pins VCC und Signal werden jeweils an einem schaltbaren uC Pin angeschlossen.
14
 Es werden also 2 Pins benötigt.
15
 Zwischen VCC und Masse wird ein 100nF Kondensator angeschlossen und der Pin von VCC läuft über einen 220 Ohm Widerstand (wie im Datenblatt beschrieben).
16
17
 */
18
#include "tsic.h"
19
20
void TSIC_INIT(void) {
21
  TSIC_PORT_DDR_Power |= TSCI_POWER_PIN;    // Ausgang
22
  TSIC_PORT_DDR &= ~TSIC_SIGNAL_PIN;      // Eingang
23
}
24
25
uint8_t readSens(uint16_t *temp_value){
26
  uint16_t strobelength = 0;
27
  uint16_t strobetemp = 0;
28
  uint16_t dummy = 0;
29
  uint16_t timeout = 0;
30
31
  while (TSIC_SIGNAL_HIGH)
32
    ; // wait until start bit starts
33
  // Measure strobe time
34
  strobelength = 0;
35
  timeout = 0;
36
  while (TSIC_SIGNAL_LOW) {    // wait for rising edge
37
    strobelength++;
38
    timeout++;
39
    Abbruch();
40
  }
41
  for (uint8_t i=0; i<9; i++) {
42
    // Wait for bit start
43
    timeout = 0;
44
    while (TSIC_SIGNAL_HIGH) { // wait for falling edge
45
      timeout++;
46
      Abbruch();
47
    }
48
    // Delay strobe length
49
    timeout = 0;
50
    dummy = 0;
51
    strobetemp = strobelength;
52
    while (strobetemp--) {
53
      timeout++;
54
      dummy++;
55
      Abbruch();
56
    }
57
    *temp_value <<= 1;
58
    // Read bit
59
    if (TSIC_SIGNAL_HIGH) {
60
      *temp_value |= 1;
61
    }
62
    // Wait for bit end
63
    timeout = 0;
64
    while (TSIC_SIGNAL_LOW) {    // wait for rising edge
65
      timeout++;
66
      Abbruch();
67
    }
68
  }
69
  return 1;
70
}
71
72
uint8_t checkParity(uint16_t *temp_value) {
73
  uint8_t parity = 0;
74
75
  for (uint8_t i = 0; i < 9; i++) {
76
    if (*temp_value & (1 << i))
77
      parity++;
78
  }
79
  if (parity % 2)
80
    return 0;  // Parityfehler
81
  *temp_value >>= 1;               // Parity Bit löschen
82
  return 1;
83
}
84
85
uint8_t getTSicTemp(uint16_t *temp_value16) {
86
    uint16_t temp_value1 = 0;
87
    uint16_t temp_value2 = 0;
88
89
    TSIC_ON();
90
    _delay_us(200);     // wait for stabilization
91
92
    readSens(&temp_value1);      // 1. Byte einlesen
93
    readSens(&temp_value2);      // 2. Byte einlesen
94
    checkParity(&temp_value1);    // Parity vom 1. Byte prüfen
95
    checkParity(&temp_value2);    // Parity vom 2. Byte prüfen
96
97
    TSIC_OFF();    // Sensor ausschalten
98
    *temp_value16 = (temp_value1 << 8) + temp_value2;
99
    return 1;
100
}