1. 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.
    Dismiss Notice

Who is the MASTER of PIC ASM?

Discussion in 'Microcontrollers' started by Alharad, Jan 2, 2011.

  1. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Nevermind, figured that out too. See post #59 again for my questions, thanks.
     
  2. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    Radix pre-specifies to the assembler whether your literal values are in decimal (dec), hexadecimal (hex), or binary (bin). When in decimal radix, you can simply type the number without having to specify the radix (i.e. d'05', which would be decimal 5...you can just simply type "5" and the assembler will know that you mean decimal 5 or any other number you wish up to 255).

    When using the internal oscillator, bit 3 in the PCON register sets the internal oscillator frequency. Setting bit 3 in the PCON register sets it up for a nominal 4MHz while clearing bit 3 sets it up for a nominal 37kHz.

    For 4MHZ -

    Code (text):

            banksel     PCON        ;switch to bank 1
            bsf     PCON,3      ;set internal oscillator for 4MHz

     
    For 37kHz -

    Code (text):


            banksel     PCON        ;switch to bank 1
            bcf     PCON,3      ;set internal oscillator for 37kHz

     
    You would place this code prior to your port configuration routine (i.e. where you set TRIS A and B for I/O).

    Now this only applies when using the PIC's internal oscillator. When using a crystal, the crystal frequency dictates the oscillator frequency.

    You have to know how many clock cycles per instruction each instruction is as well as the instruction cycle clock. The instruction cycle clock is divide by 4...meaning that you take the oscillator frequency and divide it by 4 to get the instruction clock frequency. For a 4MHz clock this would mean that the instruction clock runs at 1MHz. At 1MHz, the PIC executes 1 instruction per microsecond.

    All instructions are single cycle instructions except for instructions which modify the program counter, which take 2 cycles to complete. Call, goto, and computed goto instructions all modify the program counter. This means every time you decrement your counter registers in your delay loop, then go back to decrement it again until the register = 0, this takes 3 instructions every time you decrement the counter registers. At 1 instruction per microsecond, this takes 3 microseconds to complete.

    If you're starting the decrement at the maximum value (i.e. 255), you would multiply 255 by 0.000003, or 3 microseconds to figure out how long it takes to decrement the register from 255 to 0, which would be 765 microseconds. If you make it decrement a second register every time it fully decrements the first one, you would multiply your .000765 seconds by another 255 to figure out how long it takes to complete the entire loop. This would give us a delay of 195 milliseconds, or 0.195 seconds. This is called a "nested delay loop".

    If you nest in yet a 3rd register, but start it at a lower number, you can get delays in the seconds range. Here's an example code that will get you close to a 1 second delay -

    Code (text):

    Delay_1s
                movlw       255     ;Pre-load delay counters with starting decriment value. Value 255 is the highest value you can
                movwf       Delay1      ;use on 8 bit processors. Decimal 255 can be noted as 0xFF or b'11111111'. For long delays, use
            movwf       Delay2      ;a nested delay loop as illustrated here.

            movlw       0x05
            movwf       Delay3
           
    Wait
                decfsz      Delay, F
                goto        Wait
            decfsz      Delay2,F
            goto        Wait
            decfsz      Delay3,F
                return
     
    At the top of your code you will need this to label the general purpose RAM locations -

    Code (text):

    Delay1      EQU     0x20
    Delay2      EQU     0x21
    Delay3      EQU     0x22
     
    You can also use -

    Code (text):

            cblock      0x20
                    Delay1
                    Delay2
                    Delay3
            endc
     
    This allows you to label multiple registers in one shot.

    Now...3 loops with those values assuming a 4MHz main clock, which would give us a 1MHz instruction clock -

    .000003 x 255 = .000765 Seconds - Delay 1
    .000765 x 255 = .195075 Seconds - Delay 2
    .195075 x 5 = .975375 Seconds - Delay 3

    Which would give us close to a 1 second delay.

    Can you take a screen shot of what you're seeing here and post it? Pull up the screen on your PC, press "Print Screen", then open up Microsoft Paint and paste it in there. Finally, save the pic as a JPEG, then post it up.
     
  3. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    1.
    2.You pick a crystal say 4 mhz divide that by 4 1000000 instructions per sec
    3.same as 2

    4. you don't need to include it in you project it's just listed at the top of your code
     
    Last edited: Jan 8, 2011
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA

    He's using the internal oscillator.
     
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    It's still the same divide by 4 gives instructions per sec just have to set it in configure INTOSC

    And seeing he had that right wasn't no need to bring that up
     
    Last edited: Jan 8, 2011
  7. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    pl give a example for 16f676 for 1 sec delay
     
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    This is for a 4mhz clock

    You can generate code from here http://www.piclist.com/techref/piclist/codegen/delay.htm
    Code (text):

    ; Delay = 1 seconds
    ; Clock frequency = 4 MHz

    ; Actual delay = 1 seconds = 1000000 cycles
    ; Error = 0 %

        cblock
        d1
        d2
        d3
        endc

                ;999997 cycles
        movlw   0x08
        movwf   d1
        movlw   0x2F
        movwf   d2
        movlw   0x03
        movwf   d3
    Delay_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay_0

                ;3 cycles
        goto    $+1
        nop
     
     
    Last edited: Jan 12, 2011
  9. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    what's changing with 8 MHz clock ?
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Here look it over
    Code (text):

    ; Delay = 1 seconds
    ; Clock frequency = 8 MHz

    ; Actual delay = 1 seconds = 2000000 cycles
    ; Error = 0 %

        cblock
        d1
        d2
        d3
        endc

    Delay
                ;1999996 cycles
        movlw   0x11
        movwf   d1
        movlw   0x5D
        movwf   d2
        movlw   0x05
        movwf   d3
    Delay_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay_0

                ;4 cycles (including call)
        return
     
     
  11. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Hi out there! I just blinked an LED successfully thanks to all your help!! :)

    I'm going to make cool patterns with more LEDs now.

    Ohh, if anyone asks, I am the Master of PIC ASM! :D
     

Share This Page