-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRelay_18F46K80_GEN3.pbp
3219 lines (3014 loc) · 126 KB
/
Relay_18F46K80_GEN3.pbp
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
'************************************************************************
'Copyright (c) 2016 - OpenMotics <[email protected]>
'This file is part of OpenMotics.
'OpenMotics 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.
'OpenMotics 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 OpenMotics. If not, see <http://www.gnu.org/licenses/>.
'************************************************************************
'Firmware to be used with the OpenMotics 8 ports output (Relay) Module
'Written in Pic Basic Pro 2.6 however it should be compatible with Pic Basic Pro 3.0 but this is not yet tested
'Interrupt system of Darrel Taylor is used see http://darreltaylor.com/DT_INTS-18/home.html
'For more information see wiki.openmotics.com
@ CONFIG SOSCSEL=DIG
DEFINE OSC 40
VERSION con 6
FIRMWARE con 0
BUILT con 36
DEFINE USE_LOWPRIORITY 1
define NO_CLRWDT 1 ; No automatic clear watchdog timer
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
INCLUDE "ReEnterPBP-18LP.bas" ; Include if using Low Pr. PBP INTS
'OSCCON.0=0 'external oscillator
'OSCCON.1=0
'OSCTUNE.6=1 'PLL enabled
ANCON0=%00000000 'All ports configured as digital ports
ANCON1=%00000000 'All ports configured as digital ports
CCP2CON=0
CLEARWDT
'hier i2c
hardware var byte
hardware=8
'--------Hardware addressing ---------------------------------------------------
RS485_mode var PORTD.4 ' Mode for RS485 chip (send or receive)
input PORTC.7
output PORTC.6
sda var PORTC.4
scl var PORTC.3
Button1 var PORTB.1
Button1_2 var PORTA.5 'HW V4
Button2 var PORTB.0
Button2_2 var PORTE.0 'HW V4
Relay1On var PORTC.5
Relay1Off var PORTD.3
Relay2On var PORTD.2
Relay2Off var PORTD.1
Relay3On var PORTD.0
Relay3Off var PORTC.2
Relay4On var PORTC.1
Relay4Off var PORTC.0
Relay8On var PORTD.6
Relay8On_2 var PORTB.0 'HW V4
Relay8Off var PORTD.5
Relay7On var PORTD.7
Relay7On_2 var PORTB.1 'HW V4
Relay7Off var PORTB.2
Relay6On var PORTB.3
Relay6Off var PORTA.0
Relay5On var PORTB.4
Relay5Off var PORTB.5
Relay1On_3 var PORTB.4 'HW Gen3 with RJ45 connector
Relay1Off_3 var PORTB.5
Relay2On_3 var PORTB.3
Relay2Off_3 var PORTA.0
Relay3On_3 var PORTA.5
Relay3Off_3 var PORTB.2
Relay4On_3 var PORTE.0
Relay4Off_3 var PORTD.5
Relay5On_3 var PORTC.5
Relay5Off_3 var PORTD.3
Relay6On_3 var PORTD.2
Relay6Off_3 var PORTD.1
Relay7On_3 var PORTD.0
Relay7Off_3 var PORTC.2
Relay8On_3 var PORTC.1
Relay8Off_3 var PORTC.0
'-----------Constants and Variables for I2C-------------------------------------
'hier module indicate
NORMAL_MODE con 0 'Status led Green
NORMAL_MODE_NOT_DISCOVERED con 1 'Status led OFF
NORMAL_MODE_BUS_ERRORS con 2 'Status led Red blinking
NORMAL_MODE_LOST_COMMUNICATION con 3 'Status led Red
NORMAL_MODE_UNKNOWN con 8 'Status led OFF
INIT_MODE_DISCOVERED con 4 'Status led Orange
INIT_MODE_NOT_DISCOVERED con 5 'Status led Orange blinking
TEST_MODE_LED_ON con 6 'During Test mode, switch status led ON in Green
TEST_MODE_LED_OFF con 7 'During Test mode, switch status led OFF
BUS_OK con 0
BUS_COMMUNICATION_ERRORS con 1
BUS_COMMUNICATION_LOST con 2
BUS_UNKNOWN con 3
'Hier I2C
SENT_START con 0
REPEATED_START con 1
SENT_ADDRESS_W con 2
SENT_ADDRESS_R con 3
CHECK_ACK con 4
SENT_COMMANDCODE con 5
SENT_COMMANDDATA con 6
SENT_COMMANDDATA1 con 7
SENT_COMMANDDATA2 con 8
SENT_COMMANDDATA3 con 9
SENT_ACK con 10
SENT_NACK con 11
SENT_STOP CON 12
CHECK_INTERRUPT con 13
ENABLE_RECEIVING con 14
RECEIVE_DATA con 15
'hier eeprom
WAIT_WRITE con 16
PROCESS_DONE con 17
MAXI2CBYTES con 5
MAXI2CSTEPS con 45
TCA9539_0_ADDRESS con %11101010 'Chip for leds and buttons
TCA9539_1_ADDRESS con %11101100 'Chip for leds
MAX7312_0_ADDRESS con %01001010 'Alternative Chip for leds and buttons
MAX7312_1_ADDRESS con %01001100 'Alternative Chip for leds
M24C02_ADDRESS CON %10101100
LM75_ADDRESS con %10011110
'hier eeprom
M24LC024_ADDRESS con %10101010 'External eeprom on Base panel to store serial number and test information
TCA9539_0_OK con %00000001
TCA9539_1_OK con %00000010
MAX7312_0_OK con %00000100
MAX7312_1_OK con %00001000
M24C02_OK con %00010000
LM75_OK con %00100000
PRESSED con 0
RELEASED con 1
'hier eeprom
INT_TO_EXT con 0
EXT_TO_INT con 1
NO_SYNC con 2
'hier eeprom
INT_EEP_DAY_ADDRESS con 242 'eeprom addresses for serial number
EXT_EEP_DAY_ADDRESS con 242
INT_EEP_MONTH_ADDRESS con 241
EXT_EEP_MONTH_ADDRESS con 241
INT_EEP_YEAR_ADDRESS con 240
EXT_EEP_YEAR_ADDRESS con 240
INT_EEP_PROD_ADDRESS con 243
INT_EEP_SER_MSB_ADDRESS con 244
INT_EEP_SER_LSB_ADDRESS con 245
INT_EEP_ERR_ADDRESS con 246
PROTECTED_AREA_START con 240
PROTECTED_AREA_END con 246
INTERNAL_EEPROM_SIZE con 1024
SYNC_RANGE1_START CON 0
SYNC_RANGE1_END con 255
SYNC_RANGE2_START CON 200
SYNC_RANGE2_END con 255
SYNC_RANGE3_START CON 256
SYNC_RANGE3_END con 351
MAXRS485LENGTH con 11 'Max length (in RS485 message) that can be used. Cannot be increased without changing variables A_Queue_...
LED_ON con 1
LED_OFF con 0
YES con 1
NO con 0
'hier module indicate
STATUS_ON con 1
STATUS_OFF con 0
module_indicate_status var bit
module_indicate_status=STATUS_OFF
module_indicate_timer var word
module_indicate_fl_timer var byte
module_indicate_led_on var bit
Status_mode var byte
Status_mode_old var byte
Status_mode_old=255
Bus_mode var byte
Bus_mode=BUS_UNKNOWN
bus_comm_error_timeout var word
bus_comm_lost_timeout var word
bus_comm_error_timeout=6250
bus_comm_lost_timeout=6250
'hier core
CORE con 0
TRADITIONAL_MASTER con 1
UNKNOWN con 255
i2ccont var byte 'chip address
i2cdata var byte[MAXI2CBYTES] 'data to read or write
i2cdataTemp var byte
i2cMode var byte
i2cModeOld var byte
i2cAck var bit
i2cInt var bit
i2cCommandCode var byte
i2cCommandData var byte
i2cCommandData1 var byte
i2cCommandData2 var byte
i2cCommandData3 var byte
instruction_done var bit
I2cStep var byte[MAXI2CSTEPS]
Fase var byte
ByteNr var byte
i2cerror var word
tca9539_data0 var word
tca9539_data1 var word
tca9539_data0_old var word
tca9539_data1_old var word
'led_st_red var bit
'led_st_green var bit
i2c_hw_ok var byte
Temperature var byte
Temperature=255
FirstOutputOfBank var byte
Output0on var bit
Output1on var bit
Output0onOld var bit
Output1onOld var bit
'hier eeprom
Ext_eeprom_found var bit
Ext_eeprom_found=NO
Eeprom_sync_direction var byte
day var byte
month var byte
year var byte
i_word var word
data_byte var byte
data_byte2 var byte
eeprom_address var byte
eeprom_data var byte
Dummybyte var byte
i2c_hw_ok=0
tca9539_data0=65535
tca9539_data1=65535
tca9539_data0_old=0
tca9539_data1_old=0
RESET_PIN VAR PORTE.1 'Reset pin of TCA9538 (gnd to reset)
INTERRUPT_PIN VAR PORTE.2 'Interrupt pin from TCA9538
LED_STATUS VAR PORTE.1 'for non GEN3 hw
'hier eeprom
WP var PORTA.1 'Write Potect external eeprom/fram
WP1 var PORTA.0
WP=YES
WP1=YES
shutter_status_received var bit
shutter_status_received = 0
output RESET_PIN
high RESET_PIN
INPUT INTERRUPT_PIN
i2cMode=0
i2cInt=0
i2cerror=0
'-------------------------------------------------------------------------------
output RS485_mode
input sda
input scl
'output Relay1On
'output Relay1Off
'output Relay2On
'output Relay2Off
'output Relay3On
'output Relay3Off
'output Relay4On
'output Relay4Off
'output Relay5On
'output Relay5Off
'output Relay6On
'output Relay6Off
'output Relay7Off
'output Relay8Off
'i2c --------------------------------------------------------------------------
SSPADD.0=1 '100kHz
SSPADD.1=1
SSPADD.2=0
SSPADD.3=0
SSPADD.4=0
SSPADD.5=1
SSPADD.6=1
SSPCON1=%00101000 'turn on MSSP and set for master mode i2c
'uart--------------------------------------------------------------------------
low RS485_mode
'BAUDCON1 = %01001000
BAUDCON1.5=0 'receive data is not inverted
BAUDCON1.3=1 'BRG16=1
BAUDCON1.4=0 'idle state for Txd is 1
SPBRGH1 = 0 '115200 baud
SPBRG1 = 86
RCSTA1 = %00010000
TXSTA1 = %00000100
Mod_ID0 var byte
Mod_ID1 var byte
Mod_ID2 var byte
Mod_ID3 var byte
Mod_NE var byte 'N->New module,E->Existing module
'Variables
test_bit var bit
a var byte
b var byte
x var byte
y var byte
z var byte
q var byte
s var byte
u var byte
v var byte
r var byte
w var byte
t var byte
j var byte
s1 var byte
s2 var byte
i var byte
w1 var word
tmp var byte[8]
test var byte
test_mode var bit
test_mode=NO
reset_count var word
reset_count=0
Out var byte
out=0
OutOld var byte
OutOldMod var byte
OutRec var byte
OutConfig var byte
OutRecConfig var byte
OutChangedByButton var bit
OutChangedByButton=NO
OutputStatusReceived var bit
OutputStatusReceived=NO
OutChanged var byte
OutDelayMod var BYTE
OutChangedMod var byte
OutUpdateAll var bit
OutUpdateAll=1
OutAlert var byte
OutAlertCounter var word
OutAlertUpdate var bit
OutAlertOn var bit
OutAlert=0
OutAlertCounter=0
OutAlertUpdate=0
OutAlertOn=0
timer var byte
timer=0
indicate var byte
indicate=255
indicateInitButton var byte 'When pressing init button, appropriate led will flicker to be able to switch ON or OFF
indicateInitButton=0
Button1Pressed var bit
Button1Pressed=0
Button1Timer var word
Button1Timer2 var word
Button1Timer2=0
Butt1 var bit
Butt2 var bit
RndValue var byte
startup var bit
startup=1
Butt1=RELEASED
Butt2=RELEASED
RelayTimer var bit
RelayTimerOn var byte[8]
RelayTimerOff var byte[8]
for i=0 to 7
RelayTimerOn[i]=255
RelayTimerOff[i]=255
next i
eep_addr var word
eep_addr_temp var word
err var byte
mode var byte
mode="L"
OutBank var byte
Buffer con 180
StackSer1 var byte[buffer]
RecSer1 var bit '=1 in receiving mode
RecSerTemp1 var byte
RecSerTemp2 var byte
PointerSer1 var byte
InterpreteSer1 var bit
RecSer1=0
PointerSer1=0
InterpreteSer1=0
ReceiveTemp var byte
broadcast var bit
counter var byte
PortOld var byte[3]
CRC var word
i2cData0 var byte
i2cData1 var byte
i2cData2 var byte
i2cMode=0
i2cInt=0
'LedCounter var byte
'LedCounter=0
'LedAction var bit
'LedAction=0
RelayCounter var byte
RelayCounter=0
RelayAction var bit
RelayAction=0
OutDelay var bit[4]
CurrentOut var byte
OutMod var byte
OutConfigMod var byte
CurrentOutAtStartup var bit
CurrentOutAtStartup=YES
PowerSafe var bit
ButtonLedsOn var bit
ButtonLedsOn=0 'Keep the leds ON
PowerSafe=0 'Module not in PowerSafe Mode (Leds on)
ButtonTimer var byte
ButtonTimer=0
OutEeprom var byte
OutEeprom=0
selection_byte var byte
send_message var bit
message_check var byte[8]
startup_master var byte
startup_master=YES
A_rs485_data var byte[MAXRS485LENGTH-3]
rs485_length var byte
'hier core
Master_type var byte
'hier init
init_message_sent var bit
init_message_sent=NO
automatic_module_search var bit
automatic_module_search=NO
Serial_year var byte
Serial_month var byte
Serial_day var byte
Serial_prod_company var byte
Serial_number var word
Serial_errors var byte
'hier eeprom
rs485_eeprom_address var word
rs485_eeprom_byte var byte
rs485_eeprom_rw var byte
'led change
status_led_onoff var bit 'General if the status led is ON (normal or blinking) or OFF
status_led_current_onoff var bit 'This is the status of the led itself, during blinking, this will go ON/OFF
status_led_blink var bit
status_led_counter var byte
status_led_color var byte
module_comm_counter var word
bus_comm_counter var word
module_comm_counter=9231 '1 minute timeout
bus_comm_counter=9231 '1 minute timeout
'modechanged var bit
'modechanged=NO
RED con 0
GREEN con 1
ORANGE con 2
CLEARWDT
'Define interrupts
;----[High Priority Interrupts]-----------------------------------------------
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RX1_INT, _Receive1, PBP, no
endm
INT_CREATE ; Creates the High Priority interrupt processor
;----[Low Priority Interrupts]------------------------------------------------
INT_LIST_L macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _Timer0, PBP, yes
INT_Handler TMR2_INT, _Timer2, PBP, yes
INT_Handler SSP_INT, _SSP, PBP, no
endm
INT_CREATE_L ; Creates the Low Priority interrupt processor
ENDASM
T0CON=%00000001 ; T0 = 16-bit, 1:4 Prescaler
T2CON=%01111111
@ INT_ENABLE RX1_INT ; Enable USART Receive interrupts
@ INT_ENABLE TMR0_INT ; enable Timer 0 interrupts
@ INT_ENABLE TMR2_INT ; enable Timer 2 interrupts
@ INT_ENABLE SSP_INT
TMR0L=0 'reset value
TMR0H=0
T0CON.7=0 'timer 0 disabled
PIE1.1=1 'timer 2 interrupt enabled
T2CON.2=1 'start timer2
PIE1.3=1 'enable SSP interrupt
INTCON.6=1
INTCON.7=1
RCSTA1.7=1 'Enable UART1
RCSTA1.4=0 'disable UART1 receiver
PIE1.5=1 'Enable receive UART1 interrupt
PIR1.5=0 'Clear RC1IF
RCSTA1.4=1 'disable UART1 receiver
TXSTA1.5=1 'enable UART1 transmit
'To be used for debugging
BAUDCON2.5=0 'receive data is not inverted
BAUDCON2.3=1 'BRG16=1
BAUDCON2.4=0 'idle state for Txd is 1
SPBRGH2 = 0 '115200 baud
SPBRG2 = 86
RCSTA2 = %00010000
TXSTA2 = %00000100
'pause 400
RCSTA2.7=1 'Enable UART2
RCSTA2.4=0 'disable UART2 receiver
RCSTA2.4=1 'disable UART2 receiver
TXSTA2.5=1 'enable UART2 transmit
'hier i2c
'hier eeprom
gosub check_ext_eeprom
gosub Set_eeprom_sync_direction
gosub sync_eeprom
gosub Read_startup_data_eeprom
CLEARWDT
gosub detect_i2c_chips
gosub check_startupreset 'check if action button has been pressed during startup
out=0 'All outputs off
OutOld=0
'gosub write_output
if Mod_NE=="N" then
' Switch off all relays to make sure they are in a good state after transport
gosub AllRelaysOff
Outeeprom=0
gosub SwitchAllRelaysStaged
endif
'gosub Check_Relais
'gosub Relais_On_off 'Switch on/off relays
'high led_logo
'gosub Nightrider_on_frontpanel
gosub All_leds_on
CLEARWDT
pause 500
'gosub Indicate_mode_on_frontpanel
status_led_counter=0 'only used when blinking is ON (then set at 77 (=500ms))
gosub Switch_Shutter_off_after_startup
'gosub Set_status_led
gosub Print_serial_number
hserout2 ["Startup done F",dec version,".",dec firmware,".",dec built," ID (",Mod_NE,"): ",dec Mod_ID0,".",dec Mod_ID1,".",dec Mod_ID2,".",dec Mod_ID3,13,10,"------------------------------------",13,10]
CLEARWDT
'----------------------------------------------------------------------------
' Main routine
'----------------------------------------------------------------------------
mainloop:
CLEARWDT
gosub interprete_serial 'check information coming from the RS485 bus
gosub check_RelayAction 'When a Relay coil is activated, switch off after +/-40ms
gosub check_SerialErrors 'Check if serial errors happened
if mode="L" then
gosub check_inputs2 'check if top button or init button is pressed
else 'bus is in Initialization mode
gosub check_inputs 'during initialization, init button needs to be checked
endif
CLEARWDT
gosub Check_Relais
gosub Relais_On_off 'Switch on/off relays
gosub check_button_pressed 'Check button pressed
'led change
gosub check_LedAction 'Set leds correctly
gosub Display_AlertLeds 'Check if any leds needs to flash, normal or inverted state
gosub check_status_led 'Check status led and set correctly, also set led linked to health status of the module
'hier module indicate
gosub Check_module_indicate
gosub check_bus_health
Goto mainloop
'--------------------------------------------------------------------------
' Interrupt routines
'--------------------------------------------------------------------------
Receive1: 'RS485 receive routine
recsertemp1=recsertemp2 'Keep previous character also
recsertemp2=RCREG1
if recser1=1 then 'Receiving?
StackSer1[PointerSer1]=recsertemp2
pointerser1=pointerser1+1
if recsertemp1=13 and recsertemp2=10 then
if Pointerser1>17 then
RCSTA1.4=0 'disable receiving serial 1
RecSer1=0 'stop receiving ser1 and buffering
InterpreteSer1=1
endif
else
if pointerser1>(Buffer-2) then 'Buffer overflow -> stop receiving and interprete
RCSTA1.4=0 'disable receiving serial 1
RecSer1=0 'stop receiving ser1 and buffering
InterpreteSer1=1
endif
endif
else 'Not in receiving mode
if recsertemp1="S" and recsertemp2="T" then
Pointerser1=0
recser1=1 'start receiving ser1
endif
endif
RCSTA1.4=1 'enable UART1 receiver
@ INT_RETURN
'hier i2c
Timer0: 'interrupt after 26ms when i2c is active
T0CON.7=0 'timer 0 disabled
INTCON.2=0 'interrupt flag cleared
i2cerror=i2cerror+1
hserout2 ["Error I2C A=",BIN8 i2ccont," M=",dec i2cmode,13,10]
i2cmode=0 'stop
err=1 'error
@ INT_RETURN
Timer2: 'interrupt every 6,5ms
T2CON.2=0
PIR1.1=0 'clear timer2 interrupt flag
timer=timer+1
'led change
if status_led_counter>0 then status_led_counter=status_led_counter-1
if module_comm_counter>0 then module_comm_counter=module_comm_counter-1
if bus_comm_counter>0 then bus_comm_counter=bus_comm_counter-1
'hier module indicate
if module_indicate_timer>0 then module_indicate_timer=module_indicate_timer-1
if module_indicate_fl_timer>0 then module_indicate_fl_timer=module_indicate_fl_timer-1
if bus_comm_error_timeout>0 then bus_comm_error_timeout=bus_comm_error_timeout-1
if bus_comm_lost_timeout>0 then bus_comm_lost_timeout=bus_comm_lost_timeout-1
if timer=15 then
OutAlertOn=0
if OutAlertCounter>0 then OutAlertUpdate=1
endif
if timer>77 then
timer=0
'low led_logo
OutAlertOn=1
if OutAlertCounter>0 then OutAlertUpdate=1
ButtonTimer=ButtonTimer+1
if ButtonTimer>5 and ButtonLedsOn=1 then
PowerSafe=1 'Led off again
ButtonLedsOn=0
OutalertUpdate=1
endif
endif
'if LedCounter>0 then
' LedCounter=LedCounter-1
' if LedCounter=0 then LedAction=1
'endif
if RelayCounter>0 then
RelayCounter=RelayCounter-1
if RelayCounter=0 then RelayAction=1
endif
if outalertcounter>0 then OutAlertCounter=Outalertcounter-1
if outalertcounter=0 then
indicateInitButton=0 'if Timeout has happened, the output to toggle via button will start again at button0
endif
if Button1Pressed=1 and Button1Timer>0 then Button1Timer=Button1Timer-1
if Button1Pressed=1 then Button1Timer2=Button1Timer2+1
RndValue=RndValue+1
RelayTimer=1
T2CON.2=1
@ INT_RETURN
SSP:
PIE1.3=0 'disable interrupt
i2cMode=i2cMode+1
i2cInt=1
PIR1.3=0 'clear interrupt flag
PIE1.3=1 'enable interrupt
@ INT_RETURN
'---------------------------------------------------------------------------
' RS485 routines
'---------------------------------------------------------------------------
'led change
check_status_led:
if status_led_onoff==LED_ON then
if status_led_blink==yes then
if status_led_counter==0 then
status_led_counter=77 'set at 500ms
toggle status_led_current_onoff
gosub Set_status_led
endif
endif
endif
return
Interprete_Serial: 'Interprete data coming from the RS485 bus
if InterpreteSer1=1 then
RCSTA1.4=0 'disable receiving serial 1
InterpreteSer1=0
if pointerser1>6 then
bus_comm_lost_timeout=6250
'delete
'hserout2 ["Message received ",dec StackSer1[0]," ",dec StackSer1[1]," ",dec StackSer1[2]," ",dec StackSer1[3]," ",dec StackSer1[4]," ",dec StackSer1[5]," ",dec StackSer1[6]," ",dec StackSer1[7]," Mode=",dec mode,13,10]
if stackser1[0]="O" then 'output module
gosub Check_Output_Module
elseif stackser1[0]=0 and stackser1[1]=0 and stackser1[2]=0 and stackser1[3]=0 then 'Broadcast message
gosub interprete_broadcast
elseif mode="I" and stackser1[0]="A" and stackser1[1]="D" then 'activate device
hserout2 ["AD ",dec init_message_sent," ",dec automatic_module_search,13,10]
gosub activate_device
elseif stackser1[0]==21 and stackser1[1]==0 and stackser1[2]==0 and stackser1[3]==0 then 'Factory reset message
hserout2 ["Factory reset request",13,10]
if mode=="I" and stackser1[4]=="F" and stackser1[5]=="R" and stackser1[6]==17 and stackser1[7]==69 then 'Bus in Init mode and valid message received?
hserout2 ["Valid request, execute factory reset",13,10]
gosub clear_id
pause 5
asm
reset
endasm
endif
endif
endif
pointerser1=0
RCSTA1.4=1 'enable receiving serial 1
endif
return
Clear_id:
for i=0 to 6
write i,255
pause 10
eeprom_address=i 'addressing
eeprom_data=255 'Eeprom data
gosub write_eeprom
next i
hserout2 ["Clear ID",13,10]
read 130,DummyByte 'set pointer to place 130, if brown out or others happens, the current addresses won't be affected
return
Interprete_broadcast:
'delete
'hserout2 ["Broadcast received ",dec StackSer1[4]," ",dec StackSer1[5]," ",dec StackSer1[6]," ",dec StackSer1[7],13,10]
if stackser1[4]="L" and stackser1[5]="L" then 'Bus in Live mode
if mode="I" then
'status_led_onoff=LED_ON 'general status of the led (ON - blinking or normal, OFF)
'status_led_current_onoff=led_on 'current status of the led (when blinking, this variable will toggle)
'status_led_blink=no 'Led must blink or not
'status_led_color=GREen 'Color of the led
'gosub Set_status_led
hserout2 ["Live",13,10]
endif
mode="L"
automatic_module_search=NO
elseif stackser1[4]="I" and stackser1[5]="I" then 'Bus in Initialize mode
gosub generate_id
if mode="L" then
'status_led_onoff=LED_ON 'general status of the led (ON - blinking or normal, OFF)
'status_led_current_onoff=led_on 'current status of the led (when blinking, this variable will toggle)
'if Mod_NE=="E" then
' status_led_blink=NO 'Led must blink or not
'else
' status_led_blink=YES 'Led must blink or not
'endif
'status_led_color=ORANGE 'Color of the led
'gosub Set_status_led
hserout2 ["Init",13,10]
endif
mode="I"
elseif StackSer1[4]=="S" and StackSer1[5]=="M" then 'Search Module
if StackSer1[15]=="C" then 'CRC in message
rs485_length=15
automatic_module_search=YES
'gosub check_init_led
gosub Check_CRC16_in_RS485_message
'hserout2 ["Search Module ",dec CRC.byte1," ",dec CRC.byte0," ",dec A_StackSer1[16]," ",dec A_StackSer1[17],13,10]
if CRC.byte1==StackSer1[16] and CRC.byte0==StackSer1[17] then 'CRC OK
selection_byte=StackSer1[14]
message_check[0]=Mod_id0
message_check[1]=Mod_id1
message_check[2]=Mod_id2
message_check[3]=Mod_id3
message_check[4]=Mod_NE
message_check[5]=0 'Not used
message_check[6]=0 'Not used
message_check[7]=0 'Not used
send_message=YES
for i=0 to 7
t=selection_byte<<i
if t.bit7==1 then
'hserout2 ["Check Pos ",dec i," data ",dec message_check[i]," ",dec A_StackSer1[6+i],13,10]
if message_check[i]!=StackSer1[6+i] then send_message=NO
endif
next i
if send_message==YES then
'hserout2 ["Message can be sent!",13,10]
s1=0
s2=Mod_NE 'E->existing,N->New
gosub rs485_send
endif
endif
endif
elseif stackser1[4]="N" and stackser1[5]="N" then 'Module in Normal state (Leds on)
PowerSafe=0
OutAlertUpdate=1
elseif stackser1[4]="S" and stackser1[5]="S" then 'Module in Power Safe state (Leds off)
PowerSafe=1
OutAlertUpdate=1
elseif StackSer1[4]=="S" and StackSer1[5]=="U" then 'Start Up broadcast
startup_master=YES 'Put startup_master to NO when first direct message from Master to this module is received
elseif stackser1[4]="T" and stackser1[5]="M" then 'Instruction only requested in Test Mode
'delete
'hserout2 ["Test instruction",13,10]
indicate=255
Powersafe=0
status_mode=TEST_MODE_LED_ON
Gosub Test_instructions
'hier module indicate
elseif stackser1[4]=="M" and stackser1[5]=="I" then
hserout2 ["Indicate Rec for: ",dec stackser1[6],".",dec stackser1[7],".",dec stackser1[8],".",dec stackser1[9],13,10]
'Instruction for Module Indicate
if stackser1[6]==Mod_ID0 and stackser1[7]==Mod_ID1 and stackser1[8]==Mod_ID2 and stackser1[9]==Mod_ID3 then 'Switch ON module indicate on this module
gosub Module_indicate_on
else 'Switch OFF module indicate
gosub Module_indicate_off
endif
endif
return
'hier module indicate
Module_indicate_off:
hserout2 ["Module indicate OFF",13,10]
OutAlertOn=0 'Normal led condition
OutAlert=0
Outalertcounter=0
if module_indicate_status==STATUS_ON then
module_indicate_status=STATUS_OFF
outAlertOn=0
x=~out
gosub set_leds
gosub write_leds
'leds back to normal state
endif
return
'hier module indicate
Module_indicate_on:
module_indicate_status=STATUS_ON
module_indicate_timer=4687 '30000ms/6.4ms
module_indicate_fl_timer=20
hserout2 ["Module indicate ON",13,10]
return
'hier module indicate
Check_module_indicate:
if module_indicate_status==STATUS_ON then
if module_indicate_fl_timer==0 then
module_indicate_fl_timer=20
toggle Module_indicate_led_on
if Module_indicate_led_on==YES then 'Switch ON leds
x=0
else 'Switch OFF leds
x=255
endif
gosub set_leds
gosub write_leds
endif
if module_indicate_timer==0 then
gosub module_indicate_off
endif
endif
return
Check_bus_health:
if bus_comm_error_timeout==0 then
Bus_mode=BUS_COMMUNICATION_ERRORS
if bus_comm_lost_timeout==0 then
Bus_mode=BUS_COMMUNICATION_LOST
endif
elseif bus_mode <> BUS_UNKNOWN then
Bus_mode=BUS_OK
endif
return
Check_Button: 'Gen3
gosub Check_button_pressed
return
Print_serial_number:
hserout2 ["SN: "]
if serial_number==65535 and Serial_errors==255 then
hserout2 ["-",13,10]
else
hserout2 [dec2 Serial_year,dec2 Serial_month,dec2 Serial_day,dec1 Serial_prod_company,dec5 serial_number,"(",dec Serial_errors,")",13,10]
endif
return
'testmode
Test_instructions:
'delete
'hserout2 ["Test ",dec stackser1[4]," ",dec stackser1[5]," ",dec stackser1[6]," ",dec stackser1[7],13,10]
if stackser1[6]=="V" then 'Firmware version
test_mode=YES
high RS485_mode 'send mode
hserout ["RC",0,0,0,0,"V",Mod_ID0,hardware,version,firmware,built,13,10]
gosub Set_rs485_in_receive_mode
elseif stackser1[6]=="S" then 'Set Ports
test_mode=YES
out=stackser1[7]
gosub write_output
'hier test 2
elseif stackser1[6]=="L" then 'Set FP Leds
test_mode=YES
if stackser1[7]==0 then 'Set all FP Leds OFF
i2cdata=255
status_led_onoff=LED_OFF 'general status of the led (ON - blinking or normal, OFF)
status_led_current_onoff=LED_OFF 'current status of the led (when blinking, this variable will toggle)
status_led_blink=NO 'Led must blink or not
status_led_color=ORANGE 'Color of the led
gosub Set_status_led
gosub write_leds
'delete
'hserout2 ["All leds OFF",13,10]
else 'Set all FP Leds ON
i2cdata=0
status_led_onoff=LED_ON 'general status of the led (ON - blinking or normal, OFF)
status_led_current_onoff=LED_ON 'current status of the led (when blinking, this variable will toggle)
status_led_blink=NO 'Led must blink or not
status_led_color=ORANGE 'Color of the led
gosub Set_status_led
gosub write_leds
'delete
'hserout2 ["All leds ON",13,10]
endif
elseif stackser1[6]=="b" then 'Set Button debug mode
test_mode=YES
if stackser1[7]==0 then 'Set FP button debug OFF
else 'Set FP button debug ON
endif
elseif stackser1[6]=="P" then 'Programming Eeprom
test_mode=YES
if stackser1[7]==0 then '0->Write full serial number including date and errors
Serial_year=stackser1[8]
Serial_month=stackser1[9]
Serial_day=stackser1[10]