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 | Original Code: http://www.mikrocontroller.net/topic/82087
|
6 | rewritten by: Rolf Wagner
|
7 | Date: 2013.03.26
|
8 | Temperature-sensor uses 2 Pins, one for VCC and the second for Signal (= ZACwire Protocol).
|
9 | With call of the Function ".....getTSicTemp();" the Senor will get power measure and will be turned OFF.
|
10 | In that case it uses only energy while measuring. There is another method with Interrupts, measuring
|
11 | at an constant rate, which is not implemented jet.
|
12 | */
|
13 | #include "tsic.h"
|
14 |
|
15 | void TSIC_INIT(void) {
|
16 | TSIC_PORT_DDR_Power |= TSCI_POWER_PIN; // Ausgang
|
17 | TSIC_PORT_DDR &= ~TSIC_SIGNAL_PIN; // Eingang
|
18 | }
|
19 |
|
20 | void readSens(uint8_t *temp_value) {
|
21 | while (TSIC_SIGNAL_HIGH)
|
22 | ; // wait until start bit starts
|
23 |
|
24 | while (TSIC_SIGNAL_LOW)
|
25 | ; // wait, TStrobe
|
26 |
|
27 | // read 7 data bits
|
28 | for (int i = 0; i < 8; i++) {
|
29 | while (TSIC_SIGNAL_HIGH)
|
30 | ; // wait for falling edge
|
31 | _delay_us(60);
|
32 | if (TSIC_SIGNAL_HIGH)
|
33 | *temp_value |= 1;
|
34 | *temp_value <<= 1;
|
35 | while (TSIC_SIGNAL_LOW)
|
36 | ; // wait until line comes high again
|
37 | }
|
38 |
|
39 | }
|
40 | uint8_t readParity() {
|
41 | while (TSIC_SIGNAL_HIGH)
|
42 | ; // wait for falling edge
|
43 | _delay_us(60);
|
44 | if (TSIC_SIGNAL_HIGH)
|
45 | return 1;
|
46 | while (TSIC_SIGNAL_LOW)
|
47 | ; // wait until line comes high again
|
48 | return 0;
|
49 | }
|
50 |
|
51 | uint8_t checkParity(uint8_t temp_value, uint8_t parityadd) {
|
52 | uint8_t parity = 0;
|
53 | uint8_t mask = 0b00000001;
|
54 | for (uint8_t i = 0; i < 8; i++) {
|
55 | if (temp_value & mask)
|
56 | parity++;
|
57 | mask <<= 1;
|
58 | }
|
59 | parity += parityadd;
|
60 | return parity & 1; // AND 1 entspricht MOD 2
|
61 | }
|
62 | uint8_t getTSicTemp(uint16_t *temp_value16) {
|
63 | uint8_t temp_value1 = 0;
|
64 | uint8_t temp_value2 = 0;
|
65 | uint8_t paritybits;
|
66 |
|
67 | TSIC_ON();
|
68 | _delay_us(60); // wait for stabilization
|
69 |
|
70 | //first byte
|
71 | readSens(&temp_value1);
|
72 | paritybits = readParity();
|
73 |
|
74 | // second byte
|
75 | readSens(&temp_value2);
|
76 | paritybits |= (readParity() << 1);
|
77 |
|
78 | TSIC_OFF();
|
79 |
|
80 | if (checkParity(temp_value1, paritybits & 1))
|
81 | return 0;
|
82 | if (checkParity(temp_value2, paritybits & 2))
|
83 | return 0;
|
84 |
|
85 | *temp_value16 = (temp_value1 << 7) | temp_value2;
|
86 |
|
87 | return 1; // parity is OK
|
88 | }
|