Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I am new to PIC32 MCU, I wrote a small piece of code to run the UART.

The MCU is the Microchip PIC32MX795F512H, the code uses interrupts to handle reception and transmission on the UART channel 5 (UART5), basically the code returns the user input.

The code is almost functional just a weird behavior: the first 8 bits are returned correctly and then 3 characters are transmitted to the console.

The input is azertya and the MCU returns: azertyaaaa (it adds aaa,the letter a was the first character inputed) and sometimes azertyaÿÿÿ; then for one characters input it returns the characters and three others (not random).

Main.c

            #include <p32xxxx.h>
            #include <plib.h>
            #include "uart.h" 

            #define GetSystemClock()                (80000000ul)
            #define GetPeripheralClock()                (GetSystemClock()/(1 << OSCCONbits.PBDIV))
            #define GetInstructionClock()               (GetSystemClock())

            #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
            #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1

            #define US_TO_CT_TICKS      (CPU_CT_HZ/1000000UL)   

            #define UARTCH1 UART5


            /*------------------------- Interruptions -----------------------------------*/


            void __ISR(_UART_5_VECTOR, ipl2) IntUART5Handler(void){


                if(INTGetFlag(INT_SOURCE_UART_RX(UARTCH1)))
                {

                        INTClearFlag(INT_SOURCE_UART_RX(UARTCH1));


                        PutCharacter(UARTGetDataByte(UARTCH1), UARTCH1);

                        mPORTBToggleBits ( BIT_12 );
                }


                if ( INTGetFlag(INT_SOURCE_UART_TX(UARTCH1)) ){

                        INTClearFlag(INT_SOURCE_UART_TX(UARTCH1));

                        PutCharacter(UARTGetDataByte(UARTCH1), UARTCH1);

                        mPORTBToggleBits ( BIT_15); 
                }
            }
            /*---------------------------------------------------------------------------*/

            int main (void){

                    SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

                    ConfUART(UARTCH1); 

                    mJTAGPortEnable(DEBUG_JTAGPORT_OFF);
                    mPORTBClearBits( BIT_15 | BIT_14 | BIT_13 | BIT_12 | BIT_11 | BIT_10 | BIT_9 | BIT_8 | BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | BIT_3 | BIT_2 | BIT_1 | BIT_0 );
                    mPORTBSetPinsDigitalOut( BIT_15 | BIT_14 | BIT_13 | BIT_12 | BIT_11 | BIT_10 | BIT_9 | BIT_8 | BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | BIT_3 | BIT_2 | BIT_1 | BIT_0 );

                    WriteToUART("Test1_Test2_Test3_Test4_Test5\n", UARTCH1);

                return 0;

            }

UART.c

            #include <plib.h>

            #define GetSystemClock()        (80000000ul)
            #define GetPeripheralClock()        (GetSystemClock()/(1 << OSCCONbits.PBDIV))
            #define T_RATE_X                    (9600) 

            void WriteToUART(const char *string, char UARTCH){ 

                while(*string != '\0')
                {
                    while(!UARTTransmitterIsReady(UARTCH));
                    UARTSendDataByte(UARTCH, *string);
                    string++;
                    while(!UARTTransmissionHasCompleted(UARTCH));
                }
            }

            void PutCharacter(const char character, char UARTCH){

                while(!UARTTransmitterIsReady(UARTCH));

                    UARTSendDataByte(UARTCH, character);

                    while(!UARTTransmissionHasCompleted(UARTCH));
            }

            void ConfUART(char UARTCH){

                UARTConfigure           (UARTCH, UART_ENABLE_PINS_TX_RX_ONLY);
                UARTSetFifoMode     (UARTCH, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
                UARTSetLineControl  (UARTCH, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); /* 8 Bits, Pas de parité, 1 STOP Bits */
                UARTSetDataRate     (UARTCH, GetPeripheralClock(), T_RATE_X);
                UARTEnable              (UARTCH, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

                INTEnable                           (INT_SOURCE_UART_RX(UARTCH), INT_ENABLED);
                INTSetVectorPriority            (INT_VECTOR_UART(UARTCH), INT_PRIORITY_LEVEL_2);
                INTSetVectorSubPriority     (INT_VECTOR_UART(UARTCH), INT_SUB_PRIORITY_LEVEL_0);


                INTConfigureSystem      (INT_SYSTEM_CONFIG_MULT_VECTOR);


                INTEnableInterrupts();

            }
share|improve this question

2 Answers 2

Your UART write function is built to stop transmitting when it finds NULL character in your string ('\0' or 0x00). You don't null-terminate your strings. It doesn't know when to stop sending, so it keeps going and sends everything it finds until it finds a NULL character.

To fix this, whenever you send a string you have to add '\0' to the end of it.

EDIT: Okay, the PutChar function fixes that - do you need to clear the interrupt manually? It looks like there's two sub-sources that are OR'd together to cause the main interrupt - you clear them but do you need to clear the main interrupt as well? That could cause this issue. Have you probed your UART Tx line and checked the baud rate that way? That could be a problem - if you're using a low-accuracy clock source you may not be spot on the baud rate.

Also edit: do you see your port toggle each time it sends the extra bytes?

share|improve this answer
    
I tried this, I removed the function writetouart (just used one time and not really necessary) but the problem persists: when I boot the MCU with the code, I send the string azerty, the MCU captures and returns them correctly, then at the eighth input it returns the character and 3 others (which are the same). I notice that the unwanted returned characters are those previously inputed (from the first). For example : I send azerty (ok), azertya (ok), i send azertyaz it returns azertyazaaa. –  user9451 Apr 26 '12 at 14:44
    
Regardless, your code as posted will not work for that reason. If you want me to debug the other code, post it. –  AngryEE Apr 26 '12 at 14:46
    
Update your question with that! That code in a comment is just a mess! –  AngryEE Apr 26 '12 at 18:10
    
The code posted in the comment,is already available in the file UART.c. I think it's the function PutCharacter which is not correctly designed. –  user9451 Apr 27 '12 at 7:01

Correct me if I'm wrong, but it looks like you are in the UART5 interrupt, but are clearing the UART1 RX and TX flags.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.