36
EEE527 Embedded Systems Lecture 3:Chapter 1: Using MPLAB (X) + XC32 Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6 th ring Email: [email protected] Web site: http://www.eej.ulst.ac.uk 24/09/13 "Adapted from the text “Programming 32-bit Microcontrollers in C –Exploring the PIC32 , © 2008.” Lucio di Jasio www.eej.ulster.ac.uk/~ian/modules/EEE527/files

EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: [email protected]

Embed Size (px)

Citation preview

Page 1: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

EEE527Embedded Systems

Lecture 3:Chapter 1: Using MPLAB (X) + XC32

Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6th ringEmail: [email protected] Web site: http://www.eej.ulst.ac.uk

24/09/13

"Adapted from the text “Programming 32-bit Microcontrollers in C –Exploring the PIC32 , © 2008.” Lucio di Jasio

www.eej.ulster.ac.uk/~ian/modules/EEE527/files

Page 2: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The Plan• The first project with MPLAB IDE

– The project window– The editor– The output window

• A first statement • A first complete program

– Controlling I/Os: Ports and Pins• Building the project• Using the MPLAB SIM simulator• The first debugging experience• Hello World!

Page 3: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

PreparationThe following tools will be used in this lesson:• MPLAB X IDE, Integrated Development Environment (use version X not v8.xx )• MPLAB SIM, free software simulator

(included in MPLAB installation)• MPLAB XC32, C compiler

(free Student Edition)

The following pieces of documentation will be used during this lesson:• PIC32MX1XX/2XX datasheet Section 11. I/O Ports – DS61118E • You will also need individual sections of the PIC32 Family reference manual• Sections 12 i/o ports (DS61120E.pdf)• Section 17 ADC (DS61104E.pdf)

Make sure they are available and/or installed and ready to use on your computer.You can download them from me or the Microchip web site at:

http://www.microchip.com/ DESIGN SUPPORT->Reference Manuals, search for “PIC32 Family”DESIGN SUPPORT->Datasheets, search for “PIC32MX250F128B”DESIGN SUPPORT->Compilers tab

Page 4: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The New Project Set Up• Launch MPLAB IDE • Follow the “New Project Set Up” Checklist to create a new

project using the Project Wizard

Page 5: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The New Project Setup Checklist1. Select Microchip Embedded – Standalone Project2. Select the PIC32MX250F128B device and click Next.3. Select Simulator as the Tool to be used 4. Select the XC32 C-Compiler and click Next5. Use a Project Name of Hello and click the Browse button and

create a new folder named “EEE527” on DRIVE D: 6. Create a folder below this called “Hello”7. Accept the default project folder “Hello.X”, then click Next.8. Click on Finish to complete the project set up

Page 6: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The Windows (note the tabs as well!

Familiarize yourself with the Windows (Tile) layout of MPLAB X.

Page 7: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

You can reset to this view using the Windows top-level menu (Alt-W)

Page 8: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

To add a new file to your project

Do one of the following;

•Go to the Projects tab (top RHS of screen) and rightclick on “Source Files”

•Click on the New File ICON (1st on icon toolbar)

•Use the main FILE ->New File menu

•Use the <Ctrl-N> shortcut.

Page 9: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Choose a C main file

Page 10: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Name it main.c

Page 11: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Make sure you have main.c in these two places

Page 12: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Add the line #include <xc.h>

below the other #includes.

Find this file and open it using F)ile->Open File…

(c:\Program Files\Microchip\xc32\pic32-libs\include)

It refers to ./proc/p32mx250f128b.h open it as well

You will need to refer to this file, and to the datasheet to resolve exact names of registers and bits.

Page 13: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

I/O PINS• I/O pins can be configured as:

– Digital Inputs– Digital Ouputs (Push Pull)– Digital Outputs (Open Drain)– Analog Inputs– Dedicated inputs or outputs for

a number of peripherals– Pins to interface to

programmers (PICKIT or JTAG)

Page 14: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 15: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

PORTA and PORTB • Different PORTs group pins with different

functions • PORTB for example contains a number of pins

that can be configured as analog inputs to the Analog to Digital Converter (ADC) .

• PORTA contains a number of pins that can be used for the JTAG interface, TRACE function, and the I2C interface

• Refer to the specific device datasheet for a detailed list of each PORT/pin capabilities

Page 16: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 17: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

TRIS registers• TRIS registers control the direction of each pin

(Input/Output)• TRISA, TRISB… each port has a corresponding

tris register – Setting a bit to 1 configures a pin as Input– Clearing a bit to 0 configure the corresponding pin

as an output

Page 18: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 19: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 20: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Microchip have renamed ANSEL to ADxPCFG in some CPUs

Page 21: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Therefore to use PORT B for simple I/O we must disable the ANALOG functions available on those pins.

Similarly to use PORT A for simple I/O we must disable JTAG functionality

There are other subtle points such as using the CHANGE registers associated with I/O pins – but the powerup defaults are ok in this case.

Also a lot of peripherals can be switched to (almost) any PINs so we will need to look at “PERIPHERAL PIN SELECT” (PPS) at some stage, but, for now, the defaults are ok.

Page 22: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

/* * File: main.c * Author: ian * * Created on 24 September 2013, 10:02 */

#include <xc.h>#include <stdio.h>#include <stdlib.h>

/* * */int main(int argc, char** argv) { TRISB = 0; // all PORTB as output AD1PCFG = 0x0000; // all PORTB as digital PORTB = 0xff;

return (EXIT_SUCCESS);}

Edit your main.c to…

Page 23: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The Watch Window

Once a debugging tool (MPLAB SIM) is selected

Open the Watch Window To inspect the content of a variable (symbol) or any

of the special function registers (SFR) Select the desired output format(s)

The “NEW WATCH” under “DEBUG” top menu <ctrl-shft-F9>

Page 24: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Compiling and Linking• A compiler transforms the C source code (.c) and all

included (.h) files into a relocatable code object (.o)• The linker takes all the relocatable code objects (.o)

and libraries (.lib) and assembles them into an executable (.hex) file<<< Use the RUN->BUILD PROJECT to compile >>

Page 25: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Using the Simulator• Learn the basic debugging options offered by

the Simulator– Reset– Single Step (Over/In)– Animation– Running– Halting

Page 26: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Debugging: Hello WorldSet Watchpoints on PORTA and PORTB, set breakpoints on the pink lines and use DEBUG to singlestep and observe the ports change

Lookup JTAGEN and AD1PCFG in both xc.h and the datasheets.

Note in some pics AD1PCFG is changed to ANSELB

Page 27: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Analog Pin Functions Multiplexing

• The Analog Pins control: AD1PCFG

Page 28: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

SummaryIn this lesson we learned:• How to create a new project• How to create our first C source file• How to build a project using the MPLAB C32

compiler• About PINs and PORTs• How to configure and control simple digital output

pins• How to configure and use the MPLAB SIM

simulator

Page 29: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Advanced Material

Page 30: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The Disassembly Window• If you want to see what happens at the

machine instruction level: – Open the disassembly window

Page 31: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The Memory Gauge

• If you want to see how much memory RAM and FLASH is being used by the project

• Look at the memory guage in the dashboard

Page 32: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Notes for the PIC MCU Experts• The PIC32 PORTS are not necessarily 32-bit

large. In fact most PORTS are 16-bit at the most.

• The PIC32 PORTS are designed to be compatible with the 8-bit and 16-bit PIC PORTS

• I/O PORT control in C is easy• Use the LATx registers to control directly the

output latches

Page 33: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Tips and Tricks• Interfacing to 5V input and output signals is

possible with some caution:– Digital Input pins are 5V tolerant– Digital Output pins can be configured as Open

Drain– Use the ODCx registers to configure an output pin

for Open Drain mode.– Watch Out! Pins that are multiplexed with analog

functions are NOT 5V tolerant!

Page 34: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Suggested Excercises• If you have the Explorer16 board and an in circuit debugger:

– Use the MPLAB REAL ICE Debugging or the MPLAB ICD2 Debugging checklists to help you prepare the project for debugging.

– Insert the instructions required to disable the JTAG port.– Test the PortA example, connecting the Explorer16 board and checking the

visual output on LED0-7.• If you have the PIC32 Starter Kit:

– Use the PIC32 Starter Kit Debugging checklist to help you prepare the project for debugging.

– Modify the code to operate on PortD, but do NOT disable the JTAG port.– Test the code by checking the visual output on LED0-2 on the PIC32 Starter Kit

itself.• In both cases you can:

– Test the PortB example by connecting a voltmeter (or DMM) to pin RB0, if you can identify it on your board, and watching the needle move, between 0 and 3.3V, as you single step through the code.

Page 35: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Recommended ReadingsKernighan, B. & Ritchie, D.The C Programming LanguagePrentice-Hall, Englewood Cliffs, NJ

• When you read or hear a programmer talk about the “K&R” … they mean this book!

• Also known as “the white book”, the C language has evolved quite a bit since the first edition was published in 1978!

• The second edition (1988) includes the more recent ANSI C standard definitions of the language

• The MPLAB C32 compiler adheres to the ISO/IEC 9899:1990 (also known as C90) standard

Page 36: EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Online Resources• http://en.wikibooks.org/wiki/C_Programming• This is a Wiki-book on C programming and as

such it is a bit of a work in progress. It’s convenient if you don’t mind doing all your reading online.

• Hint: look for the chapter called “A taste of C” to find the omnipresent “Hello World!” example.