1

I have recently started using a PIC18F4550, and my first attempt at programming it in assembly was to make it blink one LED on a loop. However, I do not have very much experience at programming in this language, and so had to rely on other snippets of code to get me started. My code is below:

#include<p18f4550.inc>

CONFIG WDT = OFF
CONFIG MCLRE = ON
CONFIG DEBUG = ON
CONFIG LVP = OFF
CONFIG FOSC = INTOSCIO_EC

ORG 0


Delay1 res 2
Delay2 res 2


Start: 
        CLRF PORTB
        CLRF TRISB
        CLRF Delay1
        CLRF Delay2
    

MainLoop:
    BSF PORTB,1
    GOTO DelayA


DelayA:
    DECFSZ Delay1,1 
    GOTO DelayA
    BCF PORTB,1
    GOTO DelayB

DelayB: 
    DECFSZ Delay2,1
    GOTO DelayB
    GOTO MainLoop 

end

I cannot understand why it doesn't work, but think it may have something to do with the instruction cycle speed, which I believe is 1 MHz, causing the delays to be excessively short.

Any help greatly appreciated!

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
CJO
  • 11
  • 1
  • 2
  • At DelayA, you first call DECFSZ and then unconditionally(!) jump to Delay A. I would expect a conditional jump instead. Also note that before looping, you must initialize some kind of loop variable, otherwise the next call will either not loop at all (because the variable is already zero) or it will cause an underflow and then loop until it is zero again. I could be completely wrong though, since I don't know PIC assembly. BTW: It's also a bit strange that DelayA and DelayB are asymmetric. – doomster Jul 09 '13 at 10:36
  • @doomster You don't have to initialize a loop variable, you can implement a forever-loop in these microcontrollers – Iancovici Jul 09 '13 at 10:48
  • Sure, decfsz takes only a single clock cycle. So it can never delay more than 65536 microseconds. Quite hard to see with human eyes. Use a timer instead. Plenty of google hits from "pic using tmr0". – Hans Passant Jul 09 '13 at 12:39

1 Answers1

0

1 . Try setting the Tri-State of the pin you're toggling. You gotta indicate if it's input or output, in this case it's output.

Like

#define D0_1Tris B'11001111'

movlw   D0_1Tris    ; set direction so LEDs D0 & D1 are outputs
movwf   TRISA       ; all others are inputs (high-z)

2 . Make sure your A/D is configured as I/O digital

clrf ANSEL ; configure A/D I/O as digital (might need to clear comparators too, whichever peripheral is multiplexed with the I/O of choice)

3 . Also don't forget to use a reset vector

ORG 0x000 ; processor reset vector

nop         ; required by in circuit debugger

goto    Init            ; go to beginning of program

4 . Try at a slower clock rate to determine if it's because of a short delay. If it is a delay issue then implement a timer to make the toggling signal 50-50 duty cycle (note might have to change this to p18f4550, it's taken from pic16 example)

Iancovici
  • 5,574
  • 7
  • 39
  • 57