?? oc2輸出pwm.s
字號:
.module OC2輸出PWM.c
.area text(rom, con, rel)
.dbfile E:\xiaozhiyong2009\設計資料\項目設計\ICCAVR_mega8_C\ICCAVR_mega8_OC2輸出PWM\OC2輸出PWM.c
.dbfunc e port_init _port_init fV
.even
_port_init::
.dbline -1
.dbline 9
; //ICC-AVR application builder : 2009-3-11 13:30:36
; // Target : M8
; // Crystal: 8.0000Mhz
;
; #include <iom8v.h>
; #include <macros.h>
;
; void port_init(void)
; {
.dbline 10
; PORTB = 0x00;
clr R2
out 0x18,R2
.dbline 11
; DDRB = 0x00;
out 0x17,R2
.dbline 12
; PORTC = 0x00; //m103 output only
out 0x15,R2
.dbline 13
; DDRC = 0x00;
out 0x14,R2
.dbline 14
; PORTD = 0x00;
out 0x12,R2
.dbline 15
; DDRD = 0x00;
out 0x11,R2
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e init_devices _init_devices fV
.even
_init_devices::
.dbline -1
.dbline 20
; }
;
; //call this routine to initialize all peripherals
; void init_devices(void)
; {
.dbline 22
; //stop errant interrupts until set up
; CLI(); //disable all interrupts
cli
.dbline 23
; port_init();
rcall _port_init
.dbline 25
;
; MCUCR = 0x00;
clr R2
out 0x35,R2
.dbline 26
; GICR = 0x00;
out 0x3b,R2
.dbline 27
; TIMSK = 0x00; //timer interrupt sources
out 0x39,R2
.dbline 28
; SEI(); //re-enable interrupts
sei
.dbline -2
L2:
.dbline 0 ; func end
ret
.dbend
.dbfunc e init_timer2_PWM _init_timer2_PWM fV
; n -> R16
.even
_init_timer2_PWM::
.dbline -1
.dbline 41
; //all peripherals are now initialized
; }
;
; /************************************
; 用 途:用TC2的PWM進行相位可調輸出
; Taget :mega8
; crystal :8M
; 介 紹:輸出為OC2(PB3)
; 入口參數:n為0-255,代表占空比
; 出口參數:
; *************************************/
; void init_timer2_PWM(unsigned char n)
; {
.dbline 42
; DDRB=(1<<PB3);
ldi R24,8
out 0x17,R24
.dbline 43
; TCCR2=0;//TC2處于定時方式,先停止TC2
clr R2
out 0x25,R2
.dbline 44
; TCNT2=0;//設初值
out 0x24,R2
.dbline 45
; OCR2=255-n;//設比較參考值//恒定低電平
mov R2,R16
clr R3
ldi R24,255
ldi R25,0
movw R4,R24
sub R4,R2
sbc R5,R3
out 0x23,R4
.dbline 46
; TIMSK&=(~((1<<OCIE2)|(1<<TOIE2)));//關閉TOV2中斷
in R24,0x39
andi R24,63
out 0x39,R24
.dbline 48
; //打開TC2選擇相位可調PWM模式,64分頻
; TCCR2|=(1<<WGM20)|(1<<COM21)|(1<<COM20)|(1<<CS22);
in R24,0x25
ori R24,116
out 0x25,R24
.dbline -2
L3:
.dbline 0 ; func end
ret
.dbsym r n 16 c
.dbend
.dbfunc e timer2_PWM _timer2_PWM fV
; n -> R16
.even
_timer2_PWM::
.dbline -1
.dbline 52
; }
; //調節占空比
; void timer2_PWM(unsigned char n)
; {
.dbline 53
; OCR2=255-n;
mov R2,R16
clr R3
ldi R24,255
ldi R25,0
sub R24,R2
sbc R25,R3
out 0x23,R24
.dbline -2
L4:
.dbline 0 ; func end
ret
.dbsym r n 16 c
.dbend
.dbfunc e main _main fV
.even
_main::
.dbline -1
.dbline 57
; }
;
; void main()
; {
.dbline 58
; port_init();
rcall _port_init
.dbline 59
; init_devices();
rcall _init_devices
.dbline 61
;
; init_timer2_PWM(100);
ldi R16,100
rcall _init_timer2_PWM
L6:
.dbline 63
; while(1)
; {
.dbline 64
; ;
.dbline 65
; }
L7:
.dbline 62
rjmp L6
X0:
.dbline -2
L5:
.dbline 0 ; func end
ret
.dbend
; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -