Friday, December 16, 2011

Using the UART on the PIC32 Explorer 16 Board

Using the LEDs and buttons on the Explorer 16 development board gives a user the ability to interact with a program running on the PIC32, but it is a brutally limited form of interaction. To do any serious interaction one would want some kind of console environment. An easy way to create that is to use the RS232 port on the Explorer 16 board to create a communications link with the Windows host system. Unfortunately my Windows system does not have any RS232 ports (they are becoming increasingly rare on new motherboards). To get around this I am using a USB to RS232 adapter that runs some variant of the Prolific USB to RS232 chipset. I also needed a terminal emulator to run on my Windows system so I could communicate on the RS232 port. Hyperterminal, the old terminal emulator of choice, has been removed from recent version of windows, so after some web searching I found a replacement called RealTerm. This program has some powerful features for controlling an RS232 connection and I found it very easy to use. Now armed with an RS232 connection and a terminal emulator I programmed the PIC32 with a UART demo program from the C32 compiler's peripheral library demo folder. The demo is called "uart_basic" and is found in the:
<install path>\Microchip\MPLAB C32 Suite\examples\plib_examples\uart\uart_basic 
directory. The demo is supposed to present the user with a menu of commands where the user selects one by entering the number given for that command. Unfortunately, after programming the PIC32, connecting the RS232 cable, and starting the terminal emulator, nothing happened. After some debugging I tried restarting the Prolific device driver by disabling it and then enabling it in the Windows Device Manager. That solved the problem and the demo performed as expected.
Tip:
RealTerm is an excellent replacement for Hyperterminal, offering some powerful RS232 debugging tools.

Tip:
If you are having trouble using a USB to RS232 adapter, try restating its device driver by disabling and then enabling the device in the Windows Device Manager.

Friday, November 25, 2011

First Program on the Explorer 16 Board

For my first program I wanted to use the push-buttons and LEDs on the Explorer 16 development board as this would make the program easy to test. The goal is to make the lights blink when a button is pressed. Peripherals like buttons and LEDs are trivial to use on the Explorer 16 board, each are wired directly to a primary I/O pin on the PIC. The LEDs are activated by writing a logical 1 to their output pin. The buttons are active low, so a button press can be detected by seeing a logical 0 on their input pin. The code first disables the JTAG port. This is needed because one of the LEDs is wired to a pin also used for JTAG. It then configures the LED pins as outputs and the button pins as inputs. The rest of the program is an infinite loop that detects if any of the buttons are pressed, toggling the LED state if a button is pressed, and clearing the LED state if one is not pressed. The loop also contains a busy loop to cause a delay before the buttons are checked again. This delay is needed to hold the LEDs in their current state for a while so that they are clearly blinking to the human eye. Without it the LEDs would blink so fast they would just look very dimly lit.

Code:
Tip:
The Microchip peripheral library uses a lot of macro functions. The lowercase 'm' in the function name indicates that it is a really a C pre-processor macro and not a true C function. Macro calls can not have line breaks in them so the line break must be escaped with a '\' (back-slash). For example:

 mPORTAClearBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
   BIT_3 | BIT_2 | BIT_1 | BIT_0 );

Wednesday, November 23, 2011

Compiling a Demo Program on the Explorer 16 Board

The C32 compiler that comes as part of the install package for MPLAB has several example projects. They can be found in <install path>\Microchip\MPLAB C32 Suite\examples. I chose the project "led_message" for my first compile job. It blinks the LEDs on the Explorer 16 development board which seemed ideal to me since it would be obvious if my program image was running on the PIC or not. The source code contains a good description of exactly what the code is trying to do:
/*
** Message in a bottle
**
** Explorer16 version (long delays)
**
** Thanks to Lucio DiJasio for letting us use this example.
**
** Run this example on Explorer-16 board with PIC32MX PIM.
** Hold the board vertically from the PICtail connector size
** and wave the board back-and-forth to see message "HELLO" on LEDs
*/
To compile the code I opened the projects MCP file and selected the PICKit3 as my programmer under the Programmer --> Select Programmer sub-menu. This caused the programmer to connect to the board and triggered an error message that the device ID read by the PICKit3 did not match the device the project was setup for. To fix this I selected the PIC processor I was using by selecting Configure --> Select Device... and choosing my device from the dialog box. After this I built the project using Project --> Build All and then programmed the device using Programmer --> Program. After programming was completed the program started running on the PIC and the LEDs started blinking. My next project is to develop my own program and run that on the PIC.

Monday, November 21, 2011

Getting Started with the PIC32 Explorer 16 Board

My initial setup is made up of a PIC32MX processor, Explorer 16 development board, and a PICKit 3 programmer/debugger. I am also using the MPLAB IDE and C32 compiler both from Microchip. After updating the software from the Microchip website I plugged in the development board and found it running a demo program that would cycle through a display on the LCD screen showing the voltage and temperature being read from the various sensors. Next I tried connecting the PICKit3 and seeing if I could communicate with the board. This is where I ran into my first snag. MPLAB was able to detect the PICKit3 but it hung at the connecting stage with the connecting status bar just filling over and over again. After doing some web searches and talking to an experienced PIC developer, I learned that my issue is a common one with the PICKits. The work-around is to swap the USB port you have the PICKit plugged into on the host system. It doesn't matter which USB port you swap it to, as long as it is a different port. Rebooting can also fix this issue. With the connection issue resolved I was prompted to download new firmware for the PICKit, and after installing that, I was able to read the current image from the board. My next project will be to change the program image running on the PIC32 and then to write and install my own image.

Materials:

Tip:
After selecting the PICKit 3 programmer, if MPLAB hangs at the "Connecting..." step, try swapping the USB port for the PICKit 3.