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

📄 oc2输出pwm.s

📁 ICC mega8例子
💻 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 + -