1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Electronics Reading I/O pins on 12f629?

Discussion in 'Modding' started by hcker2000, 2 Jun 2005.

  1. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    I have been playing around with this smaller pic that I got but it is still confusing the crap out of me.

    What I'm trying to do now is get it so when I press a switch on I/O port 1 it will light up an led on I/O port 0.

    Here is the code that I have so far. It kind of sort of almost works. If there is any thing else that you see that I am doing wrong please let me know.

    PHP:
        processor  12F629
        radix  DEC
        ORG 0x0000
        DATA 0184H

    Carry       EQU   0
    Zero_       EQU   2
    RP0         EQU   5
    GPIO        EQU   0x05        
    ;Defines the register for GPIO
    TRISIO      EQU   0x85        
    ;Defines the register for TRISIO
    CM0         EQU   0
    CM1         EQU   1
    CM2         EQU   2
    x           EQU   0x20
    y           EQU   0x22


        MOVLW 0x00
        BCF   0x03
    ,RP0
        MOVWF GPIO

        BSF   0x03
    ,RP0
        CLRF  TRISIO
        BCF   0x03
    ,RP0
        BSF   0x19
    ,CM2
        BSF   0x19
    ,CM1
        BSF   0x19
    ,CM0 

        CLRF GPIO

        MOVLW 0x02                
    ;Sets all pins to input exept I/O 1
        MOVWF TRISIO

        
    GOTO main

    main

        BTFSC GPIO
    ,0x01
        BCF GPIO
    ,0x00
        BSF GPIO
    ,0x00
        

        
    GOTO main

        END
     
  2. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    Try this code:

    Code:
    	list      p=12F629	; list directive to define processor
    	#include <p12F629.inc>	; processor specific variable definitions
    
    
    	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_ON & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT  
    
    ;----VARIABLES HERE-----
    CBLOCK	0X20
    
    ;PUT YOUR VARIABLES HERE
    
    ENDC
    ;-----------------------
    
    	ORG	0X000
    	CALL	INIT
    	GOTO	MAIN
    	0RG	0X004
    	RETFIE
    
    
    INIT	CLRF	GPIO
    	MOVLW	B'00000111'	;TURN OFF COMPARATOR
    	MOVWF	CMCON
    
    	BSF	STATUS,RP0
    	
    	MOVLW	B'00000010'	;SET ALL PINS TO OUTPUT EXCEPT GPIO,1
    	MOVWF	TRISIO
    	
    	BCF	STATUS,RP0
    
    	RETURN
    
    
    
    
    MAIN	BTFSS	GPIO,1		;GPIO,0 HIGH WHILE GPIO,1 INPUT IS HIGH
    	BCF	GPIO,0
    	BTFSC	GPIO,1
    	BSF	GPIO,0
    	GOTO	MAIN
    
    	END
    

    Notes on your code:
    I suggest you change your default radix to hex.
    Lines like: BSF GPIO,0x00
    Your addressing bits when using BSF, BCF etc, so don't put a hex number as the argument.

    You're setting up TRISIO while in Bank 0, but TRISIO is in Bank 1.

    Your switch routine is a bit dodgy - your output will always light up an LED, only in pressing the switch will change between on and partly on.
     
    Last edited: 2 Jun 2005
  3. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Well that code dose work alot better than the code I posted. It still keeps the led lit untell I/O 1 gose high.

    EDIT: I don't see where you are sending 0x01 to GPIO to make I/O 0 high?
     
    Last edited: 2 Jun 2005
  4. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    "0x01 to GPIO to make I/O 0 high" doesn't make sense - BCF and BSF mean Bit Clear File and Bit Set File respectively. You're only addressing one bit of the register, so for example BSF GPIO,4 turns on bit 4 of GPIO. (no hex values anywhere!)
     
  5. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Ok I get that. So why is the led lit all the time that the I/O 1 is low?
     
  6. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    How do you have your LED connected to the PIC? Between 5V and the I/O or between the I/O and 0V?
     
  7. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Between the I/O and the 0V.

    Wouldn't it hurt the pic to feed in 5v to the I/O pin?
     
  8. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Any one got any ideas?
     
  9. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    It works fine here - are you sure you have everything connected up correctly.
     
  10. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    yep at last as far as I know it is. Below is all I have hooked up sence I'm using internal osc.

    +5v to Vdd (pin 1) and ground to Vss (Pin 8)

    Led is hooked from pin 7 (GP0) to ground
    Switch is between Pin 6 (GP1) to ground

    EDIT:

    I forgot to say I'm compiling with mplab and then burning with IC-Prog 1.05D. The hardware programer is an PG2C from sparkfun.
     
    Last edited: 5 Jun 2005
  11. g0th

    g0th What's a Dremel?

    Joined:
    12 Apr 2005
    Posts:
    319
    Likes Received:
    1
    Maybe you need pull-ups, say 4k7, on the PIC's port pins.
     
  12. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Where would the pull up resistor go?

    Between the out put pin and the led? Or are you talking about puting it between the input ping and the switch?
     
  13. g0th

    g0th What's a Dremel?

    Joined:
    12 Apr 2005
    Posts:
    319
    Likes Received:
    1
    between the PIC pin and 5V
     
  14. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Thanks I will give this a try when I get home from work. I did do some reading on the pull up and pull down resisters.

    From what I read I may need to do this.

    +5V to switch to GP1
    GP1 to 10k resister to ground

    This will pull the pin low when the switch is off and then make the pin go high when I push the button right?
     
  15. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    Yes, you had it the wrong way round. You were pulling the pin low when pressing the switch so it was doing the opposite of what it should be doing. A 10k to ground would do, or you can leave the switch as it is, change the BSF and BCF round in the code and turn on weak pullups (WPU) on the PIC.
     
  16. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Any chance you know off hand how to turn the week pull ups on in the code?

    I figured at first that switching those two commands would fix it but little did I know that the pin was floating.
     
  17. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    if you look up the datasheet for the pic, you see on page 22, there is the WPU, Weak Pull-Up register, it controls the invidual pins pull up settings, but you must enable them all by setting GPPU bit in the OPTION_REG word.
     
  18. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Ahh I saw the WPU register but I missed the GPPU bit.

    Any way I decided not to go with the WPU register because using a pull down resister seems more logical to me.

    GP1 = 0 then GP0 = 0
    GP1 = 1 then GP0 = 1

    If I used the WPU it would be like this.

    GP1 = 1 then GP0 = 0
    GP1 = 0 then GP0 = 1

    I don't know the first set just seems much more logical to me. I guess that is programers preference. :thumb:

    I'm planing on writing a basic intro to electronics that I already know about. :clap:
     

Share This Page