Electronics non-working pic program

Discussion in 'Modding' started by c.cam108, 23 Aug 2004.

  1. c.cam108

    c.cam108 Minimodder

    Joined:
    14 Feb 2004
    Posts:
    907
    Likes Received:
    3
    I have recently bought a PIC programmer and a book on PICs (PIC Your Personal Introductory Course by John Mortom).

    The only problem is, when I tried to run the first program, it doesn't work. Basically, it should turn on an LED when a button is pressed, but it doesn't. I am really quite confused about this, because it is a _very_ simple program and the compiler gives 0 errors.

    The PIC I'm using is a PIC16F627 and here is the code:

    Code:
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;					;
    ; Written by: 	double_c		;
    ; Date:		23-08-04		;
    ; Version:	1.0			;
    ; Filename:	push button 1		;
    ; For device:	PIC16F627		;
    ; Frequency:	4MHz			;
    ;					;
    ; Description:	Turns on an LED if a	;
    ;		button is pressed.	;
    ;					;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    		list	P=16F627
    
    ;===============
    ; Declarations:
    
    porta	equ	05h
    portb	equ	06h
    portc	equ	07h
    
    	org	1FFh
    	goto	Start
    	org	0
    
    ;===============
    ; Subroutines:
    
    Init	clrf	porta		;Reset I/O pins
    	clrf	portb
    
    	movlw	b'0000'		;RA0: LED, RA1-3: N/C
    	tris	porta
    	movlw	b'00000001'	;RB0: BUTTON, RB1-7: N/C
    	tris	portb
    	retlw	0
    
    ;===============
    ;Program Start:
    
    Start	call	Init
    
    Main	btfss	porta,0		;tests the push button
    	goto	LEDoff
    	bsf	portb,0		;turns the LED on
    	goto	Main
    
    LEDoff	bcf	portb,0		;turns the LED off
    	goto	Main
    
    END
    Any help on what I'm doing wrong would be appreciated.

    Thanks,

    double_c
     
  2. Mark R

    Mark R What's a Dremel?

    Joined:
    29 May 2002
    Posts:
    158
    Likes Received:
    2
    Just looking at it, your reset vector is all wrong.

    Try changing this:

    org 1FFh
    goto Start
    org 0

    to:

    org 0
    goto Start

    Probably the easiest way to check that things are working is to step through it line by line in the debugger. If it behaves correctly in the debugger, then move onto programming the chip.
     
  3. ConKbot of Doom

    ConKbot of Doom Minimodder

    Joined:
    2 Jul 2003
    Posts:
    2,160
    Likes Received:
    6
    Why are you declaring ports A,B, and C? If you are using MPLAB than you don't have to define them this may be messing with things. Secondly, rather than using the tris command, you can directly address TRISx as a register. So you could "clrf TRISA" and change the second one to "movwf TRISB".

    Also you aren't changing banks. (maybe why you are using the tris command) but I find it easier to have a subroutine that changes the bank control bits, so you just "call Bank0" to change the bits.But since that is just for initialization you might as well just set and clear RP0 manually.
     
  4. c.cam108

    c.cam108 Minimodder

    Joined:
    14 Feb 2004
    Posts:
    907
    Likes Received:
    3
    right.....

    everything you said there just went right over my head :confused:

    i'll try changing the org0 thing

    btw, what debugger?
     
  5. Hazer

    Hazer In time,you too will be relixalated

    Joined:
    14 Apr 2003
    Posts:
    957
    Likes Received:
    2
    Code should work just fine. Only thing I noticed is that when you press the button, the code branches to turn the led off, then goes right back to main to turn it right back on. You have now created a 50% duty cycle PWM when you press the button. Basically, you never turn the LED off, just give it half the power (and may never notice it).
     
  6. c.cam108

    c.cam108 Minimodder

    Joined:
    14 Feb 2004
    Posts:
    907
    Likes Received:
    3
    actually, the btfss skips the next instruction if the port is set high :D

    yay!! i know something
     
  7. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    I notice you've not included your config word. Do you set the configuration bits at any time before programming?
     
  8. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    Code:
    ;===============
    ; Declarations:
    
    
    	org	0
    	goto	Init
    
    	org	0x04
    	RETFIE
    ;===============
    ; Subroutines:
    
    Init
    	clrf	status
    	clrf	intcon
    	clrf	porta		;Reset I/O pins
    	clrf	portb
    
    	BSF	STATUS, RP0 	; Bank 1
    	MOVLW	0xFF		; All inputs
    	MOVWF	TRISA		
    	CLRF	TRISB		; ALL ouputs
    
    ;===============
    ;Program Start:
    
    Main	btfss	porta,0		;tests the push button
    	   goto	LEDoff
    	bsf	portb,0		;turns the LED on
    	goto	Main
    
    LEDoff	bcf	portb,0		;turns the LED off
    	goto	Main
    
    END
    
    also try using the include directive to get the chip's definitions, and the config directive to get the correct config words set!
     
  9. Mark R

    Mark R What's a Dremel?

    Joined:
    29 May 2002
    Posts:
    158
    Likes Received:
    2
    Hmm. You probably want a
    Code:
    BCF	STATUS, RP0 	; Bank 0
    
    at the end of the Init routine. Otherwise your button check routine will just end up reading the TRIS registers.
     
  10. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    indeed.
     

Share This Page