| 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
- Jon Chandler
- MrDEB
Forum Activity
LCD / 18F1320 - mrdeb Friday, 19 March 2010 07:19 - [0 replies]
A 'throw-away" PIC board - mrdeb Thursday, 18 March 2010 06:19 - [32 replies]
Credit Where Credit's Due - jon chandler Tuesday, 16 March 2010 12:01 - [9 replies]
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]
Recent Comments
- 2010-03-17 22:58:18 Jon C...
Graham, Thanks for fixing the spacing on the array. This is how i...
- 2010-03-17 21:14:34 ASDne...
Thanks Graham, It is a problem with Flowcode because the demo wa...
- 2010-03-17 19:50:32 Graha...
Looks like you've pretty much solved every minor issue that was enc...
- 2010-03-17 11:42:03 Jon C...
The low-cost servo does have one other feature. The origina...
- 2010-03-17 10:56:44 Graha...
I am not familiar with flowcode, though did you try powering the bo...
- 2010-03-15 21:47:55 ASDne...
Hello there, I am still trying to get the LCD to work but I am pro...
- 2010-03-15 13:16:38 Graha...
Hi Hop, I've finally got some time this weekend to delve into the ...
- 2010-03-13 16:36:21 andyo
Very nice - 900,000 packets and 0 errors - can't ask for better tha...





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