CPU = 16F628 MHZ = 10 CONFIG 16170 ; Controling the Antenna Farm ; Remote Control Side, 16F628 PIC ; by: Stu Mitchell, W7IY ; Declarations station var byte ; Define Station Number for remote press var byte ; Button Number Pressed rcode var byte ; request code timer var byte ; Timer for CTS MSG var byte(20) ; LCD Message Variable LCDNib var PortB.nib1 ; Nibble for the LCD Data lines BNib var PortB.nib1 ; Nibble for the switch matrix, columns Row1 var PortA.bit0 ; Switch matrix, row 1 Row2 var PortA.bit1 ; Switch matrix, row 2 RegSel con A2 ; R/S of the LCD Clk con A3 ; E of the LCD CR con 13 ; Carriage Return LF con 10 CTS var PortB.bit0 ; Clear to Send bit (Input) RX con B1 ; RS-232 Receive line TX con B2 ; RS-232 Transmit line RTS con B3 ; Request to Send bit (Output) ; Initialize Variables Clear cmcon=$07 ; Port A I/O, not A/D trisa=%00000011 ; Set up Port A trisb=%00000011 ; Set up Port B press = 255 ; No button pressed timer = 0 ; reset timer HIGH RTS ; No data ready station=2 ; Station Number MSG = " " ; Set up the serial port and initialize the LCD SetHSerial H19200 ; Init the serial port at 19200 baud pause 500 LCDWrite RegSel\Clk, LCDNib, [INITLCD1,INITLCD2,TWOLINE,CLEAR,HOME,SCR] ;LCDWrite RegSel\Clk, LCDNib, [CLEAR] LCDWrite RegSel\Clk, LCDNib, ["The Antenna Farm"] LCDWrite RegSel\Clk, LCDNib, [SCRRAM+$40,"STN: ",DEC station," v1.0 W7IY"] pause 2200 ; Test initial connection with the master ; Send the string 'Init' and look for a reply message ; If no reply, time out and try again Init: if CTS = 1 then LOW RTS rcode = ( station << 4 ) | $0F HSEROUT [HEX rcode,CR] HSERIN No_Connect,200,[WAIT("S2"),STR MSG\17\13] HIGH RTS gosub display_results goto main No_Connect: HIGH RTS pause 100 endif goto Init ; Main Program main: pause 250 gosub get_button ; get the number of the button if press = 255 then main ; if not pressed, loop loop: if CTS = 1 then ; If bus is free LOW RTS ; grab it gosub request_antenna ; request the antenna gosub display_results ; display the results timer = 0 ; reset the timer HIGH RTS ; Free up the bus goto main endif ; If bus isn't free timer = timer + 1 ; Increment the timer if timer < 5 then ; < than time out pause 50 goto loop ; check CTS again endif ; timed out ; MSG = "No Service " timer = 0 ; gosub display_results goto main ; Loop forever ; Scan the button matrix and return the number of the button, 0 if none ; Bring a column high, check each row for a 1, if a 1 is found, the button ; was pressed. Return the button number get_button: BNib = %0001 if Row1 = 1 then press = 1 return endif if Row2 = 1 then press = 2 return endif BNib = %0010 if Row1 = 1 then press = 3 return endif if Row2 = 1 then press = 4 return endif BNib = %0100 If (Row1=1) AND (Row2=1) then press = 0 return endif if Row1 = 1 then press = 5 return endif if Row2 = 1 then press = 6 return endif press = 255 return ; Display a message (results) to the LCD display_results: LCDWrite RegSel\Clk, LCDNib, [HOME] LCDWrite RegSel\Clk, LCDNib, ["Station: ",DEC station," "] LCDWrite RegSel\Clk, LCDNib, [SCRRAM+$40, str MSG\16] MSG=" " return ; Request the master for an antenna. ; Send the button number and wait for a return message ; Time out if nothing comes back request_antenna: rcode = (station << 4) + press HSEROUT [HEX rcode,CR] HSERIN Timed_Out,500,[Wait("S2"),STR MSG\16\13] return Timed_Out: LET MSG = "Timed Out " return