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

Electronics 12f629 delay problem :/

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

  1. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Well I got the led working like I wanted by using a pulldown resister. Now I'm trying to learn how to use delay subs. Basicly I'm trying to have the led blink at one speed when GP1 = 0 and then at a different rate when GP1 = 1. Here is the code I have. I'm thinking the problem may be the i,j,k variables not being declared right? At this point the pic dosn't light the led (or the delay is realy realy long). Thanks in advance.

    PHP:
    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 VARIABES HERE
        i
        j
        k
        ENDC
    ;-----------------------

        
    ORG    0X000
        CALL    INIT
        
    GOTO    MAIN
        ORG    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
            MOVLW    .2

            BTFSC    GPIO
    ,1
            MOVLW    .50

            BSF        GPIO
    ,0
            CALL    dly
            
            BCF        GPIO
    ,0
            CALL    dly
            
            
    GOTO    MAIN

    dly
    :    movwf i        1   start Code that isnt mine
    dly1
    :     clrf j        1
    dly2
    :     clrf k        1
    dly3
    :     decf k,F    1
            btfss STATUS
    ,Z     1/2
            
    goto dly3    2
            decf j
    ,F    1
            btfss STATUS
    ,Z    1/2
            
    goto dly2    2
            decf i
    ,F    1
            btfss STATUS
    ,Z    1/2
            
    goto dly1    2
            
    return        ; 2


            END
     
  2. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    cblock is fine, 1 letter variable names are bad, but in asm its perfectly okay practice :D Now, make ur code clearer, indent on things like ORG, or CBLOCK ie

    Code:
    ORG 0x00
         CLRF INTCON
    
    same goes when you have control structures, ie, BTFSS or DECF, makes it easyer to read.

    so a simple re-write will be:

    PHP:
    dly:    movwf i        1   start Code that isnt mine
    dly1
    :     clrf j        1
    dly2
    :     clrf k        1
    dly3
    :     DECFSZ k,F    2
          
    goto dly3    (1remeber DECFSZ ALWAYS 2 cyclesit NOPs if it dosen't excecute, so count one of the GOTO ones there!
        DECFSZ j,F    ; 2
          goto dly2    ; 2 (1)
        DECFSZ i,F    ; 2
          goto dly1    ; 2 (1)
        return        ; 2 
    END
    now dly3 = 2 * FF
    dly2 = ( 1 + dly3) * FF
    dly1 = ( 1 + dly2) * i

    say for .50, we get = (( 1 + ((1 + ( 2 *FF)) * FF)) * .50
    so 6515300 cycles. which using intrc which has an instruction cycle of 1mhz is 6.5153 seconds.
     
  3. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Well it is working great now. Thanks for the help I'm slowly picking up on how to do this.
     
    Last edited: 9 Jun 2005
  4. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Oh and on a side note what is the shortest this loop could be and how would I set it to that?
     
  5. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    well to find out how short this code could be, we must use the same equations as above, i (the input passed in W) is only taken into acount in one place, and its times 0, but as 0 can't be passed (decfsz= decrement file register, skip if = 0), as it gets decremented before been tested, 0 becomes -1 (0xFF).

    so you can only have as low as 1, so ((1+((1+(2*FF)*FF))* 1;
    130306cycles, so 0.13036th of a second running of intrc.
     
  6. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Humm maby I just need to find out how to figure all the math up. The one thing I'm not sure about is how to figure out how many cycles I would need to waste at a set clock speed to = like 20us or some thing like that.
     
  7. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    metric prefixes always get me, so i always keep them as 10 to the power x much easyer using standard form.

    now, a PIC takes 4 cycles to do fetch execute, so instruction clock = actual clock / 4

    when intrc=4x10^6, that means the instruction clock is 1,000,000, that means one instruction takes 0.1x10^-6 (1us)

    so a clock thats a twenteth of the speed of 4mhz, will do great, 200khz would do this.

    But why not just make a simple delay function?

    theres nothing wrong with have
    Code:
    Delay17:
       GOTO $+1
       GOTO $+1
       GOTO $+1
       GOTO $+1
       GOTO $+1
       GOTO $+1
       GOTO $+1
       NOP
       RETURN
    
    so doing CALL DELAY17 will waste 19 cycles, now if you've got the memory space in your 12f629, this is a great way of not needing an external clock source. This way you get a 20us speed by using call delay17 after every instruction, of course branch is harder :)

    Some PICs you can change the speed of intrc, i don't think the 629 you can, but check the datasheet.

    Whats it your doing?
     
  8. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    At this point I'm still just learning about stuff with the 12f629. Figuring out timing, reading inputs, and learning the little bits of asm I wasn't sure about.

    This is what I have done with the 12f629 so far.

    1. blink and led (simple enough once I figured out the TRSIO and GPIO registers)
    2. turn the led on when I turn a switch on. This part was harder because I didn't use a pulldown resister in my original lay out so I was having issues with the pin floating.
    3. blink the led at a rate based on the position of a switch.

    Next project I plan to give a go is moving a servo to two different locations based on the switch. After I learn that I may work on geting my 16f88 set up with the max232 that I have and do some basic serial stuff (probably make the pic echo what is sent down the serial port from the pc).

    In the end the project that I bought all of this and am learning this for is to interface some dallas 18b20 temp sensors (preferably 10-15 at max) and have the pic poll the 1-wire net and then report the temps back to my windows box.
     
  9. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    ah ic, well the LED blinking with a 20us interval will be so tiny the human eye will just see it as been dimmer.

    its really easy with a RISC like this to think each instruction (running of intrc @ 4mhz) will take 1us, some instructions (jumps + branches) take 2 cycles, they always take two cycles, so its very easy to code with time in mind.

    if you make it so when the switch is pressed, the LED is on for 5us, then off for 100us, it will go quite dark, and you shouldn't see the flicker !
     
  10. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Ok so if I am geting this right 1us = 1/1000 of a second? If that is the case this wont be to hard to under stand.
     
  11. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    Google "metric prefixes"
     
  12. hcker2000

    hcker2000 What's a Dremel?

    Joined:
    21 Oct 2004
    Posts:
    109
    Likes Received:
    0
    Ok thanks I was thinking 1us was ula-second (which I must have made up in my mind). Ok just making sure I got this right 1us = 1 micro-second.
     
  13. uncle_mick

    uncle_mick What's a Dremel?

    Joined:
    5 Apr 2006
    Posts:
    1
    Likes Received:
    0
    I know this is an old post, but I thought I should point out, that 1 micro-second (1us) = 1/1,000,000.
    and 1 milli second (ms) = 1/1,000. It's so easy to confuse them when your a beginner.
    I hope this will help the beginners.
     

Share This Page