module RS485_E

'Fit 10K pull-up to 5V on RX pin of PIC for RS485 chip
'Set rts bit definition to port pin used for rx/tx switch
'*******************************************************************************


sub procedure uw(dim in as Char)            '
sub procedure R485_Set
sub procedure Uart_Begin()

#Define HD               'comment for full duplex RS485 / 422 or RS232

implements
 Dim rts as Sbit at RC0_bit
 Dim rtz as sbit at TRISC0_bit

sub procedure R485_Set
    rts = 0    'set to receive (default)
    rtz = 0
end sub
'*******************************************************************************
sub procedure  Uart_Begin()
    Uart1_init(9600)        'Set baudrate here
   #ifdef HD then
        R485_Set          'allows half duplex mode with portC.0 as switch pin
   #endif
end sub
'*******************************************************************************
Sub procedure uw(dim in as char)   'Writes single character via RS485 half duplex
  #ifdef HD then
      rts = 1               'portc.5 = 1
 #endif
      uart1_write(in)
   #ifdef HD then
      do
      loop until TXSTA.TRMT = 1 'wait for tx reg empty before switching port
      rts = 0                    'portc.5 = 0
  #endif
end sub
'*******************************************************************************


end.