0
votes

I am trying to use UART1 on PIC32 to transmit and receive data but there is nothing coming out from that port. I am using the PIC32 Ethernet kit with 8Mhz crystal. For the programming in C I am using MPLAB IDE v2.15 and XC32 v 1.32 I don't have scope available right now so I was trying to test with multimeter which is very limited. When I connect the meter to UART1 TX pin and can see that once the initU1 routine is executed the pin goes from 0V to 3.3V which seems to be working but when the bits are supposed to be transmitted I don't see anything on the LCD (LK204-25), I also had the multimeter connected and don't see any change in voltage, it also does not show any frequency. The baud rate is set to 9600. The code I am using is shown below, please let me know what should be changed to get it to work.


/* 
 * File:   main.c
 * Author: Acer
 *
 * Created on August 27, 2014, 11:57 PM
 */

#include <stdio.h>
#include <stdlib.h>
//#include <p32xxxx.h>
#include <proc/p32mx795f512l.h>
#include <plib.h>

/*
 * 
 */

#define CTS1 _RD14
#define RTS1 _RD15
#define TRTS1 TRISDbits.TRISD15

#define BRATE1 368 //2083 //9600 @80MHz, BREGH=1
#define U_ENABLE1 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX1 0x0400 //Enable TX, Clear all flags

#define CTS2 _RF12
#define RTS2 _RF13
#define TRTS2 TRISFbits.TRISF13

#define BRATE2 2082 //9600 @80MHz, BREGH=1
#define U_ENABLE2 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX2 0x0400 //Enable TX, Clear all flags

char Port1Buffer[100], Port2Buffer[100];
int i1=0, i2=0;
int nextRun1=0, nextRun2=0;

void __attribute__ ((interrupt(ipl1),vector(27)))
U1RXInterruptHandler(void)//UART 1 RX interrupt routine
{
    if(mU1RXGetIntFlag())
    {
        // Clear the RX interrupt Flag
        mU1RXClearIntFlag();
        // Echo what we just received
        //putcUART1(ReadUART1());
            Port1Buffer[i1]=ReadUART1();
            i1++;
            if(i1==100){
                i1=0;
                nextRun1=1;
            }
        // Toggle LED to indicate UART activity
            mPORTDToggleBits(BIT_0);
    }
    // We don't care about TX interrupt
    if ( mU1TXGetIntFlag() )
    {
        mU1TXClearIntFlag();
    }

}

void __attribute__ ((interrupt(ipl2),vector(41)))
U2RXInterruptHandler(void)//UART 2 RX interrupt routine
{
    if(mU2RXGetIntFlag())
    {
        // Clear the RX interrupt Flag
        mU2RXClearIntFlag();
        // Echo what we just received
        //putcUART2(ReadUART2());
            Port2Buffer[i2]=ReadUART2();
            i2++;
            if(i2==100){
                i2=0;
                nextRun2=1;
            }

        // Toggle LED to indicate UART activity
            mPORTDToggleBits(BIT_0);
    }
    // We don't care about TX interrupt
    if ( mU2TXGetIntFlag() )
    {
        mU2TXClearIntFlag();
    }

}

void initU1(void)
{
    U1BRG = BRATE1; //Initialize baud rate generator
    U1MODE = U_ENABLE1; //Initialize UART module
    U1STA = U_TX1; //Enable Transmitter
    TRTS1 = 0; //Make RTS an output pin
    RTS1 = 1; //Set RTS default status (not ready)
    //ConfigIntUART1(UART_INT_PR1|UART_RX_INT_EN);
}

void initU2(void)
{
    U2BRG = BRATE2; //Initialize baud rate generator
    U2MODE = U_ENABLE2; //Initialize UART module
    U2STA = U_TX2; //Enable Transmitter
    TRTS2 = 0; //Make RTS an output pin
    RTS2 = 1; //Set RTS default status (not ready)
    ConfigIntUART2(UART_INT_PR2|UART_RX_INT_EN);
}

void putU1(char c)
{
//    while(CTS1);
    while(U1STAbits.UTXBF);
    U1TXREG = c;
}

void putU2(char c)
{
    while(CTS2);
    while(U2STAbits.UTXBF);
    U2TXREG = c;
}

void DelayRoutine(int delint){
    int delcount=0;
    while(delcount < delint){
        delcount++;
    }
}
void main(void) {
    DelayRoutine(0xFFFF);
//    mU1SetIntPriority(1);
//    mU2SetIntPriority(2);
    INTEnableSystemMultiVectoredInt();
    //mU1IntEnable( 1);
    //mU2IntEnable( 1);
    int y1=0, y2=0;
    initU1();
    initU2();
//    putU1(0x41);
    putU1(0xFE);
    putU1(0x58);
    while(1)
    {
        if(y1<i1 || nextRun1==1){
            putU1(Port1Buffer[y1]);
            y1++;
            if(y1==100){
                y1=0;
                nextRun1=0;
            }
        }
/*        if(y2<i2 || nextRun2==1){
            putU1(Port2Buffer[y2]);
            y2++;
            if(y2==100){
                y2=0;
                nextRun2=0;
            }
        }
*/    }
    //U2CTS=1;
    //return (EXIT_SUCCESS);
}
1

1 Answers

0
votes
void init_serial_port( U32 ulWantedBaud )
{
    unsigned short usBRG;
    /* Configure the UART and interrupts. */
    usBRG = (unsigned short)(( (float)40000000/ ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
    OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
    ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_RX_INT_EN );
    U2STAbits.UTXISEL = 0x00;
}

Just replace my "2" with a 1 and that is my exact init sequence for my UART on a PIC32MX745F512L ulWantedBaud is my desired baud rate (usually 115200, but sometimes 9600 depending on my settings)

I can't post my interrupt handler b/c it is way too involved, but use the serial port API's they are much less complex. Also, I would suggest a simple Logic Analyzer like this one (Used on the TTL side of course).

Side Note: If you use the API's they take care of the TRIS and LAT for you.

Edit: Adding Pragmas

#pragma config DEBUG    = OFF           // Background Debugger disabled
#pragma config FPLLMUL = MUL_20         // PLL Multiplier: Multiply by 20
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider:  Divide by 2
#pragma config FPLLODIV = DIV_1         // PLL Output Divider: Divide by 1
#pragma config FWDTEN = OFF             // WD timer: OFF
#pragma config POSCMOD = HS             // Primary Oscillator Mode: High Speed xtal
#pragma config FNOSC = PRIPLL           // Oscillator Selection: Primary oscillator  w/ PLL
#pragma config FPBDIV = DIV_1           // Peripheral Bus Clock: Divide by 1 //Note: Application    FPBDIV = DIV_2
#pragma config BWP = OFF                // Boot write protect: OFF
#pragma config ICESEL = ICS_PGx2        // ICE pins configured on PGx2, Boot write protect OFF.
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config CP = OFF
#pragma config PWP = OFF
#pragma config UPLLEN = ON
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
#pragma config FSRSSEL = PRIORITY_7
#pragma config FMIIEN = OFF
#pragma config FVBUSONIO = OFF          // VBUSON is controlled by the port
#pragma config FETHIO = ON            // external PHY in RMII/alternate configuration
#pragma config FUSBIDIO = OFF           // USB ID controlled by PORT function