In this step-by-step example, an LED will be toggled every 500ms and an analog value will be sent over serial UART and read using MPLAB® Data Visualizer. Both demos were featured in the Future Electronics webinar titled “Reduce Your Time to Market with the MPLAB® Ecosystem of Software and Hardware Tools” from November 3rd, 2021 which can be found here.
-
PIC18F16Q41 Product Page found here.
-
PIC18F06/16Q41 DataSheet found here.
-
PIC18F16Q41 Hardware User Guide found here.
-
Webinar video link from Future Electronics can be found here.
- MPLAB X IDE 6.20.0 or newer (MPLAB® X IDE 6.20.0)
- MPLAB XC8 3.0.0 or newer compiler (MPLAB® XC8 3.0.0)
- MPLAB Code Configurator (MPLAB® MCC)
- MPLAB Data Visualizer (MPLAB® Data Visualizer)
- PIC18F16Q41 Curiosity Nano Development Board (EV26Q64A)
- Breadboard
- Wire
- 1x, 10k Ohm potentiometer
- On the tool bar, click on New Project
- Microchip Embedded; Standalone Project
- Enter the Device
- For this Project: pic18f16q41
- Enter a name for this project, such as “LED_Toggle”
- Name: “LED_Toggle”
- Note: The project name cannot have any empty spaces
- Open MPLAB Code Configurator by clicking the blue “MCC” shield in the top toolbar
- When MCC opens, select “MCC Melody” and click “Finish”
- Modify the Clock Control under “Project Resources” in the top left panel
- Set “Clock Source” to High Frequency Internal Oscillator (HFINTOSC)
- Set “HF Internal Clock” to 4_MHz
- Set “Clock Divider” to 4
- Set Configuration Bits under “Project Resources” in the top left panel
- Set “External Oscillator Mode Selection” to “Oscillator not enabled”
- Set "Power-up Default Value for COSC" to "HFINTOSC with HFFRQ= 4MHz and CDIV= 4:1"
- LED0 is connected to pin RC1
- Set this pin as a digital GPIO output
- Assign pin RC1 a custom name of “LED” and ensure all other configurations are as seen below
- Click the generate button in MCC to create the appropriate header and source files for this configuration
- Close MCC by clicking the blue “MCC” shield again
- Upon the generation being completed, the new MCC generated header and source files will be in the project window. Select the main.c file found within your source files and you will see an empty while(1) loop where you can add your application code
- Follow this path under “Projects”
- LED_Toggle -> Header Files -> MCC Generated Files -> System -> pins.h
- Open “pins.h” and scroll down to find the defined function “LED_Toggle()”
- Copy and paste this function into your main.c,
while(1)
loop - Below this function, add a delay of 500 milliseconds using
__delay_ms(500);
- Copy and paste this function into your main.c,
int main(void)
{
SYSTEM_Initialize();
while(1)
{
LED_Toggle();
__delay_ms(500);
}
}
- Make and program the device to observe the toggling LED
- Result:
- Follow the image below to set up your bread Breadboard
- VTG from Curiosity Nano to positive rail
- GND from Curiosity Nano to negative rail
- Jumper wire from positive rail to Vcc pin on potentiometer
- Jumper wire from negative rail to GND pin on potentiometer
- Jumper wire from pin RA5 on Curiosity Nano to Output pin on potentiometer
- On the tool bar, click on New Project
- Microchip Embedded; Standalone Project
- Enter the Device
- For this Project: PIC18F16Q41
- Enter a name for this project, such as “Analog_Serial_Read”
- Name: “Analog_Serial_Read”
- Note: The project name cannot have any empty spaces
- Name: “Analog_Serial_Read”
- Open MPLAB Code Configurator by clicking the blue “MCC” shield in the top toolbar
- When MCC opens, select “MCC Melody” and click “Finish” on the next page
- Modify the Clock Control under “Project Resources” in the top left panel
- Set “Clock Source” to High Frequency Internal Oscillator (HFINTOSC)
- Set “HF Internal Clock” to 4_MHz
- Set “Clock Divider” to 1
- Set Configuration Bits under “Project Resources” in the top left panel
- Set “External Oscillator Mode Selection” to “Oscillator not enabled”
- Set "Power-up Default Value for COSC" to "HFINTOSC with HFFRQ= 4MHz and CDIV= 4:1"
- In Device Resources:
- Drivers → ADCC → ADCC
- Drivers → UART → UART1
- ADCC
- Hardware Settings to change while the rest can be left as default
- Operating Mode = Basic Mode
- Result Alignment = right
- Positive Reference = VDD
- Negative Reference = VSS
- Acquisition Count = 2
- Hardware Settings to change while the rest can be left as default
- UART1
- Software Settings:
- Enable "Redirect STDIO to UART"
- Hardware Settings:
- Enable UART box should be checked
- Set the Baud Rate to 19200
- Enable Transmit and Receive should be checked
- Everything else can be left as default settings
- Software Settings:
- UART1 TX1 is connected to pin RB7
- UART1 RX1 is connected to pin RB5
- ADCC “ANx” is connected to pin RA5
- This is the pin on the Curiosity Nano that the potentiometer is connected to
- Assign pin RA5 a custom name of “POT” and ensure all other configurations are as seen below
- Click the generate button in MCC to create the appropriate header and source files for this configuration
-
Upon the generation being completed, the new MCC generated header and source files will be in the project window. Select the main.c file found within your source files and you will see an empty
while(1)
loop where you can add your application code -
Follow this path under “Projects”
- Analog_Serial_Read → Header Files → MCC Generated Files → ADC → adcc.h
-
Open “adcc.h” and scroll down to find the defined function
adc_result_t ADCC_GetSingleConversion(adcc_channel_t channel);
- Copy and paste this function into your main.c,
while(1)
loop.
- Copy and paste this function into your main.c,
-
Pass your custom pin name through the function within a
printf()
statement to read the current value from the potentiometerint main(void) { SYSTEM_Initialize(); while(1) { printf("%d\r\n",ADCC_GetSingleConversion(POT)); } }
-
Make and program the device
- For this project, the terminal program that is being used is MPLAB Data Visualizer
- Open Data Visualizer by clicking the green “DV” shield in the top toolbar
- Click on your Curiosity Nano COM port and set:
- Baud Rate to: 19200
- Char Length: 8 bits
- Parity: None
- Stop Bits: 1 bit
- Click “Apply” to save these settings
- On the right side of the terminal window, ensure “Display As: 8-bit ASCII” is selected
- Press the drop-down carrot next to your Curiosity Nano’s COM port and select “Send to Terminal”
- While twisting the potentiometer’s dial, you will be able to see the changing values within the Data Visualizer terminal window
This application demonstrates how to toggle a LED on and off at a rate of 500 milliseconds as well as how to configure an analog-serial-read using a potentiometer.