M a r k   C r o s b i e

You are in: PIC LED flasher  Home  Resume  Linux  LEGO  Electronics  Movies  Mac OS X 


Details
Development LED flasher Square wave generator Timed delay library Serial I/O Stepper motor driver Motor Mind B LCD library
Contact Mark at mark@mastincrosbie.com

PIC LED flasher

This is my first simple PIC program. It will flash an LED continuously at approximately 1Hz. It is a very simple loop that delays for 500 milliseconds (half a second) with the LED on, and then delays for 500ms with the LED off. Thus, the LED flashes at 1 Hz!

The schematic for this circuit shows that the wiring is simple: connect a crystal oscillator across pins 15 and 16 and add the capacitors to ground. Wire a 4k7 resistor to the MCLR reset pin 4 so the PIC will reset itself at startup. Then connect the LED via a resistor to pin 17.

When a 4 Mhz cryztal is used with PIC 16C84, the LED will flash at 1 Hz.

This program is available as:
title  "Flash - Flash an LED on an off at 1Hz"

; Mark Crosbie  8/22/98
;
;  The Program simply sets up Bit 0 of Port "A" to Output and then
;  loops, setting the value alternatively low and high
;
;  Hardware Notes:
;   Reset is tied through a 4.7K Resistor to Vcc and PWRT is Enabled
;   A 220 Ohm Resistor and LED is attached to PORTA.0 and Vcc
;

  LIST P=16C84, R=DEC
  errorlevel 0,-305
  INCLUDE "P16C84.inc"
 

;  Registers
Temp    equ     12              ;  16 Bit Dlay Variable

 __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON

;  Mainline of Flash
  org 0
  clrf   PORTA                  ;  Clear all the Bits in Port "a"
  clrf   STATUS
  bsf    STATUS, RP0            ;  Goto Bank 1 to set Port Direction
  bcf    TRISA, 0               ;  Set RA0 to Output
  bcf    STATUS, RP0            ;  Go back to Bank 0

Loop
  movlw 1                       ;  Turn on the LED on Port A
  movwf PORTA                   ;

  call   Dlay                   ;  Delay Before Changing Values

  movlw 0                       ;  Turn off the LED on Port A
  movwf PORTA                   ;

  call   Dlay                   ;  Delay Before Changing Values

  goto Loop

;  Dlay Routine - Delay a Half Second before Returning
Dlay
 ifndef  Debug                  ;  If Debug NOT Defined
  movlw  0                      ;  Actual Setup the Delay Value
  movwf  Temp
  movlw  128
  movwf  Temp + 1
 else                           ;  If Debug Defined
  movlw  2                      ;  Programming Debug Delay Value
  movwf  Temp
  movlw  1
  movwf  Temp + 1
 endif

D_Loop                          ;  Loop Around Here until Complete
  decf   Temp                   ;  Decrement the lo Value
  btfsc  STATUS, Z              ;  Is the Zero Flag Set?
   decf  Temp + 1
  movf   Temp, w                ;  Are we At Zero for Both?
  iorwf  Temp + 1, w
  btfss  STATUS, Z
   goto  D_Loop
  return

  end


 
cover
PIC Microcontroller Project Book
Lot's of great PIC project ideas!
cover

Programming and Customizing the Pic Microcontroller

If you are learning to program microcontrollers then Myke's book is good start.
cover
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design. If you want to learn why your circuit is not working, read this book and you'll know why.
cover
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best practioners in the field: the MIT Artificial Intelligence Lab!
cover
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers. A handy reference to have if you need a quick solution to a problem.

© 2002-2004 Mark Crosbie   shareright © 2002 Phlash