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

📄 main.s

📁 avr 单片机 的MEGA8系列
💻 S
字号:
	.module main.c
	.area text(rom, con, rel)
	.dbfile D:\AVR\M8_PWM\main.c
	.dbfunc e port_init _port_init fV
	.even
_port_init::
	.dbline -1
	.dbline 24
; 
; // -----------------------------------------------------------------------------
; // Project Name    : M8 PWM 参考程序
; // Crystal         : 16Mhz           
; // Target Setting  : ATMaga8
; // Power  		   : 5V
; // Date            : Dec.21.2006
; // File Version    : 1.0  
; // Author		   :
; // -----------------------------------------------------------------------------
; 
; #include <iom8v.h>
; #include <macros.h>
; 
; // ------------------  频率定义 --------------------------
; #define  HZ_5K     0x0640  
; #define  HZ_10K    0x0320   
; 
; // -----------------------------------------------------------------------------
; //  函数说明 :配置PWM的输出 管脚 OC1A (PB1) 
; //   
; // -----------------------------------------------------------------------------
; void port_init(void)
; {
	.dbline 25
;  PORTB |= 0x02;
	sbi 0x18,1
	.dbline 26
;  DDRB  |= 0x02;
	sbi 0x17,1
	.dbline -2
	.dbline 27
; }
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e timer1_init _timer1_init fV
	.even
_timer1_init::
	.dbline -1
	.dbline 33
; // -----------------------------------------------------------------------------
; //  函数说明 :配置T1的PWM 
; //  产生频率 :5KHz   误差 5.00048KHz (0.0%)
; // -----------------------------------------------------------------------------
; void timer1_init(void)
; {
	.dbline 38
;   //TCCR1A|=(1<<COM1A1)|(1<<COM1A0);  //当计时器往TOP计数时若TCNT1与OCR1A匹配OC1A  
;                                     //将清零为低电平                               
;                                     //在计时器往BOTTOM计数时TCNT1与OCR1A匹配OC1A
; 									//将置位为高电平 
;   TCCR1A|=(1<<COM1A1)|(0<<COM1A0);   //当计时器往TOP计数时若TCNT1与OCR1A匹配OC1A  
	in R24,0x2f
	ori R24,128
	out 0x2f,R24
	.dbline 43
;                                     //将清零为高电平                               
;                                     //在计时器往BOTTOM计数时TCNT1与OCR1A匹配OC1A
; 									//将置位为低电平 
; 									
;   TCCR1B|=(1<<WGM13);               //设置T1为相位与频率修正PWM模式  TOP值为ICR1
	in R24,0x2e
	ori R24,16
	out 0x2e,R24
	.dbline -2
	.dbline 44
; }
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e StartPwm _StartPwm fV
	.even
_StartPwm::
	.dbline -1
	.dbline 50
; // -----------------------------------------------------------------------------
; //  函数说明 : 开启 PWM 
; //   
; // -----------------------------------------------------------------------------
; void StartPwm(void)
; {
	.dbline 51
;   TCCR1B|=(1<<CS10);                        //启动T1 不分频 即启动PWM输出 
	in R24,0x2e
	ori R24,1
	out 0x2e,R24
	.dbline -2
	.dbline 52
; }
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e ClosePwm _ClosePwm fV
	.even
_ClosePwm::
	.dbline -1
	.dbline 58
; // -----------------------------------------------------------------------------
; //  函数说明 : 关闭 PWM 
; //   
; // -----------------------------------------------------------------------------
; void ClosePwm(void )
; {
	.dbline 59
;   TCCR1B&=~(1<<CS10);                      // 停止T1分频 ,停止PWM 输出
	in R24,0x2e
	andi R24,254
	out 0x2e,R24
	.dbline -2
	.dbline 60
; }
L4:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e SetRate _SetRate fV
;         i_rate -> R16,R17
	.even
_SetRate::
	.dbline -1
	.dbline 66
; // -----------------------------------------------------------------------------
; //  函数说明 :设置PWM的占空比 
; //  设置说明 :占空比 =   OCR1A / ICR1  ;
; // -----------------------------------------------------------------------------
; void SetRate(unsigned int i_rate)
; {
	.dbline 67
;   OCR1A= i_rate;       // OCR1A控制占空比 
	out 0x2b,R17
	out 0x2a,R16
	.dbline -2
	.dbline 68
; }
L5:
	.dbline 0 ; func end
	ret
	.dbsym r i_rate 16 i
	.dbend
	.dbfunc e SetFrequency _SetFrequency fV
;          i_fre -> R16,R17
	.even
_SetFrequency::
	.dbline -1
	.dbline 74
; // -----------------------------------------------------------------------------
; //  函数说明 :设置PWM 的频率
; //   
; // -----------------------------------------------------------------------------
; void SetFrequency(unsigned int i_fre)
; {
	.dbline 75
;     ICR1 = i_fre ;    //ICR1控制分辨率 2-16
	out 0x27,R17
	out 0x26,R16
	.dbline -2
	.dbline 76
; }
L6:
	.dbline 0 ; func end
	ret
	.dbsym r i_fre 16 i
	.dbend
	.dbfunc e init_devices _init_devices fV
	.even
_init_devices::
	.dbline -1
	.dbline 82
; // -----------------------------------------------------------------------------
; //  函数说明 :初始化系统
; //   
; // -----------------------------------------------------------------------------
; void init_devices(void)
; {
	.dbline 84
;  //stop errant interrupts until set up
;  CLI(); //disable all interrupts
	cli
	.dbline 85
;  port_init();
	rcall _port_init
	.dbline 86
;  timer1_init();
	rcall _timer1_init
	.dbline 88
; 
;  MCUCR = 0x00;
	clr R2
	out 0x35,R2
	.dbline 89
;  GICR  = 0x00;
	out 0x3b,R2
	.dbline 90
;  TIMSK = 0x00; //timer interrupt sources
	out 0x39,R2
	.dbline 91
;  SEI(); //re-enable interrupts
	sei
	.dbline -2
	.dbline 92
; }
L7:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e main _main fV
	.even
_main::
	.dbline -1
	.dbline 98
; // -----------------------------------------------------------------------------
; //  函数说明 :测试 T1的PWM 
; //  
; // -----------------------------------------------------------------------------
; void main(void)
; { 
	.dbline 99
;   init_devices();
	rcall _init_devices
	.dbline 100
;   SetFrequency(HZ_5K);
	ldi R16,1600
	ldi R17,6
	rcall _SetFrequency
	.dbline 101
;   SetRate(HZ_5K/4);  // 实际占空比 :0.24
	ldi R16,400
	ldi R17,1
	rcall _SetRate
	.dbline 102
;   StartPwm();
	rcall _StartPwm
L9:
	.dbline 104
	.dbline 105
	.dbline 106
L10:
	.dbline 103
	rjmp L9
X0:
	.dbline -2
	.dbline 107
;   while(1)
;   {
;     ;
;   }
; }
L8:
	.dbline 0 ; func end
	ret
	.dbend

⌨️ 快捷键说明

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