• PicŪ Basic


  • Interrupt driver Buffered Serial Input

    I found that when combining the HW and SW serial implementations on a PICŪ, it often occurs that you miss characters because of the rather strict timing restraints of the software implementation.

    I have put together an interrupt based serial buffer that allows the HW serial port to be buffered automatically. This way the "normal" processing can continue and the SW serial port is not affected.

    This piece of code works on the 18F[24]5x as far as I have tested and does serial buffering. It operates in a circular array way.

    When more then 65 characters are received, the first one will be overwritten and all 64 previous characters will be lost! So regular checking of the input buffer is still required.

    It should be possible to implement this processing with buffers of sizes which are a power of 2 (2-4-8-16-32-64-128-256) without any problems. When using other sizes, makes sure that the generated code does not use the internal variables!!

    Code is constructed in such way that no compiler variables are altered when in the interrupt routine. This way the code should not interfere with the "normal" run of the program.

    Example Code

    'Hardware serial port buffer
    DIM BUFFER[64] AS BYTE
    DIM BUFH AS BYTE
    DIM BUFT AS BYTE
    DIM DUM AS BYTE

    ON_INTERRUPT goto INTERRUPT_ROUTINE 'hardware interrupt

    GOTO OVER_INTERRUPT

    INTERRUPT_ROUTINE:
    CONTEXT SAVE 'Only needed for 16F devices

    ' INTERRUPT HANDLING WHEN HW USART RECEIVES A CHARACTER
    IF PIR1.5 = 1 THEN
    CLEAR PIR1.5
    BUFFER[BUFT] = RCREG
    INC BUFT
    BUFT = BUFT & 63
    ENDIF
    CONTEXT RESTORE

    OVER_INTERRUPT:

    Main_Loop:
    GOSUB INIT
    ' Other things to do...

    ' -------------------------------------------------------------
    INIT:
    HRSIN {1,main_loop}, DUM 'initiate the Hardware Serial Port
    'if I don't do this, it does not work
    PIE1.5=1 'settings for 18F458
    INTCON=$C0
    RETURN

    ReadChar:
    WHILE BUFT = BUFH:WEND
    DUM= BUFFER[BUFH]
    INC BUFH
    BUFH = BUFH & 63
    RETURN
    contributed by Tom Van Den Eede.
  • Recent Activity

    towlerg-21522

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    towlerg Today, 16:26 Go to last post
    towlerg-21522

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    towlerg Today, 16:25 Go to last post
    palamont-28794

    MID$ Problem with a PIC18F25K22

    Thread Starter: palamont

    Hi everybody, I encounter a strange problem : I have a string of 230 characters. This string is divided into 10 segments of 23 characters, with a...

    palamont Today, 10:08 Go to last post