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

📄 pwm.s

📁 采用AVR16单片机写的pwm输出程序
💻 S
字号:
	.module pwm.c
	.area data(ram, con, rel)
_Table::
	.blkb 2
	.area idata
	.byte 63,6
	.area data(ram, con, rel)
	.blkb 2
	.area idata
	.byte 91,'O
	.area data(ram, con, rel)
	.blkb 2
	.area idata
	.byte 'f,'m
	.area data(ram, con, rel)
	.blkb 2
	.area idata
	.byte 125,7
	.area data(ram, con, rel)
	.blkb 2
	.area idata
	.byte 127,'o
	.area data(ram, con, rel)
	.blkb 1
	.area idata
	.byte 'y
	.area data(ram, con, rel)
	.blkb 1
	.area idata
	.byte 0
	.area data(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.dbsym e Table _Table A[12:12]c
_Data1::
	.blkb 2
	.area idata
	.byte 10,0
	.area data(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.blkb 2
	.area idata
	.byte 5,0
	.area data(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.dbsym e Data1 _Data1 A[4:4]c
_Data2::
	.blkb 2
	.area idata
	.byte 3,9
	.area data(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.blkb 2
	.area idata
	.byte 1,0
	.area data(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.dbsym e Data2 _Data2 A[4:4]c
	.area text(rom, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
	.dbfunc e delay_us _delay_us fV
;              t -> R16,R17
	.even
_delay_us::
	.dbline -1
	.dbline 16
; //***************FileName:PWM.C***************//
; //***************ICCAVR V6.30编译*****************//
; 
; #include <iom16v.h>                 
; #define uchar unsigned char
; #define uint  unsigned int
; 
; //数码管字型表,对应0,1,2,3,4,5,6,7,8,9,E//
; uchar Table[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79};
; uchar Data1[4]={10,0,5,0};      //定义初始方波占空比:50%
; uchar Data2[4]={3,9,1,0};            //设定PWM频率:8M/2046=3910Hz 
; uchar Key1_Flag,Key2_Flag;
;  
; /////////////////////////////延时///////////////////////////////////////////
; void delay_us( int t)                    //us
;     {
L2:
	.dbline 18
; 	do
;     t--;
	subi R16,1
	sbci R17,0
L3:
	.dbline 19
;     while(t>1);
	ldi R24,1
	ldi R25,0
	cp R24,R16
	cpc R25,R17
	brlt L2
	.dbline -2
	.dbline 20
;     }
L1:
	.dbline 0 ; func end
	ret
	.dbsym r t 16 I
	.dbend
	.dbfunc e delay_ms _delay_ms fV
;              t -> R20,R21
	.even
_delay_ms::
	xcall push_gset1
	movw R20,R16
	.dbline -1
	.dbline 22
; void delay_ms(unsigned int t)               //ms
;     {
	xjmp L7
L6:
	.dbline 24
	.dbline 25
	ldi R16,1142
	ldi R17,4
	xcall _delay_us
	.dbline 26
	subi R20,1
	sbci R21,0
	.dbline 27
L7:
	.dbline 23
;     while(t!=0)
	cpi R20,0
	cpc R20,R21
	brne L6
X0:
	.dbline -2
	.dbline 28
;      {
;       delay_us(1142);
;       t--;
;      }
;     }          
L5:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r t 20 i
	.dbend
	.dbfunc e Display _Display fV
;            sel -> R20
;              i -> R22
;              p -> R10,R11
	.even
_Display::
	xcall push_gset3
	movw R10,R16
	.dbline -1
	.dbline 36
; /*void DelayMs(uint i)           //Ms级延时,参数i为延时时间
; {uint j;
;  {for(j=8000;j!=0;j--)  for(;i!=0;i--)
;  {;}}
; }*/
; 
; void Display(uchar *p)         //动态显示函数,参数p为待显示的数组名
; {uchar i,sel=0x01;           
	.dbline 36
	ldi R20,1
	.dbline 37
;  for(i=0;i<4;i++)
	clr R22
	xjmp L13
L10:
	.dbline 38
	.dbline 38
	out 0x1b,R20
	.dbline 40
	mov R30,R22
	clr R31
	add R30,R10
	adc R31,R11
	ldd R30,z+0
	clr R31
	ldi R24,<_Table
	ldi R25,>_Table
	add R30,R24
	adc R31,R25
	ldd R2,z+0
	out 0x15,R2
	.dbline 41
	ldi R16,1000
	ldi R17,3
	xcall _delay_us
	.dbline 42
	lsl R20
	.dbline 43
L11:
	.dbline 37
	inc R22
L13:
	.dbline 37
	cpi R22,4
	brlo L10
	.dbline -2
	.dbline 44
;   {PORTA=sel;                  //选通最右边的数码管
;    //PORTC=~(0xff-Table[p[i]]);     //送字型码
;    PORTC=Table[p[i]];     //送字型码
;    delay_us(1000);                 //显示延时    
;    sel=sel<<1;                 //移位以显示前一位
;   }
; }
L9:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r sel 20 c
	.dbsym r i 22 c
	.dbsym r p 10 pc
	.dbend
	.dbfunc e Add_Process _Add_Process fV
;              p -> R16,R17
	.even
_Add_Process::
	.dbline -1
	.dbline 47
; 
; void Add_Process(uchar *p)
; {if(p[1]!=1)
	.dbline 47
	movw R30,R16
	ldd R24,z+1
	cpi R24,1
	breq L15
	.dbline 48
;   {p[2]=p[2]+1;
	.dbline 48
	movw R24,R16
	adiw R24,2
	movw R30,R24
	ldd R24,z+0
	subi R24,255    ; addi 1
	std z+0,R24
	.dbline 49
;    if(p[2]==10) {p[2]=0;p[1]++;}
	movw R30,R16
	ldd R24,z+2
	cpi R24,10
	brne L17
	.dbline 49
	.dbline 49
	clr R2
	movw R30,R16
	std z+2,R2
	.dbline 49
	movw R24,R16
	adiw R24,1
	movw R30,R24
	ldd R24,z+0
	subi R24,255    ; addi 1
	std z+0,R24
	.dbline 49
L17:
	.dbline 50
L15:
	.dbline -2
	.dbline 51
;   }
; }
L14:
	.dbline 0 ; func end
	ret
	.dbsym r p 16 pc
	.dbend
	.dbfunc e Sub_Process _Sub_Process fV
;           temp -> R22
;              p -> R20,R21
	.even
_Sub_Process::
	xcall push_gset2
	movw R20,R16
	.dbline -1
	.dbline 54
	.dbline 55
	.dbline 56
	movw R30,R20
	ldd R2,z+1
	ldi R24,10
	mul R24,R2
	mov R22,R0
	movw R30,R20
	ldd R2,z+2
	add R22,R2
	breq L20
	.dbline 57
	.dbline 57
	.dbline 58
	dec R22
	ldi R17,10
	mov R16,R22
	xcall div8u
	movw R30,R20
	std z+1,R16
	.dbline 59
	movw R30,R20
	ldd R2,z+1
	ldi R24,10
	mul R24,R2
	mov R2,R22
	sub R2,R0
	movw R30,R20
	std z+2,R2
	.dbline 60
L20:
	.dbline -2
	.dbline 61
; 
; void Sub_Process(uchar *p)
; {uchar temp;
;  temp=p[1]*10+p[2];
;  if(temp!=0)
;   {temp=temp-1;
;    p[1]=temp/10;
;    p[2]=temp-p[1]*10;
;   }
; }
L19:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r temp 22 c
	.dbsym r p 20 pc
	.dbend
	.dbfunc e Key_Process _Key_Process fV
	.even
_Key_Process::
	.dbline -1
	.dbline 64
	xjmp L24
L23:
	.dbline 64
	.dbline 64
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Display
	.dbline 64
	ldi R24,1
	sts _Key1_Flag,R24
	.dbline 64
L24:
	.dbline 64
;  
; void Key_Process()
; {while((PINB&0x01)==0) {Display(Data1);Key1_Flag=1;}  
	sbis 0x16,0
	rjmp L23
	xjmp L27
L26:
	.dbline 65
	.dbline 65
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Display
	.dbline 65
	ldi R24,1
	sts _Key2_Flag,R24
	.dbline 65
L27:
	.dbline 65
;  while((PINB&0x02)==0) {Display(Data1);Key2_Flag=1;}
	sbis 0x16,1
	rjmp L26
	.dbline 66
;  if(Key1_Flag==1)
	lds R24,_Key1_Flag
	cpi R24,1
	brne L29
	.dbline 67
;      {Add_Process(Data1);           
	.dbline 67
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Add_Process
	.dbline 68
; 	  Key1_Flag=0;}
	clr R2
	sts _Key1_Flag,R2
	.dbline 68
L29:
	.dbline 69
	lds R24,_Key2_Flag
	cpi R24,1
	brne L31
	.dbline 70
	.dbline 70
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Sub_Process
	.dbline 71
	clr R2
	sts _Key2_Flag,R2
	.dbline 71
L31:
	.dbline -2
	.dbline 72
;  if(Key2_Flag==1)
;      {Sub_Process(Data1);
; 	  Key2_Flag=0;} 
; }
L22:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e Set_Process _Set_Process fV
;              i -> R20,R21
;              p -> R20,R21
	.even
_Set_Process::
	xcall push_gset1
	movw R20,R16
	.dbline -1
	.dbline 75
; //??????????????????????????//	    
; void Set_Process(uchar *p)
; {uint i;
	.dbline 76
;  i=p[1]*10+p[2];
	.dbline 77
;  i=1023*i/10;
	.dbline 78
;  OCR1AH=i>>8;
	movw R30,R20
	ldd R2,z+1
	ldi R24,10
	mul R24,R2
	movw R2,R0
	movw R30,R20
	ldd R4,z+2
	clr R5
	add R2,R4
	adc R3,R5
	ldi R16,1023
	ldi R17,3
	movw R18,R2
	xcall empy16s
	ldi R18,10
	ldi R19,0
	xcall div16u
	movw R20,R16
	movw R2,R20
	mov R2,R3
	clr R3
	out 0x2b,R2
	.dbline 79
;  OCR1AL=i&0x00ff;
	movw R24,R20
	andi R25,0
	out 0x2a,R24
	.dbline -2
	.dbline 80
; }
L33:
	xcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbsym r i 20 i
	.dbsym r p 20 pc
	.dbend
	.dbfunc e Init_IO _Init_IO fV
	.even
_Init_IO::
	.dbline -1
	.dbline 83
; 
; void Init_IO(void)             //初始化I/O口
; {DDRA=0xff;                    //设置A口为推挽1输出
	.dbline 83
	ldi R24,255
	out 0x1a,R24
	.dbline 84
;  PORTA=0xff;
	out 0x1b,R24
	.dbline 85
;  DDRC=0xff;                    //设置C口为推挽1输出             
	out 0x14,R24
	.dbline 86
;  PORTC=0xff;
	out 0x15,R24
	.dbline 87
;  DDRB=0x00;                    //设置B口为三态输入
	clr R2
	out 0x17,R2
	.dbline 88
;  PORTB=0x00;
	out 0x18,R2
	.dbline 89
;  DDRD=0xff;                    //设置D口为推挽1输出
	out 0x11,R24
	.dbline 90
;  PORTD=0xff;    
	out 0x12,R24
	.dbline -2
	.dbline 91
; }
L34:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e main _main fV
;              i -> R20
	.even
_main::
	.dbline -1
	.dbline 94
; 
; void main(void)
; {uchar i;
	.dbline 95
;  Init_IO();                    //初始化I/O口
	xcall _Init_IO
	.dbline 96
;  PORTA=0xff;                   //点亮以测试所有的数码管
	ldi R24,255
	out 0x1b,R24
	.dbline 97
;  PORTC=0xff;   
	out 0x15,R24
	.dbline 99
;  //delay_ms(100);                 //延时
;  PORTC=0x00;                   //熄灭所有的数码管
	clr R2
	out 0x15,R2
	.dbline 100
;  TCCR1A=0xc3;                  //10位PWM,向上计数清除OC1A,向下计数置位OC1A
	ldi R24,195
	out 0x2f,R24
	.dbline 101
;  TCCR1B=0x02;                  //时钟8分频,最小频率1M/2046=0.5KHz    
	ldi R24,2
	out 0x2e,R24
	xjmp L37
L36:
	.dbline 103
;  while(1)
;  {Key_Process();
	.dbline 103
	xcall _Key_Process
	.dbline 104
;   Set_Process(Data1);         //设置PWM占空比
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Set_Process
	.dbline 105
;   for(i=500;i!=0;i--)
	ldi R20,244
	xjmp L42
L39:
	.dbline 106
	.dbline 106
	xcall _Key_Process
	.dbline 107
	ldi R16,<_Data1
	ldi R17,>_Data1
	xcall _Display
	.dbline 107
L40:
	.dbline 105
	dec R20
L42:
	.dbline 105
	tst R20
	brne L39
	.dbline 108
;   {  Key_Process();
;     Display(Data1);}
;   for(i=500;i!=0;i--)
	ldi R20,244
	xjmp L46
L43:
	.dbline 109
	.dbline 109
	xcall _Key_Process
	.dbline 110
	ldi R16,<_Data2
	ldi R17,>_Data2
	xcall _Display
	.dbline 110
L44:
	.dbline 108
	dec R20
L46:
	.dbline 108
	tst R20
	brne L43
	.dbline 111
L37:
	.dbline 102
	xjmp L36
X1:
	.dbline -2
	.dbline 112
;    { Key_Process();
;     Display(Data2);}
;  }
; }
L35:
	.dbline 0 ; func end
	ret
	.dbsym r i 20 c
	.dbend
	.area bss(ram, con, rel)
	.dbfile F:\练习\pwm脉宽调制器\pwm.c
_Key2_Flag::
	.blkb 1
	.dbsym e Key2_Flag _Key2_Flag c
_Key1_Flag::
	.blkb 1
	.dbsym e Key1_Flag _Key1_Flag c

⌨️ 快捷键说明

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