Chapter 4 Programming and Problem Solving. Flowcharting Flowchart A graphical representation of...

33
Chapter 4 Programming and Problem Solving
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    0

Transcript of Chapter 4 Programming and Problem Solving. Flowcharting Flowchart A graphical representation of...

Page 1: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Chapter 4

Programming and Problem Solving

Page 2: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Flowcharting Flowchart

A graphical representation of processes (tasks) to be performed and the sequence to be followed in solving computational problem

Page 3: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Example 4.1 (1 of 2)

Write instructions to load two bytes (37H and 92H) in data registers REG0 and REG1. Add the bytes and store the sum in REG2.

Steps Load the two bytes in data registers REG0 and

REG1. Add the bytes. Save the sum in data register REG2.

Page 4: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Example 4.1 (2 of 2)

ORG 0x20REG0 EQU 0x00REG1 EQU 0x01REG2 EQU 0x02

MOVLW0x37MOVWF REG0,0MOVLW0x92MOVWF REG1,0ADDWF REG0,0MOVWF REG2, 0SLEEP

Process

Start

Change the program: if the sum is larger than 50H then the result should be in REG3 AND

REG2=0; OTHERWISE, the result should be in REG2 AND REG3=0;

Page 5: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

What is wrong with this?ORG 0x20REG0 EQU 0x00REG1 EQU 0x01REG2 EQU 0x02REG3 EQU 0x03

COMPREG EQU 0x10CONST EQU 0x50

MOVLW CONSTMOVWF COMPREG,0

MOVLW 0x37MOVWF REG0,0MOVLW 0x92MOVWF REG1,0 ADDWF REG0,0 ;the result is in W

CPFSLT COMPREG,0BRA WR_REG3BRA WR_REG2

WR_REG3:MOVWF REG3, 0BRA DONE_PROG

WR_REG2:MOVWF REG2, 0

DONE_PROG:SLEEP

Page 6: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Steps in Writing and Executing Assembly Language Program

Analyze the problem. Draw a flowchart. Convert the flowchart in mnemonics. Look up Hex code and assign memory addresses. Enter the Hex code into memory of a lab training board. Execute the program. Debug the program if necessary.

Page 7: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Illustrative Program: Addition With Carry Check

Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.

If the sum generates a carry, clear the data register REG2; otherwise, save the sum in REG2.

Page 8: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Illustrative Program: Addition With Carry Check (1 of 2)

Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.

If the sum generates a carry, clear the data register REG2; otherwise, save the sum in REG2.

Page 9: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Integrated Development Environment (IDE) Steps in using IDE

Editing – generating the source code Assembling – Converting mnemonics into hex and binary; generates the

object file Linking – uses the object file and generates the binary code Downloading – transfers the binary code (instructions) to the memory of the

target device Executing – perform the tasks specified by instruction codes Simulation – Execute the program on PC (also called the simulator) Debugging – Going through the program step-by-step to find logical

problems in the instruction set

Page 10: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Writing a Program Using an Assembler

The assembly language program includes: Program in mnemonics Assembler directives Comments

Page 11: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Assembly Language Format (1 of 2)

Typical statement of an assembly language source code has four fields: Label Opcode (operation code) Operand (data, register, or memory address to be

operated on) Comment

Page 12: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Assembly Language Format (2 of 2)

Format exampleLabel Opcode Operand Comment

START: MOVLW 0xF2 ;Load F2H in W

↑ ↑ ↑ ↑

Space Space Space Semicolon

Or Colon

Page 13: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Assembler Directives ORG Origin END End of assembly EQU Equate SET Defines an assembler variable #INCLUDE Include resources from available library RADIX Number format DB Define byte DW Define word CBLOCK Define a block of constants ENDC End of block of constants RES Reserve memory

Page 14: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

View Registers and Source Program in MPLAB Simulator

Label

Data bytes

Define labels and const

Starting memory address

Page 15: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Format of Radixes Hexadecimal

0x0F H`4F` 4F 4FH

Decimal D`200`

Binary B`1001`

ASCII `This stuff are interesting!`

Page 16: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE to Write, Assemble, and Build Project (1 of 6)

Write source code using MPLAB editor. Create a new project. Select language tool suite. Name your project. Add files to assemble. Build the project.

Page 17: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE (2 of 6)

To create a new project Step 1: Open MPLAB

IDE Select Project Project Wizard Select Device PIC18F452 Next

Page 18: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE (3 of 6)

Step 2: Select a Language Toolsuite: Microchip MPASM Toolsuite Next

Page 19: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE (4 of 6)

Step 3. Name Your Project: Illust4-4 Addition with Carry Check Browse MyProj\Ch04 Next

Page 20: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE (5 of 6)

Step 4: Add Add Source Files Next

Page 21: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Using MPLAB IDE (6 of 6)

Summary Finish

Page 22: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Project Window

Page 23: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

List of Files Generated by MPLAB Assembler

Page 24: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Understanding the List File List file generated primarily for documentation Includes seven columns

Memory addresses where binary code is stored Hex code Line numbers Contents of source file

Labels Opcode Operands Comments

Page 25: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Executing a Program Using Simulator Steps in setting up MPLAB simulator

Select Debugger Select tool MPLABSIM Select Debugger Settings Change

frequency if necessary Select View Watch Add registers to

observe

Page 26: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

View Registers and Source Program in MPLAB Simulator

Page 27: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

View Registers, Source Program, and Program Memory in MPLAB Simulator

Page 28: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Debugging a Program Single-step technique

Enables user to execute one instruction at a time and observe registers for expected results

Breakpoint technique Enables user to execute a group of instructions at a time

and observe registers for expected results Tracing code

MPLAB can track execution of each instruction and display data which can be examined for errors

Page 29: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Single-Step Technique

Page 30: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Breakpoint Technique

Page 31: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Tracing Code

Page 32: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Assembler Directive Pseudo-code instructions Define constants, labels, where to assemble a program, reserves memory for

data Directives do not translate to machine language do not require memory

assignment (come for free!) Example BYTE EQU 0x02

Label BYTE is being equated to value 2Hex Example ORG 20H

Assemble the program starting at location 20H

Page 33: Chapter 4 Programming and Problem Solving. Flowcharting  Flowchart A graphical representation of processes (tasks) to be performed and the sequence to.

Number Representation