Getting Started With ChipKIT
Getting Started With ChipKIT
Getting Started With ChipKIT
by Chuck Hellebuyck
Table of Contents
INTRODUCTION..............................................................................................5
CHAPTER 1 WHAT IS CHIPKIT................................................................7
CHIPKIT OVERVIEW .........................................................................................7
HARDWARE FOR PROJECTS .............................................................................19
HOW TO GET CHIPKIT RUNNING ON MAC.......................................................20
HOW TO GET CHIPKIT RUNNING ON WINDOWS ..............................................27
MPIDE...........................................................................................................37
CHIPKIT C COMPILER ....................................................................................42
CHAPTER 2 FLASH AN EXTERNAL LED .............................................49
HARDWARE ....................................................................................................49
SOFTWARE .....................................................................................................51
HOW IT WORKS ..............................................................................................51
NEXT STEPS ...................................................................................................54
CHAPTER 3 TRAIN CROSSING...............................................................55
HARDWARE ....................................................................................................55
HOW IT WORKS ..............................................................................................57
NEXT STEPS ...................................................................................................59
CHAPTER 4 LED TRAFFIC LIGHT.........................................................61
HARDWARE ....................................................................................................61
SOFTWARE .....................................................................................................63
HOW IT WORKS ..............................................................................................64
NEXT STEPS ...................................................................................................66
CHAPTER 5 SCROLL LEDS......................................................................67
HARDWARE ....................................................................................................67
SOFTWARE .....................................................................................................69
HOW IT WORKS ..............................................................................................70
NEXT STEPS ...................................................................................................72
CHAPTER 6 SENSING A SWITCH...........................................................75
HARDWARE ....................................................................................................76
SOFTWARE .....................................................................................................77
NEXT STEPS ...................................................................................................81
CHAPTER 7 - READ A POTENTIOMETER ..............................................83
HARDWARE ....................................................................................................83
SOFTWARE .....................................................................................................85
Introduction
New electronic based products are released daily. At the
heart of these electronic devices is typically a
microcontroller. Over the years many improvements in
the tools needed to develop microcontroller based
designs have been released. One of the more popular
development modules is the Arduino that is so easy to
program, artists, designers, hobbyists and even basement
engineers have created very interesting projects using this
development path. The design is open sourced so many
variations of the Arduino exist.
Microchip Technology Inc. and Digilent Inc. with
software help from Mark Sproul and Rick Anderson of
the FUBAR Hackerspace worked together and created a
Microchip PIC based Arduino compatible module
called the chipKIT. chipKIT comes in two versions; the
smaller chipKIT UNO32 and the larger chipKIT
MAX32. These modules are based on the Microchip
PIC32 microcontroller and offer many more features than
the typical Arduino module. Despite these added
features, the chipKIT platform is designed to work with
the same simplified programming language used by
Arduino and also uses the same type of bootloader
programming through a USB connection. The chipkit
makes it easy for anyone from beginner to experienced
professional to develop interesting projects and products.
In this book Ill show you how to get started with the
chipKIT UNO32 using some very simple example
sketches (sketch is a software program in the Arduino
5
chipKIT Overview
The chipKIT UNO32 is a microcontroller board based on
the PIC32MX320F128H microcontroller. The larger
7
Module Details
Module Features
Feature
Core
Flash
Memory
(KB)
RAM (KB)
USB
CAN
chipKIT MAX32
chipKIT UNO32
80Mhz,3.3v,32-bit
128
128
YES (FS Device/Host,
OTG)
2
16
Ethernet
YES
DMA
YES
PMP/PSP
RTCC
Timers
PWM
ADC
Comparators
I2C
SPI
UART
YES
YES
16/32-bit
16/32-bit
16 ch. 1Msps, 10-bit
2
5
4
6
10
YES
YES
16/32-bit
16/32-bit
16 ch. 1Msps, 10bit
2
2
2
2
Power
Analog Pins
The chipKIT UNO32 has 6 analog pins in the same
location as the Arduino UNO. There are some minor
differences that will be explained here.
Analog to Digital Conversion (ADC): A0 thru A5 Analog inputs with 10 bit resolution (i.e. 1024 different
values). UNO32 analog pins measure from ground to 3.3
volts. The reference voltage can be changed to a lower
voltage using the AREF pin and the analogReference()
function. (Arduino analog pins read from ground to 5v)
I2C: A4 (SDA), A5 (SCL) These pins support I2C
communication using the Wire library. On UNO32 you
need to move two jumpers on the circuit board to use
pins A4 and A5 as I2C pins.
Analog Reference: AREF - Reference voltage pin for
the analog inputs. Used with analogReference(). This pin
is on the digital header row.
Power Pins
The chipKIT UNO32 shares the same power connections
as the Arduino UNO. They are described here.
13
Jumper Settings
JP4
RD4PositionPIN10isPWMpin
RG9PositionPIN10isSPISlaveSelect(SS)pin
JP5
MasterPositionPIN12isSPISDO(MISO)
SlavePositionPIN12isSPISDI(MOSI)
JP7
MasterPositionPIN11isSPISDI(MOSI)
SlavePositionPIN11isSPISDO(MISO)
JP6
A4PositionPINA4isAnalogInput
RG3PositionPINA4isI2CSDApin
JP8
A5PositionPINA5isAnalogInput
RG2PositionPINA5isI2CSCLpin
15
JP2
BYPPositionPoweratJ4PowerInput(alsoVINpin)bypasses5v
regulatorgoesdirectinto3.3vregulator(Max6vinwheninthismode!)
REGPositionPoweratJ4PowerInput(alsoVINpin)goesto5v
Regulatorinputbefore3.3vregulator(Max15vinwheninthismode!)
Communication
16
17
Physical Characteristics
The UNO32 PCB is about 2.7 and 2.1 inches with the
USB connector and power jack extending beyond these
18
19
20
21
22
25
26
27
Connect the USB cable to one of the PCs USB ports and
then to the USB connector of your chipKIT UNO32
board. The chipKIT module uses a mini-A USB cable
which is different than the Arduino.
28
http://www.ftdichip.com/Support/Documents/InstallGuides.htm
29
30
31
Click the
icon in the chipKIT environment
folder and you should see a screen similar to the one in
Figure 1-15.
32
33
34
35
36
MPIDE
The MPIDE is the same for MAC, Windows and Linux
and contains a text editor where you write your chipKIT
sketch. A toolbar with buttons for common functions is
at the top of the screen along with a series of menus.
There is a message area at the bottom to give feedback
when saving or exporting a file. It also displays any code
errors. The toolbar functions are explained below.
37
Verify/Compile
File
Here you can open existing sketches or create and save
new ones.
Edit
Copies the code of your sketch to the clipboard in a
forum suitable for posting to a forum, complete with
syntax coloring.
Copies the code of your sketch to the clipboard as
HTML, suitable for embedding in web pages.
38
Sketch
Verify/Compile
Adds a source file to the sketch (it will be copied from its
current location). The new file appears in a new tab in the
sketch window. Files can be removed from the sketch
using the tab menu.
Tools
Auto Format
39
Tools > Board and Tools > Serial Port menus. Once
you've selected the correct serial port and board, press the
upload button in the toolbar or select the Upload to I/O
Board item from the File menu. Current chipKIT boards
will reset automatically and begin the upload. Youll see
the RX and TX LEDs blink as the sketch is uploaded.
The MPIDE will display a message when the upload is
complete, or show an error.
Libraries
Serial Monitor
chipKIT C Compiler
The chipKIT uses a programming language based on C.
The chipKIT uses names that are not typical to C
programming and also has pre-built commands to make it
easier for the beginner to get started quicker. The C
language gets converted into the 1s and 0s the chipKIT
microcontroller needs by the chipKIT C compiler. To use
it properly its best to know the basics and this chapter
will cover those details.
42
Sketch
43
Comments
void loop() {
// put your main code here, to run repeatedly:
}
{ } Curly Braces
; semicolon
46
const
12;
change.
Used
here
to
#include
#include "pitches.h"
// notes in the melody:
int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3,
NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
void setup() {
48
Hardware
Connect the chipKIT UNO32 per the drawing in Figure
2-1. The long lead of the LED is connected to pin 12, the
short lead is connected to the 220 ohm resistor. The other
end of the resistor is connected to the Gnd pin of the
chipKIT UNO32
49
50
Software
/*
Blink
Turns on an LED on for one second,
then off for one second, repeatedly.
*/
void setup() {
// initialize the digital pin 12 as an output.
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
}
//
//
//
//
How It Works
The software is quite simple but still requires some
explanation.
The top of the software contains a header block that
describes what the sketch will do. Any time you want to
add a block of text that goes over many lines, just place
the text between the /* and */ characters. This indicates
to the compiler that the text is just comments and not
code.
/*
Blink
Turns on an LED on for one second,
then off for one second, repeatedly.
*/
51
The main sketch loop is the section of code that runs over
and over again. All the command lines need to be
contained within the curly brackets. The main loop will
52
The next line is the delay function. This just creates a one
second delay as the value 1000 represents 1000
milliseconds or one second.
delay(1000);
The sketch then turns the LED off by setting the same pin
low.
digitalWrite(12, LOW);
The sketch then jumps back to the top of the loop since
the second curly bracket is encountered. This operation
will repeat over and over again to create a simple
blinking light.
53
Next Steps
Simple next steps are to change the pause value to a
lower number to flash the LED faster. You could also
connect the LED to a different pin and then change the
number in the high and low command lines to make that
new connection pin flash the LED. You could also
control two LEDs at once with a second digitalWrite line.
In fact the next project uses three to create a traffic light.
54