⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led.s

📁 1.利用PWM控制占空比实现LED的亮暗。 2.可以用UP DOWM键进行控制亮暗。
💻 S
字号:
	.module LED.c
	.area text(rom, con, rel)
	.dbfile F:\AVR资料\台灯\LED.c
	.dbfunc e port_init _port_init fV
	.even
_port_init::
	.dbline -1
	.dbline 13
; //ICC-AVR application builder // Target : M16  
;       // Crystal: 4.0000Mhz  
;       #include <iom16v.h>  
;       #include <macros.h>  
;       #define uchar unsigned char 
;       #define uint unsigned int 
;       void port_init(void); 
;       void timer0_init(void); 
;       void init_devices(void); 
;       void delay_short(uint t); 
;       uchar scan_key(void); 
;       void port_init(void)  
;       {  
	.dbline 14
;       PORTA = 0x00;  
	clr R2
	out 0x1b,R2
	.dbline 15
;       DDRA   = 0x00;  
	out 0x1a,R2
	.dbline 16
;       PORTB = BIT(PB3);  
	ldi R24,8
	out 0x18,R24
	.dbline 17
;       DDRB   = BIT(PB3);  
	out 0x17,R24
	.dbline 18
;       PORTC = 0x00; //m103 output only  
	out 0x15,R2
	.dbline 19
;       DDRC   = 0x00;  
	out 0x14,R2
	.dbline 20
;       PORTD = 0x07;  
	ldi R24,7
	out 0x12,R24
	.dbline 21
;       DDRD   = 0x00;  
	out 0x11,R2
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e timer0_init _timer0_init fV
	.even
_timer0_init::
	.dbline -1
	.dbline 27
;       }  
;       // WGM: PWM Phase correct 
;       // desired value: 1KHz 
;       // actual value:   0.980KHz (-2.0%) 
;       void timer0_init(void)  
;       {  
	.dbline 28
;       TCCR0 = 0x00; //stop  
	clr R2
	out 0x33,R2
	.dbline 29
;       TCNT0 = 0x01; //set count  
	ldi R24,1
	out 0x32,R24
	.dbline 30
;       OCR0   = 0xFF;   //set compare  
	ldi R24,255
	out 0x3c,R24
	.dbline 31
;       TCCR0 = 0x62; //start timer ; 相位修正, 8分頻 
	ldi R24,98
	out 0x33,R24
	.dbline -2
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e init_devices _init_devices fV
	.even
_init_devices::
	.dbline -1
	.dbline 35
;       }  
;       //call this routine to initialize all peripherals  
;       void init_devices(void)  
;       {  
	.dbline 37
;       //stop errant interrupts until set up  
;       CLI(); //disable all interrupts  
	cli
	.dbline 38
;       port_init();  
	xcall _port_init
	.dbline 39
;       timer0_init();  
	xcall _timer0_init
	.dbline 40
;       MCUCR = 0x00;  
	clr R2
	out 0x35,R2
	.dbline 41
;       GICR   = 0x00;  
	out 0x3b,R2
	.dbline 42
;       TIMSK = 0x00; //timer interrupt sources  
	out 0x39,R2
	.dbline 43
;       SEI(); //re-enable interrupts  
	sei
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e delay_short _delay_short fV
;              i -> R20,R21
;              t -> R16,R17
	.even
_delay_short::
	xcall push_gset1
	.dbline -1
	.dbline 47
;       //all peripherals are now initialized  
;       } 
;       void delay_short(uint t) // 短延時 
;       { 
	.dbline 49
	clr R20
	clr R21
	xjmp L8
L5:
	.dbline 49
L6:
	.dbline 49
	subi R20,255  ; offset = 1
	sbci R21,255
L8:
	.dbline 49
;          uint i; 
;          for (i=0;i<t;i++); 
	cp R20,R16
	cpc R21,R17
	brlo L5
	.dbline -2
L4:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r i 20 i
	.dbsym r t 16 i
	.dbend
	.dbfunc e scan_key _scan_key fc
;              v -> R20
	.even
_scan_key::
	xcall push_gset1
	.dbline -1
	.dbline 52
;       } 
;       uchar scan_key(void)   // 按鍵掃瞄 
;       {  
	.dbline 55
;          uchar v; 
;          
;          v = 0;      
	clr R20
	.dbline 57
;          
;          if ((PIND & 0x07) != 0x07) 
	in R24,0x10
	andi R24,7
	cpi R24,7
	breq L19
	.dbline 58
;          { 
	.dbline 60
;         
;          if ((PIND & 0x01) == 0)  
	sbic 0x10,0
	rjmp L12
	.dbline 61
;          { 
	.dbline 62
;           v = 1; 
	ldi R20,1
	.dbline 63
;            delay_short(1000);   
	ldi R16,1000
	ldi R17,3
	xcall _delay_short
	.dbline 64
;          } 
L12:
	.dbline 66
;          
;          if ((PIND & 0x2) == 0)  
	sbic 0x10,1
	rjmp L14
	.dbline 67
;          { 
	.dbline 68
;            v = 2; 
	ldi R20,2
	.dbline 69
;            delay_short(1000);   
	ldi R16,1000
	ldi R17,3
	xcall _delay_short
	.dbline 70
;          } 
L14:
	.dbline 72
;           
;          if ((PIND & 0x4) == 0)  
	sbic 0x10,2
	rjmp L19
	.dbline 73
;          { 
	.dbline 74
;            v = 3; 
	ldi R20,3
	.dbline 75
;            delay_short(1000);   
	ldi R16,1000
	ldi R17,3
	xcall _delay_short
	.dbline 76
;          } 
	.dbline 77
;          }; 
	.dbline 77
L18:
	.dbline 78
L19:
	.dbline 78
;          while((PIND & 0x07) != 0x07);    // 判斷按鍵是不是放開    
	in R24,0x10
	andi R24,7
	cpi R24,7
	brne L18
	.dbline 79
;          return v;   
	mov R16,R20
	.dbline -2
L9:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r v 20 c
	.dbend
	.dbfunc e main _main fV
;         OCR0_V -> R20
;            key -> R22
	.even
_main::
	.dbline -1
	.dbline 82
;       } 
;       void main(void)  
;       {   
	.dbline 85
;       uchar key, OCR0_V; 
;         
;       init_devices();  
	xcall _init_devices
	.dbline 86
;       OCR0_V = 0xff; 
	ldi R20,255
	xjmp L23
L22:
	.dbline 89
;         
;       while(1) 
;       { 
	.dbline 90
;           key = scan_key(); 
	xcall _scan_key
	mov R22,R16
	.dbline 92
;           
;           if (key > 0) 
	clr R2
	cp R2,R16
	brsh L25
	.dbline 93
;           { 
	.dbline 94
;             if (key==1) // 減少佔空比 
	cpi R16,1
	brne L27
	.dbline 95
;            {  
	.dbline 96
;              OCR0_V -= 10; 
	subi R20,10
	.dbline 97
;              OCR0 = OCR0_V; 
	out 0x3c,R20
	.dbline 98
L27:
	.dbline 98
;            }; 
	.dbline 100
;            
;             if (key==2) // 增加佔空比 
	cpi R22,2
	brne L29
	.dbline 101
;            {  
	.dbline 102
;              OCR0_V += 10; 
	subi R20,246    ; addi 10
	.dbline 103
;              OCR0 = OCR0_V; 
	out 0x3c,R20
	.dbline 104
L29:
	.dbline 104
;            };     
	.dbline 106
;            
;             if (key==3) // 全黑,佔空比為100%  
	cpi R22,3
	brne L31
	.dbline 107
;            {  
	.dbline 108
;              OCR0_V = 0xff; 
	ldi R20,255
	.dbline 109
;              OCR0 = OCR0_V; 
	out 0x3c,R20
	.dbline 110
L31:
	.dbline 110
;            };       
	.dbline 111
;           } 
L25:
	.dbline 112
L23:
	.dbline 88
	xjmp L22
X0:
	.dbline 112
;       };  
	.dbline -2
L21:
	.dbline 0 ; func end
	ret
	.dbsym r OCR0_V 20 c
	.dbsym r key 22 c
	.dbend

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -