ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 489
Скачиваний: 0
GOTO = 10 1kkk kkkk kkkk
10 1 are the bits that define the GOTO instruction and the kkk kkkk kkkk are the eleven bits that define the new ROM address that the processor will begin executing from after this instruction executes.
If you do the math you will see that a number with 11 bits can range from 0 to
2047. This means that a GOTO instruction has enough information to let the processor jump to any ROM address from 0 to 2047. The 16F84A only has a ROM address space that ranges from 0 to 1023 so a GOTO instruction can let the processor jump to any code memory address in this device.
The assembler will look up the symbol table for the label name ‘start’ and return with the value 0.
Therefore the complete instruction becomes 10 1000 0000 0000 which is 2800h.
The final hex code assembled for this little program is 3004h 0086h 2800h.
This next line is another assembler directive and tells the assembler that this is the last line of the source file. Please don’t leave it out or the assembler will get cranky and generate an error.
end
It’s not hard to imagine now why the assembler will complain if you leave out a label name or use one that was spelled incorrectly.
After assembly the following shows what the listing file will look like.
MPASM 02.30 Released |
FIRST.ASM |
12-31-1999 17:26:38 |
||||
PAGE |
1 |
|||||
LOC |
OBJECT CODE |
LINE SOURCE TEXT |
||||
VALUE |
||||||
00001 |
Title |
"Simple Program" |
||||
00002 |
||||||
00003 |
list p=16F84A |
; processor type |
||||
00004 |
||||||
00005 |
;. |
|||||
00006 |
; |
|||||
00007 |
; ------------- |
|||||
00008 |
; PROGRAM START |
|||||
00009 |
; ------------- |
|||||
00010 |
; |
|||||
00000004 |
00011 |
TurnOnLed |
equ 0x04 ; value to turn on LED with RB2 |
|||
00000005 |
00012 |
PORTB |
equ 0x06 ; PORTB RAM address |
|||
00013 |
||||||
0000 |
00014 |
org 0h |
; startup address = 0000 |
|||
DIY K81 Project - Page 24
00015 |
||||||
0000 |
3004 |
00016 |
start |
movlw TurnOnLed |
; simple code |
|
0001 |
0086 |
00017 |
movwf PORTB |
|||
0002 |
2800 |
00018 |
goto start |
; do this loop forever |
||
00019 |
||||||
00020 |
||||||
00021 |
end |
|||||
MPASM |
02.30 Released |
FIRST.ASM |
12-31-1999 17:26:38 |
|||
PAGE |
2 |
|||||
Simple Program |
||||||
SYMBOL TABLE |
||||||
LABEL |
VALUE |
|||||
PortB |
00000006 |
|||||
TurnOnLed |
00000004 |
|||||
__16F84A |
00000001 |
|||||
start |
00000000 |
|||||
MEMORY USAGE MAP ('X' = Used, |
'-' = Unused) |
|||||
0000 |
: XXX------------- ---------------- ---------------- -------------- |
|||||
-- |
||||||
All other memory blocks unused.
Program Memory Words Used: 3
Program Memory Words Free: 1021
Errors |
: |
0 |
|||
Warnings : |
0 |
reported, |
0 |
suppressed |
|
Messages : |
0 |
reported, |
0 |
suppressed |
|
If you look through this listing you should be able to verify what has just been discussed.
On the far left of the listing, are the current ROM addresses and these increment as each instruction is added. You will notice that they start off at 0 because of the ORG 0h directive.
If a code line has an instruction on it, then next to the ROM address, you will see the 4 digit hex code that represents that instruction.
This is followed by the actual assembler code.
After the end directive you will see the Symbol Table contents, then how much of the processors memory was used, and finally, some information on errors that the assembler may have encountered.
The listing file is not used by a PIC programmer to program a device, they use the hex files. The list file can be used by us to verify how the code was created. Later in your programming life you may need to use this information.
DIY K81 Project - Page 25
If the name of your source file was called first.asm then the generated list file will be first.lst and the generated hex file will be first.hex.
These new files will be created in the same directory where first.asm is located and are simple text files that can be viewed with any text editor program such as Notepad.
After successful assembly, this is what the generated hex file looks like.
:0600000004308600002818
:00000001FF
This is the object code and has all the information that a programming device needs to write this program code to a chip. The default hex file generated by MPASM is a style called INHX8M, and this is how it is dissected.
:BBAAAATTLLHH....LLHHCC
BB This is a 2 digit value of the number of bytes on the line. 16 MAX
AAAA The starting address of this line of data.
TT |
A record type. Normally is 00, but will be 01 on the last line. |
LLHH A data word presented as low byte high byte format.
CC |
The checksum value of this line of data. |
:0600000004308600002818
:06 means 6 bytes of data on the line.
Our new code was 3 WORDS long which = 6 BYTES.
:0600000004308600002818
0000 means the bytes on this line are programmed starting from ROM address 0
:0600000004308600002818
00 means record type, but not on last line
:0600000004308600002818
04 30 86 00 00 28 are the 6 data bytes in low byte/ high byte format.
DIY K81 Project - Page 26
If you swap the bytes around and merge them into 3 words, you get
3004 0086 2800
This is the same as our code data.
:0600000004308600002818
18 is the checksum for all the data listed on this line. It is sometimes used by a programmer to make sure all the data on each line has not been corrupted when the programmer software read the data from a disk.
:00000001FF
The last hex line has 00 bytes listed and 01 in the record type which means it is the last line of the file.
MPLAB
If you haven’t installed MPLAB on your computer, please do so when convenient. It can be downloaded from the Microchip web site, but be warned - it is big. When this program is installed you will have access to the MPLAB Integrated Development Environment by Microchip.
If you have the package we are going to do our first assembly using this program. First off, make a short cut on the desktop to the MPLAB software so that you can start it easily.
Start up MPLAB and click on File - Open and select simple.asm from the DIY K81 software installation directory as the file to load. A window should appear with the source code listed.
Click on Options - Development mode and set the processor to 16F84A then make sure the MPLAB Sim - simulator item is checked and then press
Reset to close the window.
Now click on Project - Build Node and a dialogue box will appear showing the compiler configurations. Just use the default values shown and pressOK and the software will automatically run the assembler to assemble this code.
Easy isn’t it.
DIY K81 Project - Page 27
What ???? It created an error.
Oh my gosh, what now.
There should have been a Build Results window that popped up after assembly,
and in it you will have this text line...
Error[113] C:\DIYPROJECTS\SERIAL\SIMPLE.ASM 15 : Symbol not previously defined (PortB)
What this is telling you is thatPortB is not defined in the symbol table.
I’m guessing now that you are saying -Well yes it is...
Ahaaah!!! Look a little closer......
How is PortA defined??
PORTB equ 0x06 ; PortB address
Is PortB the same as PORTB.
Close the Build Results window.
Press [ALT F10]. This is the same as clicking on Project - Build Node.
Look at the Invoke Build Tool window that opened and find the option named Case Sensitivity. Do you see that it is checked. To the assembler, PortB is not the same as PORTB.
You can do one of two things here, uncheck this item and assemble, or go back and change the offending code line. For now, leave it checked and pressOK to start the assembly process again.
The same error line will appear. This time double click on the error line and this will open the code editor window with the cursor appearing on the offending line.
Change the line...
movwf PortB
to...
movwf PORTB
DIY K81 Project - Page 28
Now press [ALT F10] and OK to reassemble the code and this time no errors should be produced. Note that MPLAB always saves your simple.asm file before assembly takes place.
Click on Options - Development mode and make sure the MPLAM SIM - SIMULATOR function is checked, then press Reset to close the window. Click OK if a dialogue opens afterwards.
Now press [ALT - F10] again to reassemble.
Now you can click on the Step button on the tool bar at the top of the screen. This is the button with two feet on it. Each time you do this the code will execute one line at a time in a simulator window. You will notice that after this line executes...
goto start |
; do this loop forever |
...the processor jumps back to this line...
start |
movlw TurnOnLed |
; simple code |
...and continues indefinitely.
Press the Reset Processor button to exit this mode - the button with a chip and a step on it.
Click on the editor window and put a semicolon at the start of this code line like this.
; |
goto start |
; do this loop forever |
Now press [ALT F10] to reassemble and start stepping again.
Notice that after a few steps the processor has jumped into blank ROM space.
Remember the 0x3FFF values. This is because the GOTO instruction was not assembled with the code as the assembler now thinks it is a comment. Therefore there is no instruction there to make the loop operate as intended.
There are a lot of things you can do in MPLAB and it is quite a large and complicated program. You will not need to worry too much about all the functions it can do. Just learn them as you go and remember to look at the help files supplied.
Lets now look at the code that was supplied with the kit. This code flashes the
LED on PORTB pin 2 - or more simply RB2.
DIY K81 Project - Page 29
Close the simple.asm file in MPLAB and any other open windows. Now load the flash.asm file.
As you can see it is a bit more complicated than the simple.asm program.
There is a new __CONFIG directive which we will not worry about in this project and there are some new definitions and a bit more code.
You should still be able to see that the general layout of the code is the same.
To understand how to formulate code for a project it is sometimes useful to create a flow chart. Here is one that describes this project.
As you can see this representation makes it a lot easier to understand what is going on.
Just follow the arrowed lines to follow the code flow.
All we are doing is setting up the port pins so they work with the circuitry that will be connected to them. Then we simply turn the LED on, wait for 1/2 a second, turn the LED back off, wait for 1/2 a second and then continue the loop forever.
We still have our ORG statement at the start of the code to set the ROM address where the code will start from.
org 0h |
; startup address = 0000 |
The first thing to do is set up the ports quickly so that the pins are set up ready to control whatever circuit is connected to them.
All port pins are set as inputs when power is first applied to the chip.
In our project we need RB2 set as an output so that it can drive the LED connected to it. As you know there are also a lot more IO pins that are not going to be used. So what do we do with them. Well, we can’t simply ignore them because they don’t just vanish if we don’t write code for them.
If you only set RB2 as an output and leave the others set as an input you will create what are called Floating Inputs. This means they are trying to decide on a logic level to jump to because they are being controlled only by stray electrical charges around the circuit. This is undesirable because the internal pin circuits may be damaged and the chip may draw excess current.
DIY K81 Project - Page 30