Comm Port Error Handeling

If I were to describe the proccess of implementing a sreial port communication with a microcontroller I’d say It’s a shot in the dark. There are a fairly large number of variables that come into play when data is transmitted and received. I just wanted to touch on error handeling here just as sort of note to my self if nothing else. Presuming you get the baud rate and the number of bits set right and you are able to get the basic communication working there are a couple of things that can still happen that will put a damper on the whole thing.

Framing Errors

If you send a whole string of data to a microcontroller and the controller software isn’t paying a lot of attention to the comm port one of the things that can happen is called a framing error. In the PIC18F2320 chip there is the “rcsta” (receive status register) which is at address $fab. Bit 2 of this register will go high on a framing error. The best resolution for this error that I have found is to go ahead and read the data from the “rcreg” receive register but keep in mind that it may be bad data.

Over Run Errors

Another kind of error you can get is called over run error. This can be a serious problem because it will cause the reciever to stop working until the problem is resolved. Bit 1 of the rcsta will go high (1) on an over run error. To resolve this error turn off the continuois recieve bit (bit 4) of the rcsta (set it to 0) and then turn it back on (set it to 1). This will flush out data that is held in the receive buffer and reset the reciever to run again.

CORRECTION It will flush out the receive buffer but it will not clear the error. So don’t keep doing it in a loop because the error will clear only after the next byte is received. If you keep reseting the receiver it will never receive the next transmition.

So here is a code example for the Logochip to handle the comm port:

;CommErrorTest.txt

constants [
 [rcreg $fae][rcsta $fab]
 [spbrg $faf][pir1 $f9e][txsta $fac]
 [txreg $fad][t2con $fca]
  ] 

global[ comand ]

to powerup
 prs “CommErrorTest.txt”
 write spbrg 25  ; sets baud rate to 19200
 write rcsta $90
 write txsta $24
 write t2con 6
end

to startup
 loop[
  if testbit 1 rcsta [ ;check overrun Error
  clearbit 4 rcsta
  prs "overrun"
  setbit 4 rcsta
  ]
  if testbit 5 pir1 [
   setcomand uart-receive
   print comand
   ]
 wait 10
 ]
end

to uart-send :n
  waituntil [testbit 1 txsta]
  write txreg :n
end

to uart-receive
 if testbit 2 rcsta [  ;check frame Error
  prs "framing"
  ]
 output read rcreg
end



Leave a Reply

You must be logged in to post a comment.