1 | /*
|
2 | * TID.c
|
3 | *
|
4 | * Created on: May 27, 2015
|
5 | * Author: max
|
6 | */
|
7 |
|
8 |
|
9 | #include "TID.h"
|
10 | #include <util/delay.h>
|
11 | #include <util/parity.h>
|
12 | #include <avr/io.h>
|
13 |
|
14 |
|
15 |
|
16 | typedef enum {
|
17 | On,
|
18 | Off,
|
19 | Date
|
20 | }state;
|
21 |
|
22 | uint8_t twiSend(uint8_t);
|
23 | uint8_t tidSend(uint8_t);
|
24 | uint8_t tidParity(uint8_t);
|
25 |
|
26 |
|
27 | static state Stat;
|
28 | static uint8_t buff[dig + extra] = {1,1,0xAE,0x8A,0x98,0x86,0x9E,0x9B,0x8A,0x40};
|
29 |
|
30 | void tidInit(){
|
31 |
|
32 | SCL_DDR &= ~(1<<SCL_Bit);
|
33 | SDA_DDR &= ~(1<<SDA_Bit);
|
34 | MRQ_DDR &= ~(1<<MRQ_Bit);
|
35 | TID_Ign_DDR |= (1<<TID_Ign_Bit);
|
36 | TID_AA_DDR |= (1<<TID_AA_Bit);
|
37 |
|
38 | SCL_Port &= ~(1<<SCL_Bit);
|
39 | SDA_Port &= ~(1<<SDA_Bit);
|
40 | MRQ_Port &= ~(1<<MRQ_Bit);
|
41 | TID_Ign_Port &= ~(1<<TID_Ign_Bit);
|
42 | TID_AA_Port &= ~(1<<TID_AA_Bit);
|
43 |
|
44 | Stat = Off;
|
45 | }
|
46 |
|
47 | void tidOn(){
|
48 |
|
49 | if(Stat == On) return;
|
50 |
|
51 |
|
52 | TID_Ign_Port |= (1<<TID_Ign_Bit);
|
53 | TID_AA_Port |= (1<<TID_AA_Bit);
|
54 | _delay_ms(150);
|
55 | sdaLow();
|
56 | sclLow();
|
57 | mrqLow();
|
58 | _delay_us(750);
|
59 | sdaHigh();
|
60 | sclHigh();
|
61 | mrqHigh();
|
62 | _delay_us(750);
|
63 | sdaLow();
|
64 | _delay_us(750);
|
65 | sdaHigh();
|
66 | _delay_us(750);
|
67 | sclLow();
|
68 | _delay_us(750);
|
69 | sclHigh();
|
70 | _delay_us(750);
|
71 | mrqLow();
|
72 | _delay_us(750);
|
73 | mrqHigh();
|
74 | Stat = On;
|
75 | _delay_us(1500);
|
76 | tidRefresh();
|
77 | }
|
78 | void tidOff(){
|
79 |
|
80 | TID_Ign_Port &= ~(1<<TID_Ign_Bit);
|
81 | TID_AA_Port &= ~(1<<TID_AA_Bit);
|
82 |
|
83 | Stat = Off;
|
84 | }
|
85 | void tidDate(){
|
86 |
|
87 | TID_Ign_Port |= (1<<TID_Ign_Bit);
|
88 | TID_AA_Port &= ~(1<<TID_AA_Bit);
|
89 |
|
90 | Stat = Date;
|
91 | }
|
92 |
|
93 | void tidRefresh(){
|
94 |
|
95 | if(Stat != On) return;
|
96 | uint8_t i;
|
97 |
|
98 | mrqLow();
|
99 | while(getSda());
|
100 | _delay_us(150);
|
101 | mrqHigh();
|
102 | while(getSda() == 0);
|
103 | _delay_us(150);
|
104 | sdaLow();
|
105 | _delay_us(150);
|
106 | sclLow();
|
107 |
|
108 | if(twiSend(add)) return;
|
109 | _delay_us(150);
|
110 | mrqLow();
|
111 | _delay_us(10);
|
112 |
|
113 | for(i = 0; i < (dig + extra);i++){
|
114 | if(tidSend(buff[i])) break;
|
115 | }
|
116 |
|
117 | _delay_us(50);
|
118 | sdaLow();
|
119 | _delay_us(150);
|
120 | mrqHigh();
|
121 | _delay_us(150);
|
122 | sclHigh();
|
123 | _delay_us(150);
|
124 | sdaHigh();
|
125 | }
|
126 |
|
127 | uint8_t tidParity(uint8_t dat){
|
128 | if(parity_even_bit(dat)) return dat<<1;
|
129 | else return (dat<<1)^1;
|
130 | }
|
131 | void tidDisplay(char txt[], uint8_t symb[] ){
|
132 | uint8_t i;
|
133 |
|
134 | for(i = 0; i<dig; i++){
|
135 | if(txt[i] == 0) break; //END of String
|
136 | buff[i+extra] = tidParity(txt[i]);
|
137 | }
|
138 | while(i < dig){
|
139 | buff[i+extra] = 0x40;
|
140 | i++;
|
141 | }
|
142 |
|
143 | for(i=0; i<extra;i++){
|
144 | buff[i] = tidParity(symb[i]);
|
145 | }
|
146 |
|
147 | if(Stat != On) tidOn();
|
148 |
|
149 | }
|
150 |
|
151 | uint8_t tidSend(uint8_t data){
|
152 | uint8_t i;
|
153 | for(i = 0; i < retry; i++){
|
154 | if(twiSend(data) == 0) return 0;
|
155 | }
|
156 | return 1;
|
157 | }
|
158 |
|
159 | uint8_t twiSend(uint8_t dat){
|
160 | uint8_t i,j;
|
161 |
|
162 | for(i=0; i<8; i++){
|
163 | _delay_us(SCL_Low - 50);
|
164 | if(dat & (0b10000000>>i)) sdaHigh();
|
165 | else sdaLow();
|
166 | _delay_us(SCL_Guard);
|
167 | sclHigh();
|
168 | j=0;
|
169 | while(getScl() == 0){
|
170 | if(j++ >= 200) break;
|
171 | _delay_us(1);
|
172 | }
|
173 | _delay_us(SCL_High);
|
174 | sclLow();
|
175 | _delay_us(50);
|
176 | }
|
177 | sdaHigh();
|
178 | _delay_us(SCL_Low - 50);
|
179 | sclHigh();
|
180 | j=0;
|
181 | while(getScl() == 0){
|
182 | if(j++ >= 200) break;
|
183 | _delay_us(1);
|
184 | }
|
185 | _delay_us(SCL_High / 2);
|
186 | if(getSda()) i = 1;
|
187 | else i = 0;
|
188 | sclLow();
|
189 | _delay_us(50);
|
190 | return i;
|
191 | }
|