Log in

View Full Version : Strange Serout Behaviour



bluesmoke
- 6th August 2009, 23:15
I have been experiencing strange behaviour with Serout. The short version is this: I have a number of Serout commands (to hyperterminal on my laptop) in my code and my 16f88 would always skip the first Serout and start with the second Serout.

Here's an example of what I see on my laptop:

This is a test 2
This is a test 3
This is a test 4
etc....

When it should read:

This is a test 1
This is a test 2
This is a test 3
etc...

I added the huge pause thinking that it might 'settle down' the pic. It didn't.
I'm running a 1k resistor between the serout pin and the Com1 port on the laptop.

Here's an example of the code:

Include "modedefs.bas"

@ __config _config1, _INTRC_IO & _WDT_OFF & _MCLR_OFF & _LVP_OFF
'Interal Oscillator, WDT off, MCLR off, LVP off
'For use with Microchip Assembler

Define OSC4

i VAR Byte

OSCCON = %01101000 'OSC4, internal

'DEFINE CHAR_PACING 1000 'For leaving slight spacing between Serout characters

CMCON = 7 'Comparators Off

' Set the port directions
' 0=output 1=input
TRISA = %11111111 'Need to set A.0 as Input
TRISB = %11111011 'Set B.2 as output

ANSEL = %00000000 ' Set digital

Pause 5000

i = 0

for i = 1 to 10

High PortB.1 'Turn on LED
Pause 1000

Low PortB.1 'Turn off LED
Pause 1000

Serout PortB.2,N2400,["This is a test ", #i,10,13]
Pause 500

Next i

END

Bill Legge
- 7th August 2009, 07:10
Here is the reply I go from Melanie on this subject:

When you first power-up, your hardware, registers, buffers can have all kinds of crap lurking. There's no real way of flushing this out, so the best thing I do is send a dummy line containg a dozen Nulls ($00) or so and terminate that with a CR LF. From then onwards, we're open for business.

Now, it all depends what is connected to what... if at the receiving end you've got an ANSI Terminal, then the next thing you'll send is a Clear-Screen, if it's an Invoicing printer, then you'd send a Page-Feed, POS Terminal you'd eject a couple of inches of paper along with a paper-cut command, etc etc...

If one PIC is connected to another, then I tend to send things in packets, and I always prefix the packet with some NULLs and a CR LF. The packet always has to have a valid header and a Checksum, otherwise it's dumped.

Generally, I've found that the first thing out of the SEROUT business in wrong/odd/missing and so design your code accordingly.

Regards Bill Legge

BrianT
- 12th August 2009, 04:12
I find it pays to condition the TxData line first. For true RS232, i.e. using a MAX232 for example, the following works a treat.

HIGH PortB.2 : Pause 5
Serout PortB.2,N2400,["This is a test ", #i,10,13]

This forces the RS232 interface into the correct MARK state where the external line to the terminal device idles low at negative 6~12 volts. The Start bit, a SPACE, is then seen as a positive going bit.

HTH
BrianT