Sunday, April 12, 2009

Learn PIC Assembly Language Part - 1

PIC 18F452 Architecture :

The PIC microcontroller employs RISC machine which has got 35 or 75 instructions depending upoon whather it is 16F or 18F series microcontroller. The PIC assembler has got all the features like that of a conventional assembler. The PIC assembler MPASM is integrated with MPLAB IDE.

For writing an assembly language program with PIC microcontroller, one need to go through the data sheet of the choosen microcontroller and should understand instruction set thoroughly. In this section I will explain how to write a working assembly language program. Even if you dont have a PIC microcontrollers and it programmer, still you can validate your program for correctness using MPLAB simulator MPSIM. The simulator MPSIM is available with MPLAB IDE.

The developer should study the register file map of the choosen device. You can find it from the device specific datasheet. As I have mentioned earlier in my blogs, a PIC microcontroller has got two types of regsisters.

1. General Purpose Register ( GPR)

2. Special Function Register (SFR)

Here I will explain the memory organization of 18F452 microcontroller. The 18F452 microcontroller has got 32 K Bytes of flash memory. This means that 18F452 microcontroller can store upto 16 K of single word instructions. More details about memory organisations can be found in my previous blogs.

PROGRAM MEMORY ORGANIZATION

The executable code will be stored in program memory made up of FLASH. This figure shows the program memory organization.












The program counter ( PC ) in PIC 18F series is 21 bit wide, which can address up to maximum location till 1 FFFFF H , but the onchip program memory is limited to 3FFF H, which comes out to be around 32 K Bytes as mentioned in the device data sheet of 18F452. On Reset, PC will point to location 0000 H. The developed should use a goto instruction at the reset vector to branch the control to the start of your program. Normally programs are writtern immediately after the high priority and low priority interrupt vector. In a PIC 18F microcontroller, there are 2 interrupt vectors – high priority interrupt vector and low priority interrupt vector at locations 0008 H and 0018 H respectively. But in a PIC 16F microcontroller, there is only one interrupt vector which is at location 0004 H.

So in 18 F series microcontrollers, we should write our program above the memory location 002A because we need 16 bytes of memory location starting 0018 H.
So our assembly language program should look like this :-

Example 1

list p = 18F452 ; Set processor type

#include ; Device specific file

org 00000 h ; RESET VECTOR

goto start

org 0002A H ; Start of your program

start :

; Your program should go here. My blogs will help you to write these programs with confidence

end

This is the template for a PIC assembly language program.

list p = 18F452

#include

You should include these processor specific .INC and list files in your program depending on your device. This you can find in the MPLAB directory. These files contains register definitions needed in your program.

For writing the program, you need to study the data memory organization also.The data memory is implemented as static RAM. Each register in the data memory has 12 bit address allowing upto 4096 bytes of data memory. But in PIC 18F452 controller has got only 1536 bytes of memory also, so it contains only 6 banks.The available address space is from 0x00 to 0x5FF plus 128 SFRs.





























The data memory is divided into as many as 16 banks which contains 256 bytes each. The lower 4 bits of the Bank Select Register ( BSR <3:0> ) selects which bank will be accessed. The upper 4 bits of the BSR is not implemented. The data memory contains special function registers ( SFRs) and General Purpose registers ( GPRs) . The SFRs are used for the control and status of the controllers and other peripheral functions while GPRs are used for data storage and scratch pad operations, in the user aplication. The SFR starts at the last location of bank 15 (0xFFF H ) and extend downwards. The entire data memory can be acessed directly and indirectly. Direct addressing requires the use of BSR register. Indirect addressing use 12 bit address value and can be used across banks.
To ensure that the commonly used registers( SFRs and selected GPRs ) can be accessed in a single cycle, regardless of the current BSR values, as access bank is implemented.
A segment of bank 0 and bank 15 comprises the access RAM. Access RAM can be choosen for operation by specifying a = 0, in the instruction. I will explain this in my next article.
Please read this article along with the datasheet of PIC18F452 for more clarity and understanding.

No comments: