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

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    969
    Likes:
    45
    Location:
    England
    Hi,

    If you have got the CDs then you have all you need.

    First install the program MPLAB IDE from the CDs, this is the main working enviroment for everything - just install with the default options. - its a massive program !

    Then look for the Pickit2 Starter Kit Lessons and install them.
    Open its pdf file and it will guide you through how to get started with the lessons and some coding - it refers to the starter board, but for now you will just have to skip that bit.
     
  2. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,680
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Hi Alharad,
    If you only have the pickit 2 and a couple of pic micro's coming then you will need to make some circuit boards up to enable for the pic to work. By far the best solution for a beginner wanting to start with asm and no prior experience is to follow Nigel's Tutorials where he does show in great clarity how to build modular boards using veroboard.

    Nigel's PIC Tutorial Page

    Go and have a good read from the start and any problems you have I'm sure we'll be happy to help.

    Regards Bryan
     
  3. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    I already installed the software when i first got it :D

    so i need to look at Pickit2 starter kit lessons but what are they for? do they teach asm?
     
  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

    Hi Bryan,

    I see a lot of boards in that tutorial, but which one should I make?
     
  6. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,680
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Hi Alharad,
    The first board you should make if your going to start with the 16f628 is the main board shown first on the hardware page. Now in order to program your pic you will need to put in a 6 pin header for the pickit2 to connect. Read the datasheet for the 16f628 and make sure you get the pinouts for that header correct. I do assume the pickit2 pins are the same as the ICD2 so the header pinouts are :-

    pin 1 MLCR
    pin 2 VDD (+5 volts)
    pin 3 VSS ( Ground)
    pin 4 PGD ( Data)
    pin 5 PGC ( Clock)
    pin 6 unconnected

    This will enable you to program the chip in circuit and I feel it is good practice to put that 6 pin header in all my pic projects so I can re-program them in circuit when needed.

    Regards Bryan

    P.S. Also make the LED board then your first steps in programming can be getting those LED's to light up
     
    Last edited: Jan 2, 2011
  7. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    OK, i will make those.

    I will come back when i have the pic16f628 and finished those boards.

    thanks for the help!!! :D
     
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Make a board like this and you can do the picklt2 lessons with little changes
     

    Attached Files:

  9. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Ok, I will make just that board then.
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    This is the first one
    Code (text):

    ; *******************************************************************
    ; PICkit 2 Lesson 1 - "Hello World"
    ;
    ; This turns on DS1 LED on the Low Pin Count Demo Board.
    ;
    ; *******************************************************************
    ; * See Low Pin Count Demo Board User's Guide for Lesson Information*
    ; *******************************************************************
    ; * NOTE: The PIC16F690 requires the AC162061 header for debugging  *
    ; *******************************************************************

    #include <p16F628a.inc>
         __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
         org 0
    Start:
         bsf     STATUS,RP0       ; select Register Page 1
         bcf     TRISB,0          ; make IO Pin B0 an output
         bcf     STATUS,RP0       ; back to Register Page 0
         bsf     PORTB,0          ; turn on LED B0 (DS1)
         goto    $                ; wait here
         end
         
     
    Last edited: Jan 2, 2011
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Here the blink one
    Code (text):

    ; *******************************************************************
    ; PICkit 2 Lesson 2 - "Blink"
    ;
    ; First lesson showed how to make an LED turn on,
    ; Now we'll look at how to make it blink.  Delay loops are necessary
    ; to slow down the on and off commands so they are visible to humans.
    ; *******************************************************************
    ; * See Low Pin Count Demo Board User's Guide for Lesson Information*
    ; *******************************************************************
    ; * NOTE: The PIC16F628a requires the AC162061 header for debugging  *
    ; *******************************************************************
    ; *******************************************************************

    #include <p16F628a.inc>
         __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
         cblock 0x20
    Delay1                   ; Define two file registers for the
    Delay2                   ; delay loop
         endc
         
         org 0
    Start:
         bsf       STATUS,RP0          ; select Register Page 1
         bcf       TRISB,0             ; make IO Pin B.0 an output
         bcf       STATUS,RP0          ; back to Register Page 0
    MainLoop:
         bsf       PORTB,0             ; turn on LED B0
    OndelayLoop:
         decfsz    Delay1,f            ; Waste time.  
         goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
         decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
         goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec.
                                       ; call it a two-tenths of a second.
         
         bcf       PORTB,0             ; Turn off LED B0
    OffDelayLoop:
         decfsz    Delay1,f            ; same delay as above
         goto      OffDelayLoop
         decfsz    Delay2,f
         goto      OffDelayLoop
         goto      MainLoop            ; Do it again...
         end
         
    And here the rotate

    Code (text):

    ; *******************************************************************
    ; PICkit 2 Lesson 2 - "Rotate"
    ;
    ; First lesson showed how to make an LED turn on,
    ; Now we'll look at how to make it Rotate.  Delay loops are necessary
    ; to slow down the on and off commands so they are visible to humans.
    ; *******************************************************************
    ; * See Low Pin Count Demo Board User's Guide for Lesson Information*
    ; *******************************************************************
    ; * NOTE: The PIC16F628a requires the AC162061 header for debugging  *
    ; *******************************************************************
    ; *******************************************************************

    #include <p16F628a.inc>
         __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
        cblock 0x20
     Delay1              ; Assign an address to label Delay1
     Delay2
     Display             ; define a variable to hold the diplay
         endc
         
         org 0
    Start:
         bsf       STATUS,RP0          ; select Register Page 1
         clrf      TRISB               ; make IO PortC all output
         bcf       STATUS,RP0          ; back to Register Page 0
         movlw     0x08
         movwf     Display
    MainLoop:
         movf      Display,w           ; Copy the display to the LEDs
         movwf     PORTB
    OndelayLoop:
         decfsz    Delay1,f            ; Waste time.  
         goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
         decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
         goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec.
                                       ; call it a two-tenths of a second.
         
         bcf       STATUS,C            ; ensure the carry bit is clear
         rrf       Display,f
         btfsc     STATUS,C            ; Did the bit rotate into the carry?
         bsf       Display,3           ; yes, put it into bit 3.
         goto      MainLoop
         end
       
     
    Last edited: Jan 2, 2011
  12. Alharad

    Alharad New Member

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

    if you wouldn't mind, could you or someone else go through the code and explain each command and what it is doing?

    thanks very much! :)
     
  13. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    I made a mistake! i actually ordered the pic16f648 because the 628 didn't have samples. :D
     
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn

    Attached Files:

    Last edited: Jan 2, 2011
  15. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    ok I will read that, thanks!
     
  16. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,680
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Hi Alharad,
    That board be80be gave you will give you a good quick learning curve along with the code he has supplied. But I do see one problem when your ready to move on to other better things you would have to make another board. The reason I suggested Nigel's main 18 pin board is the ports all have header pins so it is a multi purpose board. It's up to you which way you go but when I first started I made most of Nigel's boards and they were great. A couple of years ago a friend wanted to start in pic's and I gave all my boards to him and got him to follow Nigel's tutorials. Since then those boards have gone onto atleast one or two more people wanting to get started.


    Regards Bryan
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    bryan1 Nigel's boards are the best way to go. I thought he had the low pin count board but seeing he has the guide I just posted that so he could still go over the code and try it. The ADC sample is real good that comes with the guide.

    I like Nigel's boards myself I made all of them about 4 years ago. He really needs to read Nigel's tutorials and read the code commits. Nigel's would post thats in my tutorials and I would think where and sure it was there he committed it with the code ideas that really help would of saved a lot of learning had I read all the commit's too.

    So read it all and you'll be ahead of the game.
     
    Last edited: Jan 2, 2011
  18. Mosaic

    Mosaic Well-Known Member

    Joined:
    Jun 3, 2010
    Messages:
    2,611
    Likes:
    129
    Location:
    Caribbean
    I got started about 6 months ago with the Pickit2 and the LPC from microchip. I did all of the lessons in asm and then I got the oshonsoft simulator.

    You can do a great deal of beginner stuff without having to build anything much physically.

    I 'built' my first breadboard project after having done simulations on a 16f886 driving 20LEds & a 4 Digit 7 seg display and with a full menu interface driven by a 2 button interface.

    The code was several thousand asm commands long. The device is a multifunction device involving 5 adc inputs, 4 MOSFET driven pwr outputs, 2 of them PWM plus a PID controller, eprom data logging, ..etc. etc. I learned to do ALL that between a simulator AND this remarkable forum.

    After building the 1st physical device, it took about a week to get it working becuz I had to read the data sheet carefully for things like the ICSP setup and avoiding low voltage programming and fixing the usual wiring issues.

    Now if u have no prior electronics experience, then building the simpler boards is a must, I skipped that since I am comfortable with prototyping. There is no exchange for learning by doing.

    Also, always make a real effort to debug b4 you post, and post your commented code . Use the forum search feature. Also check out PIClist, Microchip PIC

    Make little functions that u debug one at a time. Then link them together to make larger functions. That way u keep things manageable.
     
    Last edited: Jan 2, 2011
  19. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    865
    Likes:
    82
    Location:
    Fresno, CA
    I could type out a super long lesson on the instruction set for a mid range PIC with meanings and all. However, start here with the PIC 16F628 datasheet. Go to page 109 and start there. This is the instruction set summary for this PIC. It will explain the meaning behind each instruction as well as the syntax and more -

    http://www.electro-tech-online.com/custompdfs/2011/01/40300C.pdf

    The thing with assembly though is that you have to know the hardware (i.e. the particular PIC you plan to write code for) inside and out. The PIC has SFRs (Special Function Registers) that control the very functions of the PIC. Writing to an SFR makes the PIC do what it does. The SFRs are mapped in data memory so you would address them just as you would any register in RAM.

    They're also spread across "banks" so you have to know what bank each SFR is in and you must use a bank switching instruction in your code prior to accessing registers in different banks. The data sheet for each PIC shows a memory map that tells you where each SFR is located.

    Two of the most used SFRs on any PIC are the tristate registers (TRISA, TRISB) and the port status registers (PORTA, PORTB). These are the SFRs that directly control the I/O ports that interface with the outside world either via monitoring data that is sent to it or sending data to other external devices. I will explain these two in this example.

    The Tristate registers TRISA and TRISB control the direction of Port A and B respectively. By "direction" I mean the bits in the tristate registers designate which pins on the port they control are input and output. TRISA and TRISB are located in Bank 1 and are 8 bits wide and each bit in the tristate register is assigned to an I/O line on the port that the register is assigned to.

    Setting a bit in the TRIS registers (i.e. making the bit a "1") designates the pin controlled by said bit as an "input" while clearing a bit in the TRIS registers (i.e. making the bit a "0") designates the pin controlled by said bit as an "output".

    For an example code for this you would write -

    Code (text):


    ;TRISA Bit Map
    ;|RA7|RA6|RA5|RA4|RA3|RA2|RA1|RA0|
    ;| 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |

            banksel     TRISA       ;select bank in which SFR TRISA resides
            movlw       b'01010101' ;set RA7, RA5, RA3 and RA1 as output
            movwf       TRISA       ;set RA6, RA4, RA2 and RA0 as input

    ;TRISB Bit Map
    ;|RB7|RB6|RB5|RB4|RB3|RB2|RB1|RB0|
    ;| 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |

            movlw       b'10101010' ;set RB7, RB5, RB3 and RB1 as input
            movwf       TRISB       ;set RB6, RB4, RB2 and RB0 as output
     
    Notice the comments I added in. You can do this in your code so that it's easy to follow along as you read it. Anything proceeding a semicolon gets ignored by the assembler. You must use a semicolon for each new comment line.

    I commented in the bit map for TRISA and TRISB to illustrate which bits control which pins. They're read MSB first (Most Significant Bit...or the highest bit) and LSB last (Least Significant Bit...or the lowest bit).

    The first instruction, "banksel", is actually an assembler directive that tells the assembler to generate the required asm code to select the proper bank for the file register address that is the operand (i.e. the address which follows the instruction). In this case, it would be SFR TRISA, which also happens to be the same bank TRISB resides in. You can do this for any file register.

    The 2nd instruction, "movlw" is an abbreviation for "move literal value into the w register. This is a single operand instruction that moves any value you wish from 0-255 (or 00 - FF in hex). The "b" before the value tells the assembler that the value nested in the single quotes is "binary". You would use "d" for decimal and "h" or "0x" for hex. When using 0x, you don't have to use the single quotes. You can just simply say 0x00 for "hex zero".

    The 3rd instruction, "movwf" is an abbreviation for "move data in w into file". This is also a single operand instruction, and the operand is the file address that you are copying the data in W to. With MPASM and the includes file for your PIC, your SFR addresses are pre-labeled so you can simply use the label which represents the file register rather than having to look up the physical address of the SFR that you're moving data into.

    In Bank 0 you have the PORTA and PORTB registers. You would read these registers to find the current state of your input pins while writing to PORTA and PORTB writes to the port latch. You would write to PORTA and PORTB when you're wanting to change the state of an output pin (i.e. make the pin put out a "high" or a "low" signal). The bit mapping in the PORTA and PORTB registers is identical to the TRISA and TRISB bit map.

    An example code to turn on the output pins would be -

    Code (text):


    ;TRISA Bit Map
    ;|RA7|RA6|RA5|RA4|RA3|RA2|RA1|RA0|
    ;| 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |

            banksel     TRISA       ;select bank in which SFR TRISA resides
            movlw       b'01010101' ;set RA7, RA5, RA3 and RA1 as output
            movwf       TRISA       ;set RA6, RA4, RA2 and RA0 as input

    ;TRISB Bit Map
    ;|RB7|RB6|RB5|RB4|RB3|RB2|RB1|RB0|
    ;| 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |

            movlw       b'10101010' ;set RB7, RB5, RB3 and RB1 as input
            movwf       TRISB       ;set RB6, RB4, RB2 and RB0 as output

    ;PORTA Bit Map
    ;|RA7|RA6|RA5|RA4|RA3|RA2|RA1|RA0|
    ;| 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |

            banksel     PORTA       ;select bank in which SFR PORTA resides
            movlw       b'10101010' ;set all PORT A outputs "high"
            movwf       PORTA      

    ;PORTB Bit Map
    ;|RB7|RB6|RB5|RB4|RB3|RB2|RB1|RB0|
    ;| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

            movlw       b'00000000' ;set all PORT B outputs "low"
            movwf       PORTB      
     
    Setting port output pins "high" switches the pin to the Vcc pin, which is connected to the positive supply rail. This makes the pin put out a +5V voltage.

    Setting port output pins "low" switches them to the Vss pin, which is connected to ground. This makes the pin put out a 0V voltage, or a ground reference.

    So to summarize, the bits in the TRISA/TRISB SFRs designate I/O pins on Port A and Port B respectively as "input" or "output". A "1" designates "input" while a "0" designates "output".

    The bits in the PORTA/PORTB registers are for reading the state of an input pin (whether it be high or low, depending on what input you feed to the input pin) and for writing to it to set output pins high or low to send digital logic signals out from the output pins.
     
    Last edited: Jan 3, 2011
    • Like Like x 1
  20. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    Wow, you explained that perfectly to me! You are very good at explaining assembly, maybe you should write a book :)


    I'm still not sure where the PICkit2 lessons are, can someone post the pdf? I would like to go through all of them, thanks.

    I sure can't wait till I can get started, this is going to be fun!!
     
  21. Alharad

    Alharad New Member

    Joined:
    Nov 22, 2010
    Messages:
    43
    Likes:
    0
    What is LPC? You said you did all the lessons in asm, but where are they?

    Thanks, dude.
     

Share This Page