-- -------------------------------------------------------------------- -- -- converter for SeaTalk -- -- receives SeaTalk codes, sends the compass heading message -- as hex characters: -- >U1VWRR -- -- see Thomas Knaufs website for information about the format -- http://www.thomasknauf.de/seatalk.htm -- -- The language is Jal, see -- http://www.xs4all.nl/~wf/wouter/pic/jal -- -- Copyright (C) 2001 Wouter van Ooijen, Hans van Veldhuizen -- -- This library is free software; you can redistribute it and/or -- modify it under the terms of the GNU Library General Public -- License as published by the Free Software Foundation; either -- version 2 of the License, or (at your option) any later version. -- -- This library 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 -- Library General Public License for more details. -- -- You should have received a copy of the GNU Library General Public -- License along with this library; if not, write to the -- Free Software Foundation, Inc., 59 Temple Place - Suite 330, -- Boston, MA 02111-1307, USA. -- -- -------------------------------------------------------------------- include 16c84_4 include jpic include jlib include interval const bit active_high = true const bit active_low = false const byte parity_even = 0 const byte parity_odd = 1 const byte parity_none = 2 const asynch_in_polarity = active_low const asynch_out_polarity = active_high const asynch_stopbits = 1 var volatile bit asynch_in_pin is pin_b3 var volatile bit asynch_in_direction is pin_b3_direction var volatile bit asynch_out_pin is pin_b7 var volatile bit asynch_out_direction is pin_b7_direction -- intialize the input and output pins asynch_out_direction = output asynch_in_direction = input -- set the appropriate interval procedure _asynch_init is init_interval_1uS( 208 ) end procedure -- wait approximately half a bit time procedure _asynch_wait_half is -- was 104, but that was for 10 MHz delay_1uS( 50 ) end procedure -- send a byte procedure asynch_send( byte in x ) is var bit current_bit at x : 0 -- set the appropriate interval _asynch_init next_interval next_interval -- the start bit if asynch_out_polarity == active_high then asynch_out_pin = high else asynch_out_pin = low end if next_interval -- the 8 data bits for 8 loop if asynch_out_polarity == active_high then asynch_out_pin = ! current_bit else asynch_out_pin = current_bit end if x = x >> 1 next_interval end loop -- the stop bits if asynch_out_polarity == active_high then asynch_out_pin = low else asynch_out_pin = high end if for asynch_stopbits loop next_interval end loop end procedure procedure asynch_receive( byte out x, bit out received_parity ) is var bit current_bit at x : 7 -- wait for a start bit if asynch_in_polarity == active_high then while asynch_in_pin != high loop end loop else while asynch_in_pin != low loop end loop end if -- wait for half a bit time _asynch_wait_half -- set the appropriate interval _asynch_init -- now repeat 8 times: -- wait for the next interval and sample for 8 loop next_interval x = x >> 1 if asynch_in_polarity == active_high then current_bit = ! asynch_in_pin else current_bit = asynch_in_pin end if end loop -- get the parity next_interval if asynch_in_polarity == active_high then received_parity = ! asynch_in_pin else received_parity = asynch_in_pin end if -- wait for the (first) stop bit next_interval end procedure function hexchar( byte in x ) return byte is if x < 10 then return "0" + x else return "A" + x - 10 end if end function procedure SeaReceive( byte out b1, byte out b2, byte out b3 ) is var byte c = 0 var bit p while ( ! p ) | ( c != 0x9C ) loop asynch_receive( c, p ) end loop asynch_receive( b1, p ) asynch_receive( b2, p ) asynch_receive( b3, p ) end procedure var byte x1, x2, x3 forever loop SeaReceive( x1, x2, x3 ) asynch_send( ">" ) asynch_send( hexchar( x1 >> 4 ) ) asynch_send( hexchar( x1 & 0xF ) ) asynch_send( hexchar( x2 >> 4 ) ) asynch_send( hexchar( x2 & 0xF ) ) asynch_send( hexchar( x3 >> 4 ) ) asynch_send( hexchar( x3 & 0xF ) ) -- for debugging -- var byte b -- d = ( ( ( x1 & 0x30 ) >> 4 ) * 45 ) + ( x2 & 0x3F ) -- asynch_send( " " ) -- asynch_send( hexchar( d >> 4 ) ) -- asynch_send( hexchar( d & 0xF ) ) end loop