No announcement yet.

Software serial with interupts example

  • Filter
  • Time
  • Show
Clear All
new posts

  • Software serial with interupts example

    I have a project that requires the use of 3 uarts.

    The Pic I'm using is a 18f2321 running at 40Mhz.

    One of the uarts is using the hardware, and the other 2 will be implemented in software, all will run at 9600 bps.

    My problem is all of these ports can be hit with unsolicited data, so i need to figure out a way of servicing them as efficiently as possible.

    The pic is running very fast and normally would not have any trouble polling ports at that speed, but as you know the code will halt for a small amount of time to see if a character has arrived. It is during this time that other data on another port might be missed.

    So I am looking to use interupts if I can to allow the code to run full speed and only service the uarts when actually needed.

    Also I'm not even sure I can generate an interrupt on a pin that is being used by Serin2?

    Firstly does anybody have any hints or tips on what im trying to achieve, and secondly can anybody point me to a code example where multiple uarts are used.





  • #2
    Since the data could come in at any time, I assume they could also come in at the same time on two different ports.
    There would be no way to have two SERIN2's running at the same time.

    But the biggest problem is that the interrupt responds to the first transition of the signal which is the "Start bit". If you then run a SERIN2 it will see the next transition as the Start bit and the data will be garbled.

    If you had a chip with 2 USARTS, you could use an interrupt driven software UART for the third channel, but PBP doesn't have a native command for that.
    PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
    Never download a PIC datasheet from anywhere but


    • #3
      Thanks for your reply Darrel.

      You are correct about all your assumptions, this is the point Im concerned about.

      I guess what I need to know is a few technical points.

      Can an interrupt be triggered on the falling edge of the start bit, even if i need to use another pin in parallel just to detect this.

      When the Serin2 command is executed, does it need to see the high to low transition of the beginning of the start bit, or is it happy to accept that the line is low and begin from there? i.e if I interrupt on the high to low then jump to a serin2 in the ISR will that work?

      I have read that uarts generally operate by detecting the low state (not the transition) and then wait half the bit width and sample again to make sure it's not a glitch and to position the timing at approximately the mid point of the bit. It then samples every bit with in time (i.e 104us for 9600) to detect the data. is this how serin2 works?

      Thanks again for your help.



      • #4
        Yes, you can generate an interrupt on the falling edge of serial data, as long as you are on an interrupt pin.
        But no, the SERIN2 command doesn't recognize the low without a high/low transition as the start bit. It must see that transition to get the half-bit timing right.

        Here's an example of what happens ...
        The chip on the left transmits a string of text when the button is pressed.
        The chip on the right receives the string and re-transmits it unchanged.
        Two modes are selected by the switch, either with or without an interrupt.

        On the right you can see that with an interrupt, the data is garbled.
        But there is enough of a delay while the DEC number is converted to ascii that it's able to find the correct position of the start bit and the numbers are displayed properly.

        So if you have control of the transmit devices, you can send one dummy character and a small delay (1 BYTE period), then the receiver can ignore the first byte, and grab the rest.
        If you don't have control of the TX, that method probably won't work.

        Attached Files
        PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
        Never download a PIC datasheet from anywhere but


        • #5
          Thanks again Darrel,

          I see the problem and it is as I expected.

          I have bitten the bullet and added an outboard dual uart chip (SC16IS752) that communicates via I2C and that should solve all my problems.

          I think overall this solution is cleaner and more reliable in the long run, and it has nice big 64 byte buffers as well.

          BTW what is that simulator your using in your example?




          • #6
            That sounds like your best bet.

            The simulator is Proteus VSM.
            Couldn't live without it.
            PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
            Never download a PIC datasheet from anywhere but