Saturday, 8 February 2014

Assembly language tutorial for PIC Micros

Why Assembly Language

Assembly programming language is normally developed for a specific machine architecture. This is unlike high level programming language whose programs can be ported to different machine architectures. Though C language is popularly used in most embedded systems design, but Assembly is still preferred by many programmers. Assembly has closer access to hardware resulting to more efficient code and low memory usage when compared to C.




Assembly language programming basics

Microchip basic microcontroller - PIC16F84A was adopted  to make necessary explanations in this tutorial. To get started with ASM language programming, first, the programmer need to know the microcontroller memory map and its instruction set. The PIC16F84A datasheet must be kept at hand before any PIC16F84A Assembly language programming can begin. The datasheet is very essential since all that need to be known with respect to PIC ASM language programming lies within it. The key to PIC Assembly language programming is understanding and knowing how to use the instruction set! Knowing how to use the PIC16F84A instruction set to make code makes it easy to program any other PIC microcontrollers.


A brief on the Instructions Set

In the PIC16F84A datasheet, the Instructions Set which are used to command the PIC to perform various operations are listed and described on page 36. The key operands of the instruction set are "f", "b" and "d". "f" and "b" specifies the register file (f) and bit (b) to be manipulated respectively, while "d" indicates the destination (d) to save the result of a given manipulation. The register file "f" for PIC16F84A ranges from 00h to 4Fh. The destination "d" can have values of 0 or 1. If d is 0, it means that result will be stored in Working register "W", but if d is 1, it insinuates that result of manipulation remains in the register file "f" that was manipulated. The operand "k" denotes an 8 bit constant value which can be any decimal value in the range of 0 to 255. To gain a better insight on the Instruction Set Summary, you may read section 7 of the datasheet starting from page 35.


Some light on the Memory Map

Before going further to start writing an Assembly language program for a given PIC, the programmer must know the register files of the PIC and their functions. The register file map can be seen from page 6 of the PIC16F84A datasheet. The registers are categorized into two sections - Special Function Registers (00h to 0Bh) and General Purpose Registers (0Ch to 4Fh). The Special Function Registers (SFR) has special functions that assist the programmer to configure and manipulate the PIC at wish. The General Purpose Registers (GPR) are volatile storage locations available to the programmer to actualise his program. Each of the data registers are also divided into two banks (0 and 1). Banking are implemented for a number of reasons. One of the reasons will be explained below.


Important Registers of the Memory map explained

To configure and program the PIC accordingly, requires the Special Function Registers (SFR) bits to be manipulated accordingly. The functions of each of the SFR bits is completely described starting from page 7 of the PIC16F84A datasheet. During the programming, the programmer have the chioce to either use the special function registers address (00h to 0Bh) or the names (INDF to INTCON) in his program. The most important SFR that must be used in any embedded programming are STATUS (03h), PORTA (05h), and PORTB (06h) registers. The STATUS (03h) register can be used to do a number of things depending on the bit that is set. By setting bit 5 (RPO) of STATUS (03h) register high, the programmer can migrate from bank 0 to bank 1 to setup the PIC ports as inputs or outputs. Once in bank 1, PORTA and PORTB bits can be set as inputs or outputs by assigning 1s or 0s respectively. After setting up the PIC ports, bit 5 (RPO) of the STATUS (03h) register is cleared to zero in order to return to bank 0 where the main programming is done. By default, the PIC is normally in bank 0. In bank 0, PORTA or PORTB can be set high (1) or low (0) to control external devices such as LED.

You may like to take a look at a Video on PIC ASM programming to digest what was explained in this tutorial.


Related blog post

Implementing delay in PIC Assembly program - PIC16F84A

No comments:

Post a Comment