MPLAB Special Function Register Not Updating

Thread Starter

kf4knf

Joined Dec 26, 2010
2
Hi everyone,

I am new to the forum and this is my first day trying to program a PIC. I have created a small program to turn a few LEDs on and off. The program seems to compile without any errors but when I try and simulate it in MPLabSIM with Special Function Registers turned on, I dont see any changes to PORTA or PORTB. What am I doing wrong? Please understand, I am trying to get the actions down for now, I will add some delays at a later time to slow down the LED's flashing frequency.

Here is a copy of my code:

Rich (BB code):
; Program Functions:
 list P=16F690
 include "C:\Program Files\Microchip\MPASM Suite\P16F690.inc"
;============================
;Declarations:
porta equ  05h
portb equ  06h
portc equ  07h
  goto Start
;============================
;Subroutines:
Init clrf porta  ; Resets the input/output ports
  clrf portb
  clrf  portc
  movlw b'0000' ; Setting all a ports to output
  movwf porta
  movlw b'00000000' ; Setting all b ports to output
  movwf portb
  retlw 0
;============================
;Program Start:
Start call Init
Main bsf  porta,0  ;Turn on LED on porta
  bsf  portb,0  ;Turn on LED on portb
  bsf  porta,1     ;Turn off LED on porta
  bsf  portb,1     ;Turn off LED on portb
  goto Main  ;Loops back to Main
  END
 
Last edited by a moderator:

shteii01

Joined Feb 19, 2010
4,644
I am probably wrong, but, goto Start, I think that jumps you to Start and bypass the Subroutines all together, then Subroutines are never initialized.
 

Chris.M

Joined Dec 9, 2009
11
Hi everyone,

I am new to the forum and this is my first day trying to program a PIC. I have created a small program to turn a few LEDs on and off. The program seems to compile without any errors but when I try and simulate it in MPLabSIM with Special Function Registers turned on, I dont see any changes to PORTA or PORTB. What am I doing wrong? Please understand, I am trying to get the actions down for now, I will add some delays at a later time to slow down the LED's flashing frequency.

Here is a copy of my code:
There are a few problems within your code.


  1. To set the direction of a port, you need to set the TRISA/TRISB/TRISC registers.
  2. In your Main loop, your second set of BSF needs to be changed to BCF (Bit Clear F). BSF will turn on the LED, BCF will turn off the LED.
  3. In your Main loop, your second set of BSF (should be BCF as above) uses a different pin to the first set of BSF (ie, you turn on pin 0 of PORTA but turn off pin 1 of PORTA, same problem for PORTB)
  4. PORTB only has pins RB4/RB5/RB6/RB7 so using PORTB pin 1 will not work.
Here is some code which should work better:
Rich (BB code):
; Program Functions:
list P=16F690
include "P16F690.inc"
;============================
ORG    0x0000
GOTO    Start
;============================
;Subroutines:
Init
    ; Turn comparator1 off
    BANKSEL CM1CON0
    BCF    CM1CON0,C1ON
    
    ; Turn comparator2 off
    BANKSEL CM2CON0
    BCF    CM2CON0,C2ON
    
    ; Set PORTA pins as digital I/O
    BANKSEL    ANSEL
    CLRF ANSEL
    
    ; Set PORTB pins as digital I/O
    BANKSEL ANSELH
    CLRF    ANSELH
    
    ; Turn off all outputs
    BANKSEL    PORTA
    CLRF    PORTA
    BANKSEL    PORTB
    CLRF    PORTB
    BANKSEL    PORTC
    CLRF    PORTC
    
    ; Set all pins to output
    BANKSEL    TRISA
    CLRF    TRISA
    BANKSEL    TRISB
    CLRF    TRISB
    BANKSEL    TRISC
    CLRF    TRISC
    RETLW    0

;============================
;Program Start:
Start 
    CALL Init
Main 
    ; Turn on LEDs
    BANKSEL PORTA
    bsf    PORTA, 0
    BANKSEL PORTB
    bsf    PORTB, 4

    ; Turn off LEDs
    BANKSEL PORTA
    bcf    PORTA, 0
    BANKSEL PORTB
    bcf    PORTB, 4

    goto Main ;Loops back to Main
END
This code does not have any delay's so it will not show a flashing LED on a real circuit, but will show you the bits changing in MPLAB SIM. You need to step through the code to see the pins turn on and off.
 

Thread Starter

kf4knf

Joined Dec 26, 2010
2
Thanks Chris.M, those changes you mentioned got it working. I can see the TRISA and TRISB updating in the Special Function Registers now. Now can you or someone else explain what the BANKSEL actually does in simple english in this situation?
 

Markd77

Joined Sep 7, 2009
2,806
MPLAB is pretty good so if you don't see the port register changing there is a problem in the code. Check TRIS, CMCON and ANSEL registers in the datasheet and see if they need changing. Chris's code looks good.
You can use the template which should be found here which can be a help. It has the configuration word in it which your program seems to be missing (it's easier to declare it in the code).
C:\Program Files\Microchip\MPASM Suite\Template\code
 

thatoneguy

Joined Feb 19, 2009
6,359
If just starting out with PICs, I'd suggest a high level language, such as C (Boost C or MikroC for PIC16, C18 and others are added for the 18F Series).

You will get functional and small code on the first try, getting what you want done completed a bit quicker. Once you understand that the system works, you can tweak the .asm assembly language file created by the compiler for tweaks or improvements. Doing this teaches assembly fairly well at the same time as actually using the microcontrollers.

The 16F series relies on the separate banks, while the 18F series is somewhat optimized, specifically the C for programming language and compilers, with a bit more 'flat' addressing scheme for certain things, like memory access and stacks.
 

Chris.M

Joined Dec 9, 2009
11
Thanks Chris.M, those changes you mentioned got it working. I can see the TRISA and TRISB updating in the Special Function Registers now. Now can you or someone else explain what the BANKSEL actually does in simple english in this situation?
kf4knf, if you look at the documentation for you particular chip, you will see that the special function registers are located in different memory banks.

To change a particular register value, you need to first tell the microcontroller which bank the register belongs too, so it knows how to access it. Essentially the BANKSEL directive (ie it is not an assembly instruction, look up directives in the MPASM Assembler Help for more info), when compiled, generates some code to select the appropriate bank.

Therefore, when ever you need to access a register which is not in the current bank, you call BANKSEL to change banks.

You can pass a register name to BANKSEL, and it will change to the appropriate bank. To get a list of definitions that you can use for your microcontroller (eg, PORTA, TRISA, RP0, etc), look at the include file.

If just starting out with PICs, I'd suggest a high level language, such as C (Boost C or MikroC for PIC16, C18 and others are added for the 18F Series).

You will get functional and small code on the first try, getting what you want done completed a bit quicker. Once you understand that the system works, you can tweak the .asm assembly language file created by the compiler for tweaks or improvements. Doing this teaches assembly fairly well at the same time as actually using the microcontrollers.

The 16F series relies on the separate banks, while the 18F series is somewhat optimized, specifically the C for programming language and compilers, with a bit more 'flat' addressing scheme for certain things, like memory access and stacks.
As thatoneguy as stated, learning a higher level language helps remove some of this complexity.
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
Or using a PIC10F200, you don't need to worry about banks. I genuinely think it's pretty good to write the first few programs on.
By the way you don't have to write the bit in your declares section, PORTA, etc are all defined in the .inc file, but they are case sensitive so "PORTA" is not the same as "porta".
 
Top