pic32mz EF UART

0

Can anyone give me the configuration and code for sending a character in UART?

I am using Pic32mz EF starter kit and MPLAB X-IDE v5.15.

I tried a code but it is not giving out any UART in tera term or putty.

// PIC32MZ2048EFM144 Configuration Bit Settings

// 'C' source line config statements

// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FMIIEN = OFF // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config PGL1WAY = OFF // Permission Group Lock One Way Configuration (Allow multiple reconfigurations)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = OFF // USB USBID Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_3 // System PLL Input Divider (3x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ // System PLL Input Range (5-10 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (POSC is input to the System PLL)
#pragma config FPLLMULT = MUL_100 // System PLL Multiplier (PLL Multiply by 100)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (2x Divider)
#pragma config UPLLFSEL = FREQ_12MHZ // USB PLL Input Frequency Selection (USB PLL input is 12 MHz)

// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (System PLL)
#pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disable SOSC)
#pragma config IESO = OFF // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disabled, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
#pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
#pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
#pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is disabled)

// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config TRCEN = OFF // Trace Enable (Trace features in the CPU are disabled)
#pragma config BOOTISA = MIPS32 // Boot ISA Selection (Boot code and Exception code is MIPS32)
#pragma config FECCCON = OFF_UNLOCKED // Dynamic Flash ECC Configuration (ECC and Dynamic ECC are disabled (ECCCON bits are writable))
#pragma config FSLEEP = OFF // Flash Sleep Mode (Flash is powered down when the device is in Sleep mode)
#pragma config DBGPER = PG_ALL // Debug Mode CPU Access Permission (Allow CPU access to all permission regions)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable bit (MCLR pin generates a normal system Reset)
#pragma config SOSCGAIN = GAIN_2X // Secondary Oscillator Gain Control bits (2x gain setting)
#pragma config SOSCBOOST = ON // Secondary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config POSCGAIN = GAIN_2X // Primary Oscillator Gain Control bits (2x gain setting)
#pragma config POSCBOOST = ON // Primary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config EJTAGBEN = NORMAL // EJTAG Boot (Normal EJTAG functionality)

// DEVCP0
#pragma config CP = OFF // Code Protect (Protection Disabled)

// SEQ3
#pragma config TSEQ = 0xFFFF 
#pragma config CSEQ = 0xFFFF 

// DEVADC0
#pragma config ADCFG = 0xFFFFFFFF 



#include <xc.h>
#include <proc/p32mz2048efm144.h>



void main()
{
U2MODEbits.STSEL = 0;
U2MODEbits.PDSEL = 0b00;
IEC4bits.U2TXIE = 1;
IPC36bits.U2TXIP = 0b111;
IPC36bits.U2TXIS = 0b11;
U2STAbits.UTXISEL = 0b10;
U2STAbits.UTXEN = 1;
U2MODEbits.UEN = 0b00;
U2MODEbits.ON = 1;
U2MODEbits.BRGH = 0;
U2BRG = 129;
int i;
for(i=0;i<100;i++)
{
U2STAbits.UTXEN = 1; // Make sure transmitter is enabled
while(U2STAbits.UTXBF); // Wait while buffer is full
U2TXREG =(char) i;
}
}

I think there is a problem with my configuration bits and Baud rate. Can you please help me in setting the correct baud rate and configuration bits?

Thanks & Regards,

Vibhu.

frequency
clock
uart
baud-rate
pic32
asked on Stack Overflow Feb 27, 2019 by Vibhu • edited Feb 27, 2019 by Vibhu

2 Answers

0

I'm having a problem with a project of mine that I haven't figured out yet (I get garbled data), but here's what I do see in yours. You will need to set the TRISx bits for the TX and RX pins to OUT and IN, plus you'll need to map those UART functions to the physical pins through U2RXR and RPDx registers. You also need to clear the bits for those two pins to digital in the appropriate ANSELx register(s).

answered on Stack Overflow Mar 11, 2019 by Hal
0

You can calculate the baud rate by using this formula:

U2BRG = (unsigned short int)(((float)PERIPHERAL_CLOCK / ((float)BAUD_RATE * (float)BAUD_CLOCK)) - (float)0.5);

If U2MODEbits.BRGH is set to High Baud Rate, then your BAUD_CLOCK should be 4, otherwise 16.

And for writing a char by polling the TRMT flag:

while(!U2STAbits.TRMT) {}
U2TXREG = (char) i;

You should set the following bit only once, as this is an initialization process, no need to do it every time you're about to send a char.

U2STAbits.UTXEN = 1; // Make sure transmitter is enabled

And you also don't have a main while() loop, and your program is probably crashing every time the cpu finishes with executing the for loop.

answered on Stack Overflow Apr 2, 2019 by Emiliyan Nikolov • edited Apr 3, 2019 by Emiliyan Nikolov

User contributions licensed under CC BY-SA 3.0