Vous êtes sur la page 1sur 66

MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING

Part 1 The PIC18 Architecture

Overview of the PIC18 microcontroller families


The PIC18 microcontroller family provides PICmicro devices in 18- to 80-pin packages, that are both socket and software upwardly compatible to the PIC16 family.
The PIC18 family includes all the popular peripherals, such as MSSP, ESCI, CCP, flexible 8- and 16-bit timers, PSP, 10-bit ADC, WDT, POR and CAN 2.0B Active for the maximum flexible solution.

Most PIC18 devices will provide FLASH program memory in sizes from 8 to 128 Kbytes and data RAM from 256 to 4 Kbytes; operating from 2.0 to 5.5 volts, at speeds from DC to 48 MHz.

Optimized for high-level languages like ANSI C, the PIC18 family offers a highly flexible solution for complex embedded applications.

PIC18F4550 device overview


This family of devices offers the advantages of all PIC18 microcontrollers namely, high computational performance at an economical price with the addition of high-endurance, Enhanced Flash program memory. In addition to these features, the PIC18F2455/2550/4455/4550 family introduces design enhancements that make these microcontrollers a logical choice for many high-performance, power sensitive applications.

PIC18F4455/4550 (40/44-PIN) BLOCK DIAGRAM

An Introduction to Embedded Systems Architecture


The architecture of an embedded system is an abstraction of the embedded device. At the architectural level, the hardware and software components are instead represented as some composition of interacting elements.

Why Is the Architecture of an Embedded System Important?


Understand an embedded systems design or to resolve challenges faced when designing a new system. The most common of these challenges include: defining and capturing the design of a system cost limitations determining a system's integrity, such as reliability and safety working within the confines of available elemental functionality (i.e., processing power, memory, battery life, etc.) marketability and sellability deterministic requirements

Because it clearly outlines the requirements of the system, an architecture can act as a solid basis for analyzing and testing the quality of a device and its performance under various circumstances.

The various structures of an architecture can then be leveraged for designing future products with similar characteristics, thus allowing design knowledge to be reused, and leading to a decrease of future design and development costs.

Defining and understanding the architecture of an embedded system is an essential component of good system design
Every embedded system has an architecture. An architecture by definition is a set of representations of those elements and their relationships. before starting development, take control of the design by defining the architecture first.
Embedded architecture is a useful tool in understanding all of the major elements, why each component is there, and why the elements behave the way they do.

Even if the architectural structures are rough and informal, it is still better than nothing. As long as the architecture conveys in some way the critical components of a design and their relationships to each other, it can provide project members with key information about whether the device can meet its requirements, and how such a system can be constructed successfully.

The Embedded Systems Model


What the Embedded Systems Model indicates is that all embedded systems share one similarity at the highest level; that is, they all have at least one layer (hardware) or all layers (hardware, system software and application software) into which all components fall. The hardware layer contains all the major physical components located on an embedded board, whereas the system and application software layers contain all of the software located on and being processed by the embedded system.

Embedded Systems Model

The Pic File Register/Data RAM


The file register is read/write memory used by the CPU for data storage, and registers for internal use and functions. They are called data memory space to distinguish them from program (code) memory space. The data memory space in PIC is a read/write (static RAM) memory. In the PIC microcontroller literature, the data memory is also called the file register

Register File Concept

PIC18F Programming Model (1 of 2)

PIC18F Programming Model (2 of 2)

File Select Registers (FSR)

File Select Registers (FSR) Indirect Addressing

Stack and Table Pointers

Program Counter

Program Memory is Byte Addressable

a) WREG register
In the CPU, registers are used to store information temporarily. That information could be a byte of data to be processed, or an address pointing to the data to be fetched. The vast majority of PIC registers are 8-bit registers.

In the PIC there is only one data type: 8-bit. The range goes from the MSB (most significant bit) D7 to the LSB leastsignificant bit) D0. With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit chunks before it is processed.

The 8-bit WREG register is the most widely used register in the PIC micro controller. WREG stands for working register, as there is only one. The WREG register is the same as the accumulator in other microprocessors. The WREG register is used for all arithmetic and logic instructions.

To understand the use of the WREG register, we will show it in the context of two simple instructions: MOVE and ADD.

MOVLW Instruction
The MOVLW instruction moves 8-bit data into the WREG register. It has the following format: MOVLW K ;move literal value K into WREG

K is an 8-bit value that can range from 0-255 in decimal, or 00FF in hex. The L stands for literal, which means, literally, a number must be used. In other words, if we see the word literal in any instruction, we are dealing with an actual value that must be provided right there with the instruction.

Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG), which means "move a literal value to WREG, " the destination. The following instruction loads the WREG register with a literal value of 25H (i.e., 25 in hex).

MOVLW 25H

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

ADDLW Instruction
The ADDLW instruction has the following format: ADDLW K ; ADD literal value K to WREG

The ADD instruction tells the CPU to add the literal value K to register WREG and put the result back in the WREG register. Notice that in ADDLW, first comes the letter L (literal) and then the letter W (WREG), which means "add a literal value to WREG," the destination.

To add two numbers such as 25H and 34H, one can do the following: MOVLW 25H ADDLW 34H ; load 25H into WREG ; add value 34 to W (W = W + 34H)

Executing the above lines results in WREG = 59H (25H + 34H = 59H)

Literal value and WREG being fed to the PIC ALU

When programming the WREG register of the PIC microcontroller with a literal value, the following points should be noted:
Values can be loaded directly into the WREG register. There is no need for a preceding pound sign or dollar sign to indicate that a value is an immediate value as is the case with some other microcontrollers. If values 0 to F are moved into an 8-bit register such as WREG, the rest of the bits are assumed to be all zeros. For example, in "MOVLW 5H" the result will be WREG = 05H; that is, WREG = 00000101 in binary.

Moving a value larger than 255 (FF in hex) into the WREG register will truncate the upper byte and cause a warning in the .err file.

b) File register (SFRs and GPR)


The file register data RAM in PIC is divided into two sections: Special Function Registers (SFR) General-Purpose Registers (GPR)

SFRs (Special Function Registers)


The Special Function Registers (SFRs) are dedicated to specific functions such as ALU status, timers, serial communication, I/O ports, ADC, and so on. The function of each SFR is fixed by the CPU designer at the time of design because it is used for control of the microcontroller or peripheral.

The PIC SFRs are 8-bit registers. The number of locations in the file register set aside for SFR depends on the pin numbers and peripheral functions supported by that chip.

GPR ( General Purpose Registers)


Are the group of RAM locations in the file register that are used for data storage. Each location is 8-bit wide Larger than SFR Difficult to manage them by using Assembly language easier to handle them by p y C Compiler

SPECIAL FUNCTION REGISTER MAP

Note that there are two RAM columns in the chip information section of the Microchip web site. One refers to the generalpurpose registers' (GP RAM) size, and the other is the EEPROM size.

GP RAM (which constitutes most of the file register) must not be confused with the EEPROM data memory. The GPRs are used by the CPU for internal data storage, whereas the EEPROMs are considered as an add-on memory that one can also add externally to the chip.

File register and access bank in the PIC18


The file register of the PICI8 family can have a maximum of 4096 (4K) bytes. With 4096 bytes, the file register has addresses of OOO-FFFH. The file register in the PICI8 is divided into 256-byte banks. Therefore, we can have up to a maximum of 16 banks (16 x 256 = 4096).

The 256-byte access bank is divided into two equal sections of 128 bytes. These 128-byte sections are given to the generalpurpose registers and special function registers.

The 128 bytes from locations OOH to 7FH are set aside for general-purpose registers and are used for read/write storage, or what is normally called a scratch pad. These 128 locations of RAM are widely used for storing data and parameters by PIC 18 programmers and C compilers. Each location of this 128-byte RAM of general-purpose registers can be accessed directly by its address. The other 128 bytes of the access bank is used for SFRs. It has addresses of F80H to FFFH.

The PIC allows direct access to other locations in the file register for ALU and other operations. In this section we show the instructions using various locations of the file register. This is one of the most important sections in the book for mastering the topic of PIC Assembly language programming.

MOVWF instruction
the F stands for a location in the file register, while W means WREG The MOVWF instruction tells the CPU to move (in reality, copy) the source register of WREG to a destination in the file register (F).

After this instruction is executed, the location in the file register will have the same value as register WREG The location in the file register can be one of the SFRs or a location in the general purpose registers region.

MOVLW 55H MOVWF PORTB MOVWF PORTC MOVWF PORTD

;WREG ~ 55H ;copy WREG to Port B (Port B 55H) ;copy WREG to Port C (Port B 55H) ;copy WREG to Port D (Port B 55H)

c)Status Register
To indicate arithmetic conditions It is a 8-bit register

C, the carry flag


This flag is set whenever there is a carry out from the D7 bit. This flag bit is affected after an 8-bit addition or subtraction.

DC, the digital carry flag


If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set; otherwise, it is cleared. This flag bit is used by instructions that perform BCD (binary coded decimal) arithmetic. In some microprocessors this is called the AC flag (Auxiliary Carry flag).

Z, the zero flag


The zero flag reflects the result of an arithmetic or logic operation. If the result is zero, then Z = I. Therefore, Z = 0 if the result is not zero.

OV, the overflow flag


This flag is set whenever the result of a signed number operation is too large, causing the high-order bit to overflow into the sign bit. In general, the carry flag is used to detect errors in unsigned arithmetic operations while the overflow flag is used to detect errors in signed arithmetic operations.

The OV and N flag bits are used for the signed number arithmetic operations

N, the negative flag


flag reflects the result of an arithmetic operation. If the D7 bit of the result is zero, then N = 0 and the result is positive. If the D7 bit is one, then N = I and the result is negative. The negative and OV flag bits are used for the signed number arithmetic operations

d) Data format and directives


The PIC microcontroller has only one data type. It is 8 bits, and the size of each register is also 8 bits. It is the job of the programmer to break down data larger than 8 bits (00 to FFH, or 0 to 255 in decimal) to be processed by the CPU.

Data format representation


There are four ways to represent a byte of data in the PIC assembler. The numbers can be in hex, binary, decimal, or ASCII formats.

Hex numbers
There are four ways to show hex numbers: We can use h (or H) right after the number like this: MOVLW 99H Put Ox (or OX) in front of the number like this: MOVLW Ox99 Put nothing in front or back of the number like this: MOVLW 99

Put h in front of the number, but with single quotes around the number like this: MOVLW h' 99'

Binary numbers
MOVLW B'10011001' ;WREG = 10011001 or 99 in hex

Decimal numbers
MOVLW D'12' ;WREG = 00001100 or OC in hex MOVLW .12 ;WREG = 00001100 = OCH = 12

ASCII character
MOVLW A'2' ;WREG = 00110010 or 32 in hex MOVLW A9 ;WREG = 39H, which is hex number for ASCII '9' ADDLW A'l' ;WREG = 39H + 31H = 70H ; (31 hex is for ASCII '1')

Assembler directives
Directives (also called pseudo instructions) give directions to the assembler. For example, the MOVLW and ADDLW instructions are commands to the CPU, but EQU, ORG, and END are directives to the assembler.

EQU (equate)
This is used to define a constant value or a fixed address. The EQU directive does not set aside storage for a data item, but associates a constant number with a data or an address label so that when the label appears in the program, its constant will be substituted for the label. COUNT EQU Ox25; MOVLW COUNT ;WREG = 25H

SET
This directive is used to define a constant value or a fixed address. In this regard, the SET and EQU directives are identical. The only difference is the value assigned by the SET directive may be reassigned later.

ORG (origin)
The ORG directive is used to indicate the beginning of the address. It can be used for both code and data. The number that comes after ORG must be in hex.

END directive
Another important pseudocode is the END directive. This indicates to the assembler the end of he source (asm) file. The END directive is the last line of the IC program, meaning that anything after the END directive in the source code is ignored by the assembler.

LIST directive
Unlike ORG and END, which are used by all assemblers, the LIST directive is unique to the PIC assembler. It indicates to the assembler the specific PIC chip for which the program should be assembled. It is used as follows:

LIST P=18F458
The above tells the PIC assembler to assemble the program specifically for the PIC 18F458 microcontroller. We use LIST to state the target chip.

#include directive
The #include directive tells the PIC assembler to use the libraries associated with the specific chip for which we are compiling the program.

_config directive
The _ config directive tells the assembler the configuration bits for the targeted PIC chip.

radix directive
We can use the radix directive to indicate whether the numbering system is hexadecimal or decimal. The default is hex if we do not use the radix directive. If we use "radix dec", the default representation will change to decimal and any unformatted number will be interpreted as decimal rather than hex.

End of part 1, to be continued in part 2

Vous aimerez peut-être aussi