Microcontroller Programming in Assembly Language PART 3

November 16, 2008 by
Filed under: Pic Programing 

Microcontroller Programming in Assembly Language PART 3

In the previous two articles we discuss about how to create a MPLAB project and understood some basic features and instruction available in a PIC16F84A microcontroller. In this article I basically focus on how to write ‘if-then’ logic in assembly.  Below a sample program code is provided which uses simple ‘if-then’ logic. This will give you some idea about how to use it in microcontroller Programming.



#include<p16f84a.inc> ; This is the header file

org                00h ; Reset vector

main

bsf                STATUS,RP0 ; configure status bits to move into bank 1

movlw           b’00000001′ ; move a binary value into the working register

movwf          TRISB ; move the content of the working register into trisb

; above two instructions set RB0 as an input other PORTB pins output

bcf                STATUS,RP0 ; moves into bank 0

clrf                PORTB ; clears PORTB initially

loop_off ; a lable

btfss               PORTB,0 ; check the status of RB0 and wait untill it is set

goto               loop_off

bsf                  PORTB,1 ; make RB1 high when RB0 goes high

loop_on

btfsc               PORTB,0 ; check the status of RB0 and wait untill it is cleared

goto               loop_on

bcf                  PORTB,1 ; make RB1 low when RB0 goes low

goto               loop_off

end ; end of the program

The main function of this program is to test the input condition on RB0 and if RB0 is high it makes RB1 high otherwise if RB0 is low it makes RB1 low.

Most of the codes include in this program I have already discussed in my previous article. So therefore I will mainly concentrate on new commands come across in this program we will start from the very beginning one by one.

#include<p16f84a.inc> - refer my previous article

org     00h - refer my previous article

main - refer my previous article

bsf      STATUS,RP0 - refer my previous article

movlw     b’00000001′ - this command is somewhat new but it is very easy to understand. This means that, it assigns a binary value 00000001 to working register. This working register is temporary register and if we want to assign some value to a SFR, firstly the value is assigned to the working register and then that value is moved in to the particular SFR using ‘movwf’ command.

movwf          TRISB - now you know what is going to be happened in this command. The value in the working register will be moved in to the TRISB register. After that RB0 pin of the PORTB will be act as an input.

bcf     STATUS,RP0 - if you refer my previous article you know what is the purpose of this command. By using this command we jump in to Bank0 of the PIC.

clrf     PORTB - clears what ever in the PORTB initially.

loop_off - It is just a loop. refer my previous article

btfss    PORTB,0 - this is something new. This checks the value of the pin mentioned in the particular register and if the value is 0 then the next instruction is executed and if the value is 1 then the next instruction is discarded instead instruction after that will be executed. This might be confusing for you. Therefore we will take a look at the program and try to understand what happens in it.

In here it checks the status of the 0th pin. If the pin is in low stage ‘goto      loop_off’ command will be executed and if not instead of executing ‘goto             loop_off’ command it will be discarded and ‘bsf     PORTB,1′ will be executed.

goto     loop_off - refer my previous article

bsf       PORTB,1 - refer my previous article

loop_on - it is another label.

btfsc     PORTB,0 - this is the opposite of ‘btfss’ command and it will operate in the same way which ‘btfss’ command does if the particular pin is in the reset stage(0).


Rest is pretty obvious for anyone who went through these two articles.

<<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>>

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Comments

5 Comments on Microcontroller Programming in Assembly Language PART 3

  1. Jessicasaw on Mon, 11th May 2009 12:34 am
  2. I really liked this post. Can I copy it to my site? Thank you in advance.

  3. Kelly Brown on Fri, 12th Jun 2009 7:37 pm
  4. I really like your post. Does it copyright protected?

  5. abelehexa on Wed, 26th Aug 2009 10:59 am
  6. abelehexa…

    Gaia Layouts Maker

  7. isovukuzahy on Fri, 25th Sep 2009 8:14 pm
  8. isovukuzahy…

    effects lotrel side

  9. Kylie Batt on Fri, 16th Apr 2010 9:40 pm
  10. ?? ????? ?????? ?? ?? ?????. ? ??????. ??????? ??????? ???. ?????? ??? ? PM, ?????????….

    ???????? ?? ???????? In this article I basically focus on how to write ‘if-then’ logic in assembly.  Below a sample program code is provided which […….

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!