Embedded system (Chapter 2) part 2

34
MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING Noriah bt Mustafa POLITEKNIK SULTAN HAJI AHMAD SHAH CHAPTER 2 Part 2 EC501 EMBEDDED SYSTEM APPLICATIONS

Transcript of Embedded system (Chapter 2) part 2

Page 1: Embedded system (Chapter 2) part 2

MICROCONTROLLER

ARCHITECTURE &

ASSEMBLY LANGUAGE

PROGRAMMING

Noriah bt Mustafa

POLITEKNIK SULTAN HAJI AHMAD SHAH

CHAPTER 2 – Part 2

EC501 EMBEDDED SYSTEM APPLICATIONS

Page 2: Embedded system (Chapter 2) part 2

OUTCOMES

Know PIC Assembly Language fundamental

Page 3: Embedded system (Chapter 2) part 2

Overview

PIC - "Peripheral Interface Controller”

How to use PIC microcontroller?

Hardware

Software - programming

Programming language

Machine language – machine code

Assembly language

High level language C, C++, Basic

Low level

language

Page 4: Embedded system (Chapter 2) part 2

Introduction

Assembly language is programming language

use to write programs using instructions that are

the symbolic code called mnemonic.

Assembly language programs must to be

translated into machine code by a program

called assembler.

Page 5: Embedded system (Chapter 2) part 2

To program in assembly language, the

programmer must know all the registers of the

CPU and the size of each, as well as other

details.

Page 6: Embedded system (Chapter 2) part 2

Assembling and linking process

First use a text editor to type program in assembly language. In the case of PIC microcontrollers, we use the MPLAB IDE.

The “asm” source file is fed into PIC assembler. The assembler converts the instructions into machine code.

The third step is called linking. The link program takes one or more object files and produces a hex file, a list file, a map file, an intermediate abject file and a debug file.

After a successful link, the hex file is ready to be burned into PIC’s program ROM and is downloaded into PIC trainers.

Page 7: Embedded system (Chapter 2) part 2
Page 8: Embedded system (Chapter 2) part 2

Sample o a PIC Assembly Source Code (asm file)

Page 9: Embedded system (Chapter 2) part 2

Assembler Directives

While instruction tell the CPU what to do, directives (also called pseudo-instructions) give direction to the assembler.

Assembler directives are instruction used to tell the CPU what to do.

Examples of assembler directive : EQU – equate (use to define value or a fixed address) ORG – origin (use to indicate the beginning of the address for

code or data) END – indicate the end of the source (asm) file.

Page 10: Embedded system (Chapter 2) part 2

Review Questions

1. What is the purpose of pseudo-instructions?

2. _____________ are translated by the assembler into

machine code, whereas _____________are not.

3. True or false. Assembly language is a high- level

language.

4. Pseudo- instruction are also called __________.

5. True or false. Assembler directives are not used by the

CPU itself. /they are simply guide to the assembler.

6. Which one the following is an assembler directive?

a) MOVLW 25H b) ADDLW 12 c) ORG 200H d) GOTO HERE

Page 11: Embedded system (Chapter 2) part 2

Data Format Representation

The following are data type and data format using for PIC microcontrollers.

Data Type Data Format

Hexadecimal 99H

0X99

h’99’

99

Decimal D’12’

Binary B’10011001’

ASCII A’2’

Page 12: Embedded system (Chapter 2) part 2

PIC18F instruction set.

PIC18F2455/2550/4455/4550 devices

incorporate the standard set of 75 core instructions.

Page 13: Embedded system (Chapter 2) part 2

PIC18F

instruction

set.

Page 14: Embedded system (Chapter 2) part 2

PIC18F instruction set (cont.)

Page 15: Embedded system (Chapter 2) part 2

PIC18F instruction set (cont.)

Page 16: Embedded system (Chapter 2) part 2

MOVLW Instruction

MOVLW Instruction moves 8-bit data into WREG

register. It has the following format:

MOVLW K ;move literal value K into WREG

Example: Load the WREG register with value 25H.

MOVLW 25H ;move value 25H into WREG (WREG = 25H)

Operation: k → W

Page 17: Embedded system (Chapter 2) part 2

MOVWF Instruction

The MOVWF instruction tells the CPU to move (in reality,

copy) the source register of WREG to a destination

register in file register (F).

Example: Load value 66H into Port B, C and D.

MOVLW 66H ;move the value 66H to WREG (WREG = 66H)

MOVWF PORTB ; move the value in WREG to PORTB (PORTB = 66H)

MOVWF PORTC ;PORTC = 66H

MOVWF PORTD ;PORTD = 66H

MOVWF Move W to f Operation: (W) → f

Page 18: Embedded system (Chapter 2) part 2

Example

Write assembly instruction to make the I/O port as below :

a) RB4 and RB5 as input

b) PORTC as output

Solution:

a) MOVLW B’00110000’

MOVWF TRISB

b) MOVLW B’00110000’

MOVWF TRISB

Page 19: Embedded system (Chapter 2) part 2

Arithmetic Instruction

Instructions for performing 8-bit addition,

subtraction and multiplication operation.

Page 20: Embedded system (Chapter 2) part 2

Addition

These ADD instructions are design to perform 8-

bit addition.

The execution result of the ADD instruction will

affect all flag bits of the STATUS register.

Operation: (W) + k → W

Page 21: Embedded system (Chapter 2) part 2

Addition (cont..) Example

Write a program that add the hexadecimal numbers 0x20

and 0x30. Store the sum in data register at 0x50.

ORG 0X00

MOVLW 0X20

ADDLW 0X30

MOVWF 0X50,A

END

Page 22: Embedded system (Chapter 2) part 2

Example

Page 23: Embedded system (Chapter 2) part 2

Subtraction

• PIC18 microcontroller has two SUBTRACT

instructions. Subtract instruction will also affect all the

flag of STATUS register.

SUBLW Subtract W from Literal Operation: k – (W) → W

SUBWF Subtract W from f Operation: (f) – (W) → dest

Page 24: Embedded system (Chapter 2) part 2

Subtraction Example

Write a program to subtract 5 from 40H. Identify the flag bit in status register.

Solution:

#include <pic18f4550>

ORG 0X00

MOVLW D’5’

SUBLW 0X40

END

Page 25: Embedded system (Chapter 2) part 2

Logic Instruction

The logical instruction allow user to perform AND, OR,

exclusive-OR and complementing on 8-bit numbers.

Page 26: Embedded system (Chapter 2) part 2

Example

Example :

Write assembly statement for the operation 0XFF ||

0X55

MOVLW 0XFF ;move the value 0xFFin to WREG

IORLW 0X55 ;OR the value in WREG with value 0x55

Page 27: Embedded system (Chapter 2) part 2

Review Questions

Page 28: Embedded system (Chapter 2) part 2

Bit manipulation

The bit operations set, clear and toggle

test only a single bit. The bit oriented

instructions available to PIC family are

BCF, BSF, BTFSC, BTFSS and BTG.

BCF Bit Clear f

Operation: 0 → f<b>

BSF Bit Set f Operation: 1 → f<b>

Page 29: Embedded system (Chapter 2) part 2

Bit manipulation Example:

Write an assembly statement to make RB4 as

input an RC7 as output using bit addressable.

Solution:

BSF TRISB,4 ; set RB4 as input

BCF TRISC,7 ; make RC7 as output

Page 30: Embedded system (Chapter 2) part 2

Rotate Instruction

PIC 18 families has four rotate instructions. Figure 2.

Illustrates this four rotate instructions.

RLCF Rotate Left f through Carry

RLNCF Rotate Left f (No Carry)

RRCF Rotate Right f through Carry

RRNCF Rotate Right f (No Carry)

Page 31: Embedded system (Chapter 2) part 2

Rotate Example: RLCF

Page 32: Embedded system (Chapter 2) part 2

Data serialization

One of the most widely used applications of the

rotate instructions. Data serialization is a process of

sending a byte of data, one bit at a time through a

single pin of microcontroller.

The characteristic of data serialization are:

Using the serial port.

Using a programming technique to transfer data

one bit at a time and control the sequence of data

and spaces between them.

Page 33: Embedded system (Chapter 2) part 2

Data serialization Example

Write a program to bring in a byte of data serially via

pin RC7 and save it in file register location 0x21. The byte

comes in with the LSB first.

Solution:

RCNT EQU 0x20

MYREG EQU 0x21

BSF TRISC,7

MOVLW 0x8

MOVWF RCNT

AGAIN BTFSC PORTC 7

BSF STATUS,C

BTFSS PORTC,7

BCF STATUS,C

RRCF MYREG,F

DECF RCNT F

BNZ AGAIN

Page 34: Embedded system (Chapter 2) part 2

Review Questions