MPASM Tutorial - Make a LED Flash

Good starting place for anyone new to micro's.. Well not completely new, as a general knowledge of what a micro controller is and basic electronic knowledge is assumed. 

Just in-case; LED's are Light Emitting Diodes - like a traditional lamp but more efficient and "cutting edge". They can only be powered when biased correctly, that is, one lead is a positive and the other is negative (also known as anode and cathode). Here's what an LED looks like in a wiring diagram;

led_symbol.svg1

An LED needs some sort of current limiting device in series as without it, the LED will "burn out", or be over-driven. A standard LED is generally rated for about 20mA, and depending on the forward voltage drop of the LED, well you can calculate how big the resistor should be. It's safe to say that 470 ohm or greater will be suffice for most LED's.

OK, back to the programming. I've chosen to use the ever so popular 16F628 PIC micro. It's cheap, got lots of on-board features, one of them being the internal oscillator that runs at 4Mhz. Keeping in mind that PIC's physically run at 1/4 the OSC speed, this means that with the internal oscillator setup, the PIC will run at 1Million Instructions Per Second (1 MIPS).

To turn the LED on and off in a continual loop would not even make it look like anything was happening, so a delay needs to be incorperated to "burn cycles". This amungst other program features are heaviliy commented below;

 

MPASM Tutorial - LED Flash
 LIST p=16F628A ;tell assembler what micro is in use
 include "P16F628A.inc" ;include the defaults for the chip
 __config 0x3D18 ;sets the config settings (oscillator type etc)
 
 cblock 0x20 ;start of general purpose registers
 count1 ;used in delay routine
 counta ;used in delay routine 
 countb ;used in delay routine
 endc
 
 LED Equ 0 ;set constant LED = 0
 LEDPORT Equ PORTA ;set constant LEDPORT = 'PORTA'
 
 org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
 ;this is where the program starts running 
 movlw 0x07
 movwf CMCON ;turn comparators off
 
 bsf STATUS, RP0 ;select bank 1
 movlw b'00000000' ;set PORTB all outputs
 movwf TRISB
 movwf TRISA ;set PORTA all outputs
 bcf STATUS, RP0 ;select bank 0
 clrf PORTA
 clrf PORTB ;set all outputs low
 
Loop
 bsf LEDPORT, LED ;turn on RA0 only
 movlw d'250' ;delay 250 ms
 call DelayMs ;this waits for a while
 bcf LEDPORT, LED ;turn off RA0 only
 movlw d'250' ;delay 250 ms
 call DelayMs
 goto Loop ;go back and do it again
 
DelayMs movwf count1 ;Move delay count to "count1"
d1 movlw 0xC7 ;delay 1mS
 movwf counta
 movlw 0x01
 movwf countb
 
Delay_0
 decfsz counta, f
 goto $+2
 decfsz countb, f
 goto Delay_0
 decfsz count1 ,f
 goto d1
 retlw 0x00
 
 end

 

And the wiring diagram;

16F628_LED


Forum Activity

Member Access