Look Ma, no code! PWM control in under five minutes with Microchip Curiosity development board and MPLAB Code Configurator

Goal: The following describes how to build a simple PWM for motor control applications on the Microchip Curiosity Development board using the () and (MCC). We begin with basic bring up of Curiosity’s onboard PIC16F1619 running a “Hello, World!” program, and continue to set up a PWM using a Timer based on an FOSC/4 clock. All of this is done using the Microchip MCC without ever writing a single line of code.

Difficulty Level: Beginner

Hardware Required:

  • Microchip Curiosity Development Board (Outfitted with PIC16(L)F161X – Used here is the PIC16F1619)
  • USB 2.0 A-Male to Mini-B Cable

Software Required:

Recommended Platforms:

  • Windows (x86/x64)
  • 32-bit and Linux 64-bit (32-bit compatibility libraries required)
  • Mac (Versions 10.X)

Support/Documentation:

Cost Estimate:

  • Microchip Curiosity Development Board – $20 (Before tax, shipping & handling. Available from multiple distributors and www.microchipdirect.com.)
  • USB 2.0 A-Male to Mini-B Cable – $5 (Rough estimate. Available from multiple distributors.)

Powering the board and setting up a project

After downloading and installing the necessary software on your computer (installation instructions can be found at www.microchip.com/curiosity), plug the USB 2.0 Mini cable into the J2 connector located on the bottom of your Curiosity board. Doing so will illuminate the green LED D2 located near the center of the board to indicate that Curiosity is powered on, as well as LED D1 in the bottom left corner signifying that a +3.3V power supply is available on the board. If you haven’t already, move the shunt jumper to the right two pins of the J12 jumper to connect a +5V power supply (the left two pins connect +3.3V).

You’re now ready to program. Open the Microchip MPLAB X IDE from your applications folder, and click “New Project” in the upper left corner of the interface, or select it from the “File” dropdown menu. Doing so will open a “New Project” window, as shown below.

From the Microchip Embedded folder, double click “Standalone Project.” This will direct you to the next step in the project wizard, which prompts you to select a device. The Curiosity development board comes stock with the 8-bit PIC16F1619 , so select it by either typing the model number into the “Device” dropdown, or by selecting “Mid-Range 8-bit MCUs (PIC10/12/16/MCP) in the “Family” field and then opening the Device dropdown and scrolling until you find the PIC16F1619. Hit “Next.”

You’ll be prompted to select a debug header, but you can leave that with its default settings, so click Next again.

In the next step of the Wizard, you’ll be asked to select a tool, so navigate to “Curiosity – FN: XXXXXX” under the “Microchip Starter Kits” folder, and double click. This will generate a popup window that allows you to give the board a human-friendly name. For the purposes of this exercise, just call it “Curiosity.” Hit Next.

At this point we have to select a compiler. You should have already downloaded and installed Microchip’s XC8 compiler, either the free or the Pro version. The Pro version offers some enhanced optimization features, and if you’d like to try it out Microchip offers a free 60-day trial. But for the purposes of this project, the free version will work just fine. Select the XC8 compiler from the “Compiler Toolchains” folder. Hit Next.

Now you’re going to name your project. Call it CuriosityPWM. Leave all the default settings as they are, and click “Finish.” A project folder should now appear in a column on the left of the MPLAB X interface.

Before getting started on the actual application, you have to configure the project to support low-voltage programming, which allows you to program the MCU without a 12V power supply. To do this, simply right-click on your project, and select “Properties” from the subsequent dropdown. A popup window will appear that has a list of “Categories” on the left. Under the “Conf: [default]” parent, click “Starter Kit (PKOB).” The options on the right side of the window will change, revealing a dropdown next to the heading “Option categories:” Choose “Program Options” from that menu, and then check the box that says “Enable Low Voltage Programming.” This may already be checked, but just highlight it in blue as shown in Figure 5 and click “Apply.” Hit “OK.”

Hello, World!

You’re ready to roll, and can start by running a basic “Hello, World!” program just to make sure everything is working properly on the board. From the top navigation menu, go to Tools -> Embedded -> MPLAB Code Configurator. Several windows will populate to the right of the “Projects” section with project and device resources, a pin manager, and a main dashboard.

The first thing you’ll want to do is make sure that low-voltage programming is also enabled on the MCU, and to do this just select “System Module” from the Project Resources window, navigate to the “Registers” tab that appears in the main dashboard, scroll down to “Register: CONFIG2,” and change the LVP field from “High-voltage on MCLR/VPP must be used for programming” to “Low-voltage programming enabled” (Figure 6).

Now, add a GPIO from the Device Resources window viewable under your project resources by finding “GPIO::GPIO” and double clicking it. This will add a GPIO to your project, and open the Pin Manager window on the far right. Open up the port for LED D7 that will run Hello, World! by closing the lock icon on the PIC16F1619’s RC5 GPIO pin that controls that LED (Figure 7).

From here, select “GPIO Module” in the Project Resources window so we can configure LED D7 to illuminate when the application code is generated and flashed to Curiosity. When you have that selected, RC5 configuration options will appear in the PinManagerPaneViewer in the center dashboard, and here you will want to check the box labeled “Output” as we’re going to be outputting to LED D7, as well as the “Start High” box to indicate that the pin should start running on high.

Now, hit “Generate” at the top of the main dashboard to generate the Hello, World! code. A popup will appear advising you that MCC hasn’t detected a main.c file, and ask if you would like to generate one. Click Yes, then hit the “Make and Program Device” button (code box with downward arrow) in the MPLAB X top toolbar (Figure 8). The code will load to Curiosity, and LED D7 will be statically lit in a matter of seconds.

PWM control

With confidence that Curiosity’s hardware and all of your software are working correctly, you can now move forward building the PWM. You’ll be able to see the PWM in action by pulsating LED D6 in conjunction with the PWM’s timer period, so it’s time to get configuring.

In the “Device Resources” window, which should be located in the lower left of the MPLAB X interface, add the PWM3 peripheral to your project by double clicking. This will automatically add the Timer 2 (TMR2) peripheral to your project as well.

Now that the appropriate resources have been added to your project, you’ll want to make sure that PWM3 is using TMR2 as the clock source. It should default to TMR2, but just to be sure, select PWM3 from the Project Resources window, and under the Easy Setup tab make sure that the “Enable PWM” checkbox is marked and that Timer2 is populated in the “Select a Timer” field. We won’t need to adjust anything else in the PWM peripheral for our purposes, so just leave all the values at their default settings.

At this point, toggle over to the TMR2 peripheral under Project Resources where you’ll be able to manipulate the clock and timer settings that will govern our PWM. Make sure that the “Enable Timer” checkbox is ticked.

Since the PIC16F1619 system clock defaults to FOSC (the system clock and oscillator can be changed by selecting the “System Module” under Project Resources), leave the TMR2 clock source at FOSC/4. What you do want to modify though in order to observe the PWM function operating on LED D6 are the prescaler and postscaler ratios, which help define the ratio of timer overflows to pulses. These ratios, along with other timer settings and clock configurations will obviously vary based on the control application, but for the purposes of this build just max them out to 1:128 and 1:16, respectively, in order to maximize the timer period so that the PWM pulses can be observed on the Curiosity board with the naked eye.

In the “Timer Period” field, enter a value of “4 s” to push the timer period up further, and leave everything else at its default setting. This will give us a PWM period of roughly 260 ms – on the high for many control applications, but you get the idea.

Back in the Pin Manager view on the right, you will now notice that several resources have been added, among them the PWM3 and TMR2. To enable the PWM we’ll need to open up the RA2 pin by clicking the PMW3 lock there. You don’t need to link TMR2 since the PMW3 is already using that as a baseline.

Generate code. Make and build program. Watch for LED D6 to pulsate. At this point, LED D7 should still be statically lit from the previous application, with LED D6 pulsating next to it. If you want to turn LED D7 off at this point, deselect the RC5 lock in the MCC Pin Manager, regenerate the code, and remake/rebuild the application.

Conclusion

If you haven’t noticed, not only have we not written a single line of code to generate a working PWM control application, but we also haven’t checked a datasheet once. This is possible because MCC loaded in all of the PIC16F1619 MCU and Curiosity board parameters when we configured our project, saving us a lot of time that would otherwise have been spent poring through .pdfs.

If you’re interested in exploring additional capabilities, Curiosity includes a footprint for the RN4020 Bluetooth module, as well as application-specific add-on boards from MikroElektronka (www.mikroe.com/click) called “Clicks” that connect through the mikroBUS socket and can also be configured using MCC. Additionally, a 3.0 beta version of the MCC is now available on the Microchip website (www.microchip.com/mcc) that adds additional algorithm libraries and networking stacks to enable more complete builds with minimal coding effort.