Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



How to use our PIC Tutorials

To get the most out of our PIC Tutorial, you should read this page first. Here i will explain the structure of the tutorial pages and the program files, so that you will not get confused.




The Tutorial Structure

Each tutorial consists of four basic parts:



  • 1. The Intro
    • This is the very first part. The prologue and a quick description of the tutorial, as well as the purpose of it, are written within the first lines. At the top-right corner there is a photo of the circuit. A video is also attached that demonstrates the tutorial in operation.



  • 2. The Circuit
    • The next part is the circuit. This is where the schematic drawing is attached, and -if necessary- explained.



  • 3. The Code
    • The third part is the code. There is a code window with the code to be explained. Please note that in this window, there is ONLY THE IMPORTANT CODE for this tutorial. The rest of the code can be found to the attached files, at the fourth and last part of each tutorial.

      The reason that i chose to show only a part of the code is obvious. A complete PIC program consists of several initialization instructions, subroutines and other stuff. But the goal of each tutorial is not to explain the complete program structure each time. The goal is to explain a specific routine, function or instruction. The rest of the code shall be ignored for the moment and will not be discussed. As the level rises, more and more code will be explained. At the end, each and every line of a PIC program will be explained in details.

      Of course, the complete code is attached at the 4th part of the tutorial.



  • 4. The Attachments
    • This is the last part of the tutorials. Here you can find all the files of the program. You can extract the zip file into a directory and directly compile-upload the code to a PIC.






    The Files

    I have tried to keep a unified structure for all the tutorials within this book. All tutorials comes in .zip packages. Within the zip container, you will find the following files:

  • 1. The Main Assembly Listing File (.asm)
    • All tutorials will have a unique .asm file. This is the main code file. Within this file you will find the complete assembly listing of the tutorial.


  • 2. The Microchip 16F88 Header File (P16F88.INC)
    • All tutorials will have the header file 'P16F88.INC' included. This file is the Microchip header file for the 16F88 PIC. You should use the one provided from the tutorials and NOT the one from Microchip, because i have add some more macros to make our life easier. If you do not use the same as i do, it is most likely that the program will not be compiled properly


  • 3. The PIC Initialization File (init_normal.INC)
    • All tutorials will have the header file 'init_normal.inc' included. This file contain some instructions for initializing some PIC operations, such as the oscillation frequency. This file should not be taken into account unless you are asked. All functions will be explained from time to time.


  • 4. The Delay Subroutines (mSecDelays4MHz.INC) - Optional
    • It is most likely that a delay routine needs to be used from a tutorial. For this, i have made a separated header file named 'mSecDelays4MHz.inc'. This file holds a parametric delay routine for 4MHz clocked PIC. During the main code, you may see calls to these subroutines. A call to a 10mSec delay subroutine would be ''.

      Call Wait10mSec


      If you are a beginner, i encourage you to use this file. You can make your own subroutines for your custom time-delays. There will be a complete lesson for calculating and generating custom delays, but until then, these subroutines will be used extensively.



    The asm Program

    After explaining the files that exist within the container, i will explain the parts that the main assembly listing file (.asm) has:

  • 1. The Program Head
    • At the very beginning of each .asm file, you will find a similar code to the following one:

       list       F=inhx8m, P=16F88, R=hex, N=0
      #include  P16F88.inc; PIC definitions
       __config _config1,_INTRC_IO & _WDT_OFF & _LVP_OFF & _PWRTE_OFF & _MCLR_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_PROTECT_OFF & _CCP1_RB0 & _CP_OFF
       __config _config2,_IESO_OFF & _FCMEN_OFF 
       Errorlevel -302      ; switches off Message [302]: Register in operand not in bank 0.
      


      The above code should be used as-is. First, It contain information about the assembly listing. Then, the PIC16F88 header file is included into the code. The __config lines will set the configuration bits of the PIC (explained in later pages). Finally, the 302 annoying message is turned off from the compiles.


  • 2. The Definitions
    • Within this section, i will add some custom definitions for some registers that are going to be extensively used within the code. If for example i have connect a button in PORTA,0 pin, i could define a name for this pin as follows:

      ; Definitions -------------------------------------------------------------
      #Define	ButtonInput			PORTA,0
      


      Now i can refer to PORTA,0 pin by using its defined name 'ButtonInput'.


  • 3. The User Defined Registers
    • To use the free RAM of the PIC, the user defined registers must be declared. I do this within this section. I use the cblock/endc method to define my registers. An example is as follows:

      ; RAM preserved -----------------------------------------------------------
      	cblock 0x20
      		WaitCounter,WaitCounter2
      		IntervalCounter
      	endc
      


      Now i can refer to PORTA,0 pin by using its defined name 'ButtonInput'.


    The first two registers (WaitCounter and WaitCounter2) will be found in almost all tutorials. These two registers are used by the delay subroutines described above.

  • 4. The Constants
    • Many times, i use constants for declaring values. I do this for parametrization reasons. The constants are named values that cannot be changed during program execution. An example is as follows:

      ; Conastants --------------------------------------------------------------
      GLDoubleClick_Interval	=	d'15'
      GLDoubleClick_MaxDelay	=	d'30'
      GLLongClick_Interval	=	d'80'
      


      Now, i can load the value d'15' by referring to it's name: movlw GLDoubleClick_Interval.


  • 5. The Main Program Start
    • When the program starts, there are some things to be done. At first, we need to define the offset of the program, that is the 0x00. Then, we need to declare the Interrupt Service Routine at address 0x04. Then we call the initialization routine. All these steps are done within the beginning of the program. A typical program start is as follows:

      ; Program Memory ----------------------------------------------------------
                      org     0               ; Main program offset
                      goto    Init            ; Jump to initialization routine
      ; Interrupt Service Routine -----------------------------------------------
                      org     4               ; ISR beginning
      ; -------------------------------------------------------------------------
      ; Microcontroller initialization
      Init            
      				include Init_normal.inc	;Include the PIC Initialization routine
      ;##########################################################################
      


      Now, everything is ready for the main programm!


  • 6. The Main Program
    • Finally, here comes the main program. Here is placed the code that you can find in the code-window of each tutorial. You can use the tutorial files as templates, and by altering the main code, you can make your own programs! At the end of the main program, there are the inclusions of any external header files (such as the delay subroutines from the 'mSecDelays4MHz.INC' file), and the label 'Fins'. This label (from the ford Finish) shows the end of the program. This label may be omitted, but i always use it, and when i want to terminate the program, i run a GOTO instruction to this label ('GOTO Fins'). You should ALWAYS have the 'End' directive at the end of the main .asm file! This is how the file should look like:

      ;##########################################################################
      
      ; Main Program ------------------------------------------------------------
      
      Start
                      .
                      .
                      .
                      [ HERE IS THE MAIN CODE ]
                      .
                      .
                      .
      
      
      		include mSecDelays4MHz.inc
      
      Fins            
                      end             ; end of program
      














      Previous page ---- Next page



      Go back to the book contents

      Go to the discussion forum of this book





       

      Comments

        Name

        Email (shall not be published)

        Website

      Notify me of new posts via email


      Write your comments below:
      BEFORE you post a comment: Long comments or comments of general interest are NOT to be posted here. Please use the forum instead. You will receive a reply much faster.


            

    • At 17 May 2011, 20:11:08 user hadi A. Khalifa wrote:   [reply @ hadi A. Khalifa]
      • Thanks Alot Very usefull and execellent way to teach the basics of PIC
        Congratulation
       






      No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

      Read the Disclaimer


      All trademarks used are properties of their respective owners.
      Copyright © 2007-2009 Lazaridis Giorgos.
      All rights reserved.






     HOT in heaven!


  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2



  • NEW in heaven!



    New Project: Simplest and Cheapest Foot-Controlled Autocue Ever






     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy