922 |
kaklik |
1 |
//////////////////////////////////////////////////////////////////////////// |
|
|
2 |
// |
|
|
3 |
// KNIHOVNA PRO PRACI S TOUCH MEMORY FY DALLAS |
|
|
4 |
// |
|
|
5 |
//////////////////////////////////////////////////////////////////////////// |
|
|
6 |
// |
|
|
7 |
// PEFI S.ICZ a.s. 2002, 2003 |
|
|
8 |
// |
|
|
9 |
// Verze 1.0 |
|
|
10 |
// 1.1 - pridana podpora vypinani pullupu |
|
|
11 |
// pres pin TOUCH_PULLUP_PIN |
|
|
12 |
// 1.2 - vyrazena podpora pullupu, do obecne knihovny nepatri |
|
|
13 |
// - funkce checkCRC se stala soucasti fce read_sn |
|
|
14 |
// - upraveny nazvy funkci, zpetna kompatibilita neni potreba |
|
|
15 |
// 1.3 - pridana samostatna funkce pro kontrolu CRC |
|
|
16 |
// |
|
|
17 |
/////////////////////////////////////////////////////////////////////////// |
|
|
18 |
// |
|
|
19 |
// funkce: |
|
|
20 |
// int8 TM_present() |
|
|
21 |
// - vraci TRUE, pokud je TM pritomna |
|
|
22 |
// |
|
|
23 |
// int8 TM_read_byte() |
|
|
24 |
// - precte bajt z TM |
|
|
25 |
// |
|
|
26 |
// int8 TM_write_byte(int8 data) |
|
|
27 |
// - zapise bajt do TM a vraci TRUE, pokud se to povedlo |
|
|
28 |
// |
|
|
29 |
// int8 TM_read_sn(int8 *sn) |
|
|
30 |
// - precte seriove cislo z TM pomoci prikazu 0x33 |
|
|
31 |
// - vraci TRUE, pokud je SN platne (spravne CRC) |
|
|
32 |
// |
|
|
33 |
// definice a prepinace |
|
|
34 |
// #define TM_PIN /* pin, na kterem je touch memory pripojena */ |
|
|
35 |
// #bit TM_PIN_BIT /* totez, ale definovan jako bit */ |
|
|
36 |
// |
|
|
37 |
// |
|
|
38 |
// defaultni definice |
|
|
39 |
#ifndef TM_PIN |
|
|
40 |
#define TM_PIN PIN_B0 |
|
|
41 |
#bit TM_PIN_BIT = PORTB.0 |
|
|
42 |
#endif |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
/****************************************************************************************************/ |
|
|
49 |
// Privatni sekce, cist jen v pripade, ze neco nefunguje |
|
|
50 |
/****************************************************************************************************/ |
|
|
51 |
// precte bajt z touch memory |
|
|
52 |
// |
|
|
53 |
int8 TM_read_byte() { |
|
|
54 |
int8 i,data; |
|
|
55 |
|
|
|
56 |
for(i=0;i<8;++i) { |
|
|
57 |
output_low(TM_PIN); |
|
|
58 |
delay_us(14); |
|
|
59 |
output_float(TM_PIN); |
|
|
60 |
delay_us(5); |
|
|
61 |
shift_right(&data,1,input(TM_PIN)); |
|
|
62 |
delay_us(100); |
|
|
63 |
} |
|
|
64 |
return(data); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
// zapise bajt do touch memory |
|
|
69 |
// |
|
|
70 |
int8 TM_write_byte(int8 data) { |
|
|
71 |
int8 i; |
|
|
72 |
|
|
|
73 |
for(i=0;i<8;++i) { |
|
|
74 |
output_low(TM_PIN); |
|
|
75 |
delay_us(10); |
|
|
76 |
if(shift_right(&data,1,0)) { |
|
|
77 |
output_float(TM_PIN); |
|
|
78 |
delay_us(1); // k vuli nabehu jednicky pres pullup |
|
|
79 |
if(!TM_PIN_BIT) return(0); |
|
|
80 |
} else { |
|
|
81 |
output_low(TM_PIN); |
|
|
82 |
if(TM_PIN_BIT) return(0); // k vuli nabehu nuly |
|
|
83 |
} |
|
|
84 |
delay_us(50); |
|
|
85 |
output_float(TM_PIN); |
|
|
86 |
// delay_us(50); |
|
|
87 |
} |
|
|
88 |
return(TRUE); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
// testuje pritomnost touch memory, pri pritomnosti vraci TRUE |
|
|
93 |
// |
|
|
94 |
int8 TM_present() { |
|
|
95 |
int1 present; |
|
|
96 |
|
|
|
97 |
output_low(TM_PIN); |
|
|
98 |
delay_us(500); |
|
|
99 |
output_float(TM_PIN); |
|
|
100 |
if(!input(TM_PIN)) |
|
|
101 |
return(FALSE); |
|
|
102 |
delay_us(65); |
|
|
103 |
present=!input(TM_PIN); |
|
|
104 |
delay_us(240); |
|
|
105 |
if(present) |
|
|
106 |
return(TRUE); |
|
|
107 |
else |
|
|
108 |
return(FALSE); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
// zkontroluje CRC zaznamu, vraci TRUE, pokud je zanam spravny |
|
|
112 |
// |
|
|
113 |
int8 TM_check_CRC(unsigned int8 *sn, unsigned int8 num) |
|
|
114 |
{ |
|
|
115 |
#ifdef CRC_TAB |
|
|
116 |
// tabulka pro vypocet CRC |
|
|
117 |
const int8 TouchCRC[256]= { |
|
|
118 |
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, |
|
|
119 |
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, |
|
|
120 |
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, |
|
|
121 |
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, |
|
|
122 |
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, |
|
|
123 |
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, |
|
|
124 |
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, |
|
|
125 |
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, |
|
|
126 |
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, |
|
|
127 |
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, |
|
|
128 |
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, |
|
|
129 |
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, |
|
|
130 |
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, |
|
|
131 |
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, |
|
|
132 |
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, |
|
|
133 |
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53}; |
|
|
134 |
#else |
|
|
135 |
int8 j,k,l; // promenne potrebne pro algoritmicky vypocet CRC |
|
|
136 |
#endif |
|
|
137 |
int8 CRC; // prubezne CRC |
|
|
138 |
int8 i; |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
#ifdef CRC_TAB |
|
|
142 |
// vypocet CRC tabulkou |
|
|
143 |
CRC=0; |
|
|
144 |
for(i=0;i<num;i++) CRC=TouchCRC[CRC ^ *(sn+i)]; |
|
|
145 |
if(CRC!=*(sn+7)) return(0); |
|
|
146 |
else return(1); |
|
|
147 |
#else |
|
|
148 |
// vypocet CRC algoritmicky |
|
|
149 |
CRC=0; |
|
|
150 |
for(i=0 ;i<num;i++) { |
|
|
151 |
k=*(sn+i); |
|
|
152 |
#asm |
|
|
153 |
movlw 8 |
|
|
154 |
movwf j // citac bitu |
|
|
155 |
loop: |
|
|
156 |
movf k,W // presun bajt do W |
|
|
157 |
xorwf CRC,W // prixoruj CRC a uloz do w |
|
|
158 |
movwf l |
|
|
159 |
rrf l,F // naxorovany bit do CY |
|
|
160 |
movf CRC,W |
|
|
161 |
btfsc 3.0 // preskoc, je-li CY nulove |
|
|
162 |
xorlw 0x18 |
|
|
163 |
movwf CRC |
|
|
164 |
rrf CRC,F // rotuj CRC |
|
|
165 |
rrf k // na dalsi bit |
|
|
166 |
decfsz j,F |
|
|
167 |
goto loop |
|
|
168 |
#endasm |
|
|
169 |
} |
|
|
170 |
if(CRC!=0) return(0); |
|
|
171 |
else return(1); |
|
|
172 |
#endif |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
// precte seriove cislo z touch memory, vraci TRUE je-li platne (spravne CRC) |
|
|
177 |
// |
|
|
178 |
int8 TM_read_sn(unsigned int8 *sn) |
|
|
179 |
{ |
|
|
180 |
int8 i; |
|
|
181 |
// precteni SN z touch memory |
|
|
182 |
TM_write_byte(0x33); |
|
|
183 |
for(i=0;i<8;i++) *(SN+i)=TM_read_byte(); // precti SN do buferu |
|
|
184 |
|
|
|
185 |
if(*sn==0) return(0); // family kod je nulovy a to je chyba, timto nahrazujeme precteni samych nul, coz ma platne CRC, ale neexistuje |
|
|
186 |
return(TM_check_CRC(SN,8)); |
|
|
187 |
} |
|
|
188 |
// End of FIle |
|
|
189 |
|