Important Disclaimer

The purpose of this blog is purely to serve as a compilation of good technical material for my students. No financial or other motives are involved. Most of the content in this blog has been reproduced from other sources. I have made every attempt to mention the source link at the beginning of each blog. All readers are requested to kindly acknowledge that source and not this blog, in case you find the post helpful. However, I have not been able to trace the source links for some of my older posts. I wish to emphasize that this is not intentional and any help in this regard would be appreciated.

Dec 31, 2007

PIC Example programs

I am giving you some of the PIC programs which will help you understand the PIC programming better...Execute these using MPLAB and see the results

; Program to add two numbers
; By Binoy B. Nair, Lecturer EIE

#include p16f877a.inc
num equ 0X50
org 00h

a movlw 0x04
movwf num
movlw 0x01
addwf num
goto a
end

; Program to subtract two numbers
; By Binoy B. Nair, Lecturer EIE

#include p16f877a.inc
num equ 0X50
org 00h

a movlw 0x04
movwf num
movlw 0x01
subwf num,0
goto a
end

; Program to multiply two numbers num1=4, num2=3
; product is stored in num1
; By Binoy B. Nair, Lecturer EIE

#include p16f877a.inc
num1 equ 0X50
num2 equ 0x53
org 00h
start
movlw 0x03 ; loading 4-1=3 into num1
movwf num1
movlw 0x03
movwf num2
a addwf num2
decfsz num1,1
goto a
goto start
end

the next one is a little different


; Program to read from a port and write to a Port

list p=16F877A ; Selects the microcontroller

W equ 0x00 ; Variable to hold a constant '0'
F equ 0x01 ; Variable to hold a constant '1'
STATUS equ 0x03 ; Address of STATUS register
RP0 equ 0x05 ; Position of RP0 bit in STATUS register
ADCON1 equ 0x9F ; Address of ADCON1 register
PORTA equ 0x05 ; Address of PORTA register
TRISA equ 0x05 ; Address of TRISA register
PORTD equ 0x08 ; Address of PORTD register
TRISD equ 0x08 ; Address of TRISD register

org 0x0000

Main:
bsf STATUS, RP0 ; Select bank 1

movlw 0x06 ; WREG = 0x06
movwf ADCON1 ; ADCON1 = WREG, Configure PortA as
; digital I/O

movlw 0x00 ; WREG = 0x00
movwf TRISA ; TRISA = WREG, Port A is output

movlw 0xFF ; WREG = 0xFF
movwf TRISD ; TRISA = WREG, Port D is input

bcf STATUS, RP0 ; Select bank 0

main_loop:
movf PORTD, W ; WREG = PORTD, read Port D to WREG
movwf PORTA ; PORTA = WREG, Output WREG to Port A

goto main_loop ; Goto main_loop

end ; Physical end of program

No comments: