PDA

View Full Version : SEROUT workaround for 16F676



rkeene0517
- 12th February 2006, 04:39
After much pain, I discovered that SEROUT nor SEROUT2 do not work on a 16F676.
(4 Mhz internal OSC)
The exact same simple code...
SEROUT PORTC.0, N1200, ["Hello world"]
when talking to my linux box works with a 16F690.

I tried many combos of stty on linux, windows boxes, and the PIC code.

In the end I wrote a serial out routine in PIC Basic and tuned it for 1200 baud.
xmitout VAR PORTC.0

serdelayusec VAR word
' By experiment the range is 800 usec for 1200 baud with all the overhead
' in the routine sendserbyte
' Stable limits are 836 to 941 so take the middle
serdelayusec = 888
serbyte var byte

' Send serbyte as RS232 standard.
' 8 bits, 1 start bit, 1 stop bit, no parity bit.
' After this routine the xmitout is LOW.
' The start bit is xmitout HIGH.
' A data bit 1 is xmitout LOW
' A data bit 0 is xmitout HIGH
sendserbyte:
' Start bit, transmitter on
if serbyte & $ff then
HIGH xmitout
else
HIGH xmitout
endif
call waitdelay

' Send MSb first. Data is inverted, no signal is a 1, signal is a 0
if serbyte & $01 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $02 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $04 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $08 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $10 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $20 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $40 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

if serbyte & $80 then
LOW xmitout
else
HIGH xmitout
endif
call waitdelay

' Stop Bit, transmitter off
if serbyte & $ff then
LOW xmitout
else
LOW xmitout
endif
call waitdelay

return

waitdelay:
PAUSEUS serdelayusec
return
In addition, on Mandrivia Linux the single serial port on the back (used for the mous in windows) is /dev/ttyS0
You first make sure it is not a login port by checking /etc/inittab and commenting out the entry for /dev/ttyS0 if there is one.
Then use stty to setup the port.

stty -F /dev/ttyS0 1200 raw cs8
check it with
stty -F /dev/ttyS0 -a

Then read the port with
cat /dev/ttyS0
or if it is raw unreadable data,
cat /dev/ttyS0 | od -x
for a hex dump.