You are on page 1of 10

Integrated Circuit Debugger and Target Board Module (ICD/TB).

OBJECTIVES i) To familiarise with the Integrated Circuit Debugger and Target Board Module (ICD/TB).

ii) To learn how to download, run and debug the program. iii) To learn how to turn ON the LED. iv) To learn how to read from the switches.

INTRODUCTION The process of software development in machine language often involves stages such as editing, assembling, linking, simulation, downloading and testing. Since all these jobs are in one way or another related to each other, they are often used together for program development. In this session, we will teach you how to simulate and download you program using the Integrated Circuit Debugger and Target Board module (ICD/TB). The MPLAB integrated environment allows you to edit a program, assemble it, simulate and download onto the ICD/TB. In this way, you can test you program in real time on the ICD/TB. For microcontroller applications, it usually requires connection to I/O devices. Example, a program that turn ON and OFF the LED that connected to the microcontroller. During the program development and testing using the ICD/TB, if the LED is not lighted up, you can make used of the software debugging tool available in the MPLAB. Appendix A, show the diagrams of the target board together with the illustration of the connection between the computer and the ICD/TB module. The communication between the computer and the ICD/TB module is through the RS-232 link.

PART A (EDITING AND ASSEMBLING PROGRAM) 1.1 1.2 1.3 Double click on the MPLAB icon to start up the system. Create a new project (Lab3-1.pjt) and source file (Lab3-1.asm) under the c:\program files\mplab\mct directory. Now type the program given in Appendix B.
Page2

PIC Microcontroller & Applications

1.4

Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding.

1.5 1.6 1.7

Now run your program using the simulator. Invoke the watch windows and set the registers you want to monitor. Fill in the results into Table 1.

ORG BEGIN NOP CLRF

Program H00

pcl

Register W

status

PCLATH

CLRW MOVWF MAIN BSF BCF MOVLW MOVWF MOVLW MOVWF AGAIN BCF BSF GOTO PORTA STATUS,RP0 STATUS,RP1 B'00000111' ADCON1 B'00100000' TRISA STATUS,RP0 PORTA,0 AGAIN

TABLE 1

PIC Microcontroller & Applications

Page3

PART B (DOWNLOAD AND RUN PROGRAM) 2.1 After you finished filling up Table 1. You can start downloading the program into the ICD/TB. To do so, you must make sure that the ICD/TB is powered-up through a DC 9V Adaptor, the LED on the integrated debugger section of the ICD/TB will be lighted-up or blinking. A serial cable that connected to the COM 2 port of the computer and the target board section of the ICD/TB (referred to Apendix A diagram). 2.2 If everything is fine, invoke the Integrated Debugger of the MPLAB by selecting the Option>Development Mode menu item. A dialog box that looks something as shown in Figure 1 below will appear.

Figure 1: Development Mode Dialog Box 2.3 Select the radio button next to MPLAB-ICD Debugger and choose the PIC16F877 processor, then press the Apply button followed by OK button. An Integrated Debugger dialog box as shown in Figure 2 will appear. 2.4 If everything goes well, you would get into the MPLAB-ICD Debugger environment. If not, you may have probably used the wrong COM port or without powered-up the ICD/TB. (If you are still unable to get the Integrated Debugger dialog box, get help from your lecturer.) 2.5 After the Integrated Debugger dialog box appear, make sure you follow all the settings as shown in Figure 2 for settings the Integrated Debugger dialog box.

PIC Microcontroller & Applications

Page4

Statusbar

Figure 2: Integrated Debugger Dialog Box 2.6 The next step is to download the object code of the program (Lab3-1.hex) onto the target board. Clicked on the Program button in the Integarted Debugger dialog box using your cursor.
2.7

The Status bar in the Integarted Debugger dialog box will show Program Program Memory. Wait until the Status bar shows Waiting for user command. Minimise the Integrated Debugger dialog box by clicking on the hand corner of the Integrated Debugger dialog box. icon on the top right-

2.8 2.9 2.10 2.11 2.12

Follow the same procedures as you run the simulator in the first Laboratory session. Use the Debug>Run>Reset menu item or press F6 key to initialise the system. Execute the Debug>Run>Run menu item or press F9 key to run your program. The status bar
Page5

PIC Microcontroller & Applications

will change colour. 2.13 What is the target board's response (monitoring all the LEDs)? Is it what you expected?

2.14

Stop the program by executing the Debug>Run>Halt menu item or by pressing the F5 key. The status bar will change back to its original colour and the current PC and other status information will be updated.

2.15

Closed the Integrated Debugger dialog box by clicking on the corner of the Integrated Debugger dialog box.

icon on the top right-hand

CHALLENGE 3.1 Modify the program in Lab3-1.asm to light up the West & East LEDs. Demonstrate your program when you have completed.

PART C (READ FROM THE SWITCHES) 4.1 A sample program (Lab3-2.asm) in Appendix C, show you how to implement software switch debouncing for the SW1 switch on the target board. The program detects the closure of the tact-switch (SW1) which is connected to PortE bit 2 of the target board. Upon detecting a switch closure, it will be considered as one depression only after the button is released. Debouncing had been taken care of by software delay (80 ms) as there is no debouncing circuitry installed on the target board. The program will continue to look out for switch closure and toggle the NORTH LED On and Off every time the button is pressed. 4.2 4.3 4.4 4.5 4.6 Create a new project (Lab3-2.pjt) and source file (Lab3-2.asm) under the c:\program files\mplab\mct directory. Now type the program given in Appendix C. Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding. Follow the procedures in PART B of this laboratory sheet to download and run the program on the target board. Everytimes you pressed the SW1 switch, does the NORTH LED toggle as what you expected?
PIC Microcontroller & Applications Page6

CHALLENGE 5.1 5.2 Modify the program in Lab3-2.asm to toggle ALL the LEDs whenever SW2 is pressed. Demonstrate your program when you have completed it. Modify the program in Lab 3-2.asm to continuously read SW1 and SW2. When SW1 is pressed, NORTH and SOUTH LED should light up with all other LEDs off. When SW2 is pressed, EAST and WEST LEDs should light up with all other LEDs off. Demonstrate your program when you have completed it.

PIC Microcontroller & Applications

Page7

Appendix A

The System Set Up

Diagram of the Target Board

PIC Microcontroller & Applications

Page8

Appendix B:

Listing of Lab3-1 Program

;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab3-1.asm * ; Date: 31/8/2000 * ; File Version: 1 * ; Author: * ; Company: Temasek Poly * ; Description: Turn 'ON' the LED. * ; RC Clock Frequency = 2MHz * ; Instruction Clock = 2MHz/4 = 0.5MHz * ; Duration per cycle = 1/0.5MHz = 2us * ; * ;********************************************************************** list p=16f877 #include <p16f877.inc> ; define processor ; processor specific variable

;********************************************************************** ORG H'00' ; processor reset vector BEGIN NOP ; no operation CLRF PCLATH ; ensure page bits are cleared CLRW MOVWF PORTA ; off all LEDs MAIN BSF STATUS,RP0 BCF STATUS,RP1 ; bank 1 MOVLW B'00000111' ; PortA & PortE as digital input MOVWF ADCON1 ;********************************************************************** ; To configure the bit as input, write a '1' to the corresponding * ; bit. Hence, write a '0' will configure it as output * ;********************************************************************** MOVLW B'00100000' ; PortA bit0-4 as output MOVWF TRISA ; and bit5 as input ;***** Initialize all variables ***** AGAIN BCF STATUS,RP0 ; BSF PORTA,0 ; GOTO AGAIN ; END ; bank 0 turn 'ON' NORTH LED go back to AGAIN end of program

PIC Microcontroller & Applications

Page9

Appendix C:

Listing of Lab3-2 Program

;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab3-2.asm * ; Date: 31/8/2000 * ; File Version: 1 * ; Author: * ; Company: Temasek Poly * ; Description: Toggle the LED label 'NORTH', everytimes the * ; Tact-switch(SW1) is pressed. Incoporate software * ; switch debouncing for the Tact-switch(SW1). * ; RC Clock Frequency = 2MHz * ; Instruction Clock = 2MHz/4 = 0.5MHz * ; Duration per cycle = 1/0.5MHz = 2us * ; * ;********************************************************************** list p=16f877 #include <p16f877.inc> OUTER INNER EQU EQU H'20' H'21' ; define processor ; processor specific variable ; outer counter ; inner counter

;********************************************************************** ORG H'00' ; processor reset vector BEGIN NOP ; no operation CLRF PCLATH ; ensure page bits are cleared CLRW MOVWF PORTA ; off all LEDs MAIN BSF STATUS,RP0 BCF STATUS,RP1 ; bank 1 MOVLW B'00000111' ; PortA & PortE as digital input MOVWF ADCON1 ;********************************************************************** ; To configure the bit as input, write a '1' to the corresponding * ; bit. Hence, write a '0' will configure it as output * ;********************************************************************** MOVLW B'00100000' ; PortA bit0-4 as output MOVWF TRISA ; and bit5 as input MOVLW B'00000111' ; PortE bit0-2 as input MOVWF TRISE ;********************************************************************** ; If Tact-switch(SW1) is pressed, toggle the 'NORTH' LED * ; A 80msecs delay is used for the software debouncing. * ;********************************************************************** BCF STATUS,RP0 ; bank 0 CHECK BTFSC PORTE,2 ; skip, if SW1 is pressed GOTO CHECK ; check switch again, if not pressed CALL DLY80MS ; 80msecs delay BTFSC PORTE,2 ; is SW1 really pressed? GOTO CHECK ; check switch again, if not pressed PIC Microcontroller & Applications Page10

RELEASE

BTFSS GOTO MOVF ANDLW XORLW MOVWF GOTO

PORTE,2 RELEASE PORTA,W B'00000001' B'00000001' PORTA CHECK

; switch release? ; ; ; ; ; PORTA -> W mask off bit1-7 complement bit0 output to LED check SW1 again

;********************************************************************** ; A 1msecs delay subroutine * ;********************************************************************** DLY1MS MOVLW D'124' MOVWF INNER LOOP NOP DECFSZ INNER GOTO LOOP NOP RETURN ;********************************************************************** ; A 80msecs delay subroutine * ;********************************************************************** DLY80MS MOVLW D'80' ; 80msecs delay MOVWF OUTER LOOP1 CALL DLY1MS ; 1msec delay DECFSZ OUTER GOTO LOOP1 RETURN END ; end of program

PIC Microcontroller & Applications

Page11

You might also like