1 | #define TSIC_PORT PORTD // Port to use
|
2 | #define TSIC_PIN PIND
|
3 | #define TSIC_PORT_DDR DDRD
|
4 | #define TSCI_POWER_PIN PD6 // Where TSIC-Sensors VCC is connected
|
5 | #define TSIC_SIGNAL_PIN PD7 // Where TSIC-Sensors "Signal"-Pin is con.
|
6 |
|
7 |
|
8 | /******************************************************************************
|
9 | * FUNCTION MACROS
|
10 | ******************************************************************************/
|
11 |
|
12 | // Define TSCI_POWER_PIN as output, TSIC_SIGNAL_PIN as input
|
13 | #define TSIC_INIT() { TSIC_PORT_DDR |= (1<<TSCI_POWER_PIN); \
|
14 | TSIC_PORT_DDR &= ~(1<<TSIC_SIGNAL_PIN); }
|
15 |
|
16 | // Power up the TSIC-Sensor
|
17 | #define TSIC_ON() TSIC_PORT |= (1<<TSCI_POWER_PIN)
|
18 |
|
19 | // Power down the TSIC-Sensor
|
20 | #define TSIC_OFF() TSIC_PORT &= ~(1<<TSCI_POWER_PIN)
|
21 |
|
22 | //#define TSIC_SIGNAL (TSIC_PORT & (1<<TSIC_SIGNAL_PIN))
|
23 |
|
24 | // Low/High Signal of the TSIC-Sensor, e.g. "if(TSIC_SIGNAL_HIGH)..."
|
25 | #define TSIC_SIGNAL_HIGH TSIC_PIN & ( 1 << TSIC_SIGNAL_PIN )
|
26 | #define TSIC_SIGNAL_LOW !( TSIC_PIN & ( 1 << TSIC_SIGNAL_PIN ))
|
27 |
|
28 |
|
29 | /******************************************************************************
|
30 | * Function : getTSicTemp(*temp_value16);
|
31 | * Description : reads from the TSic its output value
|
32 | * Parameters : pointer for return value
|
33 | * Returns : 1: reading sucessfull, 0: parity error
|
34 | ******************************************************************************/
|
35 | uint8_t getTSicTemp (uint16_t *temp_value16) {
|
36 |
|
37 | uint16_t temp_value1 = 0;
|
38 | uint16_t temp_value2 = 0;
|
39 | uint8_t i;
|
40 | uint16_t Temperature;
|
41 | uint8_t parity;
|
42 |
|
43 | TSIC_ON();
|
44 | _delay_us(60); // wait for stabilization
|
45 | _delay_us(60);
|
46 |
|
47 | while (TSIC_SIGNAL_HIGH); // wait until start bit starts
|
48 |
|
49 | // wait, TStrobe
|
50 | while (TSIC_SIGNAL_LOW);
|
51 |
|
52 | // first data byte
|
53 | // read 8 data bits and 1 parity bit
|
54 | for (i = 0; i < 9; i++) {
|
55 | while (TSIC_SIGNAL_HIGH); // wait for falling edge
|
56 | _delay_us(60);
|
57 | if (TSIC_SIGNAL_HIGH)
|
58 | temp_value1 |= 1 << (8-i); // get the bit
|
59 | else
|
60 | while (TSIC_SIGNAL_LOW); // wait until line comes high again
|
61 | }
|
62 |
|
63 | // second byte
|
64 | while (TSIC_SIGNAL_HIGH);
|
65 | // wait, TStrobe
|
66 | while (TSIC_SIGNAL_LOW);
|
67 | // read 8 data bits and 1 parity bit
|
68 | for (i = 0; i < 9; i++) {
|
69 | while (TSIC_SIGNAL_HIGH); // wait for falling edge
|
70 | _delay_us(60);
|
71 | if (TSIC_SIGNAL_HIGH)
|
72 | temp_value2 |= 1 << (8-i); // get the bit
|
73 | else
|
74 | while (TSIC_SIGNAL_LOW); // wait until line comes high again
|
75 | }
|
76 |
|
77 | TSIC_OFF(); // switch TSic off
|
78 |
|
79 | // check parity for byte 1
|
80 | parity = 0;
|
81 | for (i = 0; i < 9; i++)
|
82 | if (temp_value1 & (1 << i))
|
83 | parity++;
|
84 | if (parity % 2)
|
85 | return 0;
|
86 |
|
87 | // check parity for byte 2
|
88 | parity = 0;
|
89 | for (i = 0; i < 9; i++)
|
90 | if (temp_value2 & (1 << i))
|
91 | parity++;
|
92 | if (parity % 2)
|
93 | return 0;
|
94 | temp_value1 >>= 1; // delete parity bit
|
95 | temp_value2 >>= 1; // delete parity bit
|
96 | Temperature = (temp_value1 << 8) | temp_value2;
|
97 | *temp_value16 = Temperature;
|
98 |
|
99 | return 1; // parity is OK
|
100 | }
|