Monday, September 3, 2018

MPLAB Code Configuration and Microchip Xpress board

As I mentioned on my Blog post about Microchip's Get Launched workshop, after some time without using the latest versions of Microchip development tools, I was playing lately with MPLAB X and MCC, and learned few tricks at the Microchip workshop about how to quickly put together a simple embedded application on MPLAB X IDE and getting it running in a low cost evaluation board.

For this particular project I used one of Microchip Xpress Evaluation Boards and MikroElektronika's 7seg display Click Board (MIKROE-1201)

This is a very simple project to show a two digit decimal counter on the seven segment displays. If you have MPLAB X and the MCC plugin properly installed it will take only few minutes to get it up and running. I will show you step by step how to make it happen.

While the Microchip Xpress Evaluation board has been initially designed to showcase the MPLAB Xpress online IDE, it can also be used with with the standalone MPLAB X on your computer but you will program it in a different way than other development boards, I will show you how.

This particular board has a QFN28 PIC16F18855 8-bit MCU with 8KB FLASH memory and 1KB RAM, and maximum clock speed of 32MHz. The Xpress board does not have an external crystal for the main clock and uses the high-precision internal oscillator. Connected to the PIC there are 5 LEDs, one push-button and a 10K potentiometer and as a bonus a EMC1001 temperature sensor. And obviously also wired to the PIC the pair of female headers for the mikroBUS™ so we can attach the seven segment display or other Click Board™.

Power is sourced from the microUSB connector, USB communication and programming is implemented using a PIC18LF25K50. The board includes a MCP1703 small LDO to step down the USB +5V to +3.3V.

The MikroElektronika 7seg Click Board™ has two standard JS-5213A common cathode single digit seven segment and decimal point displays, wired to a pair of SN74HC595 8-bit shift registers. In this board the displays are not multiplexed and driven in parallel from the MCU. The Click Board™ will take serial data to fill the registers to set the state of each output that drives all the segments and decimal points. The trick here is how to put together the stream of bits to show up the correct numbers on each digit, but don't worry here is where MCC is very helpful, the code is already there!

OK lets start putting the project together, launch MPLAB X IDE. Notice that on the top icon bar you will see MCC in blue, if you have it grayed out you must install the MCC plug-in.

Open a New Project and first select the correct target device, in our case the PIC16F188855.



Now we need to select which tool are we using to program the board. As I mentioned before since this board does not have a debugger we will select "Simulator" on the Hardware Tools tree.



Now we select the compiler. While I've both versions of XC8 I used v1.45 on this project originally before I installed v2.00. Now if you use v.200 be aware that there is a little bug, the workaround for now is to select ANSI C 90 standard.



After we complete setting up all the project properties we saved it.



This setup process creates the entire project structure and sets the correct parameters but take in account that there is not a line of code in the project yet! That's where MCC comes to play, so click on the MCC icon to make the magic happen.



Depending on how fast your PC is, it make take an instant to load the plug-in and additional menus associated with it. You will notice a new tab on the top left window labeled "Resource Management (MCC)" This tab will show various windows on the left, by default on top you will see the Project Resources and below it Device Resources.

On the Device Resources window we'll navigate to one of the last options showing "Mikro-E Clicks" open that menu tree and you will see a sub-tree with all the click boards currently supported by MCC organized by type. Then lets look at Display, and there! the LED7seg is listed as supported, double click on it.



We are not done yet since we need to tell MCC the particular configuration we have for this board and how each of the signals are assigned to the GPIO pins on the MCU.
You will see that on the main top right window now you have all the information and configuration associated with the LED7seg click board, including a picture of it and a direct link to the MikroElektronika product page.

On the bottom Output Window a new tab will appear with the Pin Manager Grid View.



Be aware that the default pin assignment may not be correct for the current Xpress board we are using, so we have some work to do. So take a look at the documentation or silkscreen on the 7seg click board and how it matches the GPIO signals on the Xpress board (RB0, RB1, etc.)

Also in the Project Resources window (top left) look for System->Pin Module and double check that your pin assignments and direction are correct. You will notice that MCC already assigned custom names for each of the signals.

After you are done just click on the Generate tab next to Project Resources.



Now if you go back to the Projects tab and look at the files tree, you will find that MCC populated the tree with all the files needed for the project. Are we there yet? Nope but close, we need to write two lines of code since what we get from MCC is the framework and an example. So look for these two files main.c and LED7Seg_example.h.



The files generated by MCC include all the code needed to drive and communicate with the 7seg click board, and includes an example about how to use this code, but the example is not yet used by our main program.

Then if you open the   file, you will find what is the function we have to call from the main program to execute the example, in this case LED7Seg_example(void) so we'll add the call on the main while loop on main.c (remember to remove the void).



And since we are calling a function that has not been pre-defined we need to include the header file also on main.c.



Save all files, and click now on the Clean and Build Main Project top Icon menu, this will compile and generate the output binary file to be programmed on the Xpress board. As you can see the process on my machine took 8 seconds !!



Now it is time to program the board. If you didn't have it previously connected to your PCB via the microUSB cable, you can do it now. You will notice that the Xpress board will present a remote USB drive on your PC, in my case XPRESS(K:). If you open a file explorer window on it you will find a README.HTM file which is just a redirect to the Xpress board product page on Microchip's website.

Then on a file explorer window find your project files, MPLAB X IDE generates various directories, one of them is dist where you find the default production image. On that directory you will see the .hex file we need, in my case based on the project name I defined is Xpress-7Seg.X.production.hex, just copy and paste that file to the remote USB drive from the Xpress board and you will see that almost instantly the PIC18 programs the PIC16 and starts to run the program. You should see a decimal counter on the LED displays going up in intervals of one second.

That's it !!

While this project is very simple, there are other more complex that can be put together using MCC, but this is a great start to have something easy running quickly so you can learn the ropes. 

I will be posting more projects soon, showing how to use other Microchip Development boards, some I designed and produced myself, and various MikroElektronika Click Boards. 

Happy Hacking

Cheers
Jorge