PIC16F877 Program & MPLAB

Mazaag

Joined Oct 23, 2004
255
Hey guys,
okay here is my first PIC program

Rich (BB code):
	list   p=16f877      ; list directive to define processor
#include <p16f877.inc>    ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word

;**********************************************************************
ORG   0      ; processor reset vector

bcf STATUS,RP1   ;
bsf STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1

movlw b'111100' ; Set RA0 and RA1 to output
movwf TRISA

bcf STATUS,RP1
bcf STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0

movlw b'000011'
movwf PORTA

end

It basically sets RA1 and RA0 to output, and sends a logicla 1 to those outputs, very very simple.

when I click on Assemble, i get the following warnings and messages

Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F877 "f877temp.asm" /l"f877temp.lst" /e"f877temp.err" /o"f877temp.o"
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 17 : Found opcode in column 1. (bcf)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 18 : Found opcode in column 1. (bsf)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 20 : Found opcode in column 1. (movlw)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Found opcode in column 1. (movwf)
Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Register in operand not in bank 0. Ensure that bank bits are correct.
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 23 : Found opcode in column 1. (bcf)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 24 : Found opcode in column 1. (bcf)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 26 : Found opcode in column 1. (movlw)
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 27 : Found opcode in column 1. (movwf)
Warning[205] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 30 : Found directive in column 1. (end)
BUILD SUCCEEDED: Sun Mar 19 10:44:16 2006

what is wrong with my code?

Secondly , how can I go through each line of code, step by step, and monitor the values of the STATUS register and the TRISA and PORTA registers ?

thanks guys

hgmjr

Joined Jan 28, 2005
9,029
Most assembly language compilers expect a label at the start of the new line. If a label is not used, it expects a space or spaces to precede the start of a opcode.

You should be able to elimnate the warning(203) by starting each new line with a space.

hgmjr

Mazaag

Joined Oct 23, 2004
255
Originally posted by hgmjr@Mar 19 2006, 12:11 PM
Most assembly language compilers expect a label at the start of the new line. If a label is not used, it expects a space or spaces to precede the start of a opcode.

You should be able to elimnate the warning(203) by starting each new line with a space.

hgmjr
[post=15185]Quoted post[/post]​

"Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Register in operand not in bank 0. Ensure that bank bits are correct.
"

Whats going on with that?

n9352527

Joined Oct 14, 2005
1,198
Originally posted by Mazaag@Mar 20 2006, 05:49 AM

"Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\CODE\F877TEMP.ASM 21 : Register in operand not in bank 0. Ensure that bank bits are correct.
"

Whats going on with that?
[post=15195]Quoted post[/post]​
MPLAB does that for every instructions that access registers in bank1 even when you have switched to bank1. It is just a annoying reminderin case you forget to switch the bank. There's an option to turn off that particular message or you can just ignore it.

Mazaag

Joined Oct 23, 2004
255
Originally posted by n9352527@Mar 20 2006, 07:20 AM
MPLAB does that for every instructions that access registers in bank1 even when you have switched to bank1. It is just a annoying reminderin case you forget to switch the bank. There's an option to turn off that particular message or you can just ignore it.
[post=15205]Quoted post[/post]​
Okay cool , i'll try to figure out how to disable that message.

So I plugged in my programmer, and set the com port adn everything. When I click on ENABLE PROGRAMMER , I get a message about the firmware.

"This device requires firmware
version 2.1.0 or higher"

How do I upgrade the firmware ? There are a few on Microchip website. However, I have no clue which one to download, and how to install it.

Mazaag

Joined Oct 23, 2004
255
Originally posted by Mazaag@Mar 20 2006, 11:55 AM
Okay cool , i'll try to figure out how to disable that message.

So I plugged in my programmer, and set the com port adn everything. When I click on ENABLE PROGRAMMER , I get a message about the firmware.

"This device requires firmware
version 2.1.0 or higher"

How do I upgrade the firmware ? There are a few on Microchip website. However, I have no clue which one to download, and how to install it.
[post=15210]Quoted post[/post]​
OKay guys this is driving me nuts.
I'm trying to debug my program and track register values.

I have debugged and traced these instructions, and it is working fine

Rich (BB code):
main
BCF STATUS,RP1  ;
BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1

MOVLW b'111100' ; Set RA0 and RA1 to output
MOVWF TRISA

;	BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0

;	MOVLW b'000011'
;	movwf PORTA

end

Now, in order for me to step the instructions , i have to save and build the project, which is fine, until I UNCOMMENT the next instructoin ( namely MOVLW b'000011' . when I try to build the project, I get a building failed error, with the following message :

Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00000000, length=0x0000000c
Errors : 1
"

I have no idea why its doing that, everything seemed to be working fine on the debugging with the first few instructions..

Need serious help guys.

Thanks

Hurdy

Joined Feb 27, 2006
137
You should take a look at www.microchip.com and go to their discussion forums. I use their forums for PIC help, the people there are pretty awesome at PIC.

Link to the forum is http://forum.microchip.com/

Microchip are the creators of MPLAB.

Papabravo

Joined Feb 24, 2006
14,378
Originally posted by Mazaag@Mar 20 2006, 02:10 PM
OKay guys this is driving me nuts.
I'm trying to debug my program and track register values.
[post=15214]Quoted post[/post]​
The linker allowed you to put four(4) instructions starting at location zero. The next instruction is going where the processor wants to go when there is an interrupt, which is to address 4. You can ignore any interrupts if they happen with the following code fragment

The solution:
Rich (BB code):
org 0
goto start
; Locations 1 through 3 are empty
org 4
CLRF INTCON  ; clear interrupt enable bits
RETFIE     ; GIE=1, but the rest are 0

start:
MOVLW b'00011'

Mazaag

Joined Oct 23, 2004
255
Originally posted by Papabravo@Mar 20 2006, 01:44 PM
The linker allowed you to put four(4) instructions starting at location zero. The next instruction is going where the processor wants to go when there is an interrupt, which is to address 4. You can ignore any interrupts if they happen with the following code fragment

The solution:
Rich (BB code):
org 0
goto start
; Locations 1 through 3 are empty
org 4
CLRF INTCON    ; clear interrupt enable bits
RETFIE        ; GIE=1, but the rest are 0

start:
MOVLW b'00011'
[post=15218]Quoted post[/post]​
Papa
I don't quite understand what you mean.

So the PIC will start at position 0, and start executing instructions up to and including the 4th instruction, why doesn't it keep going? is the 4th instruction resereved for interrupts? so we have to sorta like bypass them ? couldn't we start the code from org 5 striaght away? i'm not really sure I know what you mean.

anyway, i altered the code using the code fragment you posted, but it still isn't working, here's how the code looks like now:

Rich (BB code):
	list   p=16f877      ; list directive to define processor
#include <p16f877.inc>    ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF

ORG 0      ; processor reset vector
goto start

ORG 4

CLRF INTCON  ; clear interrupt enable bits
RETFIE    ; GIE=1, but the rest are 0

BCF STATUS,RP1  ;
BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1

MOVLW b'111100' ; Set RA0 and RA1 to output
MOVWF TRISA

start             BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0
MOVLW b'000011'
MOVWF PORTA

end

Mazaag

Joined Oct 23, 2004
255
Originally posted by Mazaag@Mar 20 2006, 02:06 PM
Papa
I don't quite understand what you mean.

So the PIC will start at position 0, and start executing instructions up to and including the 4th instruction, why doesn't it keep going? is the 4th instruction resereved for interrupts? so we have to sorta like bypass them ? couldn't we start the code from org 5 striaght away? i'm not really sure I know what you mean.

anyway, i altered the code using the code fragment you posted, but it still isn't working, here's how the code looks like now:

Rich (BB code):
	list      p=16f877          ; list directive to define processor
#include <p16f877.inc>      ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
ORG 0            ; processor reset vector
goto start

ORG 4

CLRF INTCON  ; clear interrupt enable bits
RETFIE        ; GIE=1, but the rest are 0

BCF STATUS,RP1    ;
BSF STATUS,RP0    ; RP1:RP0 = 01 Switch To Bank 1

MOVLW b'111100'; Set RA0 and RA1 to output
MOVWF TRISA

start                         BCF STATUS,RP0    ; RP1:RP0 = 00 Switch To Bank 0
MOVLW b'000011'
MOVWF PORTA
end
[post=15223]Quoted post[/post]​
okay guys i seemed to have fixed the problem myself

I let my program start at org 00005h, so now my code looks like this

Rich (BB code):
                org	00000h; Reset Vector
goto	start

org	00005h; Beginning of program EPROM

start         BCF STATUS,RP1  ;
BSF STATUS,RP0  ; RP1:RP0 = 01 Switch To Bank 1

MOVLW b'111100' ; Set RA0 and RA1 to output
MOVWF TRISA

BCF STATUS,RP0   ; RP1:RP0 = 00 Switch To Bank 0

MOVLW b'000011'
MOVWF PORTA

end

Mazaag

Joined Oct 23, 2004
255
Originally posted by Mazaag@Mar 20 2006, 02:17 PM
okay guys i seemed to have fixed the problem myself

I let my program start at org 00005h, so now my code looks like this
Rich (BB code):
                                org	00000h; Reset Vector
goto	start

org	00005h; Beginning of program EPROM

start                 BCF STATUS,RP1    ;
BSF STATUS,RP0    ; RP1:RP0 = 01 Switch To Bank 1

MOVLW b'111100'; Set RA0 and RA1 to output
MOVWF TRISA

BCF STATUS,RP0    ; RP1:RP0 = 00 Switch To Bank 0

MOVLW b'000011'
MOVWF PORTA
end
[post=15224]Quoted post[/post]​
Okay,
Now i'm debugging again and monitoring the registers, everythign works fine, but when i come to move the binary value to PORTA, the value at PORTA doesn't change !!! it worked with TRISA but not with PORTA!!! i'm oging insane guys

Papabravo

Joined Feb 24, 2006
14,378
Originally posted by Mazaag@Mar 20 2006, 03:23 PM
Okay,
Now i'm debugging again and monitoring the registers, everythign works fine, but when i come to move the binary value to PORTA, the value at PORTA doesn't change !!! it worked with TRISA but not with PORTA!!! i'm oging insane guys
[post=15225]Quoted post[/post]​
One thing at a time. There are two fixed addresses in the PIC architecture which are special. The first is address 0 and the other is address four. There are many possible ways to deal with this situation. The most common of which you have already discovered is to place a goto instruction at location zero to jump around location four.

When you execute the instruction to write to PORTA do any of the registers in the register file display turn RED? Is the number of the register that tunrs RED the same as the number of the register that corresponds to the PORTA data register?

Mazaag

Joined Oct 23, 2004
255
Originally posted by Papabravo@Mar 20 2006, 02:44 PM
One thing at a time. There are two fixed addresses in the PIC architecture which are special. The first is address 0 and the other is address four. There are many possible ways to deal with this situation. The most common of which you have already discovered is to place a goto instruction at location zero to jump around location four.

When you execute the instruction to write to PORTA do any of the registers in the register file display turn RED? Is the number of the register that tunrs RED the same as the number of the register that corresponds to the PORTA data register?
[post=15227]Quoted post[/post]​
Thats what was happening with the previous instructions, but not with this one. The PORTA data register doesn't turn red, thats why its driving me nuts.

Papabravo

Joined Feb 24, 2006
14,378
Originally posted by Mazaag@Mar 20 2006, 08:06 PM
Thats what was happening with the previous instructions, but not with this one. The PORTA data register doesn't turn red, thats why its driving me nuts.
[post=15235]Quoted post[/post]​
Look at the bits in the status register to confirm that RP1 and RP0 are both equal to zero.

If nothing new is turning RED then perhaps the data destined for PORTA is being written somewhere else, Like TRISA which has the same address but in a different bank.

PORTA is at address 05h in register bank 0
TRISA is at address 05h in register bank 1

Recheck the definition of the symbol PORTA

it should be something like

PORTA equ H'05'

As a last resort you could look at the listing file to see if you agree with the assembler.

MOVWF PORTA should look like 0805 in hexadecimal.

Mazaag

Joined Oct 23, 2004
255
Originally posted by Papabravo@Mar 20 2006, 07:53 PM
Look at the bits in the status register to confirm that RP1 and RP0 are both equal to zero.

If nothing new is turning RED then perhaps the data destined for PORTA is being written somewhere else, Like TRISA which has the same address but in a different bank.

PORTA is at address 05h in register bank 0
TRISA is at address 05h in register bank 1

Recheck the definition of the symbol PORTA

it should be something like

PORTA equ H'05'
As a last resort you could look at the listing file to see if you agree with the assembler.

MOVWF PORTA should look like 0805 in hexadecimal.
[post=15237]Quoted post[/post]​
Papa,
I even tried simply coding a program to write the binary value out to PORTA, and it still iddn't work, PORTA stays 000000.. I have no idea what to do

Papabravo

Joined Feb 24, 2006
14,378
Originally posted by Mazaag@Mar 21 2006, 03:47 PM
Papa,
I even tried simply coding a program to write the binary value out to PORTA, and it still iddn't work, PORTA stays 000000.. I have no idea what to do
[post=15260]Quoted post[/post]​
I have finally figured out what is going on. You really need to develop your skills at reading data sheets. In addition to being I/O pins, there are other functions which are multiplexed with PORTA. In the case of the PIC16F877 it is the A/D Converter. When you write to PORTA the data goes to the PORTA data latch. When you read from PORTA you are reading the state of the pins, which is not the same as the content of the PORTA data latch.

Enclosed is a zip file which modifies your program to correctly configure PORTA as a general purpose I/O. I copied this piece of code right out of the data sheet which describes the behavior of the PORTA pins. In a version of the PIC without an A/D converter your code would have worked. It also has a loop which writes the contents of W to PORTA, then PORTB, then VAR1 which is located in the register file at address H'20' and behaves like read write memory. Then W is incremented and the same three locations are written again.

When you step the simulator you should see PORTA change to the value of W, PORTB will remain unchanged because it is not configured as general purpose I/O, and finally location 20 and PORTA should have the same value.

RTFDS!
[attachmentid=1297]

kuttan

Joined Dec 11, 2008
1
sir please send some programs mentioned below