-
Notifications
You must be signed in to change notification settings - Fork 3
/
IOCycle.c
341 lines (274 loc) · 6.31 KB
/
IOCycle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifdef __XC__
#include <xc.h>
#else
#include <htc.h>
//#include <stddef.h>
#endif
#include "IOConfig.h"
#include "IOCycle.h"
#include "DS18B20.h"
#include "DHT22.h"
#include "CAPSense.h"
unsigned char CurrentIOStatus;
near ConfigUnion CurrentIOSensor;
near unsigned char CurrentIOPin;
near unsigned char CurrentIOCycle;
SensorDataUnion IOSensorData[INPUT_COUNT] @ 0x1A0;
BigSensorDataUnion WorkingSensorData;
// Counter will be on same bank
// than IOSensor data (bank 3)
// IOSensor is 10 data of 6 BYTE
// to speed up interrupt
// counter are alternating between COUNTER & ALT_COUNT
// ICOUNTER is use by interrupt to speed it up
unsigned char ICOUNTER[5] @ 0x1E0;
unsigned short COUNTER[5] @ 0x1E6;
volatile unsigned char _TMR0;
volatile unsigned short _TMR0_MSB;
volatile unsigned char BitCount;
volatile unsigned char WorkingByte;
volatile unsigned char CSum;
volatile unsigned char ByteIndex;
bit Timer0Overflow;
bit TimerSecFlag;
bit ResetCounterFlag;
near volatile unsigned char DHTFlag @ 0x070;
near volatile IOBits_t IOCounterFlag @ 0x071;
near volatile IOBits_t IOCounterReset @ 0x072;
near unsigned char Retry=0;
void DealWithError(void)
{
TMR0IE=0;
TMR0IF=0;
#ifndef USEASM
WorkingSensorData.WORD[2]=0xFFFF;
#else
{
#asm
movlw 255
movlb 0
movwf _WorkingSensorData+5
movwf _WorkingSensorData+6
#endasm
}
#endif
if(CurrentIOSensor.DHT)
DHT22Error();
}
void ScanNextIOPin(void)
{
unsigned char loop;
Retry=0;
#ifndef USEASM
CurrentIOPin++;
#else
{
#asm
incf _CurrentIOPin,f
#endasm
}
#endif
if(CurrentIOPin >= INPUT_COUNT)
CurrentIOPin=0;
CurrentIOSensor.Config= Setting.IOConfig[CurrentIOPin];
CurrentIOCycle= IO_CYCLE_IDLE;
CurrentIOStatus=IO_STATUS_UNKNOWN;
Timerms=0;
TMR0IE=0;
Timer0Overflow=0;
if(CurrentIOSensor.CAP_SENSE)
Timer0Overflow=1;
for(loop=0;loop<SENSOR_DATA_BYTE_MAX;loop++)
WorkingSensorData.BYTE[loop]=0;
}
void DoIOCycle(void)
{
SensorDataUnion *SensorPt;
unsigned char loop;
if(CurrentIOCycle== IO_CYCLE_END)
{
if(CurrentIOStatus==IO_STATUS_UNKNOWN)
DealWithError();
// Re-organize DATA
SensorPt = &IOSensorData[CurrentIOPin];
// DHT22 Nothing to do data is correct
if(CurrentIOStatus==IO_STATUS_BAD)
{
#ifndef USEASM
SensorPt->WORD[0]=0xffff;
#else
{
#asm
movf DoIOCycle@SensorPt,w
movwf 6
movlw 1
movwf 7
movlw 255
movwi[0]fsr1
movwi[1]fsr1
#endasm
}
#endif
}
else
{
SensorPt->BYTE[0]=0;
SensorPt->BYTE[1]=CurrentIOStatus;
}
if(CurrentIOSensor.Config == IOCONFIG_DHT11)
{
// OK device is only 8 bits so move MSB to LSB and zero MSB
// SensorPt->WORD[0]= CurrentIOStatus;
SensorPt->BYTE[3]=WorkingSensorData.BYTE[0];
SensorPt->BYTE[5]=WorkingSensorData.BYTE[2];
SensorPt->BYTE[2]=0;
SensorPt->BYTE[4]=0;
}
else if(CurrentIOSensor.Config == IOCONFIG_DHT22)
{
// OK device is only 8 bits so move MSB to LSB and zero MSB
// SensorPt->WORD[0]= CurrentIOStatus;
SensorPt->WORD[1]=WorkingSensorData.WORD[0];
SensorPt->WORD[2]=WorkingSensorData.WORD[1];
}
if(CurrentIOSensor.DS18B20)
{
// Reorganize BYTE
// SensorPt->WORD[0]= CurrentIOStatus;
SensorPt->BYTE[2]=WorkingSensorData.BYTE[1];
SensorPt->BYTE[3]=WorkingSensorData.BYTE[0];
SensorPt->BYTE[4]=0;
SensorPt->BYTE[5]=WorkingSensorData.BYTE[4];
}
if(CurrentIOSensor.CAP_SENSE)
{
// CAPSENSE
SensorPt->DWORD=WorkingSensorData.DWORD;
}
// now it is time for next sensor;
ScanNextIOPin();
}
else
// if(CurrentIOCycle < IO_CYCLE_WAIT)
{
if(CurrentIOSensor.DHT)
DoDHT22Cycle();
else if(CurrentIOSensor.DS18B20)
DoDS18B20Cycle();
else if(CurrentIOSensor.CAP_SENSE)
DoCAPSENSECycle();
else
{
// sensor doesn't need cycle jump to the next one
ScanNextIOPin();
}
}
}
void ResetIOCycle(void)
{
unsigned char loop;
for(loop=0;loop<INPUT_COUNT;loop++)
{
if((Setting.IOConfig[loop]==IOCONFIG_DHT11) ||
(Setting.IOConfig[loop]==IOCONFIG_DHT22))
SetIOChange(loop,0);
CurrentIOCycle= IO_CYCLE_IDLE;
CurrentIOSensor.Config = Setting.IOConfig[0];
CurrentIOPin=0;
DHTFlag=0;
}
}
void SetInputMode(unsigned char Pin)
{
unsigned char _tmp= IOMASK[Pin];
if(Pin<5)
TRISB |= _tmp;
else
TRISA |= _tmp;
}
void SetOutputMode(unsigned char Pin)
{
unsigned char _tmp= NOT_IOMASK[Pin];
if(Pin<5)
TRISB &= _tmp;
else
TRISA &= _tmp;
}
char ReadIOPin(unsigned char Pin)
{
unsigned char _tempb;
unsigned char mask = IOMASK[Pin];
if(Pin<5)
_tempb = (unsigned char) (PORTB & mask);
else
_tempb = (unsigned char) (PORTA & mask);
if(_tempb==0)
return 0;
return 1;
}
void WriteIO(unsigned char Pin,unsigned char value)
{
unsigned char mask = IOMASK[Pin];
unsigned char nmask = NOT_IOMASK[Pin];
di();
if(Pin <5)
{
if(value==0)
{
PORTB &= nmask;
}
else
{
PORTB |= mask;
}
}
else
{
if(value==0)
{
PORTA &= nmask;
}
else
{
PORTA |= mask;
}
}
ei();
}
void SetIOChange(unsigned char Pin, unsigned char value)
{
unsigned char _temp;
di();
if(Pin<5)
if(value==0)
{
_temp = NOT_IOMASK[Pin];
#ifndef USEASM
IOCBN&= _temp;
#else
{
#asm
movf SetIOChange@_temp,w
movlb 7
andwf _IOCBN&0x7F,f
#endasm
}
#endif
}
else
{
_temp= IOMASK[Pin];
#ifndef USEASM
IOCBN|= _temp;
#else
{
#asm
movf SetIOChange@_temp,w
movlb 7
iorwf _IOCBN&0x7f,f
#endasm
}
#endif
}
ei();
}