Microcontroller Programming in Assembly language PART 2
Microcontroller Programming in Assembly language PART 2
In the previous article, ‘Microcontroller Programming in assembly language PART 1‘, I showed you the way to create a project using MPLAB. In this article I will show you the way to understand basic instructions used in writing assembly language for a PIC16F84A microcontroller using a sample program written in assembly. Though it is not a complicated program by any means, it will help you to understand the basics. Here is the sample program.
#include<p16f84a.inc> ; this is the header file
org 00h ; Reset vector
main
bsf STATUS,RP0 ; configure status bits to move into bank 1
clrf TRISB ; clear trisb register to make portb pins as outputs
bcf STATUS,RP0 ; moves into bank 0
loop ; a label
bsf PORTB,0 ; set RB0 pin high
goto loop ; make the program stay in a loop
end ; end of the program
This program is written to give a output from a RB0 pin of the PIC.
This is the program and we will try to understand what is the meaning of each code written in the program. To understand the program you have to have some idea about the DATA SHEET of the PIC16F84A microcontroller.
In the program you can see that in each line there some text written after a ‘ ; ‘ mark. That is the way to write a comment. In the first line ‘this is the header file’ text is a comment.
#include<p16f84a.inc> - this ‘.inc’ must include in the beginning of each program. This differs from PIC to PIC. In this context since we are using a PIC16F84A microcontroller ‘p16f84a.inc’ file has to be included and for a PIC16F877A microcontroller ‘p16f877a.inc’ file has to be inserted in the program. Likewise this ‘.inc’ will be different for different PICs.
org 00h - This command is called as the reset vector. There are memory spaces in a PIC and each memory location is given a unique address. These addresses are given hexadecimals. In this command you can see the term ‘00h’. ‘00′ implies the address of the memory location in hexadecimals and ‘h’ shows that the value is in hexadecimals. As it indicates, it is the zeroth location in the memory. You may know that the written program will be stored in the memory and this command instructs the program to go to the first location of the memory and to start executing the program from that location.
Main - this is only label. It is not a command. The use of a label will be discussed later.
bsf STATUS,RP0 - To understand this command you need to study the data sheet of the PIC. Every memory location has a certain number of bits. PIC16F84A has 8 bits in a single memory location. So it is a 8 bit memory. A single memory location in a memory is called as a ‘Register’. In every PIC there are certain registers called as ‘Special Function Registers’. As name implies these registers are to perform some special tasks. These registers are manufacture designed ones. If you refer the data sheet you can see what these registers are, what are the purpose of each register and how they are located in the memory etc. So ‘STATUS’ mentioned in the above command is also a SFR (Special Function Registers).
In the PIC16F84A, the whole memory is divided in to two sections and each section is called as a ‘bank’. The two banks are called as ‘bank0′ and ‘bank1′. From PIC to PIC, the number of banks available changes. As an example, in PIC16F877A there are 4 banks available. By referring the data sheet, the available registers in each bank can be found out. So, if someone wants to do a particular task on a certain register it must be accessed through the relevant bank which the register is located.
Selecting the bank is done by the above mentioned command. Bank will be selected by means of setting or clearing the RP0 bit of the STATUS register. To jump to bank0, RP0 has to be cleared and to jump to bank1, RP0 has to be set. If you consider the command ‘bsf STATUS,RP0′ , ‘bsf’ means set the bit and the rest of the command tells which bit to set. In this context, it sets the RP0 bit of the STATUS register. You can set any bit in any register by this command and only thing you have to change the name of the register and the bit. After the executing the command, it will be jumped to bank1. As usual more information about the instruction set and the registers can be found out from the data sheet.
clrf TRISB - In PIC16F84A, there are 13 pins available for input and output purposes. They are called as PORTA and PORTB. In PORTA, 8 pins are available and in PORTB, 5 are available. According to the need, we can either use those ports as inputs or outputs. This will be defined in the program.
For each port there is a separate register assigned in the memory. In here TRISA for PORTA and TRISB for PORTB. We can define which bits of the port to be output and which are to be input by simply setting and resetting the relevant bits in the corresponding register. Setting means ‘1′ and resetting means ‘0′. As an example think that we want to set the 1 pin of the PORTA to be an input and 2 pin of the PORTB to be an output. To do that you have to set the 1 bit of the TRISA register and reset the 2 bit of the TRISB register. Likewise you can define input and outputs.
In the above command, ‘clrf’ means clearing (setting to 0) all the bits of the mentioned register. In here the register is TRISB. So I hope you can understand the meaning of the command now. It will define all the pins of PORTB as outputs.
bcf STATUS,RP0 - in here ‘bcf’ means clearing/resetting the bit of the mentioned register. Here it will clear the RP0 bit of the STATUS register. Now you know by doing this what we are trying to do. After the execution of this command it will be jumped in to bank0.
You wonder why we need to jump in to bank0. As in the next step we want to set the RB0 pin. If you refer the data sheet you will see that PORTB is in the bank0. To set the RB0 pin of the PORTB it must be needed to be in the bank0.
Loop - like the ‘main’ label, this is also a label and the use of this label will be explained in the next step.
bsf PORTB,0 - understanding this command will not be anymore difficult to you if you have read upper part of the article well. This will set the 1 pin of the PORTB.
goto loop - this will show you why we use the ‘loop’ label. From this command program will jump to the location where ‘loop’ label stands. After the jump it will again execute the ‘bsf PORTB,0′ command and this will continue. It is no need to mention what will be the result after the execution of these two commands.
End - simply this is the end of the program as it implies.
So I hope that you may now have some rough idea about assembly programming.
<<Go to previous article Go to next article>>
Other related Articles
Microcontroller Programming in assembly language PART 1
Microcontroller Programming for beginners with PIC16F84A
Beginner guide to Microcontroller Programming
Parallel Port Programming in Visual Basic
Related E-Books downloads
Micro controllers in practice by Loan Susnea and Marian Mitescu
PIC in practice by David W. Smith
The PIC Microcontroller; Your Personal Introductory Course by John Morton
Programming and Customising the PIC Microcontroller by Myke Predko
Pick’n up the Pace - Microcontroller Application Guide by Walter G. Jung
Introduction to Microprocessors and Microcontrollers by John Crisp
PIC Microcontrollers Second Edition - An Introduction to Microelectronics by Martin P. Bates
Related Software Downloads>>
Download more e-books and software>>
Comments
8 Comments on Microcontroller Programming in Assembly language PART 2
-
Kautsar on
Thu, 13th Nov 2008 6:49 am
-
Admin on
Thu, 13th Nov 2008 7:35 am
-
Beginner guide to a PIC Programing | Promeganet on
Sat, 15th Nov 2008 11:20 pm
-
Easy way to make a PCB | Promeganet on
Sat, 15th Nov 2008 11:25 pm
-
Microcontroller Programming in assembly language | Promeganet on
Sun, 16th Nov 2008 8:28 am
-
alex on
Fri, 12th Dec 2008 7:25 pm
-
viagra on
Wed, 1st Jul 2009 1:36 pm
-
forwrders on
Wed, 6th Jan 2010 1:34 pm
Good blog, i love it. Keep posting man…
You are welcome Kautsar. Thanks for your comments.
[...] Microcontroller Programming in assembly language PART 2 [...]
[...] Microcontroller Programming in assembly language PART 2 [...]
[...] the previous two articles we discuss about how to create a MPLAB project and understood some basic features and instruction [...]
save to my Bookmarks
Thanks !… >-) ..
I’ve never been a great lover of trying to code in assembler or similar low level languages - too fiddly and time consuming. But this was a good tutorial and I have to say made it seem far more do-able than my old training led me to believe! Thanks.
Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!







