?? tailc.asm
字號:
.file "tailC.asm"
;;
;; ATmega8 taillight circuit
;; Last edited: 14-7-2004
;; Purpose: An assembly language program that generates 5 different
;; static patterns with switching from pattern-to-pattern controlled
;; by the depression of one push-button switch (S2).
;;
;; A compared C code is followed by "--> " and before the assembly
;; List of all registers are defined in this code
;;------------------------------------------------;;
;; R16 --- PINC
;; R17 --- 0X08 ---> PORTC
;; R18 --- ser 0xFF -->DDRD
;; R19 --- ADCH & OCR1AL
;; R20 --- ADCH & OCR1AH
;; R21 --- TIFR
;; R22 ---
;; R23 ---
;; R24 --- switchNo
;; R25 --- pattern
;; R26 --- 0x00 ---> TCCR1A
;; R27 --- TCCR1B
;; R28 --- bounce0
;; R29 --- bounce1
;; R30 --- S2
;;------------------------------------------------;;
;; Access the register and register bit definitions
.include "avr-mega8.asm"
;; The variable 'pattern' is kept in register 25
;; Bit set means LED on (complemented before output)
;; --> unsigned char pattern = 0x00;
LDI R25,0x00 ;;Load immediate
;; The variable 'switchNo' is kept in register 18
;; --> unsigned int switchNo = 1;
LDI R24,0x01 ;;Load immediate
;; The variable 'switchNo' is kept in register 18
;; --> unsigned char S2 = 0x08;
LDI R30,0x08 ;;Load immediate
;; --> unsigned char S2Previous;
LDI R28,0x08 ;;Load immediate
;; Set up timer 1A control registers
;; --> TCCR1A = 0;
LDI R26,0x00 ;;Load immediate
OUT TCCR1A,R26 ;;Out to I/O location
;; Set up timer 1B : divide by 64 prescaler using OCR1A
;; Frequency of operation is about 62.5 kHz
;; --> TCCR1B = 0x0A;
LDI R27, 0x0B ;;Load immediate
OUT TCCR1B,R27 ;;Out to I/O location
;; Set OCR1A to 0x01
;; Set a delay time 0.5 ms. It check S2 every 0.5 ms
;; --> OCR1AH = 0x01 ;
LDI R20,0x01 ;;Logical OR with Immediate
OUT 0x2B,R20 ;;Out to I/O location
;; --> OCR1AL = 0x00 ;
LDI R19,0x00 ;;Logical OR with Immediate
OUT 0x2A,R19 ;;Out to I/O location
;; Set up Port D for output
;; --> DDRD = 0xFF;
SER R18 ;;Set all Bits in Register
OUT DDRD,R18 ;;Out to I/O location
loop:
;; Update LEDs
;; --> PORTD = pattern;
COM R25 ;;One's complement
OUT PORTD,R25 ;;Out to I/O location
;; Enable PC3 as inputs. (default)
;; --> DDRC &= ~(1 << PC3);
CBI DDRC,3 ;;Clear Bit in I/O Register
;; Enable pull-ups for tact switches.
;; --> PORTC = (1 << PC3);
LDI R17,0x08 ;;Load immediate
OUT PORTC,R17 ;;Out to I/O location
;; By the depression of one push-button switch (S2)
;; to switch 5 different static patterns.
;; S2 = 0 for S2 pressed; S2 = 1 for S2 unpressed
;; --> unsigned char S2 = (PINC & (1 << PC3));
IN R16,PINC ;;In from I/O location
AND R16,R17 ;;Logical AND
;; By press S2 one time, the patterns changes
;; --> if (!S2)
;; --> bounce0 ++;
;; --> else bounce1 = 1;
;; --> bounce0 = 0;
CPI R16,0x08 ;;Skip if bit in register clear
BRNE else_bounce
LDI R28,0x00 ;;Skip if bit in register clear
LDI R29,0x01
RJMP end_bounce
else_bounce:
INC R28 ;;Logical AND with Immediate
end_bounce:
;; --> if ((bounce1 == 1)&&(bounce0 == 8))
;; --> switchNo ++;
;; --> bounce1 = 0;
CPI R29,0x01 ;;Skip if bit in register clear
BRNE end_check
CPI R28,0x08 ;;Skip if bit in register clear
BRNE end_check
INC R24 ;;Logical AND with Immediate
LDI R29,0x00
end_check:
;; When press S2 six times, switch to the first patterns
;; --> if (switchNo == 6)
CPI R24,0x06 ;;Compare with immediate
BRNE end_switch_full ;;Branch if not equal
;; --> switchNo = 1;
LDI R24,0x01 ;;Load immediate
end_switch_full:
;; Form switchNo to decide which pattern will be used in this loop
;; --> if (switchNo == 1)
CPI R24,0x01 ;;Compare with immediate
BRNE else1 ;;Branch if not equal
;; --> pattern = 0x24;
LDI R25,0x24 ;;Load immediate
RJMP end_if ;;Relative jump
else1:
;; --> else if (switchNo == 2)
CPI R24,0x02 ;;Compare with immediate
BRNE else2 ;;Branch if not equal
;; --> pattern = 0x66;
LDI R25,0x66 ;;Load immediate
RJMP end_if ;;Relative jump
else2:
;; --> else if (switchNo == 3)
CPI R24,0x03 ;;Compare with immediate
BRNE else3 ;;Branch if not equal
;; --> pattern = 0x99;
LDI R25,0x99 ;;Load immediate
RJMP end_if ;;Relative jump
else3:
;; --> else if (switchNo == 4)
CPI R24,0x04 ;;Compare with immediate
BRNE else4 ;;Branch if not equal
;; --> pattern = 0x81;
LDI R25,0x81 ;;Load immediate
RJMP end_if ;;Relative jump
else4:
;; --> else
;; --> pattern = 0xFF;
SER R25 ;;Set all Bits in Register
RJMP end_if ;;Relative jump
end_if:
checktime:
;; Test OCF1A flag in TIFR
;; --> while (!(TIFR & (1 << OCF1A)));
IN R21,TIFR ;;In from I/O location
ANDI R21,0x10 ;;Logical AND with Immediate
BREQ checktime ;;Branch if Equal
;; OCF1A flag bit is set, so reset it (write 1 to it)
;; --> TIFR |= (1 << OCF1A);
ORI R21, 0x10 ;;Logical OR with Immediate
OUT TIFR, R21 ;;Out to I/O location
;; Continue forever
RJMP loop ;;Relative jump
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -