Вы находитесь на странице: 1из 7

Assembly Language – Writing to

the Ports
Lecture No. 6
Example 1 – Writing to the Ports
• This Example Code in Assembly Language is Aimed at Switching On
and Off an LED Periodically
• On and Off Time is Fixed, termed as ‘Duration’
• The LED is Connected to Pin ‘0’ of Port A
• Since Port A is Multi Purpose Port – Need Special Handling
Background
• Registers of Interest
• PORTA – LSB PIN used to Control the LED
• TRISA – Control PORT A as Input or Output
• ADCON1 – To make PORTA as Digital I / O Port
• STATUS – Bank Bits Status
Example 1 - The Source Code
List p=16f877A ; list directive to define processor main
BANKSEL TRISA ;Select Bank 1
#include <p16f877A.inc> ; processor specific variable
MOVLW b'00000111'
definitions MOVWF ADCON1
MOVLW b'00000000'
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & MOVWF TRISA
_PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON &
_CPD_OFF & _DEBUG_ON BANKSEL PORTA ;Select Bank 0
MOVWF PORTA ;Clear PORT A
Begin_LED
ORG 0x000 ; processor reset vector
movlw Duration
nop movwf COUNT1 ; Delay Count1
goto main ; go to beginning of program movwf COUNT2 ; Delay Count2

#define Duration 0XFF LED_ON MOVLW 0x01


MOVWF PORTA ; Switch ON LED 1
COUNT1 equ 0x20
LoopON decfsz COUNT1,1 ;Inner Delay Loop
COUNT2 equ 0x21
goto LoopON
PORTA equ 0x05 movlw Duration
TRISA equ 0x85 movwf COUNT1 ; Reset Inner Delay Loop
ADCON1 equ 0x9F decfsz COUNT2,1 ; Outer Delay Loop
goto LoopON
movlw Duration
movwf COUNT2 ; Reset Outer Delay Loop
Example 1 - The Source Code
LED_OFF MOVLW 0x02
MOVWF PORTB ; Switch OFF LEDs

LoopOFF decfsz COUNT1,1


goto LoopOFF
movlw Duration
movwf COUNT1 ; Delay Count1

decfsz COUNT2,1
goto LoopOFF

goto Begin_LED

END ; directive 'end of program'


Assembler Input / Output Files
MpLab IDE
• Microcontroller remains the Heart of Embedded Systems
• Developing Embedded System Comprises:
• Selection of Hardware (Microcontroller)
• Firmware / Software running in Microcontroller
• We Need
• A development system for embedded controllers applications
• MpLab contains All Desired Components
• Target Selection
• Editor
• Assembler / Compiler
• Debugger
• Output Generations

Вам также может понравиться