ledpwm.asm --- drive a blue LED with PWM 21/04/2006 ; Copyright (c) 2006 John Honniball .include "tn2313def.inc" ;.include "m8def.inc" .org 0x0000 ; Blue LED on Port B bit 2 .equ LEDPORT = PortB .set LEDBIT =0 ; ##################### equ durch set ersetzt, um die leds durchschalten zu können ### ; This program drives a single LED connected to the AVR's I/O port. It ; is connected so that the cathode of the LED is wired to the AVR pin, ; and the anode of the LED is wired to the 5V power supply via a ; resistor. The value of that resistor depends on the colour of the LED, ; but is usually a few hundred ohms. ; We control the brightness of the LED with Pulse Width Modulation (PWM), ; for two reasons. Firstly, we have no analog outputs on the AVR chip, ; only digital ones. Secondly, a LED's brightness does not respond ; linearly to variations in supply voltage, but it responds much better ; to PWM. ; Pulsating LED looks better if it never quite goes "off", but cycles from ; full brightness to a dim state, and back again .equ MINBRIGHT = 100 .equ MAXBRIGHT = 255 ; This value controls how fast the LED cycles from bright to dim. It is ; the number of PWM cycles that we generate for each step in the brightness ; ramp, up and down. Larger numbers will make the pulsation slower. .equ NCYCLES =1 ; Start of program execution after a Reset ldi r16,low(RAMEND) ; Initialise stack to top of RAM out SPL,r16 ;ldi r16,high(RAMEND) ;out SPH,r16 ; Initialise the hardware ldi r16,0b00000111 ; Set Port B to all outputs out DDRB,r16 ldi r16, 0xFF out PORTB, r16 ldi r16, 0x00 ; ddrd als eingang out DDRD, r16 ldi r16, 0xFF ; An allen Pins vom Port D die Pullup-Widerstände out PORTD, r16 nop ; ldi r21, 0 ; r19 0 sbi LEDPORT,LEDBIT ; Switch off blue LED by setting output pin high ; Start with LED at its lowest level, then ramp up to maximum dopwm: ldi r17,MINBRIGHT ; R17 holds current brightness level l1: ldi r18,NCYCLES ; R18 counts PWM cycles, and hence pulsation speed l2: cbi LEDPORT,LEDBIT ; Output pin low, LED on mov r16,r17 ; R16 controls length of delay (= R17) rcall delayn4us ; Call delay subroutine sbi LEDPORT,LEDBIT ; Output pin high, LED off ldi r16,255 sub r16,r17 ; R16 controls length of delay (= 255 - R17) rcall delayn4us ; Call delay subroutine dec r18 ; Decrement PWM cycle counter brne l2 inc r17 ; Increase brightness by one step ;############################# mein part den ich hinzugefügt habe ############################# sbic PIND, 0 ; ist taster 1 gedrückt dann überspringe rjmp ll9 rjmp ll9 inc r21 ; erhöhe r21 cpi r21,3 ;vergleiche r21 mit 3 breq setnew ;ist r21=3 dann springe zu "setnew" rjmp ll8 ; springe zu "ll8" setnew: ldi r21,0 ; setze den zähler r21 auf 0 ll8: cpi r21,0 ; vergleiche r21 mit 0 brne led2 ; wenn r21 nicht 0 ist dann springe zu led2 .set LEDBIT =0 ;das ledbit setzen rjmp ll9 ; zum ende des codeblocks springen led2: cpi r21,1 ;vergleiche r21 mit 1 brne led3 ;wenn r21 nicht 1 ist dann springe zu led2 .set LEDBIT =1 ;das ledbit setzen rjmp ll9 ;zum ende des codeblocks springen led3: cpi r21,2 ;vergleiche r21 mit 2 brne ll9 ;wenn r21 nicht 2 ist dann springe zum ende des codeblocks .set LEDBIT =2 ;das ledbit setzen ll9: ;############################# mein part den ich hinzugefügt habe ende ############################# brne l1 ; Now ramp back down to the minimum brightness ldi r17,MAXBRIGHT ; R17 holds current brightness level l3: ldi r18,NCYCLES ; R18 counts PWM cycles, and hence pulsation speed l4: cbi LEDPORT,LEDBIT ; Output pin low, LED on mov r16,r17 ; R16 controls length of delay (= R17) rcall delayn4us ; Call delay subroutine sbi LEDPORT,LEDBIT ; Output pin high, LED off ldi r16,255 sub r16,r17 ; R16 controls length of delay (= 255 - R17) rcall delayn4us ; Call delay subroutine dec r18 ; Decrement PWM cycle counter brne l4 dec r17 ; Decrease brightness by one step cpi r17,MINBRIGHT ; Have we reached the minimum? brne l3 rjmp dopwm ; Loop back to start ; DELAYN4US ; Delay for (R16 * 4) microseconds delayn4us: tst r16 ; R16 = 0? (no delay) breq dly4 dly2: ldi r24,low(16) ldi r25,high(16) dly3: sbiw r24,1 ; 2 cycles brne dly3 ; 2 cycles dec r16 brne dly2 dly4: ret ; Return to caller