writing ISR for UART

Discussion in 'C Programming' started by amit, Dec 8, 2009.

  1. amit

    amit Guest

    hy, i am not very expert in C. Can any one give me a sample example on ISR
    for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    i add "case" in side ISR? can any one give me any tips or any link.

    thanks in advance
    amit, Dec 8, 2009
    #1
    1. Advertising

  2. amit

    amit Guest

    amit:
    > hy, i am not very expert in C. Can any one give me a sample example on
    > ISR for UART.i am using MPLAB with c18. how to write IRQ in ISR. also
    > how can i add "case" in side ISR? can any one give me any tips or any
    > link.
    >
    > thanks in advance


    hello, is anyone there?

    maybe i need to give more details...

    i want to execute individual primitives of Zigbee protocol. let's take two
    primitives like MLME-ASSOCIATE.request and MLME-ASSOCIATE.indication.i'll
    use them as two different "case". Actually my main intention is to measure
    the power consumption for each primitives of PHY,MAC,NWK and APL.

    I am thinking to do it like this.as i don't find any complete Zigbee
    device i choose CC2420 Zigbee node connected with PICDEM Z board
    containing a micro-controller PIC18f4620. usually i use the MPLAB IDE to
    run the project and MPLAB ICD 2 to debug the program that contains
    complete Zigbee stack command. instead of running the complete stack my
    intension is to run the individual command and to observe the power change
    in the CC2420. to avoide the multiple debugging my plan is like this: i'll
    add all the command in the LabVIEW simulator and the each command will
    written as a 'case' in C.by using the MPLAB and ICD 2 the program will be
    debugged in the PIC18f4620.this program should contain an ISR so that once
    i debug the program i just select the command by clicking a button in the
    LabVIEW simulator connected to the PICDEM Z by a serial port (UART).
    according to the command it will directed to a interrupt table that
    trigger an ISR. and accordingly the case will be selected and the register
    value of the CC2420 will change. then i'll record the power change.

    With a help from a colleague i sketch my program could be like this :

    The interrupt service routine for the UART is something like the
    following:

    at the beginning of main
    - open the serial setting the right baud rate
    - enable IRQ

    ---------------------------

    :::few coding line though i am not sure whether it works:::::

    #include <p18cxxx.h>
    #include <usart.h>

    void main (void)
    {

    // Configure all PORTB pins for output

    TRISB = 0;

    / *
    open the serial setting the right baud rate

    Open the USART configured as
    8N1, 2400 baud, in polled mode

    */

    OpenUSART (USART_TX_INT_OFF &
    USART_RX_INT_ON &
    USART_ASYNCH_MODE &
    USART_EIGHT_BIT &
    USART_CONT_RX &
    USART_BRGH_HIGH, 103);

    TXSTA = 0x20; // setup USART transmit
    RCSTA = 0x90; // setup USART receive

    // Use high priority interrupt
    IPR1bits.TXIP = 1;

    ""Here i have to enable IRQ"""'

    }

    """now i have to write the ISR. i got an example like following""" but
    don't know how to write my own program.::::

    /*********************************************************************
    * Function: void UART_ISR(void)
    *
    * PreCondition: UART interrupt has occured
    *
    * Input: None
    *
    * Output: None
    *
    * Side Effects: None
    *
    * Overview: None
    *
    * Note: This function is supposed to be called in the ISR
    * context.
    ********************************************************************/

    void UART_ISR(void)
    {
    // NOTE: All local variables used here should be declared static

    static BYTE rxdata;

    // Store a received byte, if pending, if possible

    if(PIR1bits.RCIF)
    {
    // Get the byte
    rxdata = RCREG;

    // Clear the interrupt flag so we don't keep entering this ISR
    PIR1bits.RCIF = 0;

    // YOU CAN USE THE rxdata HERE
    // OR PUT IN A CIRCULAR BUFFER TO BE USED IN THE WHILE(1) IN THE
    MAIN
    // IN THIS EXAMPLE
    // Copy the byte into the local FIFO, if it won't cause an overflow

    if(RXHeadPtr != RXTailPtr - 1)
    {
    if((RXHeadPtr != vUARTRXFIFO + sizeof(vUARTRXFIFO)) ||
    (RXTailPtr != vUARTRXFIFO))
    {
    *RXHeadPtr++ = i;
    if(RXHeadPtr >= vUARTRXFIFO + sizeof(vUARTRXFIFO))
    RXHeadPtr = vUARTRXFIFO;
    }
    }
    }

    #IF 0 //TX CODE CAN BE SKIPPED

    // Transmit a byte, if pending, if possible

    if(PIR1bits.TXIF)
    {
    if(TXHeadPtr != TXTailPtr)
    {
    TXREG = *TXTailPtr++;
    if(TXTailPtr >= vUARTTXFIFO + sizeof(vUARTTXFIFO))
    TXTailPtr = vUARTTXFIFO;
    }
    else // Disable the TX interrupt if we are done so that we don't
    keep entering this ISR
    {
    PIE1bits.TXIE = 0;
    }
    }

    #ENDIF

    }

    """"Moreover, in the main application, i need to declare the needed
    variables:""""""""

    #define BAUD_RATE 19200

    // Ring buffers for transfering data to and from the UART ISR:
    // - (Head pointer == Tail pointer) is defined as an empty FIFO
    // - (Head pointer == Tail pointer - 1), accounting for wraparound,
    // is defined as a completely full FIFO. As a result, the max data
    // in a FIFO is the buffer size - 1.
    static BYTE vUARTRXFIFO[65];
    static BYTE vUARTTXFIFO[17];
    static BYTE *RXHeadPtr = vUARTRXFIFO, *RXTailPtr = vUARTRXFIFO;
    static BYTE *TXHeadPtr = vUARTTXFIFO, *TXTailPtr = vUARTTXFIFO;

    [...]

    now the point is how i can make the program complete. could you please
    help me out. and another important question how to add my "case" in the
    program.

    thanks to read the long writing.

    thanks in advance and waiting for a kind guidance.
    amit, Dec 8, 2009
    #2
    1. Advertising

  3. amit

    Seebs Guest

    On 2009-12-08, amit <> wrote:
    > hy, i am not very expert in C. Can any one give me a sample example on ISR
    > for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    > i add "case" in side ISR? can any one give me any tips or any link.


    Here's a useful tip: What you're asking about has virtually nothing to
    do with C, and a great deal to do with the target platform you're working
    on, which you have neglected to mention. Normally, people would respond
    by emailing you suggestions or better places to ask, but you provided a
    bogus email address, so they can't.

    Figure out what platform you're on, and find a group specific to it, and
    you'll do a lot better.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Dec 8, 2009
    #3
  4. amit

    Boon Guest

    amit wrote:

    > hy, i am not very expert in C. Can any one give me a sample example on ISR
    > for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    > i add "case" in side ISR? can any one give me any tips or any link.


    Kenny, will you stop trolling already?
    Boon, Dec 8, 2009
    #4
  5. amit

    Boon Guest

    Seebs wrote:

    > Here's a useful tip: What you're asking about has virtually nothing to
    > do with C


    Posting through AIOE about

    why kernel32.dll is mapped at 77E90000 where is user address space??
    How to reduce the size of C runtime heap?
    writing ISR for UART

    spells T-R-O-L-L.
    Boon, Dec 8, 2009
    #5
  6. amit

    Seebs Guest

    On 2009-12-08, Boon <root@localhost> wrote:
    > Seebs wrote:
    >
    >> Here's a useful tip: What you're asking about has virtually nothing to
    >> do with C

    >
    > Posting through AIOE about
    >
    > why kernel32.dll is mapped at 77E90000 where is user address space??
    > How to reduce the size of C runtime heap?
    > writing ISR for UART
    >
    > spells T-R-O-L-L.


    Or newbie.

    I like to at least give newbies one try at helping them, because a lot of
    newbies suffer unjustly for sounding like trolls.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Dec 8, 2009
    #6
  7. amit

    Kaz Kylheku Guest

    On 2009-12-08, amit <> wrote:
    > hy, i am not very expert in C. Can any one give me a sample example on ISR
    > for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    > i add "case" in side ISR? can any one give me any tips or any link.


    You can't write ISR's in C, because typically ISR routines have very
    machine-architecture-specific requirements. So you need a piece of glue which
    meets those requirements (like saving registers, obtaining some interrupt code
    in a pariticular way, etc). Your machine language glue code can then dispatch a
    C function. When that function returns, the glue code will have yet more
    machine-specific work to do to arrange a proper return from the interrupt.

    So the C function is not a complete ISR. It's just C code executed in an ISR
    context.

    In short, an interrupt cannot simply be vectored to the address of a function
    which has C calling conventions. (Maybe there is some platform where this is
    the case, but that would be an astonishing exception).
    Kaz Kylheku, Dec 8, 2009
    #7
  8. In article <4b1ed140$0$30451$>,
    Boon <root@localhost> wrote:
    >amit wrote:
    >
    >> hy, i am not very expert in C. Can any one give me a sample example on ISR
    >> for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    >> i add "case" in side ISR? can any one give me any tips or any link.

    >
    >Kenny, will you stop trolling already?


    Heh heh. I do have to give 'amit' full marks for a job well done.
    While I am sure that most of it is jibberish, I did look up 'Zigbee' and
    there really is such a thing.
    Kenny McCormack, Dec 8, 2009
    #8
  9. amit <> wrote:
    >hy, i am not very expert in C. Can any one give me a sample example on ISR
    >for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    >i add "case" in side ISR? can any one give me any tips or any link.


    I found that your exact question, word for word, was answered in
    February 2008 on this website:

    http://www.motherboardpoint.com/writing-isr-uart-t179101.html

    It must be your lucky day!

    -Beej
    Beej Jorgensen, Dec 9, 2009
    #9
  10. amit

    Noob Guest

    Feh!

    Subject: why kernel32.dll is mapped at 77E90000 where is user address space??
    From: lostlander
    Date: Sat, 13 Oct 2007 14:47:13 -0000
    Newsgroups: microsoft.public.win32.programmer.kernel
    http://www.archivum.info/microsoft....apped_at_77E90000_where_is_user_address_space


    Subject: How to reduce the size of C runtime heap?
    From: motonari.ito
    Date: Sun, 30 Sep 2007 16:14:46 -0000
    Newsgroups: microsoft.public.win32.programmer.kernel
    http://www.archivum.info/microsoft....0051/How_to_reduce_the_size_of_C_runtime_heap


    Subject: writing ISR for UART
    From: sohagiut
    Date: Thu, 07 Feb 2008 04:55:04 -0600
    Newsgroups: comp.arch.embedded
    http://www.archivum.info/comp.arch.embedded/2008-02/01701/writing_ISR_for_UART
    Noob, Dec 9, 2009
    #10
  11. amit

    Nobody Guest

    On Tue, 08 Dec 2009 21:05:12 +0000, amit wrote:

    > hy, i am not very expert in C. Can any one give me a sample example on ISR
    > for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    > i add "case" in side ISR? can any one give me any tips or any link.


    I suggest asking in comp.arch.embedded.

    This question has more to do with the PIC18 architecture than with C, even
    allowing for the fact that C compilers for 8-bit microcontrollers
    typically don't conform to the ISO C standards (for good reason).
    Nobody, Dec 9, 2009
    #11
  12. Nobody <> writes:

    > On Tue, 08 Dec 2009 21:05:12 +0000, amit wrote:
    >
    >> hy, i am not very expert in C. Can any one give me a sample example on ISR
    >> for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    >> i add "case" in side ISR? can any one give me any tips or any link.

    >
    > I suggest asking in comp.arch.embedded.


    Someone did, almost two years ago. This question appears there
    word-for-word (including the odd spacing and capitalisation).

    <snip>
    --
    Ben.
    Ben Bacarisse, Dec 9, 2009
    #12
  13. amit

    Thad Smith Guest

    Kaz Kylheku wrote:
    > On 2009-12-08, amit <> wrote:
    >> hy, i am not very expert in C. Can any one give me a sample example on ISR
    >> for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can
    >> i add "case" in side ISR? can any one give me any tips or any link.

    >
    > You can't write ISR's in C, because typically ISR routines have very
    > machine-architecture-specific requirements. So you need a piece of glue which
    > meets those requirements (like saving registers, obtaining some interrupt code
    > in a pariticular way, etc). Your machine language glue code can then dispatch a
    > C function. When that function returns, the glue code will have yet more
    > machine-specific work to do to arrange a proper return from the interrupt.
    >
    > So the C function is not a complete ISR. It's just C code executed in an ISR
    > context.
    >
    > In short, an interrupt cannot simply be vectored to the address of a function
    > which has C calling conventions. (Maybe there is some platform where this is
    > the case, but that would be an astonishing exception).


    Many C compilers for embedded processor targets provide an extension to
    specify that a function is the service routine for a particular
    interrupt. There are many different extensions for this, varying from
    one vendor to another. These compilers take care of most glue, such as
    saving all needed registers.

    --
    Thad
    Thad Smith, Dec 11, 2009
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand P Paralkar

    UART Implementation

    Anand P Paralkar, Jun 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    12,930
    Rudolf Usselmann
    Jul 7, 2003
  2. Mike Treseler

    Re: vhdl UART

    Mike Treseler, Aug 8, 2003, in forum: VHDL
    Replies:
    1
    Views:
    3,907
    SneakerNet
    Aug 10, 2003
  3. Shashi

    Issues on clockless UART

    Shashi, Apr 21, 2004, in forum: VHDL
    Replies:
    3
    Views:
    687
  4. Eirik Midttun

    ISR (interrupt service routine) code in isr.c

    Eirik Midttun, Feb 7, 2007, in forum: C Programming
    Replies:
    2
    Views:
    639
    Jack Klein
    Feb 8, 2007
  5. ISR and a normal function

    , May 21, 2008, in forum: C Programming
    Replies:
    8
    Views:
    833
    Richard Tobin
    May 22, 2008
Loading...

Share This Page