Heres all the code:

Code:
clear

DEFINE OSC 4 
OSCCON.6 = 1          ' INTERNAL OSCILLATOR SELECT TO 4MHZ
OSCCON.5 = 1          ' DURING MEPROG CONFIGURATION, USE 
OSCCON.4 = 0          ' OSCILLATOR INTIO2
' Define the USART registers
DEFINE HSER_BAUD 9600 'Hser baud rate 
DEFINE HSER_CLROERR 1 'Hser clear overflow automatically 
DEFINE HSER_SPBRG 25  'Hser spbrg init 
DEFINE HSER_RCSTA 80h 'Hser receive status init 
DEFINE HSER_TXSTA 20h 'Hser transmit status init 
DEFINE HSER_BITS 8    'Use 8th bit for parit
PAUSE 1000            ' PAUSE FOR A SECOND, WAITING FOR SYSTEM TO ENERGIZE

' INITIALIZE PORT E
TRISE = %00000110      ; set PORTE

' INITIALIZE PORT D
LATD =%00000000       ' CLEAR DATA LATCHES
TRISD =%00001100      ' PORT D inputs/outputs

' INITIALIZE PORT C   CONTAINS TX FOR COMMUNICATION TO LCD SCREEN
LATC =%00000000         ' CLEAR DATA LATCHES
TRISC =%00000110        ; set all PORTC 
TXSTA =%00100000        ; SYNC = 0, TX ENABLED, BRGH = 0
RCSTA =%10000000        ; TX/RX ENABLED
BAUDCON.3 = 1           ; BRG16 = 1
SPBRGH =%00000000       ; SPBRG = 25
SPBRG =%00011001
PIE1.4 = 0              ; DISABLE TX INTERRUPT

SSPADD = $28             'I2C BRG
SSPSTAT.7 = 1
SSPCON1 = %00101000
I2C_COMM VAR PORTC.4
I2C_CLK VAR PORTC.3
ADDR1 VAR BYTE
CONT_SEND VAR WORD
ADDR1 = $50


' INITIALIZE PORT B
LATB =%00000000     'CLEAR DATA LATCHES
TRISB =%00001011    'RB2 IS CANTX, RB3 IS CANRX
INTCON2.7 = 1       'PORT B WEAK PULL UP DISABLE
' 
' INITIALIZE PORT A
LATA =%00000000       ' CLEAR DATA LATCHES
TRISA =%11111111      ' PORT A ALL INPUTS
ADCON1 =%00111111     'ALL DIGITAL I/O 

'PIN DEFINITION
ALARM1 VAR LATD.4      ' PIN 27     GENERAL ALARM
ALARM2 VAR LATD.5      ' PIN 28     BEACON ALARM
ALARM3 VAR LATD.6      ' PIN 29     FLASHER FAIL ALARM
ALARM4 VAR LATD.7      ' PIN 30     OPTIONAL OL ALARM
ALARM5 VAR LATB.4      ' PIN 37      POL ALARM1
ALARM6 VAR LATB.5      ' PIN 38     POL ALARM2
ALARM7 VAR LATB.6      ' PIN 39     POL ALARM3
ALARM8 VAR LATB.7      ' PIN 40     SPARE AT THE MOMENT (9/15/11)



SYSTEM_OK VAR LATD.0    'PIN 19

POL_ALARM_INPUT VAR PORTB.1 'PIN 34
POL_ALARM_INPUT2 VAR PORTC.1 'PIN 16
POL_ALARM_INPUT3 VAR PORTC.2 'PIN 17
PPC_INPUT VAR PORTB.0   'PIN 33


NUMBER_OUT VAR WORD 

DIP_A0 VAR PORTA.0     ' DIPS SWITCH ON PORTA FOR NUMBER OF BEACONS
DIP_A1 VAR PORTA.1     ' DIP_AO WILL BE NUM_OF_BEACONS.Bit0
DIP_A2 VAR PORTA.2     ' ....
DIP_A3 VAR PORTA.3     ' DIP_A5 WILL BE NUM_OF_BEACONS.Bit5
DIP_A4 VAR PORTA.4
DIP_A5 VAR PORTA.5
DIP_B1 VAR PORTE.1
DIP_B2 VAR PORTE.2

'VARIABLE
WAIT_TIME VAR BYTE      ' TIME DELAY VARIABLE LOST BEACON
X VAR BYTE              ' VARIABLE FOR ROLLING THROUGH BEACONS
Y VAR BYTE              ' VARIABLE FOR ROLLING THROUGH ALARM CONFIGURATION
NUM_OF_BEACONS VAR word[1]  ' VARIABLE TO HAVE NUMBER OF BEACON STORED INTO IT
FLASH_STATE VAR BIT      ' VARIABLE TO STORE THE STATE OF THE FLASH
                         ' ON = 1      OFF = 0
STATUS_CHANGE VAR BIT    ' VARIABLE BIT TO TELL THE CONTROLLER CHANGE HAS HAPPENED
TEMP_RXB0SIDH VAR BYTE   ' TEMP BUFFER HIGH TO COMPARE THE REAL BUFFER TO
TEMP_RXB0SIDL VAR BYTE   ' TEMP BUFFER LOW TO COMPARE THE REAL BUFFER TO
LED_ALARM var bit[65]    ' CREATES 65 BITS, LABELED LED_ALARM[0-64]
LOST_ALARM VAR BIT[65]   ' CREATES 65 BITS, LABELED LOST_ALARM[0-64]
FF_ALARM VAR BIT[65]     ' CREATES 65 BITS, LABELED FF_ALARM[0-64]
NORMAL_BEACON VAR BIT[65]  ' CREATES 65 BITS, LABELED NORMAL_BEACON[0-64]
NUM_OF_POLS VAR word

FFA_OK VAR BYTE         
LOST_OK VAR BYTE
LED_OK VAR BYTE
COMM_OK VAR BYTE


HIGH ALARM1                   ' START ALL ALARMS OFF
HIGH ALARM2  
HIGH ALARM3  
HIGH ALARM4  
low ALARM5  
LOW ALARM6  
LOW ALARM7
LOW ALARM8 
 


'***************** BEGINNING OF ECAN CONFIGURATION******************
CANCON=%10000000      ' Configuration Mode
while CANSTAT !=$80   ' Wait until Canstat is in Config Mode
PAUSE 10              ' Pause if not 
WEND                  ' proceed
ECANCON =%00000000    ' Mode 0

' Baud Rate = 50bps @ 4 MHZ WITH 10 TQ
BRGCON1 =$03  
BRGCON2 =$92  
BRGCON3 =$02    

' Setting up Receive Mask 0 register
RXM0SIDH =%10000000      ' 1'S IMPORTANT, 0'S WILL CHANGE
RXM0SIDL =%11100000
' Setting up Receive Filter 0 register
RXF0SIDH =%00000000      ' $004, STANDARD FOR BEACON TO CONTROLLER ID'S
RXF0SIDL =%10000000      ' $00C, $014, $01C...

CANCON=%00000000         ' NORMAL Mode
while CANSTAT !=$00      ' Wait until Canstat is in NORMAL Mode
PAUSE 10                 ' Pause if not 
WEND
'***************** END OF ECAN CONFIGURATION******************

'RECEIVE BUFFER 0 CONTROL REGISTER
RXB0CON.6 = 0             ' MEANS BUFFER 0 ONLY RECEIVES STANDARD ID'S
RXB0CON.5 = 1     

'TRANSMIT BUFFER 0 INITIATE, USED FOR CALLING OUT TO BEACON
TXB0DLC =%00000001        ' LOAD DATA LENGTH, 1 BYTE
TXB0D0 =%01110111         ' DATA IS $77, MEANING "BEACON CHECK IN"
TXB0CON.0 = 1             ' LOW Priority
TXB0CON.1 = 0             

'TRANSMIT BUFFER 1 INITIATE, USED TO SEND FLASHING SIGNAL
TXB1DLC =%00000001        ' LOAD DATA LENGTH, 1 BYTE
TXB1SIDH =%00000000       ' ID $000
TXB1SIDL =%00000000       ' ALL BEACON WILL ACCEPT THIS ID
TXB1CON.0 = 1             ' HIGHEST PRIORITY INSIDE CHIP
TXB1CON.1 = 1        

'VARIABLE CONFIGURATION
STATUS_CHANGE = 1               ' START WITH CHANGE, MAKES LCD SCREEN DISPLAY SOMETHING
FLASH_STATE = 0                 ' BEACONS STARTS OFF
TEMP_RXB0SIDH =%00000000        ' STARTING RECEIVE BUFFER ID $004
TEMP_RXB0SIDL =%10000000        ' FIRST BEACON WILL SEND ID $00C

'INITIATE NUM_OF_POLS

IF DIP_B2 = 0 AND DIP_B1 = 0 THEN           'DIP SETTINGS FOR AMOUNT OF POLS
NUM_OF_POLS = 0
ENDIF

IF DIP_B2 = 0 AND DIP_B1 = 1 THEN
NUM_OF_POLS = 1
ENDIF

IF DIP_B2 = 1 AND DIP_B1 = 0 THEN
NUM_OF_POLS = 2
ENDIF

IF DIP_B2 = 1 AND DIP_B1 = 1 THEN
NUM_OF_POLS = 3
ENDIF



'INITIATE NUM_OF_BEACONS
IF DIP_A0 = 0 THEN
NUM_OF_BEACONS.BIT0 = 0
ELSE
NUM_OF_BEACONS.BIT0 = 1
ENDIF

IF DIP_A1 = 0 THEN
NUM_OF_BEACONS.BIT1 = 0
ELSE
NUM_OF_BEACONS.BIT1 = 1
ENDIF

IF DIP_A2 = 0 THEN
NUM_OF_BEACONS.BIT2 = 0
ELSE
NUM_OF_BEACONS.BIT2 = 1
ENDIF

IF DIP_A3 = 0 THEN
NUM_OF_BEACONS.BIT3 = 0
ELSE
NUM_OF_BEACONS.BIT3 = 1
ENDIF

IF DIP_A4 = 0 THEN
NUM_OF_BEACONS.BIT4 = 0
ELSE
NUM_OF_BEACONS.BIT4 = 1
ENDIF

IF DIP_A5 = 0 THEN
NUM_OF_BEACONS.BIT5 = 0
ELSE
NUM_OF_BEACONS.BIT5 = 1
ENDIF

NUM_OF_BEACONS.BIT6 = 0
NUM_OF_BEACONS.BIT7 = 0

IF NUM_OF_BEACONS =%00000000 THEN 
NUM_OF_BEACONS = 64
ENDIF
'**********************Read and clear RXBO*************************'
'IF RXB0D0 = %00000000 THEN    ' SIMPLY READ THE BUFFER, it will flush itself
'PAUSE 1                       ' DO NOTHING
'ELSE 
'PAUSE 1                       ' DO NOTHING
'ENDIF 
'****************ALARM BIT CONFIGURATION************************
Y = 1                      ' START AT FIRST BIT OF EACH ARRAY 
WHILE Y <= NUM_OF_BEACONS  ' ALL ALARMS FOR ALL BEACON MUST BE SET TO ZERO
LED_ALARM[X] = 0           ' 0 IS NO ALARM, 1 IS AN ALARM CONDITION 
LOST_ALARM[Y] = 0
FF_ALARM[Y] = 0
NORMAL_BEACON[Y] = 1       ' NORMAL_BEACON = 1 IS GOOD, NORMAL_BEACON = 0
Y = Y + 1                  ' MEANS THERE IS AN ALARM CONDITION
WEND                       ' CONTINUE WHEN ALL BEACONS ARE ASSIGNED
'*************INTERRUPT CONFIGURATION******************************
INTCON =%10100000    ' ENABLE GLOBAL INTERRUPT
                     ' TIMER 0 INT ENABLED, FLAG LOW
                     
ON INTERRUPT GOTO INTAWHAT

'**************TIMER0  CONFIGURATION
T0CON =%00000100      ' TIMER 0, 16BITS, PRESCALE 1:32, TIMER0 IS OFF
TMR0H =%10110000      ' CREATES .66 SECOND START TIME
TMR0L =%10000000

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$52,$30]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Point Lighting Corp"]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Obstruction Lighting"]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"     Controller"]
PAUSE 10

PAUSE 2000

'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'$$$$$$$$$$$$$$$$$$$$$$$$____OPERATING CODE STARTS HERE_____$$$$$$$$$$$$$$$$$$


RXB0CON.7 = 0             ' CLEAR RECEIVE BUFFER 0 FULL BIT
T0CON.7 = 1               ' TURN ON TIMER0 JUST BEFORE MAIN PROGRAM

START_CHECK:              ' STARTS THE BEACON CHECK SEQUENCE, first display info from last cycle
                           ' RETURN TO HERE AFTER ALL BEACON HAVE BEEN CHECKED    
                           
TOGGLE SYSTEM_OK           'TOGGLE SYSTEM OK LED SO WE KNOW THE UNIT ISNT HUNG 


IF POL_ALARM_INPUT != 1 and POL_ALARM_INPUT2 != 1 and POL_ALARM_INPUT3 != 1 AND ALARM1 != 0 AND ALARM2 !=0 AND ALARM3 !=0 AND ALARM4 != 0  THEN 
HIGH alarm4                    'TURN OFF POL ALARM                                                 
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"System Operational"]
PAUSE 10

'I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Beacons: ",NUM_OF_BEACONS, " "]
'PAUSE 10

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Beacons: "]
PAUSE 10
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE, str num_of_beacons\1]
PAUSE 10


if num_of_polS >0 THEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"Obst Lights (OL): ", NUM_OF_POLS]  
PAUSE 10 
ENDIF
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Number of Alarms: 0"]
PAUSE 10
endif
 
HSEROUT [DEC 6777]
PAUSE 100
HSEROUT [DEC 6888]


IF POL_ALARM_INPUT = 1 OR POL_ALARM_INPUT2 = 1 OR POL_ALARM_INPUT3 = 1 THEN       ' CHECK POL ALARMS
LOW ALARM4          'TURN ON POL ALARM
LOW ALARM1          'TURN ON GENERAL ALARM
goto DISPLAY_ERROR
ELSE
HIGH ALARM4
LOW ALARM5
LOW ALARM6
LOW ALARM7                   'RESETING THE POL ARLARMS ON THE BOARD
ENDIF

IF ALARM2 != 0 AND ALARM3 != 0 AND ALARM4 != 0 AND ALARM5 != 1 AND ALARM6 != 1 AND ALARM7 != 1 AND ALARM8 != 1 THEN
HIGH ALARM1
ENDIF             'RESETING GENERAL ALARM ONLY IF NO OTHER ALARMS ARE ACTIVE


 
                      
IF POL_ALARM_INPUT = 0 THEN       'POL 1 ALARM INPUT  RESET TO PLC
HSEROUT [DEC 9981]
PAUSE 50
ENDIF

IF POL_ALARM_INPUT2 = 0 THEN    'POL 2 ALARM INPUT   RESET    TO PLC
HSEROUT [DEC 9982]
PAUSE 50
ENDIF

IF POL_ALARM_INPUT3 = 0 THEN    'POL 3 ALARM INPUT RESET     TO PLC
HSEROUT [DEC 9983]
PAUSE 50
ENDIF

                  
TXB0SIDH =%00000000       ' ID $000, RETURN TO STARTING TRANSMIT BUFFER 1 ID'S
TXB0SIDL =%00000000       ' ID LOW DOES NOT CHANGE
        
TEMP_RXB0SIDH =%00000000   ' STARTING RECEIVE BUFFER ID $000, WILL ALWAYS BE 
                           ' INCREMENTED TO $00C FOR FIRST BEACON
X = 1                      ' STARTS AT FIRST BEACON
NEXT_CHECK:
WHILE X <= NUM_OF_BEACONS   ' WHILE X IS LESS THEN OR EQUAL TO NUMBER OF BEACONS

TXB0SIDH = TXB0SIDH + 1            ' INCREMENT TX BUFFER TO NEXT BEACON ID
TEMP_RXB0SIDH = TEMP_RXB0SIDH + 1  ' INCREMENT TEMP RX BUFFER TO NEXT BEACON ID

TXB0CON.3 = 1                ' REQUEST TRANSMISSION ON BUFFER 0
WAIT_TIME = 250              ' 250 X PAUSE 12 = 3 SECOND
WHILE TEMP_RXB0SIDH != RXB0SIDH AND WAIT_TIME > 0 ' RECEIVE BUFFER 0 HAS A NEW MESSEGE FROM
    RXB0CON.7 = 0            ' CLEAR RECEIVE BUFFER 0 FULL BIT 
    WAIT_TIME = WAIT_TIME - 1' DECREMENT COUNTER
    PAUSE 10                 ' 12 MILLISECOND PAUSE
WEND 
                             ' 3.0 SECOND HAS EXPIRE AND NO RESPONSE
if wait_time = 0 THEN
GOTO LOST_BEACON             ' GOTO LOST BEACON
ENDIF
    
READ_DATA0:   ' reads data from beacon
       
if RXB0D0 !=%00000000 THEN   ' DATA INDICATES A BEACON NOT NORMAL       
GOSUB  BEACON_ALARM           ' GOTO LED ALARM SUBROUTINE
ENDIF                        ' DATA0 WAS NORMAL (%00000000)

IF RXB0D0 =%00000000 AND NORMAL_BEACON[X] = 0 THEN ' BEACON IS NORMAL ANS IT WASN'T BEFORE
STATUS_CHANGE = 1          ' INDICATE THAT CHANGE HAS HAPPENED
LED_ALARM[X] = 0           ' 0 IS NO ALARM, 1 IS AN ALARM CONDITION 
LOST_ALARM[X] = 0
FF_ALARM[X] = 0
NORMAL_BEACON[X] = 1       ' NORMAL_BEACON = 1 IS GOOD, NORMAL_BEACON = 0 BAD
ENDIF

RXB0CON.7 = 0          ' CLEAR RECEIVE BUFFER 0 FULL BIT 
X = X + 1              ' INCREMENT COUNTER X 
GOTO NEXT_CHECK        ' CHECK NEXT BEACON X LESS THEN NUM_OF_BEACONS
WEND                   ' CHECKED ALL BEACON, X = NUM_OF_BEACONS
                       ' NOW WE DISPLAY WHAT WE FOUND OUT FROM ALL THE BEACONS

IF STATUS_CHANGE = 1 THEN     ' IF A CHANGE HAS OCCURED, LCD DISPLAY MUST CHANGE
STATUS_CHANGE = 0             ' RESET STATUS_CHANGE BIT
Y = 1                         ' START AT FIRST BIT OF EACH ARRAY 
WHILE Y <= NUM_OF_BEACONS     ' SCROLLING THROUGH ALL ACTIVE BEACONS
IF NORMAL_BEACON[Y] = 1 THEN  ' IF BEACON IS NORMAL

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
if LOST_ALARM[Y] = 0 THEN     'CHECKING IF A BEACON IS LOST
NUMBER_OUT = 1900 + Y
HSEROUT [DEC4 NUMBER_OUT]        'TELLS THE PLC THAT THE BEACON HAS RECOVERED
PAUSE 50
ENDIF
   
if FF_ALARM[Y] = 0 THEN           'CHECKING FOR FLASHER FAILURE
NUMBER_OUT = 2900 + Y
HSEROUT [DEC4 NUMBER_OUT]          'TELLS THE PLC THE FF ERROR IS FIXED
PAUSE 50
ENDIF
 
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Y = Y + 1                     ' INCREMENT Y TO TEST NEXT BEACON

ELSE                  ' IF ONE BEACON IS NOT NORMAL
GOTO DISPLAY_ERROR            ' GOTO DISPLAY ERROR
ENDIF
WEND                          ' WILL ONLY ESCAPE WHILELOOP HERE IF ALL BEACON 
pause 5                       ' ARE NORMAL 




FFA_OK = 67
LOST_OK = 67
LED_OK = 67

LOOKDOWN2 FF_ALARM, [1], FFA_OK                      'CHECKING TO SEE IF THE VALUE 1
LOOKDOWN2 LOST_ALARM, [1], LOST_OK                    'IS STORED IN THE ERROR LOCATIONS
LOOKDOWN2 LED_ALARM, [1], LED_OK
                                                       'IF THERE IS NO "1" THEN THERES NO ERROR
IF FFA_OK = 67 AND LOST_OK = 67 AND LED_OK = 67 THEN                    'SO WE CAN RESET THE ALARMS
HIGH ALARM3                                                                
HIGH ALARM1
HIGH ALARM2
ENDIF

IF FFA_OK = 67 AND LOST_OK = 67 AND LED_OK != 67 THEN                        'IF THE COMM AND FF ARE OK, 
HIGH ALARM3                                                                     'BUT THE LED IS STILL OUT, 
HIGH ALARM1                                                                   ' THEN WE CANT RESET THEM
'HIGH ALARM2   NOT INCLUDED HERE                                              'ALL. ALARM2 (BEACON) IS LEFT OUT OF THE RESET
ENDIF






GOTO START_CHECK
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DISPLAY_ERROR:

Y = 1                         ' START AT FIRST BIT OF EACH ARRAY 
WHILE Y <= NUM_OF_BEACONS      ' SCROLLING THROUGH ALL ACTIVE BEACONS
IF LED_ALARM[Y] = 1 THEN        'CHECKING LED ARRAYS
LOW  ALARM2                    'TURN ON BEACON ALARM
LOW  ALARM1                     'TURN ON GENERAL ALARM

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]         ' ALWAYS $FE, $51 = CLEAR SCREEN
'HSEROUT [$FE,$45,$00,"System Error"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Beacon: ", str y\2," " ]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"LED Failure"]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
IF NUM_OF_POLS > 0 THEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," ","OLs: ",  str NUM_OF_POLS\2]
ELSE
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," "]
ENDIF
PAUSE 50
 


NUMBER_OUT = 3000 + Y
HSEROUT [DEC4 NUMBER_OUT]
PAUSE 100
ENDIF



if LOST_ALARM[Y] = 1 THEN     'CHECKING IF A BEACON IS LOST
LOW  ALARM2                    'TURN ON BEACON ALARM
LOW  ALARM1                     'TURN ON GENERAL ALARM
LOW ALARM3                      'TURN ON FFA ALARM

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]         ' ALWAYS $FE, $51 = CLEAR SCREEN
'I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"System Error"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Beacon: ", str y\2," " ]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Lost Beacon"]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
IF NUM_OF_POLS > 0 THEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," ","OLs: ", str NUM_OF_POLS\2]
ELSE
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Total Beacons: ", str NUM_OF_BEACONS\2," "]

ENDIF
pause 500
 

NUMBER_OUT = 1000 + Y
HSEROUT [DEC4 NUMBER_OUT]

PAUSE 100

ELSEif LOST_ALARM[Y] = 0 THEN     'CHECKING IF A BEACON IS LOST
NUMBER_OUT = 1900 + Y
HSEROUT [DEC4 NUMBER_OUT]        'TELLS THE PLC THAT THE BEACON HAS RECOVERED

PAUSE 100
ENDIF



if FF_ALARM[Y] = 1 THEN           'CHECKING FOR FLASHER FAILURE

LOW  ALARM2                    'TURN ON BEACON ALARM
LOW  ALARM1                     'TURN ON GENERAL ALARM
LOW ALARM3                      'TURN ON FFA ALARM

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]         ' ALWAYS $FE, $51 = CLEAR SCREEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Beacon: ",str y\2," " ]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Flasher Failure"]
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
IF NUM_OF_POLS > 0 THEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," ","OLs: ",  str NUM_OF_POLS\2]
ELSE
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," "]
ENDIF
pause 100
 
NUMBER_OUT = 2000 + Y
HSEROUT [DEC4 NUMBER_OUT]

PAUSE 100


ELSEif FF_ALARM[Y] = 0 THEN           'CHECKING FOR FLASHER FAILURE

NUMBER_OUT = 2900 + Y
HSEROUT [DEC4 NUMBER_OUT]          'TELLS THE PLC THE FF ERROR IS FIXED

PAUSE 100

ENDIF

Y = Y + 1                  ' NEXT BEACON
WEND                       ' CONTINUE WHEN ALL BEACONS ARE ASSIGNED
ENDIF                      ' ENDIF OF (IF STATUS_CHANGE = 1) 

 '------------------------------------------------------------------------
IF POL_ALARM_INPUT = 1 THEN       ' CHECK POL ALARM 1
HIGH ALARM5
STATUS_CHANGE = 1 

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]            ' ALWAYS $FE, $51 = CLEAR SCREEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"POL Alarm: Level 1"] '$45 cursor position, 
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position, 
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ", str NUM_OF_BEACONS\2," ","OLs: ", str NUM_OF_POLS\2]
PAUSE 100
HSEROUT [DEC 9991]

ELSE
LOW ALARM5
ENDIF  

IF POL_ALARM_INPUT2 = 1 THEN            'CHECK POL ALARM 2
HIGH ALARM6
STATUS_CHANGE = 1 

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]            ' ALWAYS $FE, $51 = CLEAR SCREEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"POL Alarm: Level 2"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ",str NUM_OF_BEACONS\2," ","OLs: ",  str NUM_OF_POLS\2]

PAUSE 100
HSEROUT [DEC 9992]

ELSE
LOW ALARM6
ENDIF 

IF POL_ALARM_INPUT3 = 1 THEN       'CHECK POL ALARM 3
HIGH ALARM7
STATUS_CHANGE = 1 

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]            ' ALWAYS $FE, $51 = CLEAR SCREEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"POL Alarm: Level 3"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ",str NUM_OF_BEACONS\2," ","OLs: ",str  NUM_OF_POLS\2]
PAUSE 100
HSEROUT [DEC 9993]
PAUSE 100

ELSE

LOW ALARM7
ENDIF                          
'-----------------------------------------------------------------

GOTO START_CHECK




BEACON_ALARM:           ' ALL ALARM MESSEGES GO TO THIS LOCATION, MUST FILTER
                        ' ALARM AND SET ALARM BIT TO 1 OF FAILED BEACON 
IF RXB0D0 = %00001111 and RXB0DLC = %00000010 THEN' DATA0 ID $0F FOR LED ALARM ON BEACON
STATUS_CHANGE = 1               ' CHANGE HAS HAPPENED
NORMAL_BEACON[X] = 0            ' NORMAL BIT OFF
LED_ALARM[X] = 1                ' LED ALARM BIT ON
ENDIF

IF RXB0D0 = %11111111 THEN      ' DATA0 ID FOR FLASHER FAIL ON BEACON
STATUS_CHANGE = 1               ' CHANGE HAS HAPPENED
NORMAL_BEACON[X] = 0            ' NORMAL BIT OFF
FF_ALARM[X] = 1                 ' FF ALARM BIT ON
ENDIF
RETURN                          ' RETURN BACK TO CODE AFTER BITS ARE ASSIGNED

LOST_BEACON:                    ' LOST BEACON COME HERE
STATUS_CHANGE = 1               ' CHANGE HAS HAPPENED
NORMAL_BEACON[X] = 0            ' NORMAL BIT OFF
LOST_ALARM[X] = 1               ' LOST ALARM BIT ON
X = X + 1                       ' INCREMENT COUNTER X





GOTO NEXT_CHECK                 ' GOTO NEXT CHECK, NO DATA0 TO READ




'***************************INTERRUPT BEGINS**********************************
DISABLE                      ' MUST DISABLE BEFORE INTERRUPT ROUTINE IS USED

INTAWHAT:                    ' INTERRUPT HAS OCCURRED
TOGGLE SYSTEM_OK


T0CON.7 = 0                  ' STOP TIMER WHILE EVERYTHING IS RESET

IF FLASH_STATE = 1 THEN      ' IF BEACONS WERE ON
FLASH_STATE = 0              ' TURN FLASH_STATE BIT LOW FOR NEXT CYCLE

TXB1D0 =%00000000            ' DATA IS $00, MEANING "FLASHING OFF"
TXB1CON.3 = 1                ' REQUEST TRANSMISSION ON BUFFER 1
WHILE TXB1CON.3 != 0         ' HAS IT TRANSMITTED
if COMSTAT.0 = 1 THEN        ' TX ERROR WARNING OR RX ERROR WARNING

'comm error code here           'PROGRAM LOOPS HERE UNTIL COMM IS GOOD

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]            ' ALWAYS $FE, $51 = CLEAR SCREEN
'HSEROUT [$FE,$45,$00,"System Error"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Communcation Error"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Flasher Failure"] '$45 cursor position, $40 second row
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ",str NUM_OF_BEACONS\2," ","OLs: ",  str NUM_OF_POLS\2]

HSEROUT [DEC 7777]

hserout [dec 8888]



STATUS_CHANGE = 1
PAUSE 1000                     ' PAUSE 1000 MS WHILE WAITING

ENDIF
PAUSE 10                     ' PAUSE 10 MS WHILE WAITING
WEND                         ' TRANSMITTION SUCCESSFUL

INTCON.2 = 0                 ' RESET TIMER0 INT FLAG
TMR0H =%10110000             ' CREATES .66 SECOND BETWEEN NEXT INTERRUPT
TMR0L =%10000000 

ELSEif flash_state = 0 then      ' ELSE BEACONS WERE OFF  
FLASH_STATE = 1              ' TURN FLASH_STATE BIT HIGH FOR NEXT CYCLE

TXB1D0 =%11111111            ' DATA IS $FF, MEANING "FLASHING ON"
TXB1CON.3 = 1                ' REQUEST TRANSMISSION ON BUFFER 1
WHILE TXB1CON.3 != 0         ' HAS IT TRANSMITTED

if COMSTAT.0 = 1 THEN        ' TX ERROR WARNING OR RX ERROR WARNING

'comm error code    here         'PROGRAM LOOPS HERE UNTIL COMM IS GOOD

I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$51]            ' ALWAYS $FE, $51 = CLEAR SCREEN
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$00,"Communcation Error"] '$45 cursor position, $00 top left
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$40,"Flasher Failure"] '$45 cursor position, $40 second row
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$14,"____________________"] '$45 cursor position,
I2CWRITE I2C_COMM, I2C_CLK, CONT_SEND, ADDR1, [$FE,$45,$54,"Beacons: ",str NUM_OF_BEACONS\2," ","OLs: ",  str NUM_OF_POLS\2]


HSEROUT [DEC 7777]

hserout [dec 8888]



STATUS_CHANGE = 1 

PAUSE 1000                     ' PAUSE 1000 MS WHILE WAITING
ENDIF
PAUSE 10                     ' PAUSE 10 MS WHILE WAITING
WEND                         ' TRANSMITTION SUCCESSFUL



INTCON.2 = 0                 ' RESET TIMER0 INT FLAG
TMR0H =%00001010             ' CREATES 2 SECOND BETWEEN NEXT INTERRUPT
TMR0L =%00000000
ENDIF

T0CON.7 = 1                  ' START TIMER 
RESUME                       ' GO BACK TO WHERE CODE WAS INTERRUPTED
ENABLE                       ' MUST USE ENABLE

end                          ' MUST END WITH END