Skip to content

High speed RS232 for PIC

Figure 3. 230400, start bit (4IC), bit0 (5IC),  bit1(4IC), bit2(4IC),…

Listening air

Before we will be able receiving data with high speeds, we have to decide how we will detect the start bit. Basically, there are two ways: via interrupt or in a polling loop. Let us analyze pos and cons for these two solutions. The shortest polling loop is:
     btfsc    port, pin
     goto     $-1
However, this loop polls the pin every third cycle and, when it exists, start bit already hold for 2–4 IC. Port reading itself also has some non-deterministic period: PIC reads data from a port on the raising edge of Q2, (see [4], Figure 9.7) which gives us non-deterministic period ‑0.75 – +0.25 IC. Altogether polling loop error, bit mistiming (0.4 IC) and non-determinism make 1.2 – 4.3 IC (or 2.75±1.55) and contributes 36% error. Definitely, it would be difficult to achieve reliable communication with such error 3. The highest baud rate, where polling loop may be used, is 128000 at 4 MHz (1.55*IC=bit*0.2). So, let us consider what we can do with interrupts.

Catching a fish

Table 3 . Interrupt routine tasks

# Task Duration
I hardware latency 3
II jump to interrupt handler 2
III saving context 3
IV detecting interrupt source 2
V clearing working file 1
VI receiving data 37
VII setting up buffer 2
VIII saving data into buffer 2
IX incrementing buffer pointer 1
X incrementing byte counter 1
XI restoring context 4
XII clearing interrupt flag 1
XIII leaving interrupt routine 2

There may be few consecutive bytes coming, so the routine must store data to a buffer and increment buffer pointer. Nevertheless, we may assume that this buffer (FSR) is preset externally and is always valid in an interruption caused by RX pin. With this assumption, we may drop (VII) setting up the buffer and (VIII) saving data into buffer. Instead, we will set bits directly in INDF. In addition, we may sacrifice jump (II) and make the routine starting at org 0x04. Application may use interrupts for other purposes, so there should be branch for other sources (IV). With a straightforward implementation, the first read-bit instruction may be placed only at ninth position and the routine may exit ten cycles after the last bit read, which is not acceptable. Let us look for another implementation.

3 Before I wrote this chapter, I have tried a receiver routine with polling loop at 230400 and it failed. This failure has induced me to find an explanation for it.

Post a Comment

Your email is never published nor shared. Required fields are marked *