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. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    ONLINE
    Alharad I posted you 3 of them even how to make you a board that lets you run the code.

    And the link to where to down load the lessons for the LPC but before you get your hopes up you don't have the same chips
    that are on the LPC board so you'll have to change the code to match your chips
     
  2. Mosaic

    Mosaic Well-Known Member

    Joined:
    Jun 3, 2010
    Messages:
    2,611
    Likes:
    129
    Location:
    Caribbean
  3. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Hello,

    I will read some of the tutorials and make the board while I'm waiting for the PICs to come in the mail.

    if i have questions, I will let you all know. :)

    thank you!
     
  4. dave

    Dave New Member

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


     
  5. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0

    Awesome! a few PIC12f648's has come in the mail today, so I'm ready to get started.

    I will let you know how my first program goes!!
     
  6. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    Thanks! As far as writing a book I honestly don't think I'm quite qualified for that yet lol. I'm actually a beginner myself when it comes to microcontrollers, PICs and assembly language. However, I've been doing electronics for almost 20 years so I understood what the hardware did. I just needed to learn the language and figure out how to bridge the gap between hardware and software.

    I dabble mainly in MIDI stuff as I am a guitarist who's been doing musical electronics for 13 years.

    However, the one thing I caught onto in PIC asm is that the instructions are merely just abbreviations for an instructional sentence. Just like how I explained that "movwf" means "move contents of the w register into the file address/label that proceeds this instruction". Most if not all PIC instructions are merely just abbreviations for an instructional sentence. That made it easy to grasp PIC assembly for me.
     
    Last edited: Jan 7, 2011
  7. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    I'm not sure i understand this

    http://www.mstracey.btinternet.co.uk/pictutorial/progtut2.htm

    so we set bit 5 of TRISB only to the bank 1? the other bits of TRISB are left in bank 0? That would be confusing to remember all of them!!! Am I looking at this wrong?
     
    Last edited: Jan 8, 2011
  8. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    No you set bit 5 of the STATUS SFR, which resides at address 0x03, to switch banks. Once you set bit 5 of STATUS, you can then address all 8 bits of TRISA, TRISB, and all other SFR's that reside in bank 1. Then you must clear bit 5 in the STATUS register to switch to bank 0 in order to address the registers which reside in bank 0.
     
    Last edited: Jan 8, 2011
  9. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Ok, so I set bit 5 of STATUS SFR. i looked in the datasheet and I see that bit 5 '01' will set bank 1.
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    ONLINE
    Just use banksel and for get all that bank switching stuff

    Then it's a simple
    Code (text):

    banksel TRISB           ;Since this register is in bank 1,
                              ;not default bank 0, banksel is
                              ;used to ensure bank bits are correct.
      clrf    TRISB           ;Clear TRISB. Sets PORTB to outputs.
      banksel PORTB           ;banksel used to return to bank 0,
                              ;where PORTB is located.
      movlw   0x55            ;Set PORTB value.
      movwf   PORTB
      goto    $
      end                     ;All programs must have an end.
     
     
    Last edited: Jan 8, 2011
  11. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    Bit 5 of the STATUS SFR is bit RP0 so you can type the instruction 3 different ways -

    "bsf STATUS,RP0"

    "bsf STATUS,5"

    "bsf 0x03,5"

    Or you can simply type -

    "banksel TRISA"

    When you want to select bank 0, you can type -

    "banksel PORTA"
     
  12. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Oh I know what you mean about that, I'll use BANKSEL instead. It looks easier too.
     
  13. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Using BANKSEL it would look like this.

    Code (text):

    BANKSEL TRISB
    MOVLW 0xFE     ; Set first bit as output for LED
    MOVWF TRISB
    look good?
     
    Last edited: Jan 8, 2011
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    ONLINE
    All of them are good but this one to big of chance to plug the wrong numbers in
    "bsf 0x03,5"
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    ONLINE
    It's easy to read and less likely to make a mistake
     
  16. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Alrighty, perfect! I'm going to do a blinking LED program and run it on the PIC! this will be awesome, I hope it will work!! :)

    thanks to the MASTERs of PIC ASM! :D
     
  17. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    BTW...when you right your code, use the tab button rather than a single space. Single space does work, however the code becomes easier to manage/read when you use a tab space rather than a single space.

    Also, instructions go in column 2. In your example you have them in column 1, which is the label column. The code will not assemble if instructions are in the label column. This means you must tab over first prior to typing instructions.

    Code should look like this -

    Code (text):

    Whitespace  banksel     TRISA
                movlw       0xFE
                movwf       TRISA
     
    Not this -

    Code (text):

    banksel     TRISA
    movlw       0xFE
    movwf       TRISA
     
    The "Whitespace" label was merely to illustrate that there should be white space between the page margin and the instruction unless you want to label that particular instruction line.
     
    Last edited: Jan 8, 2011
  18. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    So here's my first blinking LED code! :) does it look ok?

    Code (text):
    #include <p16F648a.inc>
         __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
         
         ORG 0
         
         START:
            BANKSEL TRISA
            MOVLW 0xFE      ; Arrange bit 0 as output
            MOVWF TRISA
           
            BLINK:
                BANKSEL LATA
                MOVLW 0x01      ; Turn on output to LED
                MOVWF LATA
                CALL Delay_1s
                BANKSEL LATA
                MOVLW 0x00      ; Turn off output to LED
                MOVWF LATA
                CALL Delay_1s
                GOTO BLINK      ; Loop forever
               
         ; Subroutines
         Delay_1s:
            MOVLW d'1000'       ; This is 1 second, I think?
            MOVWF Delay
           
            Wait:
                NOP
                DECFSZ Delay, F
                GOTO Wait
                RETURN
    so now I need help on setting up my PIC files. how do I add the header file for the PIC16f648a? do I need to include object and library files, etc?
     
  19. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    Here is your code corrected. Compare mine to yours and take note of all differences you see -

    Code (text):


            list        p=16F648A   ;define processor
            include     <p16F648a.inc>  ;include header file
                __CONFIG    _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
                radix       dec     ;define radix as decimal unless otherwise noted

                ORG      0

    ;Define RAM location

    Delay1      EQU     0x20
    Delay2      EQU     0x21

    START
                banksel     TRISA       ;switch to bank 1
                movlw       0xFF        ;Arrange Port A as output
                movwf       TRISA

            movlw       0xFF        ;Arrange Port B as output. Even though PORT B is not used, it must be defined
            movwf       TRISB       ;as output if no pull up resistors exist on the PORT B pins

            movlw       0x07
            movwf       CMCON       ;disable on chip comparator (required instruction for this PIC if not using the comparator)
           
    BLINK
                banksel     PORTA       ;switch to bank 0
                bsf     PORTA,1     ;Turn on output to LED
                call        Delay_1s    ;Wait
                bcf     PORTA,1     ;Turn off output to LED
                call        Delay_1s    ;Wait
                goto        BLINK       ;Loop forever
               
    ; Subroutines
       
    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.
           
    Wait
                decfsz      Delay, F
                goto        Wait
            decfsz      Delay2,F
            goto        Wait
                return



     
    In regards to the header file, that is what the "include" instruction is for.

    Also, "banksel" only needs to be used when switching from one bank to another. You don't have to use it every time you address a register...only when switching to the bank that said register resides in. If you're already in the bank that the register you're addressing resides in, using banksel to address the register becomes redundant and generates unneeded code.

    Also, you cannot have labels on the same line that a "banksel" directive resides on.
     
    Last edited: Jan 8, 2011
  20. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Wow, now i have a lot of questions!

    1. What is radix?
    2. How do i know my processor frequency? is it 4 MHZ or what not?
    3. I read where you calculate your delay from your processor frequency, how do i do that?
    4. I meant how to include my header file in the little box, it shows source file, header file, object file, library file and a few others. is the header file for p16f648a in my C drive?

    I think i understand everything else, thanks man. :)
     
  21. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    I figured out how to get the header file thing working and source code. But when I build the program it says "Do you want this project to generate absolute or relocatable code?"

    what should I do?
     

Share This Page