1

I just started to familiarize myself with PIC assembler and trying out shifting bits and lighting LEDs accordingly.

I am using a Xpress DM164140 development board with MPLAB X IDE.

I have 4 LEDs on RA0-RA3. I want to light them one by one with shifting the R5 "user" register's value and assinging it to the PORTA register.

This is the assembly code I am using. Without the use of the DELAY subroutine all the 4 LEDs light up, but when I use the DELAY subroutine the RA3 will not light up. The shifting works until RA3.

So the problem must be in the DELAY subroutine somehow. Can't figure it out. I have also tried lslf and addwf instructions to try to implement the desired behaviour but no luck so far.

Does anyone have a clue why is this happening?

Thanks in advance!

    ; Assembly source line config statements

#include "p16f18855.inc"

; CONFIG1
; __config 0x3FEC
 __CONFIG _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT1 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
; CONFIG2
; __config 0x3FFF
 __CONFIG _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_ON & _STVREN_ON
; CONFIG3
; __config 0x3FFF
 __CONFIG _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_ON & _WDTCWS_WDTCWS_7 & _WDTCCS_SC
; CONFIG4
; __config 0x3FFF
 __CONFIG _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_ON
; CONFIG5
; __config 0x3FFF
 __CONFIG _CONFIG5, _CP_OFF & _CPD_OFF

 ;user define registers @ bank0   
R1 equ 0x20
R2 equ 0x21
R3 equ 0x22
R4 equ 0x23
R5 equ 0x24
R6 equ 0x25

RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

MAIN_PROG CODE                      ; let linker place main program

START
    CLRW
    MOVLB 0x11 ;select bank17 to change clock freq of HFINT1
    ;(high frequency internal oscillator)
    MOVLW 0x06
    MOVWF OSCFRQ    ;clock divider of 32 = 1MHz frequency

    MOVLB 0x00  ;move 0x00 to BSR aka select bank0, where TRISA reg is located     
    MOVLW 0xF0  
    MOVWF TRISA ;RA0-3 to output and RA4-RA7 to input
    MOVLW 0x01 ;00000001
    MOVWF R5
    MOVWF PORTA


MAIN
    CALL DELAY  ;delay to see the blinking
    bcf STATUS,C    ;clear carry bit
    rlf R5,1    ;shift contents of R5 by 1 to the left and save it to R5
    MOVF R5,0   ;move shifted R5 to W
    movwf PORTA ;move W to PORTA

    GOTO MAIN


;1 instruction cycles takes 4 clock cycles
;count to 250000@1MHz clock to get 1s delay
DELAY
    MOVLW 0x08  ;8
    MOVWF R1
DELAY_1
    MOVLW 0xFA  ;250
    MOVWF R2
DELAY_2
    MOVLW 0xFA  ;250
    MOVWF R3    
DELAY_3
    DECFSZ R3   ;decrease R2
    GOTO DELAY_3    
    DECFSZ R2   ;decrease R1
    GOTO DELAY_2
    DECFSZ R1   ;decrease R1
    GOTO DELAY_1
    RETURN

    END

2 Answers 2

3

As I can see you have enabled the watchdog timer. Probably the watchdog timer has reset the MicroController before the 4th LED had lightened up.

So you must clear the watchdog timer somewhere in the main loop.

Put the instruction...

CLRWDT

after the label MAIN.

Sign up to request clarification or add additional context in comments.

Comments

1

May be it's too late, but anyway. For outputs you must use LATx command. So replace both strings with PORTA to LATA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.