This blog acts as my own engineering journal while I explore the world of Embedded Systems development. I am also hoping that this blog helps others like me as they start their journey and design their own projects. If you have any questions or comments related to this blog please post them.
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.
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:
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.
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.