UART and Peripheral Pin Select (PPS) on PIC32MZ
Code for this post
- PIC32MZ_4.c 9 KB On GitHub
How to use the PPS functionality and how to set up the UART
It's been a while, so time to blog about something worthwhile! A lot of the cool peripherals (devices) on the PIC32MZ aren't assigned to specific pins, you can choose between a set of pins on which to place them. For today's example, I will be looking at how to set up the PPS pins to give me UART port 5 on pins RF0 and RF1. There are two pins that we want, the receive pin (RX) and the transmit pin (TX). So let's go look at the datasheet and see what it says.

These are some of the input pins we can set up. We are looking for U5RX, the RX pin for UART number 5. As you can see, it's on the left. The pins on the right are the possible pin numbers we can place that pin on. I want to place it on pin RF1, which is also called RPF1. Looking on the right, I can see that the code to do this is 0100. So now, we go to the code and set U5RX to 0b0100, like this:
U5RXR = 0b0100; // RF1 = U5RX
Simple, if you know how. But wait, there's more! Because this is now an input pin, we need to set up the TRISF register to make that pin an input too.
TRISFbits.TRISF1 = 1; // Make RF1 an input
OK, now let's set up the TX pin. Let's look at the datasheet again.

For the output pins, the peripherals are listed on the right and the possible ports on the left. We can see on the right that U5TX has a code of 0011, and as we want it on pin RF0 we set it up as follows:
RPF0R = 0b0011; // RF0 = U5TX
OK, now we've assigned the UART 5 peripheral to pins RF0 and RF1, it's time to initialise it. In this example, I want to set it up at a baud rate of 38400bps so I can communicate with my FTDI cable connected at those pins. I like doing this for debug logs and just communicating with the PC in general. Unfortunately, when we look at the datasheet and the UART section we get to read this:
"This data sheet summarizes the features of the PIC32MZ EF family of devices. It is not intended to be a comprehensive reference source. To complement the information in this data sheet, refer to Section 21. “Universal Asynchronous Receiver Transmitter (UART)”(DS60001107) in the “PIC32 Family Reference Manual”, which is available from the Microchip web site (www.microchip.com/PIC32)."
Wonderful, so we google DS60001107 and find the UART datasheet and down the rabbit hole we go. Nothing is quick and easy to do on the PIC32. Let's look at the speed calculation formula:

Very long story short, the UART peripherals are run off of Peripheral Bus Clock 2 (PBCLK2), which we set to 100Mhz in the previous blog post. There is a "standard speed" mode and a "high speed mode". The standard speed mode has a maximum speed of (PBCLK2 / 16) which is 6.25Mbps in our example. The high speed mode has a maximum speed or (PBCLK2 / 4), which is 25Mbps for us. As I'm using a much, much slower speed I'm going to be setting it up in standard mode. Here's the code to initialise UART 5 at 38400bps:
void UART_Init()
{
int pbClk;
pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz
U5MODE = 0; // Set UART 5 off prior to setting it up
U5MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U5MODE to 0 so this is just for explanation's sake
U5BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
U5STA = 0; // Disable the TX and RX pins, clear all flags
U5STAbits.UTXEN = 1; // Enable the TX pin
U5STAbits.URXEN = 1; // Enable the RX pin
U5MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
U5MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
U5MODEbits.ON = 1; // Turn on the UART 5 peripheral
}
Phew. That's actually the hardest part done. Now we can immediately start writing data to UART 5 by writing to U5TXREG, like this:
char c;
c = 'A';
U5TXREG = c;
But there's a much cooler and easier way to do this that allows us to use printf. We need to overwrite the _mon_putc function, which is thankfully really easy:
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete
U5TXREG = c;
}
And that's it! Now we can use printf to send data directly to the serial port UART 5. Remember, when connecting the FTDI cable you need to connect the FTDI's TX pin to the PIC32's RX pin and the FTDI's RX pin to the PIC32's TX pin. I've attached a very basic, but working, program that outputs a counter to the serial port every second.
Here's the code Have fun!
Work it out: uart baud rate
34 comments
Will Thompson
Thanks for the post Aidan, it has helped us tremendously.
In our setup we have connected an Arduino due to the pic32 starter kit in order to test the UART com in the Arduino serial monitor.
We are having issues however regarding the transmission of characters. We can see transmittion is occurring (our "if serial available" is responding) but don't receive the values themselves and not at the proper data rate we defined. Any ideas?
Thanks
Aidan Author
Hi Will, thanks for visiting.
It sounds like the baud rate isn't being set properly on the PIC32 side which can be related to your clock being set incorrectly, the specific peripheral bus clock being set incorrectly or even the UART baud rate being set incorrectly. The PIC32MZ isn't very welcoming :) I'll need a bit more information to be able to try and help you:
1) Are you using a PIC32MZ or another PIC32 device? (There are many kinds of starter boards but my code should work on all of them).
2) May I see your config settings/fuses (the "pragma config" stuff) and your UART init code?
3) Are you using my set_performance_mode() code to set up the peripheral buses?
Will Thompson
Thanks for the quick reply and your help!
1. We are using the pic32mz EF starterkit (Microchip PN Dm320007).
2. We didnt change anything on the pragma. Set performance mode is the same. Delay functions are the same. Basically everything except for the following! The only thing we changed was assigning UART6 instead of UART5.
// Initialise peripheral UART 6
void UART_Init()
{
int pbClk;
pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz
U6MODE = 0; // Set UART 6 off prior to setting it up
U6MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U6MODE to 0 so this is just for explanation's sake
U6BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
U6STA = 0; // Disable the TX and RX pins, clear all flags
U6STAbits.UTXEN = 1; // Enable the TX pin
U6STAbits.URXEN = 1; // Enable the RX pin
U6MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
U6MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
U6MODEbits.ON = 1; // Turn on the UART 6 peripheral
}
// Overwrite _mon_putc() to enable us to use printf
void _mon_putc (char c)
{
while (U6STAbits.UTXBF); // Wait til current transmission is complete
U6TXREG = c;
}
void main()
{
int cnt;
// Set up all ports to be digital and not analog
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
ANSELF = 0;
ANSELG = 0;
ANSELH = 0;
ANSELJ = 0;
// Set up PPS for UART 6
U6RXR = 0b0010; // RPB14 = U6RX
RPG6 = 0b0100; // RPG6 = U6TX
TRISFbits.TRISF1 = 1; // Make RF1 an input
// Set max performance mode
set_performance_mode();
// Initialise peripheral UART 6
UART_Init();
cnt = 0;
// Main loop
while (1)
{
printf("Counter value is %d\n", cnt);
delay_ms(1000); // Delay 1 second
cnt++;
}
}
3. Set performance mode same as above.
Thanks so much again
Aidan Author
I see a potential big problem. You haven't setup RB14 as an input and it has to be in order to receive data, likewise with setting up RG6 as an output. Change this line:
TRISFbits.TRISF1 = 1; // Make RF1 an input
to this:
TRISBbits.TRISB14 = 1; // Make RB14 (U6RX) an input
Also, add this line:
TRISGbits.TRISG6 = 0; // Make RG6 (U6TX) an ouput
to set that pin as an output.
Hopefully that fixes it!
Will Thompson
You sir deserve a nobel prize! Fixed! Thanks
Quick question, what does the B and G signify in the TRIS lines above? How would we know when to put this designation? Thanks again
Aidan Author
Excellent news, glad it works. TRIS is, basically, how we set whether a pin is an input (1) or an output (0) on PIC. On
On Arduino, to make a pin an output you'd go:
pinMode(B14, INPUT)
and on PIC we go:
TRISB14 = 1
Similarly, to make a pin an output on Arduino would be:
pinMode(G6, OUTPUT)
and PIC is:
TRISG6 = 0
It's a little bit more complex than that but that's the gist of it.
Will Thompson
Thanks, makes sense.
This might be a big ask, but I was wondering if you would have any reference or resources with respect to reading in a simple analog input. We are struggling!
Aidan Author
Oof the ADC is not a terribly easy to use thing on the PIC32MZ. It's powerful but it's a pain and one of the more difficult things of the PIC32MZ.
Unfortunately I don't have any code right now, the best I can do is make some in a day or two and post it here.
If it's urgent then google for it but remember to search for PIC32MZ not PIC32MX.
Will Thompson
You truly are amazing. Any help would be awesome. We are working now and trying to make sense of all these registers and config settings, there seems to be hundreds :(
We are trying to follow the code on pg 66 of the datasheet, just to initialize the thing but are not making much progress.
In case its helpful we are using pin AN3
Aidan Author
Hi Will,
The only really bad part of using the PIC32MZ ADC is setting it up but once that's done it's OK.
Here is some code that I've tested and seems to be working fine with AN3.
If you are a bit lost, continue using AN3 as I have in the code and you should be fine :) Good luck!
Blom
I do not exactly understand what the function "set_performance_mode()" exactly does and means..
Can you explain that to me plz? =)
Thanks in advance, and b.t.w. very nice explaining and code.
I think this is possible to connect my PIC32MZ2048EFH064 to a "ADM00393 - Evaluation Module, For MCP2200, USB-To-UART," so I can send serial to my terminal on my laptop right?
Aidan Author
Hi Blom, and welcome. You can find a fairly detailed explanation of that function here.
Yes, that code should work with any USB UART bridge IC. Good luck!
Blom
Thanks for the fast reply.
I can see a virtual com-port with the module, but my Program freeze or gets stuck because of the "printf" function. The printf is in the main loop function.
by the way I have a 8MHz external oscillator, so I think I also need to set some different in the pragma.
My SPI is also not working yet, don't know why..
In the main loop I call the UART and SPI init.
somehow the whole program freezes/gets stuck at the line printf("HELLO.\n");
Blom
Hello Aidan,
Do you maybe know if I made a mistake somewhere in coding?
Or maybe the external oscillator should be 24MHz like yours instead off 8MHz
Thank you for your time.
Cheers,
Marc
Blom
I think I know why the program freezes at the prinf line.
This is what I read on the microchip page:
"If using the USB peripheral, POSC must be 12MHz or 24MHz. You must also configure the USB PLL for a 12MHz or 24MHz input frequency."
So I think I need to replace my 8MHz oscillator for a 24MHz oscillator.
Blom
So I replaced my external clock with a 12MHz, but still the program freezes at printf..
Don't know what I am doing wrong..
Aidan Author
Hi Blom,
Sorry for the late reply I'm on holiday. Will check out your code in a bit and see if I can see anything.
Blom
Hi Aidan,
No problem at all, I have just a situation/problem what I can't fix at my own atm.
Would appreciate it really much If you can check my code out.
Below is my code version programmed for the PIC32MZ2048EFH064 with the 12MHz oscillator.
Here is also a link to my code, so you can see it more clear.
https://www.microchip.com/forums/m1086039.aspx
// PIC32MZ2048EFH064 Configuration Bit Settings
// 'C' source line config statements
// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config PGL1WAY = OFF // Permission Group Lock One Way Configuration (Allow only one reconfiguration)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF // USB USBID Selection (Controlled by the USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // System PLL Input Divider (8x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ// System PLL Input Range (34-68 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (FRC is input to the System PLL)
#pragma config FPLLMULT = MUL_66 // System PLL Multiplier (PLL Multiply by 128)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (32x Divider)
#pragma config UPLLFSEL = FREQ_12MHZ // USB PLL Input Frequency Selection (USB PLL input is 24 MHz)
// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (Fast RC Osc w/Div-by-N (FRCDIV))
#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 (Enable SOSC)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = EC // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enabled, FSCM Enabled)
#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 Enabled)
#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 enabled)
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config TRCEN = OFF // Trace Enable (Trace features in the CPU are enabled)
#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 = OFF // 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 = OFF // 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 // Boot Flash True Sequence Number (Enter Hexadecimal value)
#pragma config CSEQ = 0xFFFF // Boot Flash Complement Sequence Number (Enter Hexadecimal value)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
/*
* File: main.c
* Author:
*
* Created on February 1, 2019, 12:36 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <xc.h>
#include <proc p32mz2048efh064.h="">
#define SYS_FREQ 198000000
//Variables
int heartbeat=0;
void set_performance_mode()
{
unsigned int cp0;
// Unlock Sequence
asm volatile("di"); // Disable all interrupts
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
// PB1DIV
// Peripheral Bus 1 cannot be turned off, so there's no need to turn it on
PB1DIVbits.PBDIV = 1; // Peripheral Bus 1 Clock Divisor Control (PBCLK1 is SYSCLK divided by 2)
// PB2DIV
PB2DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
PB2DIVbits.PBDIV = 1; // Peripheral Bus 2 Clock Divisor Control (PBCLK2 is SYSCLK divided by 2)
// PB3DIV
PB3DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled)
PB3DIVbits.PBDIV = 1; // Peripheral Bus 3 Clock Divisor Control (PBCLK3 is SYSCLK divided by 2)
// PB4DIV
PB4DIVbits.ON = 1; // Peripheral Bus 4 Output Clock Enable (Output clock is enabled)
while (!PB4DIVbits.PBDIVRDY); // Wait until it is ready to write to
PB4DIVbits.PBDIV = 0; // Peripheral Bus 4 Clock Divisor Control (PBCLK4 is SYSCLK divided by 1)
// PB5DIV
PB5DIVbits.ON = 1; // Peripheral Bus 5 Output Clock Enable (Output clock is enabled)
PB5DIVbits.PBDIV = 1; // Peripheral Bus 5 Clock Divisor Control (PBCLK5 is SYSCLK divided by 2)
// PB7DIV
PB7DIVbits.ON = 1; // Peripheral Bus 7 Output Clock Enable (Output clock is enabled)
PB7DIVbits.PBDIV = 0; // Peripheral Bus 7 Clock Divisor Control (PBCLK7 is SYSCLK divided by 1)
// PB8DIV
PB8DIVbits.ON = 1; // Peripheral Bus 8 Output Clock Enable (Output clock is enabled)
PB8DIVbits.PBDIV = 1; // Peripheral Bus 8 Clock Divisor Control (PBCLK8 is SYSCLK divided by 2)
// PRECON - Set up prefetch
PRECONbits.PFMSECEN = 0; // Flash SEC Interrupt Enable (Do not generate an interrupt when the PFMSEC bit is set)
PRECONbits.PREFEN = 0b11; // Predictive Prefetch Enable (Enable predictive prefetch for any address)
PRECONbits.PFMWS = 0b010; // PFM Access Time Defined in Terms of SYSCLK Wait States (Two wait states)
// Set up caching
cp0 = _mfc0(16, 0);
cp0 &= ~0x07;
cp0 |= 0b011; // K0 = Cacheable, non-coherent, write-back, write allocate
_mtc0(16, 0, cp0);
// Lock Sequence
SYSKEY = 0x33333333;
asm volatile("ei"); // Enable all interrupts
}
void delay_us(unsigned int us)
{
// Convert microseconds us into how many clock ticks it will take
us *= SYS_FREQ / 1000000 / 2; // Core Timer updates every 2 ticks
_CP0_SET_COUNT(0); // Set Core Timer count to 0
while (us > _CP0_GET_COUNT()); // Wait until Core Timer count reaches the number we calculated earlier
}
void delay_ms(int ms)
{
delay_us(ms * 1000);
}
void UART_Init(){
int pbClk;
pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz
//100
U5MODE = 0; // Set UART 5 off prior to setting it up
U5MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U5MODE to 0 so this is just for explanation's sake
U5BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
U5STA = 0; // Disable the TX and RX pins, clear all flags
U5STAbits.UTXEN = 1; // Enable the TX pin
U5STAbits.URXEN = 1; // Enable the RX pin
U5MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
U5MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
U5MODEbits.ON = 1; // Turn on the UART 5 peripheral
}
void _mon_putc (char c)
{
while (U5STAbits.UTXBF); // Wait til current transmission is complete
U5TXREG = c;
}
void SPI_init()
{
SPI2CONbits.ON = 0; // Turn off SPI2 before configuring
SPI2CONbits.MSTEN = 1; // Enable Master mode
SPI2CONbits.CKP = 1; // Clock signal is active low, idle state is high
SPI2CONbits.CKE = 0; // Data is shifted out/in on transition from idle (high) state to active (low) state
SPI2CONbits.SMP = 1; // Input data is sampled at the end of the clock signal
SPI2CONbits.MODE16 = 0; // Do not use 16-bit mode
SPI2CONbits.MODE32 = 0; // Do not use 32-bit mode (combines with the above line to activate 8-bit mode)
SPI2BRG = 0; // Set Baud Rate Generator to 0 [discussed below]
SPI2CONbits.ENHBUF = 0; // Disables Enhanced Buffer mode [also discussed below]
SPI2CONbits.ON = 1; // Configuration is done, turn on SPI2 peripheral
SPI_send(data2);
SPI2BUF= data2;
}
int SPI_send(int data)
{
data = 245;
SPI2BUF = data; // Send *data* to SPI2BUF.
while (SPI2STATbits.SPIRBE); // Which the receive buffer is empty, loop
return (int)SPI2BUF; // Return whatever is in SPI2BUF
}
void main(){
setup();
// Set up PPS for UART 5
U5RXR = 0b0100; // RPRF1 = U5RX
RPD3R = 0b0011; // RPD3 = U5TX
TRISFbits.TRISF1 = 1; // Make RF1 an input
TRISDbits.TRISD3 = 0; // Make RD3 an output
// Set max performance mode
// Set up PPS for SPI2 on RB3 and RB5
//SDI2R = 0b1000; // RB3 = SDI2
RPB5R = 0b0110; // RB5 = SDO2
//RB4=CS EN SCK2=RG6
//TRISBbits.TRISB3 = 1; // Set RB3 as input because it's SDI2
// Initialise peripheral UART 5
set_performance_mode();
UART_Init();
SPI_init();
// __XC_UART=5;
while(1){
heartbeat++;
if(heartbeat<20000){
PORTBbits.RB2=1;
PORTBbits.RB8=1;
// PORTGbits.RG8=1;
// heartbeat++;
// PORTGbits.RG9=1; //TEST
}
if(heartbeat>=20000 && heartbeat <40000){
PORTBbits.RB2=0;
PORTBbits.RB8=0;
// heartbeat++;
printf("HELLO.%d\n", heartbeat);
// delay_ms(3000);
}
if(heartbeat==40000){
heartbeat=0;
}
// setbuf(stdout, NULL);
}
}
setup(){
// Set up all ports to be digital and not analog
ANSELB = 0;
ANSELG = 0;
// Set outputs en inputs
TRISBbits.TRISB12=1;
TRISBbits.TRISB13=1;
TRISGbits.TRISG9=0;
TRISGbits.TRISG7=0;
TRISBbits.TRISB7=0;
TRISBbits.TRISB8=0;
TRISBbits.TRISB3=0;
TRISBbits.TRISB6=0;
// TRISBbits.TRISB7=;
// TRISBbits.TRISB8=;
TRISBbits.TRISB9=1;
TRISBbits.TRISB2=0;
// TRISFbits.TRISF1=0;
PORTBbits.RB3=1;
PORTBbits.RB7=1;
for(delay1=0; delay1<100000; delay1++){
//delay
}
PORTBbits.RB3=0;
PORTBbits.RB7=0;
PORTBbits.RB6=0;
PORTGbits.RG7=0;
return;
Hans Nelisse
This blog is a life-saver! Thanks!
Aidan Author
Hi Hans,
Thanks for dropping by and you're welcome.
Blom
Hi Aidan,
Do you also know how to read the serial bus?
Kind regards,
Marc
Aidan Author
Hi Marc,
I'm not sure what you're asking. Do you mean reading from the UART module?
You can check UxSTATbits.URXDA to see if there's data and then read it from UxRXREG.
For example, to read from UART2:
uint8_t data;
if (U2STAbits.URXDA)
data = U2RXREG;
Gaston Scazzuso
Mr. Aidan ,,, I´m starting with PIC32 and your blog is great !
Very helpfull your coments and as you said before, no arrogance like tons of stupids in Microchip forum :)
.. thank you very much !.. and anything i could help just let me know.
Gaston Scazzuso
motorsports electronics
MoTeC support
Aidan Author
Hi Gaston,
Sorry for the very, very late reply and thank you for stopping by and leaving a nice comment. I am glad you found the site useful :)
Gary Tennyson
Aidan,
Many thanks for the blog. Just wanted to let you know people are reading this. I'm just starting with blinking an LED on the PIC 32 MZ EF Starter Kit. Next step is the UART. So please don't pull the blog down anytime soon. Thanks again
Aidan Author
Hi Gary,
Thanks for stopping by and your kind comment. I may not be updating the site much any more but I have no plans to remove it.
yuming chen
Aidan
Thank for sharing, I have a question about UART, I Buy a DEV BOARD( Click 2 for PIC32MZ), and this board default Uart is RD14--RX RD15--TX. when I set U5RXR = 0b0100, It's working, but U5RXR = 0b1011 is not working, I dont know what happen, and I have test all Onboard U5RXR pin selection as follow
0000--ok
0001--fail
0010--ok
0011--fail
0100--ok
0101--fail
0110--fail
0111--NA
1000--NA
1001--NA
1010--fail
1011--fail
1100--ok
1101--ok
1110--NA
1111--NA
I buy two board have same question. Do you know any possible reason? Thanks
Aidan Author
Hi Yuming,
As you know U5RXR controls which pin UART5 will use for receiving data. When using a pin as an input you need to set the TRIS pin to 1 for it to work. So maybe try setting U5RXR = 0b0001 (for RG8) and also setting TRISGbits.TRISG8 = 1 and see if it works. If not maybe something else is using that pin too?
yuming chen
Hi Aidan
Thanks. I Have found the reason, RD14 is AN pin, and I didn't set ANSELDbits.ANSD14 = 0. , and other pin is same reason.
Aidan Author
Now that I did cover in my code!
I always set ALL the ANSEL bits to 0 by default precisely for this reason :) Glad you found the problem.
AlPl PaL
Hello, Aidan
in the PIC32 Family Reference Manual Section 12. I/O Ports Microchip writes following statements for PPS:
Register 12-2: [pin name]R: Peripheral Pin Select Input Register
Register 12-3: RPnR: Peripheral Pin Select Output Register
Note: Values in this register can only be changed if the IOLOCK Configuration bit (CFGCON<13>) = 0.
So according to this information unlocking and locking sequences would be needed for assigning peripherals to a pin (a familiar procedure also with PIC18 and dsPIC devices).
To give an example for UART2:
/* Unlock system for PPS configuration */
SYSKEY = 0x00000000U;
SYSKEY = 0xAA996655U;
SYSKEY = 0x556699AAU;
CFGCONbits.IOLOCK = 0U;
/* PPS Input Remapping */
U2RXR = 1; // U2RX to RPG6
/* PPS Output Remapping */
RPB14R = 2; // U2TX to RPB14
/* Lock back the system after PPS configuration */
CFGCONbits.IOLOCK = 1U;
SYSKEY = 0x00000000U;
In your example for the UART you did not include this. What is your opinion on this subject?
Aidan Author
Hi there,
It looks like you are absolutely right. And yet I've never had a problem with it. I thought setting IOL1WAY = 0 would be enough but not according to the documents.
Thanks for the information if I upload a new post I'll be sure to mention it and give you credit :)
AlPl PaL
Hi, I also experienced strange behaviour with PIC18F and dsPIC33 devices: sometimes PPS worked without the locking/unlocking sequences, sometimes it did not work. To not worry I always include them now.
Aidan Author
Yes I will be sure to mention that in the next post, I didn't know it was needed for the PPS bits. Thanks again :)
Comments are hosted by Disqus, which loads only when you ask for it.