Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > writing ISR for UART

Reply
Thread Tools

writing ISR for UART

 
 
amit
Guest
Posts: n/a
 
      12-08-2009
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
 
Reply With Quote
 
 
 
 
amit
Guest
Posts: n/a
 
      12-08-2009
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.
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      12-08-2009
On 2009-12-08, amit <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Boon
Guest
Posts: n/a
 
      12-08-2009
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?
 
Reply With Quote
 
Boon
Guest
Posts: n/a
 
      12-08-2009
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      12-08-2009
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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      12-08-2009
On 2009-12-08, amit <(E-Mail Removed)> 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).
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      12-08-2009
In article <4b1ed140$0$30451$(E-Mail Removed)>,
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.

 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      12-09-2009
amit <(E-Mail Removed)> 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/writ...t-t179101.html

It must be your lucky day!

-Beej

 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      12-09-2009
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.p..._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.p...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.e...g_ISR_for_UART
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ISR (interrupt service routine) code in isr.c Eirik Midttun C Programming 2 02-08-2007 03:52 AM
Issues on clockless UART Shashi VHDL 3 04-22-2004 12:05 AM
Issues on Shift Register in a Clockless UART Shashi VHDL 0 04-20-2004 11:51 PM
Re: vhdl UART Mike Treseler VHDL 1 08-10-2003 08:45 PM
UART Implementation Anand P Paralkar VHDL 2 07-07-2003 05:53 AM



Advertisments