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   



PIC Tutorial - A Simple LED Flasher
Before reading this tutorial, make sure you have read the How to use our PIC Tutorials page!


Another simple circuit is the LED flasher. With this tutorial you will become familiar with the delays. It is very important to understand how to generate delays with the PIC.


In Action



The Circuit




What this circuit does, is to simply flash an LED every half second. To maintain the simplicity of the code, there is no button or other switch conencted to it, as this would be rather off-subject. Bellow is the PIC code of this tutorial.




The Code


; RAM preserved -----------------------------------------------------------
	cblock 0x20
		WaitCounter,WaitCounter2
	endc
; Main Program ------------------------------------------------------------
Start
				bank1					;Go to bank 1
				movlw b'11111111'		;
				movwf TRISA				;Set the port pin types of the RA

				movlw b'11111110'		;
				movwf TRISB				;Set the port pin types of the RB
				bank0					;Go to bank 0
MainLoop
				call Wait250mSec
				call Wait250mSec
				bsf PORTB,0				;Set RB0 output
				call Wait250mSec
				call Wait250mSec
				bcf PORTB,0				;Clear RB0 output
				goto MainLoop

Wait250mSec                     movlw d'250'
				movwf WaitCounter2		
BackWaitLoop2                   movlw d'163'			
                                call WaitWx4Cycle
                                movlw d'163'
                                call WaitWx4Cycles
			        nop
			        nop
			        decf WaitCounter2,f
			        btfss Zero
			        goto BackWaitLoop2		
			        return				



WaitWx4Cycles                   movwf WaitCounter
BackWaitLoop                    decfsz WaitCounter,f
                                goto BackWaitLoop
                                return


At the end of this tutorial, you will find all the files including the above asm file in one zip to download and test it.

The first thing to be noticed is that all ports are inputs except the RB0 port. This is our only output and the LED is connected there.

When the program reaches the MainLoop, it calls for a label named 'Wait250mSec'. This label is our calculated 250mSec delay. Twice a call means 1/2 second delay. Now how this delay really work:

Go to this label to follow the code. You will notice that the decimal number '250' is loaded to the WaitCounter2 register defined at the beginning of the code. Right after, a 'BackWaitLoop2' label is defined. 8 instructions below, there is this 'goto BackWaitLoop2' instruction. This block, will generate 1mSec delay and will be called as many times as the WaitCounter2 carries, in our occasion that will be 250 times equals to 250mSec.

To generate the 1mSec delay, we need to know something about the timing of the PIC. Each instruction cycle needs 4 clock pulses to be implemented. Our PIC is clocked at 4MHz (defined in the OSCCON register within the Init_normal.inc header file). So, if you divide the 4.000.000 Hz by 4, you will get the astonishing number of 1.000.000 instructions per second! Our PIC is capable to execute 1.000.000 instructions per second, and this is our time base to calculate the delay. Follow my thinking:

  • 1.000.000 instructions needs 1 Second to be executed
  • How many instructions needs 0.001 Second to be executed (0.001Sec = 1mSec)?
  • The answer is hidden in plain mathematics. The formula for the above calculation is:

    Instructions = 1.000.000
    1000

    And that means that we need to execute 1000 instructions to generate 1mSec delay. That is the basic idea how to calculate delays. During the intermediate lessons, there will be e fully explanation on how to measure the instruction cycle and delay, because not all instructions have the same cycle. But to avoid all this mess, you can just use the code provided to this tutorial. This will work perfectly for the 4MHz oscillator. It can be used for direct delays from 1mSec to 256mSec. Just load the value in mSec that you want to the WaitCounter2 register and call the BackWaitLoop2 subroutine. If you want more than 256 mSec delay, you can call more than once the same subroutine, as done in this tutorial for the 500mSec (half second) delay.

    Now, back to the MainLoop. It is most obvious what is done over there. At first, a 500mSec delay is called. Then, the RB0 output is SET, the LED turns on. Afterwards, another 500mSec is called and the RB0 output is CLEARED, the LED is turned off. That keeps going infinitely, and there is your LED flasher!



    The project Files


    Following are the files for this project:

     PIC Tutorial - LED Flasher











    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:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


          

  • At 23 August 2015, 19:18:21 user alyanelc wrote:   [reply @ alyanelc]
    • hi how to change assembly code to c code
     






    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 Theory: AC electric motor working principle






     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