Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

what am i doing wrong?

Status
Not open for further replies.

hjl4

Member
Why is it that led on GP5 never turns on? I get led working on GP1, but after pressing momentary switch on GP3, the delay should count for about a minute, and turn GP5 on. Can anyone see whats wrong?









Code:
	list      p=12f629            ; list directive to define processor
	#include <p12f629.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT  






;***** VARIABLE DEFINITIONS
w_temp        EQU     0x20        ; variable used for context saving 
status_temp   EQU     0x21        ; variable used for context saving
d1            EQU     0x40
d2            EQU     0x41
d3            EQU     0x42


;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register





		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
                                  ;very small delay 5 micro seconds
        nop                   
        nop
        nop
        nop
        nop
        
                                  ;GPIO port setup

        bcf STATUS,RP0            ;set file register bank to 0
        clrf GPIO                 ;clear port
        movlw 07h                 ;set GP0,GP1 and GP2 
        movwf CMCON               ;turn comparator off
        bsf STATUS,RP0            ;set file register to bank 1
        movlw 1Ch                 ;move literal(value) 1C in hex or 00011100 in binary to working register.
        movwf TRISIO              ;move literal(value) in working register to TRISIO making GP2, GP3 and GP4 as inputs
                                  ;and leaving GP0,GP1, and GP5 as outputs.
        bcf STATUS,RP0            ;set file register bank to 0

start
     
       bsf GPIO,1                 ;set GP1 high
                                  ;LED on GP1 should be on.
                                  ;to confirm that microprocessor has setup all ports to input and outputs.
                                  


check   nop                       ;5us delay to allow PIC to settle.
        nop
        nop
        nop
        nop
        btfss GPIO,3              ;test GP3 if button on computer was pressed
        goto  check               ;if not then return to check again
        goto doublecheck          ;if set then goto doublecheck 
        
doublecheck
        nop
        nop
        nop
        nop
        nop    
        btfss GPIO,3              ;test GP3 if button on computer was pressed
        goto  check               ;if not then return to check again
        call Delay





ready   movlw 10h
        movwf 05                  ;make GP5 high turning HMR3000 on
        nop                       ;again small delay
        nop
        nop
        nop
        nop
        nop
        goto scanswitch


scanswitch
        btfss GPIO,3              ;scan GP3 for state change 1=high, 0=low
        goto scanswitch           ;if not set then do nothing but rescan button(switch).
        
        goto stop


stop    movlw 0h                  ;make  bit 5(GP5) low shuting down HMR3000
        movwf 05h
        nop                       ;again small delay
        nop
        nop
        nop
        nop
        nop
        goto check                ;after shutting down HMR3000 return to check
                                  ;for change of state on GP3.
        

Delay
			;1 minute delay
	movlw	0x23
	movwf	d1
	movlw	0xCB
	movwf	d2
	movlw	0x83
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return


; initialize eeprom locations

		ORG	0x2100
		DE	0x00, 0x01, 0x02, 0x03



		END                       ; directive 'end of program'
 
Code:
ready   movlw 10h
        movwf 05                  ;make GP5 high turning HMR3000 on

This is wrong

10h = 00010000 which sets GPIO4 not GPIO5
You need to use 20h

Either use;

Code:
ready   movlw 20h
        movwf GPIO                  ;make GP5 high turning HMR3000 on

or better still
Code:
        bsf GPIO,5

There may be other problems, I haven't looked any further into the code
 
I find doing it like this:
movlw b'00010000'
much easier to read and fault find than
movlw 10h
as you don't have to convert from hex to binary in your head to figure out what your changing.

I find 0x10 easier as I instantly know which bit it is. When I see b'00010000' I have to count the zeroes.

Mike.
 
I find 0x10 easier as I instantly know which bit it is. When I see b'00010000' I have to count the zeroes.

I would agree, but it's really just personal choice, and I've been using HEX for a LONG time.

Where I do tend to use binary is in bitmapped tables, where you can see the 'picture' in the ones and zeros.
 
I drill the decimal hex binary conversion into the students heads. I am convinced that it will prevent numerous wasted hours fixing problems due the the lack of this knowledge.

At first I had them study it and do timed tests in class. They did not improve. We turned it into a game and they loved it. I would write the numbers on the blackboard in one base and they would convert to the other two. They became so good at it that a single hesitation would knock them out of the running. In the end it came down to how fast they could write. We all need to be that quick.

In the material I write for the general public I tend to use binary because the untrained/inept can always count bit positions. :p


I find doing it like this:
movlw b'00010000'
much easier to read and fault find than
movlw 10h
as you don't have to convert from hex to binary in your head to figure out what your changing.
 
I would agree, but it's really just personal choice, and I've been using HEX for a LONG time.

Where I do tend to use binary is in bitmapped tables, where you can see the 'picture' in the ones and zeros.

If it is something like 6Eh do you still know by just looking at it? Is their an easy on the fly way of converting it that I'm missing? I know the textbook ways of doing the conversions, but counting 8 bits is quicker for me than converting it.... suppose if I was an old-timer I would be able to do it as well.. :D
 
If it is something like 6Eh do you still know by just looking at it? Is their an easy on the fly way of converting it that I'm missing? I know the textbook ways of doing the conversions, but counting 8 bits is quicker for me than converting it.... suppose if I was an old-timer I would be able to do it as well.. :D

I instantly know the bit pattern of any hex number. I can go from binary to hex and back again without thinking. I struggle doing decimal to hex/binary except for the easy ones like 200 = 0xc8.

Mike.
 
Try calling your delay after you turn on GPIO5.

Code:
doublecheck
		nop
		nop
		nop
		nop
		nop	 
		btfss	GPIO,3		;test GP3 if button on computer was pressed
		goto	check		;if not then return to check again
	[COLOR="blue"];	call	Delay   <<<remove[/COLOR]





ready		movlw	[COLOR="Red"]2[/COLOR]0h     [COLOR="Blue"];<<Change[/COLOR]
		movwf	05		;make GP5 high turning HMR3000 on
		[COLOR="Blue"]call	Delay   ;<<<Add[/COLOR]
		nop			;again small delay
		nop

Mike.
 
If it is something like 6Eh do you still know by just looking at it?
Yes. All your life you've been memorizing 10 numbers. It seems so obvious. So why is it such a problem to memorize 16 numbers? It just takes a little practice. The more you do it the easier it gets.

Is their an easy on the fly way of converting it that I'm missing
Convert a nybble (4-bits) at a time. With a bit of practice (programming) you can do it easily in your head. A hex number represents 4-bits per digit, and each digit (nybble) can represent any number from 0 to 15, or $0 to $f. So $6 = binary 0110. And $e = binary 1110. Put em together and they're 0b01101110.

If you have to think about it, count the binary nybble values from right to left like this: 1, 2, 4, 8. Add up the values of the set bits in your head. After a while you memorize it and just know without counting like that.
 
Last edited:
I instantly know the bit pattern of any hex number. I can go from binary to hex and back again without thinking. I struggle doing decimal to hex/binary except for the easy ones like 200 = 0xc8.

Mike.

240=0xF0, 224=0xE0, 192=0xC0, 128=0x80 ....

Decimal to hex/binary and the wonderful world of IP subnetting - welcome to my day job:)


Actually I like this, easier to see what you're loading and in this example easy to change the I/O line the LED is connected to throughout the code.

Code:
someLED equ 5             
                     movlw         1<<someLED
                     movwf         GPIO
; or
                     bsf             GPIO, someLED
 
Code:
	list      p=12f629            ; list directive to define processor
	#include <p12f629.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT  






;***** VARIABLE DEFINITIONS
w_temp        EQU     0x20        ; variable used for context saving 
status_temp   EQU     0x21        ; variable used for context saving
d1            EQU     0x40
d2            EQU     0x41
d3            EQU     0x42


;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register





		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main
		call    0x3FF             ; retrieve factory calibration value
		bsf     STATUS,RP0        ; set file register bank to 1 
		movwf   OSCCAL            ; update register with factory cal value 
		bcf     STATUS,RP0        ; set file register bank to 0
                                  ;very small delay 5 micro seconds
        nop                   
        nop
        nop
        nop
        nop
        
                                  ;GPIO port setup

        bcf STATUS,RP0            ;set file register bank to 0
        clrf GPIO                 ;clear port
        movlw 07h                 ;set GP0,GP1 and GP2 
        movwf CMCON               ;turn comparator off
        bsf STATUS,RP0            ;set file register to bank 1
        movlw 1Ch                 ;move literal(value) 1C in hex or 00011100 in binary to working register.
        movwf TRISIO              ;move literal(value) in working register to TRISIO making GP2, GP3 and GP4 as inputs
                                  ;and leaving GP0,GP1, and GP5 as outputs.
        bcf STATUS,RP0            ;set file register bank to 0

start
     
       bsf GPIO,1                 ;set GP1 high
                                  ;LED on GP1 should be on.
                                  ;to confirm that microprocessor has setup all ports to input and outputs.
                                  


check   nop                       ;5us delay to allow PIC to settle.
        nop
        nop
        nop
        nop
        btfss GPIO,3              ;test GP3 if button on computer was pressed
        goto  check               ;if not then return to check again
        goto doublecheck          ;if set then goto doublecheck 
        
doublecheck
        nop
        nop
        nop
        nop
        nop    
        btfss GPIO,3              ;test GP3 if button on computer was pressed
        goto  check               ;if not then return to check again
       





ready   movlw 20h
        movwf 05                  ;make GP5 high turning HMR3000 on
        call Delay
        nop                       ;again small delay
        nop
        nop
        nop
        nop
        nop
        goto scanswitch


scanswitch
        btfss GPIO,3              ;scan GP3 for state change 1=high, 0=low
        goto scanswitch           ;if not set then do nothing but rescan button(switch).
        
        goto stop


stop    movlw 0h                  ;make  bit 5(GP5) low shuting down HMR3000
        movwf 05h
        nop                       ;again small delay
        nop
        nop
        nop
        nop
        nop
        goto check                ;after shutting down HMR3000 return to check
                                  ;for change of state on GP3.
        

Delay
			;1 minute delay
	movlw	0x23
	movwf	d1
	movlw	0xCB
	movwf	d2
	movlw	0x83
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return


; initialize eeprom locations

		ORG	0x2100
		DE	0x00, 0x01, 0x02, 0x03



		END                       ; directive 'end of program'
 
What happens if you change your code from start onwards to,
Code:
start
			 
		bsf	GPIO,1		;set GP1 high
					;LED on GP1 should be on. 
					;to confirm that microprocessor has setup all ports to input and outputs. 
			 
check		bcf	GPIO,5		;turn off LED
		btfss	GPIO,3		;test GP3 if button on computer was pressed
		goto	check		;if not then return to check again
		
		bsf	GPIO,5		;turn on LED
		goto	check		;do it all again


		end

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top