Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I got to code the typical example of 8 LEDS turning ON and OFF, then all getting ON in a loop.

I got a PIC 18F4550, and coded this:

    List    P=18F4550               ;Microcontrolador a utilizar
    include <P18F4550.inc>          ;Definiciones de constantes


;**********   Palabra de conficuración  *******************************************
    CONFIG FOSC = INTOSC_EC ;INTOSC_EC          ; Internal oscillator                                
    CONFIG PWRT= ON             ; Power-up Timer Enable bit
    CONFIG BOR=OFF              ; Brown-out Reset disabled in hardware and software
    CONFIG WDT=OFF              ; WDT disabled
    CONFIG MCLRE=ON             ; MCLR pin enabled
    CONFIG PBADEN=OFF           ; PORTB<4:0> pins are configured as digital I/O
    CONFIG LVP=OFF              ; Single-Supply ICSP disabled
    CONFIG DEBUG = OFF                  ; Background debugger disabled
    CONFIG XINST = OFF          ; Extended Instruction disabled
;******************************Variables***********************************
    conta equ 0x00

;**********************************************************************************


    org     0x0000      
    movlw   0x62        
    movwf   OSCCON    
    clrf    TRISD      

CICLO   
    bcf PORTD,0         
    call RETARDO
    bsf PORTD,0       
    bcf PORTD,1
    CALL RETARDO

        bcf PORTD,1         
    call RETARDO
    bsf PORTD,1        
    bcf PORTD,2
    CALL RETARDO

        bcf PORTD,2         
    call RETARDO
    bsf PORTD,2      
    bcf PORTD,3
    CALL RETARDO    

        bcf PORTD,3        
    call RETARDO
    bsf PORTD,3       
    bcf PORTD,4
    CALL RETARDO    

        bcf PORTD,4        
    call RETARDO
    bsf PORTD,4      
    bcf PORTD,5
    CALL RETARDO        

        bcf PORTD,5         
    call RETARDO
    bsf PORTD,5       
    bcf PORTD,6
    CALL RETARDO        

        bcf PORTD,6        
    call RETARDO
    bsf PORTD,6       
    bcf PORTD,7
    CALL RETARDO    

        bcf PORTD,7       
    call RETARDO
    bsf PORTD,7   
        bcf PORTD,0
    CALL RETARDO        


bra  CICLO

RETARDO 
    movlw   .255  
    movwf   conta

    CICLO2
         DECFSZ   conta,F  

    bra  CICLO2

    nop
    nop
    nop
    nop



    return 


    end 

tWhen I simulate, That code is almost working. Sometimes they get turning ON and OFF one after another starting at first led, but sometimes the sequence leaps and some random LEDS get ON unexepectedly.

I tried to add this portion of code before bra CICLO to produce the whole leds ON effect, but it just made it behave the worst.

bcf PORTD,0
bcf PORTD,1
bcf PORTD,2
bcf PORTD,3
bcf PORTD,4
bcf PORTD,5
bcf PORTD,6
CALL RETARDO
bsf PORTD,1
bsf PORTD,2
bsf PORTD,3
bsf PORTD,4
bsf PORTD,5
bsf PORTD,6
bsf PORTD,7
CALL RETARDO

This is a picture of my simulation:

enter image description here

What am I doing wrong? How could I make sure, working at 4 MhZ that delays will be 1 second?

share|improve this question
    
Hello, Diego. I'm a reviewer and I've just rejected the edit you made to Akintoyeii's answer. It's nothing personal, it's just that the correct way to report what fixed your problem is for you to post your own answer (yes, you can answer your own question, if that makes sense) or you can post a comment to his answer complementing what else you had to do to solve your problem. Then, if he chooses to do so, he can edit his own answer. –  Ricardo Jun 9 at 1:29

1 Answer 1

Try writing to the respective LATDx registers use only PORTD for read operations

    bcf LATD,0

This should fix your problem

share|improve this answer
1  
@diegoaguilar I had to reject your edit as "too radical", even though the information is useful. In a situation like this, it's better to post it as a comment, rater than an edit. –  Nick Alexeev Jun 9 at 1:59
2  
Code, which was proposed in Diego's edit, is bcf LATD,x and bsf LATD,x –  Nick Alexeev Jun 9 at 2:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.