📄 oc2上输出矩形波.s
字号:
.module OC2上输出矩形波.c
.area text(rom, con, rel)
.dbfile E:\xiaozhiyong2009\设计资料\项目设计\ICCAVR_mega8_C\ICCAVR_mega8_OC2上输出矩形波\OC2上输出矩形波.c
.dbfunc e port_init _port_init fV
.even
_port_init::
.dbline -1
.dbline 9
; //ICC-AVR application builder : 2009-3-11 13:07:26
; // 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_CTC _init_timer2_CTC fV
; p -> R16
.even
_init_timer2_CTC::
.dbline -1
.dbline 42
; //all peripherals are now initialized
; }
;
; /************************************
; 用 途:TC2用CTC模式输出频率可调矩形波
; Taget :mega8
; crystal :8M
; 介 绍:初始化以后就可以在OC2上输出矩形波了,
; 但OC2必须设为输出(PB3)
; 入口参数:p可用来调节频率,为0-255中的一个
; 出口参数:
; *************************************/
; void init_timer2_CTC(unsigned char p)
; {
.dbline 43
; DDRB|=(1<<PB3);
sbi 0x17,3
.dbline 44
; TCCR2=0x0;//TC2工作于定时方式,先停止TC2
clr R2
out 0x25,R2
.dbline 45
; TCNT2=0x00;//计数器初始值设为0
out 0x24,R2
.dbline 46
; OCR2=p;//设置比较参考值
out 0x23,R16
.dbline 47
; TIMSK|=(1<<OCIE2);//打开TOV2的比较区配中断
in R24,0x39
ori R24,128
out 0x39,R24
.dbline 51
; //TC2工作于定时方式CTC(WGM2[1:0]=2)
; //比较匹配时触发OC2取反(COM2[1:0]=1)
; //系统时钟256分频(CS2[2:0]=0xC)
; TCCR2=(1<<WGM21)|(1<<COM20)|(1<<CS22)|(1<<CS21);
ldi R24,30
out 0x25,R24
.dbline -2
L3:
.dbline 0 ; func end
ret
.dbsym r p 16 c
.dbend
.dbfunc e timer2_CTC _timer2_CTC fV
; x -> R16
.even
_timer2_CTC::
.dbline -1
.dbline 55
; }
; //改变频率
; void timer2_CTC(unsigned char x)
; {
.dbline 56
; OCR2=x;
out 0x23,R16
.dbline -2
L4:
.dbline 0 ; func end
ret
.dbsym r x 16 c
.dbend
.dbfunc e main _main fV
.even
_main::
.dbline -1
.dbline 60
; }
;
; void main()
; {
.dbline 61
; port_init();
rcall _port_init
.dbline 62
; init_devices();
rcall _init_devices
.dbline 64
;
; init_timer2_CTC(20);
ldi R16,20
rcall _init_timer2_CTC
L6:
.dbline 66
; while(1)
; {
.dbline 67
; ;
.dbline 68
; }
L7:
.dbline 65
rjmp L6
X0:
.dbline -2
L5:
.dbline 0 ; func end
ret
.dbend
; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -