-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
2386 lines (1968 loc) · 47.9 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: main.c
* Author: daniel
*
* Created on 27 mars 2014, 21:59
*
* Version 1.05 November 2021
* - add COUNTER_PULLUP feature
* - increase delay in ReadDS18B20
*
* Version 1.04 October 2017
* - Fix start signal for DHT22 and DHT11. Start pulse is 20ms
* - Fix upper and lower case .c and .h file
* - Fix signed/unsigned warning
* - Fix memory overlap between Received buffer and DHT22 buffer
*/
#ifdef __XC__
#include <xc.h>
#else
#include <htc.h>
//#include <stddef.h>
#endif
#include "IOCycle.h"
#include "DHT22.h"
#include "DS18B20.h"
#include "CAPSense.h"
#include "IOConfig.h"
#include "RCServo.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MultiIO10 Multifunction 10 pins
//
// Simple program with different function on each pin
// using RS-485 with modbus protocol to communicate
//
//
// List of IO mode
//
//
// 1- Digital input. ( IO0..IO4 could have PULL_UP resistor)
// 2- Digital output.
// 3- Analog input withc selectable reference voltage.
// (1.024,2.048,4.096V and VDD).
// 4- R/C servo pwm mode . select step value in usec.
// ( Servo use software timer).
// 5- PWM output with 10 bits resolution (0..1023).
// Hardware comparator at 1.9Khz cycle ferquency
// 5- Reading of DHT11,DHT22 and AMM2302 temperature sensor.
// 6- Reading of DS18B20 temperature sensor
// 7- Cap sensor signale with 4 different power setting.
// 8- Pulse counter. Give Pulse count at Pulse/sec.
// Date: Dec 1 2021
// Version 1.06
// Processeur PIC16F1827
// Compiler XC8 V1.44 or lower
// add CRC check on DS18B20 and counter pull-up option
// Date: 27 Mars 2014
// programmer: Daniel Perron
// Version: 1.0
// Processeur: PIC12F1827
// logiciel de compilation: Microchip MPLAB X IDE (freeware version)
//
// The system use serial communication at 57600 BAUD with modbus protocol.
//
// Pin RB0 control the serial communication direction.
//
//
// MODBUS REGISTER LIST
//
//
///// Modbus Function 01 & 02
//
// Lire IO0 to IO9
//
// Modbus [SLA][1][0][A][0][1][CRC]
//
// SLA - Adresse
// A - 0=IO0 1=IO1 ... 9=IO9
// CRC - Cyclical redundancy check.
//
// Ex: Python Read IO0 Bit on slaveAddress module 1 (using PicModule.py)
//
// import PicModule
// m1 = PicModule.PicMbus(1)
// m1.readIO(0)
//
//
//
///// Modbus Function 3
//
// Modbus [SLA][3][0][A][0][1][CRC]
//
// A =
// 0..9 : Read R/C servo or PWM value of IO
// 160 : Read slave modbus address
// 250 : Read Software version
// 251 : Read Software Id number
// (0x100 .. 0x109): Read IO configuration Mode
//
//
//
// Ex: Python
// Lire numéro d'identification du logiciel
// m1.readId():
//
// or the long way
//
// m1.module.read_register(251,0,3);
//
//
///// Modbus Fonction 4
//
// Modbus [SLA][4][0][A][0][Number of register][crc]
//
// Function 4 Read Current Register
//
// Address 0x1000: Current VRef 2.048V A/D value
// Address 0x1001: Current Build-in Temperature Sensor
// Address 0xn0: Read current IOn
// A = 0x00N0: Read IO Sensor data where N is the IO id number.
// 0x1000: Read the 2.048V ref with the VDD has reference.
// 0x1001: Read build-in temperature diode. (VDD=Vref).
//
//
// Ex: Read DS18B20 Sensor At IO2
//
// m1.readDS18B20(2)
//
//
//
///// Modbus Fonction 5
//
// Modbus [SLA][5][0][A][0][DATA][crc]
//
// A = 0..9 : Set IO bit output
//
//
///// Modbus Fonction 6
//
// Modbus [SLA][6][0][A][16 bits DATA][CRC]
//
// A = 0..9: Set R/C Servo or PWM digital output
// Also clear COUNTER (In counter Mode)
// 160: Set new modbus slave address
// 0x100..0x10a: Set new IO configuration (IOCONFIG.H)
//
// ex: Change IO0 configuration to read DHT22 SENSOR
//
// m1.config(0,m1.IOCONFIG_DHT22)
//
//
#define SOFTWARE_ID 0x653A
#define RELEASE_VERSION 0x0106
/////////////////// How to program the IC.
//
// 1 - Use MPLAB with pickit 3 (need version 8.90 at least)
// or
// 2 - Use standalone pickit2 v 2.61 (Select the I.C. and load the corresponding hex file).
// or
// 3 - Use Raspberry Pi board with burnVLP.py . Software available from https://github.com/danjperron/burnLVP
//
//////////////////////////////////// GPL LICENSE ///////////////////////////////////
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* COMMUNICATION PROTOCOL
rate 57600 , 8 bits data , no parity
*/
////////////////////////// PORT DESCRIPTION
/*
* IO5 RA0
* IO6 RA1
* IO7 RA2
* IO8 RA3
* IO9 RA4
* RA5 MCLR (RESET)
* RA6 CRYSTAL CLOCK
* RA7 CRYSTAL CLOCK
* RB0 RS-485 DIRECTION
* IO1 RB1
* RB2 SERIAL IN
* IO0 RB3
* IO2 RB4
* RB5 SERIAL OUT
* IO3 RB6
* IO4 RB7
*
* P.S. ONLY IO0..IO4 support Counter, Pull-up, Cap Sense and DHT type sensor.
*
*
*/
//rs-485 data direction
#define TXM_ENABLE RB0
#ifndef BORV_LO
#define BORV_LO BORV_19
#endif
#define iabs(A) (A<0 ? (-A) : A)
// CONFIG1
#ifdef USE_EXTERNAL_XTAL
#pragma config FOSC= HS
#else
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#endif
#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#ifdef USE_EXTERNAL_XTAL
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#else
#pragma config PLLEN = OFF // PLL Enable (4x PLL enabled)
#endif
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
//Set default value
// IO0..IO9 config , modbus address
//__EEPROM_DATA(IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT,IOCONFIG_INPUT);
__EEPROM_DATA(IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT,IOCONFIG_OUTPUT);
__EEPROM_DATA(IOCONFIG_INPUT,IOCONFIG_INPUT,127,0xff,0xff,0xff,0xff,0xff);
unsigned char VRange; //1 = 1.024V, 2 = 2.048V, 3 = 4.096V else = VDD
unsigned char BadIO;
SettingStruct Setting;
#define TIMER_100MS 100
near volatile unsigned short Timerms; // Interrupt Timer counter in 1 ms
near volatile unsigned short PrimaryTimerms;
near volatile unsigned char TimerDeciSec; // modbus timer out in 1/10 of sec in 0.5 ms count
#pragma pack 1
typedef union {
unsigned short USHORT;
unsigned char BYTE[2];
}ByteShortUnion;
//near unsigned char CurrentTimer1H;
//near unsigned char CurrentTimer1L;
// serial buffer
#define SERIAL_BUFFER_SIZE 32
near volatile unsigned char InFiFo, OutFiFo; // these are the buffer pointers for interrupt serial communication; InFiFo ctrl by putch, OutFiFo ctrl by interrupt
near volatile unsigned char RcvInFiFo, RcvOutFiFo;
char SerialBuffer[SERIAL_BUFFER_SIZE];
char RcvSerialBuffer[SERIAL_BUFFER_SIZE];
unsigned char SerialSum; // use for check sum
unsigned char RcvSerialSum; // use for check sum verification
bit ModbusOnTransmit;
bit EnableConfigChange;
bit ForceReset;
char ModbusPacketBuffer[SERIAL_BUFFER_SIZE];
const unsigned char IOMASK[11]={0b00001000,0b00000010,0b00010000,0b01000000,0b10000000,\
0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0};
const unsigned char NOT_IOMASK[11]={0b11110111,0b11111101,0b11101111,0b10111111,0b01111111,\
0b11111110,0b11111101,0b11111011,0b11110111,0b11101111,0b11111111};
//cap sense mask
const unsigned char CSMASK[10]={0b00001001,0b00001011,0b00001000,0b00000101,0b00000110,\
0b00000000,0b00000001,0b00000010,0b00000011,0b00000100};
// MODBUS
// CRC16 source code is in CRC16.c file
extern unsigned short CRC16(unsigned char * puchMsg, unsigned char usDataLen);
unsigned char ModbusFunction;
unsigned char ModbusSlave;
unsigned short ModbusAddress;
unsigned short ModbusData;
volatile unsigned short ModbusCRC;
unsigned char ModbusFramePointer;
unsigned char ModbusBuffer[10];
//MODBUS EXCEPTION
#define ILLEGAL_FUNCTION 1
#define ILLEGAL_DATA_ADDRESS 2
#define ILLEGAL_DATA_VALUE 3
#define SLAVE_DEVICE_FAILURE 4
#define ACKNOWLEDGE 5
#define SLAVE_DEVICE_BUZY 6
#define NEGATIVE_AKNOWLEDGE 7
#define MEMORY_PARITY_ERROR 8
/* Timer utilisation
Timer0 interrupt timer
Timer1 Sensor timer utility,
Timer2 Servo Utility
Timer4 Hardware PWM
*/
void SaveSetting(void)
{
unsigned char idx;
unsigned char * pointer = (unsigned char *) &Setting;
for(idx=0; idx < sizeof(Setting);idx++)
eeprom_write(idx, *(pointer++));
}
void SetAnalogConfig(unsigned char Pin)
{
// SET ANALOG PIN
unsigned char ioconfig = Setting.IOConfig[Pin];
unsigned char _tmp= IOMASK[Pin];
if(Pin<5)
{
TRISB |= _tmp;
ANSELB |= _tmp;
}
else
{
TRISA |= _tmp;
ANSELA |= _tmp;
}
// SET REFERENCE VOLTAGE
FVRCONbits.ADFVR = ioconfig;
// VREF or VDD
if(ioconfig== IOCONFIG_ANALOGVDD)
ADCON1bits.ADPREF=0;
else
ADCON1bits.ADPREF=3;
}
void SetOutputConfig(unsigned char Pin)
{
unsigned char ioconfig = Setting.IOConfig[Pin];
unsigned char _tmp= NOT_IOMASK[Pin];
#ifndef USEASM
if(Pin<5)
{
PORTB &= _tmp;
TRISB &= _tmp;
ANSELB &= _tmp;
}
else
{
PORTA &= _tmp;
TRISA &= _tmp;
ANSELA &= _tmp;
}
#else
if(Pin<5)
{
#asm
movf SetOutputConfig@_tmp,w
movwf ??_SetOutputConfig
movf ??_SetOutputConfig,w
movlb 0
andwf 13,f
movlb 1
andwf 13,f
movlb 3
andwf 13,f
#endasm
}
else
{
#asm
movf SetOutputConfig@_tmp,w
movwf ??_SetOutputConfig
movf ??_SetOutputConfig,w
movlb 0
andwf 12,f
movlb 1
andwf 12,f
movlb 3
andwf 12,f
#endasm
}
#endif
}
char SetPWMConfig(unsigned char Pin,unsigned short value)
{
unsigned char msb;
unsigned char lsb; // lsb will hold the CCPIxCON + 2 lsb bits
msb = value >>2;
lsb = value & 3;
lsb <<=4;
lsb |= 0b00001100;
if(Setting.IOConfig[Pin]!=IOCONFIG_PWM)
return 0;
if(Pin==0)
{
// CCP1
TRISBbits.TRISB3=0;// output
CCP1CON= lsb;
CCPR1L = msb;
}
else if(Pin==3)
{
TRISBbits.TRISB6=0;// output
CCP2CON= lsb;
CCPR2L = msb;
}
else if(Pin==8)
{
TRISAbits.TRISA3=0;// output
CCP3CON= lsb;
CCPR3L = msb;
}
else if(Pin==9)
{
TRISAbits.TRISA4=0;// output
CCP4CON= lsb;
CCPR4L = msb;
}
else
{
BadIO=0;
return 0;
}
ServoTimer[Pin]=value;
return 1;
}
void SetPullUp(unsigned char Pin, unsigned char PullUp)
{
if(Pin<5)
{
if(PullUp)
WPUB |= IOMASK[Pin];
else
WPUB &= NOT_IOMASK[Pin];
}
else BadIO=1;
}
void SetInputConfig(unsigned char Pin)
{
unsigned char _tmp = IOMASK[Pin];
unsigned char _ntmp= NOT_IOMASK[Pin];
if(Pin<5)
{
TRISB |= _tmp;
ANSELB &= _ntmp;
}
else
{
TRISA |= _tmp;
ANSELA &= _ntmp;
}
}
void SetIOConfig(unsigned char Pin)
{
unsigned char loop;
ConfigUnion ioconfig;
ioconfig.Config = Setting.IOConfig[Pin];
ResetIOCycle();
IOSensorData[Pin].DWORD=0;
IOSensorData[Pin].WORD[2]=0;
if(Pin<5)
{
IOCounterFlag.Byte & = NOT_IOMASK[Pin];
}
if(Pin==0)
{
CCP1CON=0;
}
if(Pin==3)
{
CCP2CON=0;
}
if(Pin==8)
CCP3CON=0;
if(Pin==9)
CCP4CON=0;
if(Pin<5)
{
SetPullUp(Pin,1); // By default pull up is there
SetIOChange(Pin,0);
}
ServoTimer[Pin]=0; // disable Servo
if(ioconfig.Config<IOCONFIG_INPUT)
{
if(Pin<5)
SetPullUp(Pin,0);
SetAnalogConfig(Pin);
}
else if((ioconfig.SERVO) || (ioconfig.Config==IOCONFIG_OUTPUT))
{
SetOutputConfig(Pin);
}
else if(ioconfig.Config == IOCONFIG_PWM)
{
SetPWMConfig(Pin,0);
}
else if(ioconfig.COUNTER)
{
if(!ioconfig.PULLUP)
SetPullUp(Pin,0);
SetInputConfig(Pin);
SetIOChange(Pin,1);
if(Pin==0)
IOCounterFlag.IO0=1;
if(Pin==1)
IOCounterFlag.IO1=1;
if(Pin==2)
IOCounterFlag.IO2=1;
if(Pin==3)
IOCounterFlag.IO3=1;
if(Pin==4)
IOCounterFlag.IO4=1;
}
else if((ioconfig.CAP_SENSE) || (ioconfig.Config == IOCONFIG_INPUT))
{
if(ioconfig.CAP_SENSE)
{
if(Pin>4)
{BadIO=1;return;}
}
if(Pin<5)
SetPullUp(Pin,0);
SetInputConfig(Pin);
}
else if(ioconfig.DHT)
{
if(Pin>4)
{BadIO=1;return;}
}
else
SetInputConfig(Pin);
}
unsigned short ReadA2D(unsigned char channel)
{
ByteShortUnion value;
ADIE=0; // clear interrupt flag
ADIF=0;
ADON=1;
ADCON0bits.ADON=1;
ADCON0bits.CHS=channel;
__delay_ms(1);
ADCON0bits.ADGO=1;
while(ADCON0bits.ADGO==1);
__delay_ms(1);
ADCON0bits.ADGO=1;
while(ADCON0bits.ADGO==1);
value.BYTE[1]=ADRESH;
value.BYTE[0]=ADRESL;
return value.USHORT;
}
static void interrupt isr(void){
static volatile unsigned char _temp;
// timer 1 use by R/C Servo
if(TMR1IE)
if(TMR1IF)
{
TMR1IF=0;
if(ServoIndex<5)
{
#ifndef USEASM
PORTB &= ServoMask;
#else
{
#asm
movf _ServoMask,w
andwf _PORTB,f
#endasm
}
#endif
}
else
{
#ifndef USEASM
PORTA &= ServoMask;
#else
{
#asm
movf _ServoMask,w
andwf _PORTA,f
#endasm
}
#endif
}
TMR1ON=0;
}
if(IOCIE)
if(IOCIF)
{
// IOCBF=0;
#ifndef USEASM
_TMR0 = TMR0;
#else
{
#asm
movf _TMR0,w
movwf __TMR0
#endasm
}
#endif
if(IOCBF & DHTFlag)
{
// to speed up things, reducing interrupt process ,
// store timer 0 info into circular buffer and
// figure out timing calculation on main program
#ifndef USEASM
DHTBitBuffer[DHTBufferIndex++]=_TMR0;
if(DHTBufferIndex<46)
DHTBufferIndex++;
TMR0=0;
#else
{
#asm
clrf _TMR0
banksel(_DHTBufferIndex)
movf _DHTBufferIndex^512,w
addlw 0x20
movwf 6
movlw 2
movwf 7
movf _DHTBufferIndex^512,w
sublw 45
skipnc
incf _DHTBufferIndex^512,f
banksel(0)
movf __TMR0,w
movwf 1
; IOCBF&=~DHTFlag;
comf _DHTFlag,w
movlb 7; select bank 7
andwf _IOCBF&0x7f,f
#endasm
}
#endif
}
//}
#ifndef USEASM
_temp =(unsigned char) (IOCBF & IOCBN);
#else
{
#asm
movlb 7 ; select bank7
movf 22,w
andwf 21,w
skipnz
goto SKIPIOC
movlb 0
movwf isr@_temp
#endasm
}
#endif
if(_temp&8)
{
//IO0
IOCBFbits.IOCBF3=0;
if(IOCounterFlag.IO0)
{
// IOSensorData[0].DWORD++;
// COUNTER0++;
// use assembly since variable need to be big endian
// MODBUS use BIG ENDIAN
// IOSensorData force to be bank3 so it is ^384
// IOSensorData is 6BYTE
// to reduce time on irq just increment ICOUNTER
// and then on main program add ICOUNTER IOSensorData[0].DWORD++;
#asm
banksel(_ICOUNTER)
incf _ICOUNTER^384,f
btfss _IOCounterReset,3
goto INCCOUNTERIO0
bcf _IOCounterReset,3
movf (_COUNTER)^384,w
movwf (_IOSensorData+4)^384
movf (_COUNTER+1)^384,w
movwf (_IOSensorData+5)^384
clrf (_COUNTER)^384
clrf (_COUNTER+1)^384
INCCOUNTERIO0:
incf ((_COUNTER+1)^384),f
skipnz
incf (_COUNTER^384),f
movlb 0
#endasm
}
}
if(_temp&0x02)
{
//IO1
IOCBFbits.IOCBF1=0;
if(IOCounterFlag.IO1)
{
// IOSensorData[1].DWORD++;
// COUNTER1++;
// use assembly since variable need to be big endian
//
//
//
// to reduce time on irq just increment ICOUNTER
// and then on main program add ICOUNTER IOSensorData[1].DWORD++;
#asm
banksel(_ICOUNTER)
incf (_ICOUNTER+1)^384,f
btfss _IOCounterReset,4
goto INCCOUNTERIO1
bcf _IOCounterReset,4
movf (_COUNTER+2)^384,w
movwf (_IOSensorData+4+ARRAY1)^384
movf (_COUNTER+3)^384,w
movwf (_IOSensorData+5+ARRAY1)^384
clrf (_COUNTER+2)^384
clrf (_COUNTER+3)^384
INCCOUNTERIO1:
incf ((_COUNTER+3)^384),f
skipnz
incf ((_COUNTER+2)^384),f
movlb 0
#endasm
}
}
if(_temp&0x10)
{
//IO2
IOCBFbits.IOCBF4=0;
if(IOCounterFlag.IO2)
{
// IOSensorData[2].DWORD++;
// COUNTER2++;
// use assembly since variable need to be big endian
// to reduce time on irq just increment ICOUNTER
// and then on main program add ICOUNTER IOSensorData[2].DWORD++;
#asm
banksel(_ICOUNTER)
incf (_ICOUNTER+2)^384,f
btfss _IOCounterReset,5
goto INCCOUNTERIO2
bcf _IOCounterReset,5
movf (_COUNTER+4)^384,w
movwf (_IOSensorData+4+ARRAY2)^384
movf (_COUNTER+5)^384,w
movwf (_IOSensorData+5+ARRAY2)^384
clrf (_COUNTER+4)^384
clrf (_COUNTER+5)^384
INCCOUNTERIO2:
incf ((_COUNTER+5)^384),f
skipnz
incf ((_COUNTER+4)^384),f
movlb 0
#endasm
}
}
if(_temp&0x40)
{
//IO3
IOCBFbits.IOCBF6=0;
if(IOCounterFlag.IO3)
{
// IOSensorData[3].DWORD++;
// COUNTER3++;
// use assembly since variable need to be big endian
// to reduce time on irq just increment ICOUNTER
// and then on main program add ICOUNTER IOSensorData[3].DWORD++;
#asm
banksel(_ICOUNTER)
incf (_ICOUNTER+3)^384,f
btfss _IOCounterReset,6
goto INCCOUNTERIO3
bcf _IOCounterReset,6
movf (_COUNTER+6)^384,w
movwf (_IOSensorData+4+ARRAY3)^384
movf (_COUNTER+7)^384,w
movwf (_IOSensorData+5+ARRAY3)^384
clrf (_COUNTER+6)^384
clrf (_COUNTER+7)^384
INCCOUNTERIO3:
incf ((_COUNTER+7)^384),f
skipnz
incf ((_COUNTER+6)^384),f
movlb 0
#endasm
}
}
if(_temp&0x80)
{
//IO4
IOCBFbits.IOCBF7=0;
if(IOCounterFlag.IO4)
{
// IOSensorData[4].DWORD++;
// COUNTER4++;
// use assembly since variable need to be big endian
// to reduce time on irq just increment ICOUNTER
// and then on main program add ICOUNTER IOSensorData[4].DWORD++;
#asm
banksel(_ICOUNTER)
incf (_ICOUNTER+4)^384,f
btfss _IOCounterReset,7
goto INCCOUNTERIO4
bcf _IOCounterReset,7
movf (_COUNTER+8)^384,w
movwf (_IOSensorData+4+ARRAY4)^384
movf (_COUNTER+9)^384,w
movwf (_IOSensorData+5+ARRAY4)^384
clrf (_COUNTER+8)^384
clrf (_COUNTER+9)^384
INCCOUNTERIO4:
incf ((_COUNTER+9)^384),f
skipnz
incf ((_COUNTER+8)^384),f
movlb 0
#endasm
}
}
}
asm ("SKIPIOC:"); // fast lane to skip IOC
// this is the second test to remove glitch
// timer 1 use by R/C Servo
if(TMR1IE)
if(TMR1IF)
{
TMR1IF=0;
if(ServoIndex<5)
{
#ifndef USEASM
PORTB &= ServoMask;
#else
#asm
movf _ServoMask,w
andwf _PORTB,f
#endasm
#endif
}
else
{
#ifndef USEASM
PORTA &= ServoMask;
#else
#asm
movf _ServoMask,w
andwf _PORTA,f
#endasm
#endif
}
TMR1ON=0;
}
// Timer 2 clock system
if(TMR2IF){
TMR2IF=0;
if(TimerSecFlag)
{
ResetCounterFlag=1;
//Tell system to Transfer/reset counter
IOCounterReset.Byte = IOCounterFlag.Byte;
#asm
// need to set bank since we play with it in assembly
// TimerSecFlag is a bit so need to dive by 8
// banksel should be 0 anyway but just in case
banksel(_TimerSecFlag/8)