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.