• 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

    teo-16018

    Microsoft security essentials blues

    Thread Starter: shantanu@india

    Hi, A strange thing happened just now.My Windows 7 Micrsoft Sercurity Essentials suddenly gave a warning that prplus.exe(Compiler) is a potential...

    teo Today, 19:23 Go to last post
    barak-1433

    SPI mssp

    Thread Starter: picprotonguy

    Just ordered Digital to Analog IC MCP4822 with SPI interface and immediately opened proton manual. I'm very disappointed, it looks like Proton...

    barak Today, 22:50 Go to last post
    David Barker-251

    UMC bootloader 18F46K22

    Thread Starter: shunt010

    Hi all. I am trying to build a UMC bootloader file for the 18F46K22, and it doesn't appear to work. It returns (in the error log) Error ...

    David Barker Today, 11:20 Go to last post