Reading hardware serial input pin

amdunn

New Member
On an M2 part... is it possible to read the input state of the HSERIN pin independently of the background serial receive also watching it? Especially since the background serial receive is a bit different on the M2s. The documentation doesn't describe any interaction between hardware serial receive and reading the pin or if one negates or interferes with the other.

In other words...

Do an HSERSETUP that sets up background serial receive... (which on a 14M2 is pin B.1)...

Then, test IF PIN B.1 = 0 ....

and elsewhere still check for background receive, like this:
w1 = $FFFF
hserin w1
if w1 <> $FFFF then....

Do the reading of the pin and the background serial receive interfere with each other... or does HSERSETUP totally preempt the ability to read the pin in the first place?

The reason for the question is a desire to have the main program determine if the serial in is IDLE (which for an M2 part with no inversion would be HIGH) prior to seizing the bus and sending something out, but if not IDLE (remote serial device has started to send) don't transmit, but wait for the serial receive instead which might be multiple bytes.

Testing with HSERIN only returns a result at the END of a complete byte receive operation... I want to determine the serial line is in use at the START of the operation (in other words, I want to see the start bit).

The question also extends to whether this interaction would work with a COUNT command... something like this:

COUNT B.1,40,b0 ; count any low-to-high in a 40ms span - a byte at 300 baud takes 30ms and has at least one low-to-high
IF b0 > 0 then.... ; serial port was in use so try to receive
IF PIN B.1 = 0 then... ; serial port is in use right now! wait a bit and try to receive
;aha - serial port NOT in use - go ahead and transmit

or if COUNT is incompatible with background serial receive.


Even better would be the ability to use SETINT to set an interrupt that fires if B.1 goes low, which it would when receiving a start bit... but you can't do interrupts on port B on an M2 part!
 

AllyCat

Senior Member
Hi,
Do the reading of the pin and the background serial receive interfere with each other... or does HSERSETUP totally preempt the ability to read the pin in the first place?
No, I think it is probably possible to do as you describe, but IMHO there are "better" ways to do it. However, most of these methods do involve some use of the PEEK/POKESFR commands and maybe getting your hands dirty with the "base PIC" Microchip Data Sheet. Also, the term "Background Receive" may be misleading, because the M2s have only a two byte "silicon hardware" buffer, with very little support (and some hindrance) from the Operating System. :(

Firstly, the "easy" solution would be to allocate a second pin, connected in parallel with the HSERIN input. Then there can be no issue with conflicts in reading the pin(s) and the additional pin might be interrupt-compatible. ;)

Personally, I rarely use the COUNT instruction because it is "blocking" (i.e. the program can do nothing else whilst it is executing) and also it hardly seems to meet your requirement. The HSERIN hardware in M2s can use only Idle-High ("TTL polarity") so the start bit is Negative-going and there is a possibility that the first (required) positive edge to count will be the start of the STOP pulse. Also, don't overlook that the PICaxe interrupts are not Latching so may have disappeared before the interrupt is executed. :(

Just to list a few other possibilities; there is the S-R Latch (although I've never found it useful) which is "supported" by the OS, or the "Timer1 Gate" hardware (which can be used as a configurable Latch independent of the Timer itself), or the HSERIN input can be "swapped" (via APFCON) onto a pin which is "interruptable". But perhaps the most useful feature is the "Interrupt on Change Flags", where the emphasis (with PICaxe) is on the "Flags" not the "Interrupt" itself. PICaxe cannot be interrupted by the IOCs, but SFR commands can configure and read the flags, which can be set by positive and/or negative edges on some pins (including HSERIN).

Cheers, Alan.
 
Top