I'm trying to learn the basics of Assembly language with a PIC18F458. Using MPLAB IDE 8.92 in MPLABSIM mode.
Code:
#include <P18F458.inc>
List P=18F458
;
org 0x000
; this program adds value 3 to WREG ten times
COUNT EQU 0x25 ; use location 25H for counter
;
MOVLW d'10' ; WREG = 10 (decimal) for counter
MOVWF COUNT ; load the counter
MOVLW 0 ; WREG = 0
AGAIN ADDLW 3 ; add 03 to WREG (WREG = sum)
DECFSZ COUNT, F ; decrement counter, skip if count = 0
GOTO AGAIN ; repeat until count becomes 0
MOVWF PORTB ; send sum to PORTB SFR
;
end
I can watch WREG and see its result. I can watch COUNT and see its result. I know the final value should be 30 once decrementing and addition are done.
My question is why can't I see the final result in PORT B using a watch window? Is there some other method that would allow this?
Its because at power on all I/O pins are set to INputs by default.
You first need to set PORTB to Output and then it will run as intended.
However using W as the Sum is not a good practice as W is basically the systems work register and is constantly being changed; use a standard registers like count2 etc.
Okay, this is straight out of the textbook and they have not discussed PORT direction yet. Also they are showing WREG and how it functions at present. So this may not best programming methodology.
I have added "CLRF TRISB ; Make Port B output" statement per your example.
PORTB stays a constant 0x1E. I was expecting the B port to have its value flushed to say zero then a value of 30 / 1E once all the loops had completed.
Your program code just stops after the last instruction, normally it always continues in some form of loop.
I've added the CLRF PORTB so as you Single Step though the code, it will be set on as 30* then the next instruction clears it to zero, then the Goto 'loops' it back to near the beginning.
* in Sims Watch window if you select PORTB and right click you can change the display value to hex or decimal or binary.
Seems like the watch window retains the last values place in it.
Code:
#include <P18F458.inc>
List P=18F458
;
org 0x000
; this program adds value 3 to WREG ten times
COUNT EQU 0x25 ; use location 25H for counter
;
CLRF TRISB ; MAKE PORT b PINS OUTPUT
LOOP MOVLW d'10' ; WREG = 10 (decimal) for counter
MOVWF COUNT ; load the counter
MOVLW 0 ; WREG = 0
AGAIN ADDLW 3 ; add 03 to WREG (WREG = sum)
DECFSZ COUNT, F ; decrement counter, skip if count = 0
GOTO AGAIN ; repeat until count becomes 0
MOVWF PORTB ; send sum to PORTB SFR
CLRF PORTB
GOTO LOOP
end
Couple of things might help, though don't want to conflict with the course you are doing.
If you look in MPlabs folders you will find a Templates Folders showing a coding template for each pic chip so you have a bit of code to get you set up.
You will find one for the 458 but it will look a bit complicated so I 'cleaned' it up and incorperated it in to your code.
Why, because when you come to run your code on a chip, it must have the extra parameters shown to work properly.
Also I have shown the basic location of whats called the Interrupt routines, though it not something you will be using for a while its best if you know where they come in your code.
Thanks once again. I appreciate the help with the template. I have been looking at the P18F458.inc file and thought it was a complicated piece for me at this time. I knew there was help in there but did not fully understand the configuration bits that I would need.
I have read over the configuration bits in the datasheet but it is still not clear what is or is not needed. There is a lot to digest in there.
They key ones are OSC which determines what type of oscillator component is used, the Watchdog needs to be Off in not in use and for most programmers the LVP option also needs to be off.
You will find a lot of help in these links, the first one is by a regular guru of the forum .