| MPASM Tutorial - Make a LED Flash |
|
|
| 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;
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;
Comments (4)
Joomla components by Compojoom
|
|
| Last Updated ( Wednesday, 29 April 2009 12:25 ) |
Whos Online
- ta1dr
- andyo
- Jon Chandler
Forum Activity
the LCD demo hello world - jon chandler Tuesday, 09 March 2010 23:48 - [28 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Way Off Topic: Worldwide Ignite Week - jon chandler Wednesday, 03 March 2010 20:33 - [4 replies]
PIC USB with C and Visual C++ Project - hop Tuesday, 02 March 2010 21:49 - [0 replies]
Function generator help - magic Tuesday, 02 March 2010 16:37 - [10 replies]
Recent Comments
- 2010-03-09 13:27:12 air j...
link:http://www.b2c2u.com/ [air jordan] [url=http://www.handba...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...
- 2010-03-05 12:17:40 andyo
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
- 2010-03-05 11:46:37 mrpse...
Thanks AndyO. What I have to do is design a device to monitor temp...
- 2010-03-05 03:08:58 andyo
The circuit used for the remote sensor can be found here: [url]http...
- 2010-03-04 07:19:06 renno
plz i need help do u know from where i could download it or how cou...
- 2010-03-04 07:16:05 renno
hello everyone i'm trying to finish my final project and i want to ...
- 2010-03-04 02:49:38 Saira...
Thanks that was helpful





Nice delay usage. How about changing it a bit so you can reuse the delay for any millisecond amount you want like:
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