pic development system - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory...

20
Department of Electronic Engineering School of Systems Engineering PIC Development System Year 3 Projects

Upload: trannhi

Post on 28-Oct-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Department of Electronic Engineering

School of Systems Engineering

PIC Development System

Year 3 Projects

Page 2: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 1

1. Download Files You will need to download some C source files (for example hello.c). Use a web browser to open:

http://www.elec.rdg.ac.uk/jbg.html Select: Teaching / Part 3 modules / EE3P2 Project. Here you will find copies of the laboratory manual, the PIC16F87X data sheet, the Millennium board manual and the source code files (in Zip format). Select: Source code files. The Zip archive will be opened with PicoZip. Extract the source code files to your working directory on the D: drive. You should see the following files in your working directory:

debug.c hello.c lcd.c time.c irq.c debounce.c digitalpot.c i2ctest.c

Page 3: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 2

2. Setting Up a Project MPLAB is used to debug programs and to control compilation. On the desktop click on: MPLAB. You should now see a window containing the MPLAB IDE:

Before starting to compile a program you need to create a new project. Select: Project/Project Wizard/Next.

In the device menu select: PIC18F452 and click on: Next. Now you should set up the language toolsuite:

Page 4: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 3

In the Active Toolsuite menu select: CCS C Compiler for PIC12/14/16/18 and in the Location window browse to: C:\Program files\Picc\CCSC.exe and click on: Open. Click on Next.

In the Project Name window enter: Debug and in the Project Directory window browse to your directory on the D: drive. Click: Next.

Page 5: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 4

Browse to the file: debug.c in your directory, and click on: Add>>; this file will now appear in the right-hand window. If this file is not in your directory you must download it using the instructions in section 3. Tick the box and click on: Next.

This summarises the project that you have just created. Click on: Finish.

Page 6: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 5

In the left-hand pane click on: debug.c; a new pane will open up containing the source file as shown above.

// Title : Debug Example // LastEdit: 13 May 2005 // Author : J. B. Grimbleby #include <18F452.H> #fuses HS,NOOSCSEN,NOPROTECT,NOBROWNOUT,NOWDT,NOSTVREN,NOLVP,PUT #use delay(clock=20000000) #define led pin_b0 #use standard_io(b) short int p = 0; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19}; void toggle() { p = !p; output_bit(led, p); } void delay(long int t) { long int k = 0; while (k < z) k++; } void flash() { for (;;) { delay(20000); toggle(); } }

Page 7: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 6

int main() { flash(); return 0; }

Lines starting with // are comments and are ignored by the compiler. Comments make a program easier to understand and should be used wherever the function of a section of code is not obvious. Every program should have header comments specifying its function, the author, and the date of last edit. Indenting computer programs as shown above makes them more readable, and helps to find syntax errors such as unmatched brackets.

#include <18F452.H>

This specifies a header file for the target PIC, in this case the PIC18F452. Take a look at this file (it can be found in: C:\Program Files\Picc\Examples). You will see that it consists primarily of #define statements associating I/O pins with internal PIC registers.

#fuses HS,NOOSCSEN,NOPROTECT,NOBROWNOUT,NOWDT,NOSTVREN,NOLVP,PUT

This statement sets some of the fuse-programmable options on the PIC. In this case the clock is selected to be a high-speed (HS) crystal resonator rather than, for example, a less accurate but cheaper RC circuit.

#use delay(clock=20000000)

The compiler provides a delay function, but it needs to know the clock frequency to determine the size of the delay loop. This statement indicates that a clock frequency of 20 MHz will be used.

#define led pin_b0

This defines the I/O pin to be used to drive the LED: wherever “led” appears in the program it is replaced by “pin_b0” which is one of the I/O pins. By using a #define statement it is possible to change the I/O pin throughout the program simply by modifying the #define statement.

#use standard_io(b)

This statement determines the way the led output pin will be driven. Using “standard_io” means that the data direction of the pin will automatically be set before an input or output operation.

int main()

Every program must contain a function main(). When the device is powered up the function main is automatically executed.

Page 8: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 7

for (;;) {

Programs for embedded microprocessors must never terminate, so they will always contain an infinite loop. In this case a for loop is used, but it could equally well have been a do loop or a while loop.

output_bit(led, p);

This statement drives the led pin (B0) to the logical value of the short int (1-bit) variable p.

Page 9: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 8

3. Compiling the Program Select: Project/Compile. Alternatively click the Compile icon on the speedbar: A new window will appear containing information about the compilation. The program debug.c contains deliberate errors:

Double click on the error message line; this will open the source code window at the position of the error:

The while statement contains a variable z which has not been declared. It is obvious from the context that this variable should have been t. Make the correction and re-compile. You should now find that the program compiles without errors (although with some warnings).

Page 10: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 9

Use Windows Explorer to browse to your directory on the D: drive. You should see a file: debug.hex; this is the object file containing the machine instruction codes. These codes must now be uploaded to the ROM memory of the PIC.

Page 11: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 10

4. Uploading the Object Code You will be using a Millennium prototyping board to test and debug your program. The appropriate adaptor for PIC18F452 devices is the AC160251 ICD Header:

This plugs into a standard 40-pin socket, and also has a connector for the cable to the ICD2.

Programs can now be developed and debugged on the Millennium board.

Page 12: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 11

The adaptor can be used with any PIC system. Be aware, however, that the debugger makes use of pins B6 and B7, and these should not be used for other purposes. This board must be supplied with power from a 9V power supply (connector upper left) and connected to a Microchip ICD2 using the flat grey multi-way cable (connector lower left). A PIC18F452 should be inserted in the 40-pin socket as shown.

The flat grey multi-way cable (connector lower left) should connect the ICD2 to the Adaptor, and a USB cable (connector lower right) should connect the ICD2 to a USB port on your computer. The USB2 derives its power via the multi-way cable and does not need a separate supply. Now in the MPLAB IDE click on: Debugger/Select Tool/1 MPLAB ICD 2. Then click on: Debugger/Connect. You should see a message appear in the Output window indicating that the IDC2 is connected, finishing with: “Running ICD Self Test ...Passed MPLAB ICD 2 Ready”. If this is not successful you should check that the Millennium board and USB2 are connected correctly, and that power is being supplied. Connect pin B0 to one of the LEDs using single-strand wire. Now you are ready to upload the program. Click on: Debugger/Program. Alternatively

Click the Upload icon on the speedbar:

Page 13: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 12

After a short delay you should see the message: “Programming succeeded MPLAB ICD 2 Ready” in the Output window. Run the program by clicking on: Debugger/Run. You should see the red LED connected to pin B0 flash at approximately 2 Hz. Congratulations! You have succeeded in compiling, uploading and running your first PIC program. The way you have uploaded the program is designed for easy debugging, and is not suitable for a stand-alone system. You can confirm this by disconnecting, and then re-connecting the power supply to the Millennium board: the LED will no longer flash. It is easy to upload a stand-alone program, and the procedure is described in the Appendix A.

Page 14: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 13

5. Debugging Upload the object code again. Before running the program click on: View/7 Watch. The watch window will appear. Select: k from the Add Symbol menu; then click on the Add Symbol button.

Note the speed-bar on the upper right of the IDE:

Run

Halt

Animate Step Into Step Over Reset

Click on the Run icon of the speedbar, then click on the Halt icon. The program will stop running, almost certainly within the while loop of the delay() function. The position is indicated by the green triangle in the source code window:

Page 15: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 14

The Watch window will now show the value of the loop variable k:

It is shown in red because its value has changed since the last time that the program was halted. Now add the global variables p and primes to the Watch list. The variable p is a short int, that is a 1-bit or Boolean variable. The compiler will have assigned it to one bit of an 8-bit memory location. Click on the + symbol to the left of variable identifier and the 1-bit value will be displayed:

The contents of arrays can be inspected by clicking on the + symbol to the left of the array identifier. Inspect the contents of the array primes. You will probably initially see a column of dots. This is because the information is being displayed as ASCII. Right click

Page 16: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 15

on the primes row, and select: Properties from the menu. Under Watch Properties select: Decimal from the Format menu and click: Apply. The integer values of the array will now be displayed in decimal format:

A powerful feature of the debugger is the ability to set breakpoints. When a running program encounters a breakpoint it will halt and the Watch window will show the values of the variables. Right click on the line: k++ of the delay function and select: Set Breakpoint. A breakpoint icon will now appear against this line:

Click on the Reset icon (to restart the program at the beginning) and then click on the Run icon. The program will run until it hits the breakpoint:

Page 17: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 16

Since this is the first time through the while loop the Watch window should show k=0. Click the Run icon. This time the program should halt with k=1. Note that the value of k is shown in red (because it has changed), but the value of p is shown in black. Breakpoints can be removed by right-clicking anywhere on the source program and selecting: Breakpoints/Remove All Breakpoints. The PIC executes machine instructions generated by the compiler from the C source program. These instructions can be inspected by clicking on: View/1 Disassembly Listing:

Clicking on the Step-Into icon will execute a single instruction. Step round the loop a couple of times, while observing the Watch window. Now remove the breakpoint and click on the Animate icon. The PIC will step around the loop automatically.

Page 18: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 17

Appendix A: A Stand-Alone Program So far you have uploaded the program to the PIC using the debugger. This is convenient during program development but does not produce a stand-alone system in which the program can execute without connection to the ICD2. Try the following: use the debugger to program the PIC, and check that the program runs satisfactorily. Now disconnect the cable between the Millennium board and the ICD2. Press the RESET button on the Millennium board: the program will probably fail to execute. To produce a stand-alone system click on: Programmer/Select Programmer/2 MPLAB ICD2. Then click on: Programmer/Program. Disconnect the cable between the Millennium board board and the ICD2 and press the RESET button on the Millennium board. The program should execute correctly. Temporarily remove power from the Millennium board. When the power is restored: the program should execute correctly.

Page 19: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 18

Appendix B: Minimal System The circuit shown below is a minimal PIC18F252 system with ICD2 port.

Page 20: PIC Development System - personal.rdg.ac.ukstsgrimb/teaching/pic_prototyping_for... · laboratory manual, the PIC 16F87X data sheet, the Millennium board manual and the source code

Page 19

A pcb layout for this design is shown below:

And a populated board:

The circuit diagram and layout can be downloaded from:

http://www.elec.rdg.ac.uk/jbg.html Select: Teaching / Part 2 modules / EE2A2 Embedded Microprocessor Systems. Here you will find a zip archive: mimimal.zip containing the diagram and layout.