Minggu, 05 September 2010

LED AVR

1.1. Turn On/Off LED

In this program you will experiment, how to turn OFF 4 MSB port and turn ON 4 LSB port. To make all Port C function as output, you must send logic '1' to DDRC.

.include "8535def.inc"
start:
ldi R16,0b11111111
out DDRC,R16
;
ldi R16,0b11110000
out PortC,R16
stop:
rjmp stop

1.2. Turn On/Off LED ( single bit )

To set singgle bit of port C as input or output, you have to use SBI ( set bit I/O) to make the bit action as output, or CBI ( clear bit I/O ) to make the bit action as input into DDRC.

.include "8535def.inc"
start:
sbi DDRC,0 ;PortC, bit 0 as output
cbi PORTC,0 ;send logic '0' to PortC bit 0
;
stop:
rjmp stop

1.3. Turn On/Off LED ( singgle bit )

Another experiment to activate 2 bit port C as output

.include "8535def.inc"
start:
sbi DDRC,0 ;PortC, bit 0 & 7 as output
sbi DDRC,7
cbi PORTC,0 ;send logic '0' to PortC bit 0 & 7
cbi PORTC,7
;
stop:
rjmp stop

1.4. LED Blink

This experiment will demonstrate how to turn ON and OFF 8 bit on Port C, periodically , using Delay Rutin.

.include "m8535def.inc"
.org 0x0000
;
ldi r16,low(RAMEND)
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16
;
init:
ldi R16, 0b11111111 ; Make pin PC output
out DDRC, R16 ;
main:
ldi R16, 0b00000000 ;
out PORTC,R16
rcall delay
ldi R16, 0b11111111 ;
out PORTC,R16
rcall delay
getout:
rjmp main ;repaet until power removed
delay:
; provides some delay so that the LED is visible
; =============================
; delay loop
; 499998 cycles: about 1/2 seconds
; -----------------------------
; delaying 499995 cycles:
ldi R29, $Ff
WGLOOP0: ldi R30, $ff
WGLOOP1: ldi R31, $10
WGLOOP2: dec R31
brne WGLOOP2
dec R30
brne WGLOOP1
dec R29
brne WGLOOP0
ret

1.5. Turn On LED Sequentially

Another experiment to blink a singgle LED

.include "m8535def.inc"
.org 0x0000
;
ldi r16,low(RAMEND)
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16
;
ldi R16,0b11111111
out DDRC,R16
main:
ldi R16,0b11111111
out PORTC,R16
rcall delay
;
cbi PORTC,0
rcall delay
cbi PORTC,1
rcall delay
cbi PORTC,2
rcall delay
cbi PORTC,3
rcall delay
cbi PORTC,4
rcall delay
cbi PORTC,5
rcall delay
cbi PORTC,6
rcall delay
cbi PORTC,7
rcall delay
;
rjmp main ; repaet untilpower removed :-)
; provides some delay so that the LED is visible
delay:
ldi R29, $20
WGLOOP0: ldi R30, $ff
WGLOOP1: ldi R31, $ff
WGLOOP2: dec R31
brne WGLOOP2
dec R30
brne WGLOOP1
dec R29
brne WGLOOP0
ret

1.6. Running LED

Running LED on Port C, start on LSB first

.include "m8535def.inc"
.org 0x0000
;
ldi r16,low(RAMEND)
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16
;
ldi R16,0b11111111
out DDRC,R16
ldi R16,0b11111110
sec
main:
out PORTC,R16
rol R16
rcall delay
;
rjmp main ; repaet until power removed :-)
; provides some delay so that the LED is visible
delay:
ldi R29, $20
WGLOOP0: ldi R30, $ff
WGLOOP1: ldi R31, $ff
WGLOOP2: dec R31
brne WGLOOP2
dec R30
brne WGLOOP1
dec R29
brne WGLOOP0
ret

Tidak ada komentar:

Posting Komentar