Vous êtes sur la page 1sur 8

7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

HOME
TUTORIALS
ARDUINO TUTORIAL PIC TUTORIAL BEAGLEBONE TUTORIAL ESP8266 TUTORIAL RASPBERRY PI TUTORIAL
STM32 TUTORIAL
PROJECTS
ARDUINO PROJECTS PIC PROJECTS BEAGLEBONE PROJECTS ESP8266 PROJECTS RASPBERRY PI PROJECTS
STM32 PROJECTS
FEATURES REFERENCE
PRODUCT REVIEWS GUEST POST GENERAL PIC ASSEMBLY INSTRUCTION SET | MIDRANGE DEVICES
ABOUT
CONTACT US WRITE FOR US! ADVERTISE WITH US!

Mobile Device Management


Easy Digital Photo Printing Cloud Service Providers
Software

Desktop Computer Deals The Best Internet Browser Active Directory Security Tools

RECOMMENDED PRODUCT: An Introduction to PIC Assembly Search …

Language Programming SEARCH

ESP32 WiFi Camera Most people have nightmares about PIC assembly language
programming while some would say it's a waste of time. I've SOCIAL
experienced this both so I agree. PICs can be programmed
much easier using high-level languages like C and Basic.       
RECENT POSTS However, learning to code in assembly helps you learn more
about the microcontroller's internal hardware. This in turn
allows you to tailor-fit programs according to your intention. 1 Programming for Beginners
1.3" I2C OLED with Arduino and In embedded systems, where performance is critical and
ESP8266 code is hardware specific, this is very important. This
Last time, I
featured the 0.96” tutorial will help you start learning PIC assembly language 2 Mobile Device Management
OLED display and programming.
how …

3 Best VPN Services of 2019


Mobile Device Management
1 a 2 Programming for Beginners a
Software
Getting Started with WeMos D1 Mini
The WeMos D1
Mini is part of the 4 Create a Photo Slideshow
WeMos D1 …
Contents [show]

5 Easy Digital Photo Printing

Using the Grove Gesture Sensor Why Learn Assembly


Wouldn’t it be cool
if you could control 6 The Best Internet Browser
something by …
High-level languages often have additional headers that
take up program memory space. In microcontroller 7 Best EHR Software
programming, saving program space is essential as you
How to Use the MAX7219 Cascaded
only have a few to work on. Take this simple LED blink
Dot Matrix Module 8 Internet Browser Freeware
program written in XC8, for example:
With the 8 x 8 LEDs
in a dot matrix, …
1 #define _XTAL_FREQ 4000000       //4 MHz crystal
2 #include <xc.h>
3  
4 void main(void) {
5     
How to Use Lookup Tables in PIC
Assembly
6
7
    TRISB0 = 0;          //make RB0 output
    while(1){            //infinite loop POPULAR TUTORIALS
Programming a 8         RB0 = 1;         //set pin
microcontroller in 9         __delay_ms(65);  //65 millisecond delay between
assembly 10         RB0 = 0;         //clear pin Arduino PID Control
languages takes 11         __delay_ms(65); Tutorial In control
time and to … 12     } systems, a controller
13     return;
14 } corrects the output of...

https://www.teachmemicro.com/pic-assembly-language-intro/ 1/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

This code when compiled and assembled generates a 182- Intro to NodeMCU and
byte hex file. Now take this PIC assembly code which does The NodeMCU is a
the same thing as the code above: development board

featuring the popula...


1 ; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
2 #INCLUDE <P16F84A.INC> Creating a Simple
3 RES_VECT  CODE    0x0000            ; processor reset vector NodeMCU Web
4     GOTO    START                   ; go to beginning of pro Server We're done
5  
6 ; TODO ADD INTERRUPTS HERE IF USED with basics of the NodeMCU in
7   my last post...
8 CBLOCK 0x0C
9 COUNT1 How to Use L298N
10 COUNT2 Motor Driver You can
11 ENDC drive a LED on or off
12  
13 MAIN_PROG CODE                      ; let linker place main using a microcontroller l...
14   Raspberry Pi Serial
15 START
16     BSF STATUS, RP0
(UART) Tutorial The
17     MOVLW 0xFE serial port is a
18     MOVWF TRISB common way to “talk” to devices
19     BCF STATUS, RP0
20      an...
21 MAIN Interrupt Tutorial II -
22     BSF PORTB,0
23     CALL DELAY
Arduino Timer
24     BCF PORTB,0 Interrupt On my
25     CALL DELAY previous Arduino Interrupt tutorial, I
26     GOTO MAIN
showed how...
27     
28 DELAY NodeMCU Pinout
29     LOOP1 DECFSZ COUNT1, 1 Reference The
30     GOTO LOOP1
31     DECFSZ COUNT2,1 NodeMCU is an
32     GOTO LOOP1 Arduino-compatible board that
33     RETURN
feature...
34  
35     END HC-05 Bluetooth AT
Command List Test
The hex file for this code is 163 bytes. A 19 byte difference command Command
in microcontroller programming is already significant! Response Parameter A...

Our First PIC Assembly Language


Code
As an introduction to PIC assembly programming, we'll be
using PIC16F84A, a x14 architecture microcontroller by
Microchip. This is the chip we introduced in the previous
article. The PIC1684A, having only a few registers to work
with, is good for beginners to microcontroller programming.

1. MOBILE DEVICE MANAGEMENT SOFTWARE a

2. BEST VPN SERVICES OF 2019 a

3. CREATE A PHOTO SLIDESHOW a

4. PROGRAMMING FOR BEGINNERS a

5. EASY DIGITAL PHOTO PRINTING a

We'll take a look again at the assembly code posted above:

1 ; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR


2 #INCLUDE <P16F84A.INC>
3 RES_VECT  CODE    0x0000            ; processor reset vector
4     GOTO    START                   ; go to beginning of pro
5  
6 ; TODO ADD INTERRUPTS HERE IF USED
7  
8 CBLOCK 0x0C
9 COUNT1
https://www.teachmemicro.com/pic-assembly-language-intro/ 2/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials
10 COUNT2
11 ENDC
12  
13 MAIN_PROG CODE                      ; let linker place main
14  
15 START
16     BSF STATUS, RP0
17     MOVLW 0xFE
18     MOVWF TRISB
19     BCF STATUS, RP0
20     
21 MAIN
22     BSF PORTB,0
23     CALL DELAY
24     BCF PORTB,0
25     CALL DELAY
26     GOTO MAIN
27     
28 DELAY
29     LOOP1 DECFSZ COUNT1, 1
30     GOTO LOOP1
31     DECFSZ COUNT2,1
32     GOTO LOOP1
33     RETURN
34  
35     END

One thing you need to understand is that assembly code is


processed line by line. The speed at which is line is
processed is the microcontroller's instruction cycle. The
instruction cycle time is given as:

Where fosc is the oscillator clock frequency. This tells us


that the instruction cycle is dependent on the value of the
crystal oscillator you are using. A common crystal oscillator
value is 4 MHz so:

We'll go back to this later.

Save up to 50% on select Microchip Development Kits

1 Mobile Device Management a 5 The Best Internet Browse

2 Best VPN Services of 2019 a 6 Programming for Beginn

3 Create a Photo Slideshow a 7 Best EHR Software

4 Easy Digital Photo Printing a 8 Internet Browser Freewa

Dissecting the Code


In assembly programming, it is important to provide
comments on the code so that it is easier to trace.
Comments in assembly starts with a semicolon like this:

1 ;this is a comment

There are a number of comments on the assembly program


above.

The first significant line on the program is

1 #INCLUDE <P16F84A.INC>

https://www.teachmemicro.com/pic-assembly-language-intro/ 3/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

This tells the assembler to add the include file for this
particular microcontroller. Each microcontroller has their
own include file which can be found in C:\Program Files
(x86)\Microchip\MPASM Suite folder. The include file
makes it possible to call on the register's name rather than
its address.

Microcontroller programming in assembly focuses on


manipulating the registers. Here are the registers of the
PIC16F84A:

As we go along with this tutorial, we get to know the


function of each register shown.

The next signficant lines on the program is

1 RES_VECT CODE 0x0000        ;processor reset vector


2 GOTO START                  ;go to beginning of program

These gives the reset vector for the code. A reset vector
points to which part of the program is the first one to be
executed. Here, that part is the one with the label START.
Another vector used is the INT_VECT or interrupt vector.
This vector is discussed in the interrupt tutorial.

Open the P16F84A.INC file and check out its contents.


Make sure not to change anything inside it, unless you know
what you're doing.

CBLOCK
The lines,

1 CBLOCK 0x0C
2 COUNT1
3 COUNT2
4 ENDC

use the CBLOCK assembly operative. This assigns the


aliases COUNT1 and COUNT2 to the general purpose
registers with address 0Ch and 0Dh respectively. If you look
at the PIC16F84A's register file map above, these address
are part of SRAM and don't have specific names. It means
https://www.teachmemicro.com/pic-assembly-language-intro/ 4/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

we can use them any way we like. The COUNT1 and


COUNT2 registers will be used for our delay subroutine.

Changing Banks
The actual program execution starts on the line,

1 BSF STATUS, RP0

The command BSF stands for "bit set register f" where the
next statement is the register f and the bit associated with
that register. View the rest of the instruction set. For the
one above, the register f is the STATUS register:

The RP0 bit is called the Register Bank Select Bit. The
PIC16F84A groups its registers into two banks. Let's look at
the device's register map again:

Before you are able to manipulate a specific register, you


must first go to the bank where it is located. This is done
via clearing (make it zero) or setting (make it one) the RP0
bit. Note that some registers including the STATUS register
are located in both banks which means you don't need to
switch banks to access them.

TRIS Register
So why did we set the RP0 bit? That is because the next
lines,

1 MOVLW 0xFE
2 MOVWF TRISB

uses the register TRISB which is located at bank 1.

The operative MOVLW means "move literal value to W


register". The W register is a general purpose register which
acts as a temporary storage as you move values from
register to register.

https://www.teachmemicro.com/pic-assembly-language-intro/ 5/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

The operative MOVWF means "move the contents of W to


TRISB". This means that after these two lines, the TRISB
register now has the value 0xFE.

So why this value to TRISB? First we'll discuss the function


of the TRISB register.

TRIS is short for tri-state. This describes the characteristics


of the ports of the microcontroller meaning the a port can
be an (1) input, (2) output or (3) left hanging. The
PIC16F84A has two ports: PORTA and PORTB. This means
there are also two TRIS registers: TRISA and TRISB. A zero
in any TRIS bit makes the corresponding PORT bit an
output. Conversely, a one in any TRIS bit makes the
corresponding PORT bit an input.

For our example, the value of TRISB is 0xFE:

You see that only bit 0 is low or cleared. This corresponds


to PORTB.0 or RB0 becoming an output pin and the rest as
input pins. If for example you want to make PORTB.0 and
PORTB.1 output pins and the rest as input pins, then your
TRISB would be:

which is 0xFC in hexadecimal value.

The next line:

1 BCF STATUS, RP0

simply brings us back to bank 0 because the next lines:

1 MAIN
2    BSF PORTB, 0
3    CALL delay
4    BCF PORTB, 0
5    CALL delay
6    GOTO main

involves the use of the PORTB register which is located in


bank 0.

The op code BSF stands for "bit set f". The line BSF PORTB,
0 command sets bit zero of the PORTB register. Remember
that the PORTB register is the one tied to the physical RB
pins. So when PORTB.0 is set, the RB0 pin is high.

The line BCF PORTB, 0 does the opposite of the command


just discussed. This will make the RB0 pin low.

Delay Subroutine

In between the bsf and bcf commands is a call to the delay


subroutine which is found at the end part of the code:

1 DELAY
2 LOOP1 DECFSZ COUNT1, 1
3 GOTO LOOP1
4 DECFSZ COUNT2,1
5 GOTO LOOP1
6 RETURN

https://www.teachmemicro.com/pic-assembly-language-intro/ 6/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

Why is this routine needed? As mentioned above, each line


is processed at a speed of 1 microseconds for a 4 MHz
crystal oscillator. If there is no delay subroutine, the time
between RB0 being high and then low would just be 1
microsecond! The LED blinking will be too fast to see in real
time.

The DECFSZ operative, used in the delay subroutine, is


short for "decrement f skip if zero". It would decrement
COUNT1 until it reaches zero and then it will skip the next
line. If the COUNT1 is still not zero, the program will
process the loop as indicated by the "goto loop1"
instruction. The number "1" next to COUNT1 means the
result of the decrement is placed to COUNT1. The other
possible value here is "0" which will place the result of the
decrement to the W register.

By the way, COUNT1 has an initial value of 0xFF or 255. This


is the default value of unused registers. This means the
loop will continue for 255 instruction cycles!

Once COUNT1 reaches zero, it will exit the loop and go to


the DECFSZ COUNT2, 1 line. This will decrement COUNT2
but the you'll see that the next line points the program back
to loop1! This means COUNT1 will be decremented again
and again until it reaches zero. After that, COUNT2 (now
with a value of 254) will be decremented and the loop
continues.

All in all, the routine would take 255 x 255 instruction cycles
to process. This is equivalent to 65 ms of waiting time.

Summary
Our first PIC assembly language program can now be
summarized from top to bottom:

Point the label START as the rst line in the program (lines
3 and 4)
Register COUNT1 and COUNT2 as variables for the delay
subroutine(8 to 11)
Go to bank 1 and set RB0 as output and then go back to
bank 0 (lines 16 to 19)
Set RB0, wait for 65 ms (with the help of the delay
subroutine in lines 22 to 23)
and then clear it then wait for 65 ms again (lines 24 to 25)
Go back to the portion of the program with label "main",
effectively looping the code endlessly (line 26)
Start of delay subroutine (line 28)
End of program (line 35)

That's it! I suggest you try the above code and use the
circuit provided on this article. Also, check out all the
assembly instructions. Have fun coding in PIC assembly
language!

Next>>Input/Output with PIC Microcontrollers

https://www.teachmemicro.com/pic-assembly-language-intro/ 7/8
7/17/2019 An Introduction to PIC Assembly Language Programming | Microcontroller Tutorials

Related

1. Mobile Device Management Software

2. Best VPN Services of 2019

3. Create a Photo Slideshow

4. Programming for Beginners

5. Easy Digital Photo Printing

6. The Best Internet Browser

ads by media.net

CATEGORIES TAGS RECENT COMMENTS


Arduino Projects (10) accelerometer ajax apache asm breakout board camera Roland Pelayo on Arduino PID
Arduino Tutorial (57) esp8266 gprs gps gsm hall Control Tutorial
Teach Me Microcontrollers! ccp control system
BeagleBone Tutorial (16) e ect hc-sr04 hd44780 lcd humidity i2c IMU Bekie on Arduino PID Control
ESP8266 Projects (3) Tutorial
Arduino, PIC, Raspberry Pi, STM32,
Beaglebone Tutorials
ESP8266 Tutorial (14) Internet interrupt iot javascript LED linux gyro Ege C on Raspberry Pi Serial
Features (7) motion sensor motor mpu6050 mqtt oled pdf (UART) Tutorial
General (4) pic16f877a pinout pwm python schematic sensor David Fletcher on Arduino
All Rights Reserved 2019
PIC Projects (5) serial serial-to-usb sim800 spi temperature ttl-to-usb Compass with HMC5883L
PIC Tutorial (20) ubuntu continuous adc web server xc8 Magnetometer
Product Reviews (2) David Fletcher on Arduino
Raspberry Pi Projects (1) Compass with HMC5883L
Raspberry Pi Tutorial (14) Magnetometer
Reference (8)
STM32 Tutorial (6)

Developed by Dessign

https://www.teachmemicro.com/pic-assembly-language-intro/ 8/8

Vous aimerez peut-être aussi