ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 974

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 4 - Assembly Language Programming

4.6 EQU Defining assembler constant

Syntax:

<name_constant> equ <value>

Description:

To the name of a constant <name_constant> is added value <value>

Example: five equ 5

six equ 6 seven equ 7

Similar instructions: SET

4.7 ORG Defines an address from which the program is stored in microcontroller memory

Syntax:

<label>org<value>

Description:

This is the most frequently used directive. With the help of this directive we define where some part of a program will be start in the program memory.

Example:

Start org 0×00 movlw 0xFF movwf PORTB

The first two instructions following the first 'org' directive are stored from address 00, and the other two from address 10.

4.8 END End of program

Syntax: end

Description:

At the end of each program it is necessary to place 'end' directive so that assembly translator would know that there are no more instructions in the program.

Example:

.

.

movlw 0xFF movwf PORTB end

Conditional instructions

Chapter 4 - Assembly Language Programming

4.9 IF

Conditional program branching

Syntax: if<conditional_term>

Description:

If condition in <conditional_term> was met, part of the program which follows IF directive would be executed. And if it wasn't, then the part following ELSE or ENDIF directive would be executed.

Example:

if level=100 goto FILL else

goto DISCHARGE endif

Similar directives: #ELSE, ENDIF

4.10 ELSE The alternative to 'IF' program block with conditional terms

Syntax:

Else

Description:

Used with IF directive as an alternative if conditional term is incorrect.

Example:

If time< 50 goto SPEED UP

else goto SLOW DOWN endif

Similar instructions: ENDIF, IF

4.11 ENDIF

End of conditional program section

Syntax: endif

Description:

Directive is written at the end of a conditional block to inform the assembly translator that it is the end of the conditional block

Example:

If level=100

Chapter 4 - Assembly Language Programming

goto LOADS else

goto UNLOADS endif

Similar directives: ELSE, IF

4.12 WHILE

Execution of program section as long as

condition is met

Syntax:

while<condition>

.

endw

Description:

Program lines between WHILE and ENDW would be executed as long as condition was met. If a condition stopped being valid, program would continue executing instructions following ENDW line. Number of instructions between WHILE and ENDW can be 100 at the most, and number of executions 256.

Example:

While i<10 i=i+1 endw

4.13 ENDW

End of conditional part of the program

Syntax:

endw

Description:

Instruction is written at the end of the conditional WHILE block, so that assembly translator would know that it is the end of the conditional block

Example: while i<10

i=i+1

endw

Similar directives: WHILE

4.14 IFDEF

Execution of a part of the program if symbol

was defined

Syntax:

ifdef<designation>

Description:

If designation <designation> was previously defined (most commonly by #DEFINE instruction),


Chapter 4 - Assembly Language Programming

instructions which follow would be executed until ELSE or ENDIF directives are not would be reached.

Example:

#define test

.

ifdef test ;how the test was defined

......; instructions from these lines would execute endif

Similar directives: #DEFINE, ELSE, ENDIF, IFNDEF, #UNDEFINE

4.15 IFNDEF

Execution of a part of the program if symbol

was defined

Syntax:

ifndef<designation>

Description:

If designation <designation> was not previously defined, or if its definition was erased with directive #UNDEFINE, instructions which follow would be executed until ELSE or ENDIF directives would be reached.

Example:

#define test

..........

#undefine test

..........

ifndef test ;how the test was undefined

..... .; instructions from these lines would execute endif

Similar directives: #DEFINE, ELSE, ENDIF, IFDEF, #UNDEFINE

Data Directives

4.16 CBLOCK Defining a block for the named constants

Syntax:

Cblock [<term>]

<label>[:<increment>], <label>[:<increment>]......

endc

Description:

Directive is used to give values to named constants. Each following term receives a value greater by one than its precursor. If <increment> parameter is also given, then value given in <increment> parameter is added to the following constant.

Value of <term> parameter is the starting value. If it is not given, it is considered to be zero.

Example:

Chapter 4 - Assembly Language Programming

Cblock 0x02

First, second, third ;first=0x02, second=0x03, third=0x04 endc

cblock 0x02

first : 4, second : 2, third ;first=0x06, second=0x08, third=0x09 endc

Similar directives: ENDC

4.17 ENDC End of constant block definition

Syntax: endc

Description:

Directive was used at the end of a definition of a block of constants so assembly translator could know that there are no more constants.

Similar directives: CBLOCK

4.18 DB Defining one byte data

Syntax:

[<label>]db <term> [, <term>,.....,<term>]

Description:

Directive reserves a byte in program memory. When there are more terms which need to be assigned a byte each, they will be assigned one after another.

Example:

db 't', 0×0f, 'e', 's', 0×12

Similar instructions: DE, DT

4.19 DE Defining the EEPROM memory byte

Syntax:

[<term>] de <term> [, <term>,....., <term>]

Description:

Directive is used for defining EEPROM memory byte. Even though it was first intended only for EEPROM memory, it could be used for any other location in any memory.

Example: org H'2100'

de "Version 1.0" , 0

Similar instructions: DB, DT

Chapter 4 - Assembly Language Programming

4.20 DT Defining the data table

Syntax:

[<label>] dt <term> [, <term>,........., <term>]

Description:

Directive generates RETLW series of instructions, one instruction per each term.

Example:

dt "Message", 0

dt first, second, third

Similar directives: DB, DE

Configurational directives

4.21 _CONFIG

Setting the configurational bits

Syntax:

_ _config<term> or_ _config<address>,<term>

Description:

Oscillator, watchdog timer application and internal reset circuit are defined. Before using this directive, the processor must be defined using PROCESSOR directive.

Example:

_CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC

Similar directives: _IDLOCS, PROCESSOR

4.22 PROCESSOR

Defining microcontroller model

Syntax:

Processor <microcontroller_type>

Description:

Instruction sets the type of microcontroller where programming is done.

Example: processor 16F84

Assembler arithmetic operators

Operator Description Example


Chapter 4 - Assembly Language Programming

Chapter 4 - Assembly Language Programming

Files created as a result of program translation

As a result of the process of translating a program written in assembler language we get files like:

Executing file (Program_Name.HEX)

Program errors file (Program_Name.ERR)

List file (Program_Name.LST)

The first file contains translated program which was read in microcontroller by programming. Its contents can not give any information to programmer, so it will not be considered any further. The second file contains possible errors that were made in the process of writing, and which were noticed by assembly translator during translation process. Errors can be discovered in a "list" file as well. This file is more suitable though when program is big and viewing the 'list' file takes longer.

The third file is the most useful to programmer. Much information is contained in it, like information about positioning instructions and variables in memory, or error signalization.

Example of 'list' file for the program in this chapter follows. At the top of each page is stated information about the file name, date when it was translated, and page number. First column contains an address in program memory where a instruction from that row is placed. Second column contains a value of any variable defined by one of the directives : SET, EQU, VARIABLE, CONSTANT or CBLOCK. Third column is reserved for the form of a translated instruction which PIC is executing. The fourth column contains assembler instructions and programmer's comments. Possible errors will appear between rows following a line in which the error occured.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (16 of 19) [2/25/2002 3:02:09 PM]

Chapter 4 - Assembly Language Programming

Chapter 4 - Assembly Language Programming

At the end of the "list" file there is a table of symbols used in a program. Useful element of 'list' file is a graph of memory utilization. At the very end, there is an error statistic as well as the amount of remaining program memory.

Macros

Macros are a very useful element in assembly language. They could briefly be described as "user defined group of instructions which will enter assembler program where macro was called". It is possible to write a program even without using macros. But with their use written program is much more readable, especially if more programmers are working on the same program together. Macros have the same purpose as functions of higher program languages.

How to write them:

<label> macro [<argument1>,<argument2>,......<argumentN>]

........

.......

endm

From the way they were written, we could be seen that macros can accept arguments, too which is also very useful in programming. Whenever argument appears in the body of a macro, it will be replaced with the <argumentN> value.

Example:

The above example shows a macro whose purpose is to place on port B the ARG1 argument that was defined while macro was called. Its use in the program would be limited to writing one line: ON_PORTB 0xFF , and thus we would place value 0xFF on PORTB. In order to use a macro in the program, it is necessary to include macro file in the main program with instruction include "macro_name.inc". Contents of a macro is automatically copied onto a place where this instruction was written. This can be best seen in a previous list file where file with macros "bank.inc" was copied below the line #include"bank.inc"


Chapter 4 - Assembly Language Programming

Previous page

Table of contents

Chapter overview

webmaster.


Chapter 5 - MPLAB

Previous page

Table of contents

Chapter overview

Next page

CHAPTER 5

MPLAB

Introduction

5.1 Installing the MPLAB program package

5.2 Introduction to MPLAB

5.3 Choosing the development mode

5.4 Designing a project

5.5 Designing new assembler file

5.6 Writing a program

5.7 MPSIM simulator

5.8 Toolbar

Introduction

MPLAB is a Windows program package that makes writing and developing a program easier. It could best be described as developing environment for some standard program language that is intended for programming a PC computer. Some operations which were done from the instruction line with a large number of parameters until the discovery of IDE "Integrated Development Environment" are now made easier by using the MPLAB. Still, our tastes differ, so even today some programmers prefer the standard editors and compilers from instruction line. In any case, the written program is legible, and well documented help is also available.

5.1 Installing the program -MPLAB

Chapter 5 - MPLAB

MPLAB consists of several parts:

-Grouping the projects files into one project (Project Manager)

-Generating and processing a program (Text Editor)

-Simulator of the written program used for simulating program function on the microcontroller.

Besides these, there are support systems for Microchip products such as PICStart Plus and ICD (In Circuit Debugger). As this book does not cover these , they will be mentioned only as options.

Minimal computer requirements for staring the MPLAB are:

·PC compatible computer 486 or higher

·Microsoft Windows 3.1x or Windows 95 and new versions of the Windows operating system

·VGA graphic card

·8MB memory (32MB recommended)

·20MB space on hard disc

·Mouse

In order to start the MPLAB we need to install it first. Installing is a process of copying MPLAB files from the CD onto a hard disc of your computer. There is an option on each new window which helps you return to a previous one, so errors should not present a problem or become a stressful experience. Installment itself works much the same as installment of most Windows programs. First you get the Welcome screen, then you can choose the options followed by installment itself, and, at the end, you get the message which says your installed program is ready to start.

Steps for installing MPLAB:

1.Start-up the Microsoft Windows

2.Put the Microchip CD disc into CD ROM

3.Click on START in the bottom left corner of the screen and choose the RUN option

4.Click on BROWSE and select CD ROM drive of your computer.

5.Find directory called MPLAB on your CD ROM

6.Click on SETUP.EXE and then on OK .

7.Click again on OK in your RUN window

Installing begins after these seven steps. The following pictures explain the meaning of certain