Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC32MX - UART TX interrupt ISR activation problem

Status
Not open for further replies.

Drugo

Junior Member level 2
Joined
Feb 23, 2011
Messages
20
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,537
Hello everyone,
after having read the PIC32MX UART and interrupt documentation I can't fix the problem and I'm stuck with my project. That is, how to mange the TX Interrut Service Routine using PIC32MX795F512L. In the sense that, once I configure UART2 to generate interrupt after data transmission, the UART TX ISR is continuously called and U2TXIF is always 1, even if I reset it with an instruction at the end of the UART ISR.

I explain better my problem with the UART example present in MPLAB C32 Suite, which can be open through "uart_interrupt.mcp" project file. To test this project I'm using Microchip Explorer 16 Demo Board.
In this project, originally, it's just enabled the UART RX interrupt (instruction INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED)). Therefore, to trigger the ISR also when I transmit a character, after that instruction I added the corresponding INTEnable(INT_SOURCE_UART_TX(UART2), INT_ENABLED) one. But in this case the project doesn't work properly, because ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void) ISR function is continuously called. I attach the related code to be more clear. In this code I didn't add all the code I need to manage character transmission, because before I have to properly manage the UART TX ISR operation.

Please, since I don't find a solution to this problem and I'm stuck on the project, can anyone help me? Many many thanks!

Code:
[FONT=Courier New]#include <plib.h>                    // Peripheral Library


// *****************************************************************************
// *****************************************************************************
// Section: Configuration bits
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
// *****************************************************************************
// *****************************************************************************
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1

// *****************************************************************************
// *****************************************************************************
// Section: System Macros
// *****************************************************************************
// *****************************************************************************
#define    GetSystemClock()             (80000000ul)
#define    GetPeripheralClock()        (GetSystemClock()/(1 << OSCCONbits.PBDIV))
#define    GetInstructionClock()        (GetSystemClock())

#define DESIRED_BAUDRATE        (9600)      //The desired BaudRate


void WriteString(const char *string);

int main(void)
{

    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states, RAM
    // wait state and enable prefetch cache but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above..
    SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    mPORTAClearBits(BIT_7);         // Turn off RA7 on startup.
    mPORTASetPinsDigitalOut(BIT_7);    // Make RA7 as output.


    // Explorer-16 uses UART2 to connect to the PC.
    // This initialization assumes 36MHz Fpb clock. If it changes,
    // you will have to modify baud rate initializer.
    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(UART2, GetPeripheralClock(), DESIRED_BAUDRATE);
    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

    // Configure UART2 RX Interrupt
    INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED);[/FONT][FONT=Courier New]
    INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);
     
 [COLOR=red]   /*** [/COLOR][/FONT][FONT=Courier New][COLOR=red]CODE I ADDED TO MICROCHIP EXAMPLE[/COLOR][/FONT][COLOR=red] ***/

[/COLOR][FONT=Courier New][COLOR=red]     // Configure UART2 TX Interrupt[/COLOR][/FONT][COLOR=red]
     [/COLOR][FONT=Courier New][COLOR=red]INTEnable(INT_SOURCE_UART_TX(UART2), INT_ENABLED);                           [/COLOR][/FONT][COLOR=red]
[/COLOR] [FONT=Courier New][COLOR=red]    INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);[/COLOR][/FONT][COLOR=red]
[/COLOR] [FONT=Courier New][COLOR=red]    INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);
[/COLOR][/FONT][FONT=Courier New][COLOR=red] 
   /*** [/COLOR][/FONT][FONT=Courier New][COLOR=red]END CODE I ADDED TO MICROCHIP EXAMPLE[/COLOR][/FONT][COLOR=red] ***/[/COLOR][FONT=Courier New]

    // configure for multi-vectored mode
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    // enable interrupts
    INTEnableInterrupts();


   WriteString("*** UART Interrupt-driven Application Example ***\r\n");
   WriteString("*** Type some characters and observe echo and RA7 LED toggle ***\r\n");


    // Let interrupt handler do the work
    while (1);

    return 0;
}
// helper functions
void WriteString(const char *string)
{
    while(*string != '\0')
    {
        while(!UARTTransmitterIsReady(UART2))
            ;

        UARTSendDataByte(UART2, *string);

        string++;

        while(!UARTTransmissionHasCompleted(UART2))
            ;
    }
}
void PutCharacter(const char character)
{
        while(!UARTTransmitterIsReady(UART2))
            ;

        UARTSendDataByte(UART2, character);


        while(!UARTTransmissionHasCompleted(UART2))
            ;
}

// UART 2 interrupt handler
// it is set at priority level 2
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void)
{
    // Is this an RX interrupt?
    if(INTGetFlag(INT_SOURCE_UART_RX(UART2)))
    {
        // Clear the RX interrupt Flag
        INTClearFlag(INT_SOURCE_UART_RX(UART2));

        // Echo what we just received.
        PutCharacter(UARTGetDataByte(UART2));

        // Toggle LED to indicate UART activity
        mPORTAToggleBits(BIT_7);
    }

    // We don't care about TX interrupt
    if ( INTGetFlag(INT_SOURCE_UART_TX(UART2)) )
    {
        INTClearFlag(INT_SOURCE_UART_TX(UART2));
    }
}[/FONT]
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top