Код: Выделить всё
.include "tn13Adef.inc"
.def ZERO=R0
.def STR=R1
.def TEMP=R16
.def BRI=R17
.def FULL=R19
.def PWMR=R20
.def PWMG=R21
.def PWMB=R22
.def PWMC=R23
.def MASK=R24
.equ R=PB2
.equ G=PB1
.equ B=PB0
.equ CONT=PB3
.macro CJNE
CPI @0,@1
BRNE @2
.endm
.macro DJNZ
DEC @0 BRNE @1
.endm
.macro IJNZ
INC @0 BRNE @1
.endm
.macro OUTI
LDI TEMP,@1 OUT @0,TEMP
.endm
.macro DELAY_d100s ;Delay X/100 seconds
.if @0<256
LDI TEMP,@0
RCALL DEL
.else
LDI TEMP,255
RCALL DEL
LDI TEMP,(@0-255)
RCALL DEL
.endif
.endm
.macro SetColor ;Set RGB Color
MOV PWMR,@0 MOV PWMG,@1 MOV PWMB,@2
.endm
.macro WFI ;wait for interrupt
CLR R18
WAIT:CPI R18,0 ;(PIN Cnange)
BREQ WAIT
CLR R18
.endm
.cseg
CLR ZERO ;Clear ZERO
RJMP RESET ;RESET
SER R18 ;PCINT
RETI ;T0_OV
RETI ;EE_RDY
RETI ;ANA_COMP
RETI ;T0=A
RETI ;T0=B
RETI ;WDT
PWM: IN STR,SREG ;ADC
INC PWMC ;increment PWM counter
LDI MASK,((1<<CONT)>>3)
CP PWMC,PWMR ;compare counter and RED value
ROL MASK
CP PWMC,PWMG ;compare with GREEN
ROL MASK
CP PWMC,PWMB ;compare with BLUE
ROL MASK
OUT PORTB,R24 ;apply MASK to port
OUT SREG,STR
RETI
RESET: SER FULL
OUTI CLKPR,(1<<CLKPCE) ;Reset Fclk/8 Divider
OUT CLKPR,ZERO
OUTI SPL,low(RAMEND) ;Stack
OUTI DDRB,(1<<R)|(1<<G)|(1<<B) ;Set PORT Direction
OUTI PORTB,(1<<CONT) ;Set PORT PullUPs
OUTI ADMUX,(1<<ADLAR)|(1<<MUX1)|(1<<MUX0)
OUTI ADCSRA,(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(1<<ADATE)|(1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0)
OUT ADCSRB,ZERO
OUTI PCMSK,(1<<CONT) ;Set Pin Change
OUTI GIMSK,(1<<PCIE) ;interrupt
SetColor ZERO,ZERO,ZERO ;Turn off all LEDs
SEI
RES:CLR BRI
CLR R18
SBIS PINB,CONT
RJMP SEQ2
SEQ1: Delay_d100s 50 ;Black -> Yellow 2 min (351/100s 256 ~ 15 min)
SetColor BRI,BRI,ZERO
CPI R18,0
BRNE RES
IJNZ BRI,SEQ1
SER BRI
LOOP1: Delay_d100s 24 ;Yellow -> Red 1 min
SetColor FULL,BRI,ZERO
CPI R18,0
BRNE RES
DJNZ BRI,LOOP1
WFI ;wait for pin change
SEQ2: Delay_d100s 24 ;Red -> Yellow 1 min
SetColor FULL,BRI,ZERO
CPI R18,0
BRNE RES
IJNZ BRI,SEQ2
SER BRI
LOOP3: Delay_d100s 50 ;Yellow -> Black 2 min
SetColor BRI,BRI,ZERO
CPI R18,0
BRNE RES
DJNZ BRI,LOOP3
LOOP4: Delay_d100s 24 ;Black -> Blue 1 min
MOV R2,BRI ;копируем
LSR R2 ;делим на 2 сдвигом вправо
SetColor ZERO,ZERO,R2
CPI R18,0
BRNE RES
IJNZ BRI,LOOP4
LOOP5: Delay_d100s 24 ;Blue -> Black 1 min
MOV R2,BRI ;копируем
LSR R2 ;делим на 2 сдвигом вправо
SetColor ZERO,ZERO,R2
CPI R18,0
BREQ XXX1
RJMP RES
CJNE R18,0,RES
XXX1:DJNZ BRI,LOOP5
WFI ;wait for pin change
RJMP SEQ1
DEL: LDI XH,0x54 ;Delay
LDI XL,0x73
SBIW XL,1
BRNE PC-1
DEC TEMP
BRNE DEL
RET