buy hot The Fixer cool video Year One download Kasabian album nice Terminator Salvation hit cool track Jordin Sparks USA Whatever Works video download Check My Brain melodies nice video Finding Nemo nice Pop - Various Artists hit cheap DIVX Year One

digital-diy.com

MPASM Tutorial - Make a LED Flash Print E-mail
( 3 Votes )
  
Thursday, 09 April 2009 14:06

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;

	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



Comments (4)
  • AtomSoft

    Nice delay usage. How about changing it a bit so you can reuse the delay for any millisecond amount you want like:

    Code:

    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 counta ;Move delay count to "counta"
    d1 movlw 0xC7 ;delay 1mS
    movwf countb
    movlw 0x01
    movwf countc
    Delay_0
    decfsz countb, f
    goto $+2
    decfsz countc, f
    goto Delay_0

    decfsz count1 ,f
    goto d1
    retlw 0x00
  • picem

    Thanks :wink: Thats a much better way to go about it

  • picem

    Updated with the new delay routine :)

  • Nolan  - How should I configure my PIC18C252

    Very nice helpful example.

    Could you give me some direction (or code) for doing this experiment on my PIC18C252?

    Particularly the __config settings.

    Thanks
    -N

Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security
Please input the anti-spam code that you can read in the image.
Last Updated ( Wednesday, 29 April 2009 12:25 )