writing ISR for UART

A

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
 
A

amit

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.
 
S

Seebs

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
 
B

Boon

amit said:
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?
 
B

Boon

Seebs said:
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.
 
S

Seebs

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
 
K

Kaz Kylheku

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).
 
K

Kenny McCormack

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.
 
N

Noob

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
 
N

Nobody

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).
 
B

Ben Bacarisse

Nobody said:
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>
 
T

Thad Smith

Kaz said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top