DSP汇编程序设计举例_第1页
DSP汇编程序设计举例_第2页
DSP汇编程序设计举例_第3页
DSP汇编程序设计举例_第4页
DSP汇编程序设计举例_第5页
已阅读5页,还剩53页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《DSP技术与应用》学时:32(含8学时实验)1第6章

TMS320C54XXDSP汇编程序设计举例

2例7-3(段定义及数据传送举例)

1.实现数组a[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}x[20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}的初始化,将两数组置于数据存储器中0100H开始的连续地址空间中(0100H-0127H);2.将数据存储器中的x[20]复制到数组y[20](0128H-013BH);3.将数据存储器中的a[20]写入到程序存储器PROM(2000H-2013H);4.程序存储器PROM中20个数据存入数据存储器DATA(0200H-0213H)。3

.mmregs.def_c_int00

.dataTBL:.word:0,1,2,3,4,5,6,7,8,9,10,11,12.word13,14,15,16,17,18,19.word1,1,1,1,1,1,1,1,1,1.word1,1,1,1,1,1,1,1,1,1PROM: .usect"PROM",20

.bssa,20

.bssx,20

.bssy,20DATA: .usect "DATA",20

汇编源程序:4_c_int00bstartnopnopstart:STM #a,AR1;a[20]={0,1,2,3,4,5,6,7,8,9,10, ;11,12,13,14,15,16,17,18,19} RPT #39 ;x[20]={1,1,1,1,1,1,1,1,1,1

MVPDTBL,*AR1+;1,1,1,1,1,1,1,1,1,1}

;完成任务1STM#x,AR2;将数据存储器中的数组x[20]复制到数组y[20] STM #y,AR3;y[20]={1,1,1,1,1,1,1,1,1,1RPT #19;1,1,1,1,1,1,1,1,1,1}

MVDD*AR2+,*AR3+

;完成任务2.text5

STM#a,AR1;数据存储器中a[20]写入程序存储器

LD #PROM,A;PROM段

STM #19,AR3LOOP1:WRITA*AR1+;完成任务3ADD#1,A,ABANZLOOP1,*AR3-LD#PROM-1,A;程序存储器PROM中20个数据存入数据

STM #DATA,AR1;存储器DATA段

ST #19,BRCRPTBLOOP2ADD#1,A,ALOOP2:READA*AR1+

;该指令为单字指令(完成任务4)WAIT: NOPBWAIT.end6链接命令文件:

MEMORY{PAGE0: RAM:origin=1000h,length=800h RAM1:origin=2000h,length=300hPAGE1:DARAM1:origin=0100h,length=100hDARAM2:origin=0200h,length=100h}SECTIONS{.data:>RAM PAGE0.text:>RAM PAGE0

PROM :>RAM1PAGE0.bss :>DARAM1PAGE1DATA :>DARAM2PAGE1}7定点DSP中数的定标有Q和S两种表示方法(如下表所示),16位的二进制中有一个符号位、15-Q个整数位"."Q个小数位。DSP中小数的表示一般情况下以Q0格式表示整数以Q15格式表示小数。两个Q15格式的小数相乘结果为Q30格式小数,出现符号位冗余,解决方法是将FRCT置1,系统自动将相乘结果左移1位(即x2),精度允许时只保存高16位Q15定标:8[例7-4]使用C54汇编语言编程计算并观察计算结果:z1=x1+y1;z2=x1-y1;z3=x1*y1;z4=x2*y2.

其中,x1=20,y1=54,x2=0.5,y2=-0.5837

例7-4

(整数、小数运算举例)9

汇编源程序:

.title“suanshu.asm”.mmregs.defstart,_c_int00.bssx1,1

;地址0400h.bssx2,1

;地址0401h.bssy1,1

;地址0402h.bssy2,1

;地址0403h.bssz1,1

;地址0404h.bssz2,1

;地址0405h.bssz3_h,1

;地址0406h.bssz3_l,1

;地址0407h.bssz4,1

;地址0408hv1.set014Hv2.set036Hv3.set04000H

;x2=0.5v4.set0b548H

;y2=-0.5837.text_c_int00bstartnopnopstart:LD#x1,DP

;(DP=?)

ST#v1,x1ST#v2,y1LDx1,A

ADDy1,ASTLA,z1

;(单元地址?)NOPLDx1,A

SUBy1,ASTLA,z2

;(单元地址?)NOP

RSBXFRCTLDx1,T

MPYy1,A

STHA,z3_h

;(单元地址?)STLA,z3_l

;(单元地址?)NOPST#v3,x2ST#v4,y2

SSBXFRCTLDx2,16,A

MPYAy2

STH

B,z4

;(单元地址?)NOPend:Bend.end

例7-4(数据运算举例)10链接命令文件:suanshu.obj-msuanshu.map-osuanshu.outMEMORY{PAGE0:ROM1:origin=0080h,length=1000hROM2:origin=0060h,length=10hPAGE1:OTHER:origin=0400h,length=40h

}SECTIONS{.text:{}>ROM1PAGE0.data:{}>ROM1PAGE0.bss:{}>OTHERPAGE1.stack:{}>OTHERPAGE1}11

例7-5(小数乘法举例)

汇编源程序:

.title“chef.asm”.mmregs.defstart,_c_int00.bssx,4.bssa,4.bssy,1.datatable:

.word1*32768/10.word2*32768/10.word-3*32768/10.word4*32768/10.word8*32768/10.word6*32768/10.word-4*32768/10.word-2*32768/10

.text_c_int00bstartnopnop

start:

SSBXFRCTSTM#0,SWWSRSTM#x,AR1RPT#7MVPDtable,*AR1+STM#x,AR2STM#a,AR3STM#y,AR4RPTZA,#3

MAC*AR2+,*AR3+,A

STHA,*AR4done:Bdone.end12链接命令文件:

MEMORY{PAGE0: RAM:origin=1000h,length=800h RAM1:origin=2000h,length=300hPAGE1:DARAM1:origin=0100h,length=100hDARAM2:origin=0200h,length=100h}SECTIONS{.data:>RAM PAGE0.text:>RAM PAGE0PROM :>RAM1PAGE0.bss :>DARAM1PAGE1DATA :>DARAM2PAGE1}13例7-6

(带符号除法且商为小数)汇编源程序:

.title“shuf.asm”.mmregs.defstart,_c_int00.bssnum,1.bssden,1.bssquot,1.datatable.word4*32768/10;分子.word-8*32768/10;分母.text_c_int00bstartnopnop

start:STM#num,AR1;分子RPT#1MVPDtable,*AR1+STM#den,AR1

;分母LD*AR1-,16,A

MPYA*AR1+

ABSA

;分母绝对值STHA,*AR1-

LD*AR1,16,A

ABSA

;分子绝对值

STHA,*AR1LD*AR1+,A

RPT#14

;(15bit除法)SUBC*AR1,A

XC1,BLTNEGA

STLA,*(quot).end14

…..

.mmregs

.bssx,4

.bssy,2

.dataa .word1,2,3,4

.text STM#a,AR2 STM#x,AR3 LD#0,A LD*AR2+,T MAC*AR3+,A LD*AR2+,T

MAC*AR3+,A LD*AR2+,T MAC*AR3+,A LD*AR2,T MAC*AR3,A STLA,@y STHA,@y+1 …..算法一:举例1:乘加运算15链接命令文件:

MEMORY{PAGE0: RAM:origin=1000h,length=1000hPAGE1:DARAM:origin=2000h,length=1000h}SECTIONS{.text:>RAM

PAGE0.bss :>DARAMPAGE1.data

:>DARAM

PAGE1}16

…..

.mmregs

.bssx,4

.bssy,2

.dataa .word1,2,3,4

.text STM#a,AR2 STM#x,AR3 LD#0,A STM#3,BRC

RPTBss-1 LD*AR2+,T MAC*AR3+,Ass:STLA,@y STHA,@y+1 …..算法二:举例1:乘加运算17

…..

.mmregs

.bssa,4

.bssx,4

.bssy,2

.datatable .word1,2,3,4

.text STM#a,AR1 RPT#3

MVPDtable,*AR1+ LD#0,A

STM#a,AR2 STM#x,AR3 STM#3,BRC RPTBss-1 LD*AR2+,T MPY*AR3+,Ass: LD#a,DP STLA,@y STHA,@y+1 …..算法三:举例1:乘加运算18链接命令文件:

MEMORY{PAGE0: RAM:origin=1000h,length=1000hPAGE1:DARAM:origin=2000h,length=1000h}SECTIONS{.data:>RAM PAGE0.text:>RAM PAGE0.bss :>DARAMPAGE1}19

…..

.mmregs

.bssa,4

.bssx,4

.bssy,2

.datatable .word1,2,3,4

.word8,7,6,5

.text STM#a,AR1 RPT#7

MVPDtable,*AR1+

STM#a,AR2 STM#x,AR3 RPTZA,#3 MAC*AR2+,*AR3+,A LD#a,DP STLA,@y STHA,@y+1 …..算法四:举例1:乘加运算20

.title “DIV" .mmregs .def_c_int00DAT0 .set 01HDAT1 .set 02HDAT2 .set 03HDAT3.set 04H .databuff .space100h*16 .text_c_int00: Bstart举例2:除法运算21

start: LD#020h,DP ;置数据页指针

SSBXINTM ;禁止中断

DIV: ST#0008h,DAT0 ST#0002h,DAT1 RSBXSXM **无符号除法操作:DAT0÷DAT1;结果:DAT2:商;DAT3:余数

LDDAT0,A RPT#15 SUBCDAT1,A STLA,DAT2 STHA,DAT3.end22

.mmregs .bssa,4 .bssx,4 .bssy,1.def_c_int00 .datatable .word1,5,3,4 .word8,6,7,9.sect"vectors"rstB_c_int00 NOPNOP .text_c_int00STM#a,AR1 RPT#7 MVPDtable,*AR1+

CALLMAXend: Bend

MAX: STM#a,AR1 STM#x,AR2 STM#2,AR3 LD*AR1+,T MPY*AR2+,A

loop: LD*AR1+,T MPY*AR2+,B

MAXA BANZloop,*AR3- STLA,@y RET.end

找出乘积项中的最大值举例3:23链接命令文件:

MEMORY{PAGE0: RAM:origin=1000h,length=500h

RAM1:origin=1800h,length=100hPAGE1:DARAM:origin=2000h,length=1000h}SECTIONS{.data:>RAM PAGE0.text:>RAM PAGE0vectors:>RAM1 PAGE0.bss :>DARAMPAGE1}24FIR滤波器设计

7.5.1线性缓冲区法

又称为延迟线法。对于N阶FIR滤波器在数据区开辟一个N单元的线性缓冲区存放N个样本;每次滤波从最老样本开始,每个样本参与运算后下移一单元并在顶部结束读入新样本。其特点是依次下移覆盖/底部淘汰/顶部更新。设N阶FIR滤波器的系数(单位脉冲冲击响应)为:h(0),h(1),...h(N-1),x(n)表示滤波器在n时刻的输入,则n时刻的输出y(n)为:线性缓冲区示意图(N=6)系数表x(n-1)x(n-2)x(n-(N-2))x(n-(N-1))1次y(n)计算结束ARx指向顶部读入新样本,覆盖更新数据延迟算法由LTD指令完成25**采用线性缓冲区法编写的FIR数字滤波器程序*****

.title“fir.asm” .mmregs .defstart .bssy,1x .usect“x”,6h .usect“h”,6PA0 .set0PA1 .set1

.dataTable: .word2*32768/10 .word-3*32768/10 .word4*32768/10 .word-3*32768/10 .word2*32768/10 .word4*32768/10 .textStart: STM#h,AR2 RPT#5 MVPDtable,*AR2+

STM#x+5,AR1

;AR1指向x(n-5) STM#h+5,AR2

;AR2指向h(5)

STM#5,AR0

SSBX,FRCT LD#x,DP

PORTRPA1,@x

FIR: LD*AR1-,T

;x(n-5)--TLD#0,A

MPY*AR2-,A

;h5*x(n-5)--A

LTD*AR1-

;x(n-4)--T,x(n-4)--(n-5)

MAC*AR2-,A

LTD*AR1-

;x(n-3)--T,x(n-3)--(n-4)

MAC*AR2-,A

LTD*AR1-

;x(n-2)--T,x(n-2)--(n-3)

MAC*AR2-,A

LTD*AR1-

;x(n-1)--T,x(n-1)--(n-2)

MAC*AR2-,A

LTD*AR1

;x(n)--T,x(n)--(n-1)

MAC*AR2+0,A STHA,@y

;savey(n)

Portw@y,PA0 BDFIR

PORTRPA1,*AR1+0

;

x(n+1)--顶部

.end26MEMORY{PAGE0:RAM:origin=1000h,length=100h RAM1:origin=1200h,length=500hPAGE1:DARAM1:origin=0100h,length=100h DARAM2:origin=0200h,length=100h}SECTIONS{.data:>RAMPAGE0.text:>RAM1PAGE0.bss:>DARAM1PAGE1x:>DARAM2PAGE1h:>DARAM2PAGE1}链接命令文件:27FIR滤波器设计

7.5.2循环缓冲区法

又称为滑动窗法。对于N阶FIR滤波器在数据区开辟一个N单元的循环缓冲区存放N个样本;每次滤波从更新最老样本开始,在循环寻址作用下于最老样本结束读入新样本。其特点是用最新样本覆盖最老样本/其余数据无需移动。x(n-1)x(n-2)x(n-(N-2))x(n-(N-1))1次y(n)计算结束ARx指向最老样本被最新样本覆盖,更新数据设N阶FIR滤波器的系数(单位脉冲冲击响应)为:h(0),h(1),...h(N-1),x(n)表示滤波器在n时刻的输入,则n时刻的输出y(n)为:28*****采用循环缓冲区法编写的FIR数字滤波器程序*****

.mmregs .global start .def start,_c_int00KS .set 256

;输入的样本数

N .set 17 ;FIR滤波器阶数COEF_FIR

.sect “COEF_FIR”;FIR滤波器系数表

.word 0,158,264,-290,-1406,-951,3187,9287,12272

.word 9287,3187,-951,-1406,-290,264,158,0

.dataINPUT .copy“firin.inc”

;输入数据在数据区0x2400-24FFOUTPUT .space 1024 ;输出数据在数据区0x2500-25FFCOEFTAB .usect “FIR_COEF”,N;17个系数在数据区0x2600DATABUF .usect “FIR_BFR”,N;17个样本在数据区0x2611BOS .usect “STACK”,0FhTOS .usect “STACK”,1

.text .asg AR4,DATA_P;样本数据缓冲区指针 .asg AR5,COEF_P;滤波系数缓冲区指针 .asg AR6,INBUF_P;输入数据指针 .asg AR7,OUTBUF_P;输出数据指针汇编源程序:29_c_int00:B startstart: SSBX FRCT ;小数乘法,FRCT=1 STM #COEFTAB,COEF_P;滤波系数指针-AR5

RPT #N-1;FIR系数从程序区搬移到数据区 MVPD COEF_FIR,*COEF_P+

STM #-1,AR0

;地址步进-1

STM #DATABUF,DATA_P;样本数据指针-AR4

RPTZA,#N-1;数据缓冲区清0 STL A,*DATA_P+

STM#(DATABUF+N-1),DATA_P

;数据缓冲区指针指向底部

STM#(COEFTAB+N-1),COEF_P

;系数缓冲区指针指向底部

STM #INPUT,INBUF_P;输入数据指针-AR6 STM#OUTPUT,OUTBUF_P;输出数据指针-AR7

STM #KS-1,BRC

;块重复操作次数(256)

RPTBDLOOP-1

STM #N,BK

;FIR循环缓冲区大小(17)

LD *INBUF_P+,A

;装载输入数据

STL A,*DATA_P+0%

;用最新样本值替代最旧样本值

RPTZ

A,N-1;重复乘加操作(17)

MAC *DATA_P+0%,*COEF_P+0%,A

;滤波运算

STH A,*OUTBUF_P+ ;滤波输出

LOOP: B LOOP.end30fir.obj-ofir.out-mfir.mapMEMORY{PAGE0:ROM(RIX):origin=0080h,length=100h

ROM1(RIX):origin=180h,length=20hPAGE1:INTRAM1(RW):origin=2400h,length=200h INTRAM2(RW):origin=2600h,length=100h INTRAM3(RW):origin=2700h,length=100h B2B(RW):origin=0070h,length=10h}SECTIONS{ .text:{}>ROMPAGE0

COEF_FIR:{}>ROM1PAGE0.data :{}>INTRAM1PAGE1FIR_COEF:{}>INTRAM2PAGE1FIR_BFR:{}>INTRAM2PAGE1STACK :{}>B2BPAGE1}链接命令文件:31当样本数据为单位冲击脉冲时滤波输出:

32当样本数据为1000Hz方波时滤波输出:

33采用线性缓存区与间接寻址方法,设计一个6级FIR滤波器。1.设滤波器系数为:

0.1,-0.3,0.2,0.2,-0.3,0.1;2.设输入样本数据已存放在.data段中:7fffH,0,0,0,0,0;3.参考例程写出汇编程序代码;4.按下列要求写出链接命令文件:程序存储器分为两个区域,首地址分别为:0x100H,0x200H,将滤波器系数/样本数据(.data段)和正文(.text段)分别映射到这两个区域中;数据存储器也分为两个区域,首地址分别为:0x500H,0x800H,将其他数据段分别映射其中;5.试在CCS开发环境下建立工程、编译、链接、调试、运行并查看程序执行结果。实验3:FIR滤波器设计与实现-线性缓存区法一、实验任务二、实验要求34参考程序:.title“fir.asm” .mmregs .def_c_int00 .bssy,10x .usect“x”,6h .usect“h”,6

.dataTabx:.word7fffH

.word0

.word0

.word0

.word0

.word0Tabh: .word1*32768/10 .word-3*32768/10 .word2*32768/10 .word2*32768/10 .word-3*32768/10 .word1*32768/10

.text_c_int00:

STM

#x,AR2 RPT

#11 MVPDTabx,*AR2+ STM#h+5,AR1 STM#x+5,AR2

STM#y,AR3

STM#5,AR0

SSBX

FRCT

FIR: LD*AR2-,TLD#0,A

MPY*AR1-,A

LTD*AR2-

MAC*AR1-,A

LTD*AR2-

MAC*AR1-,A

LTD*AR2-

MAC*AR1-,A

LTD*AR2-

MAC*AR1-,A

LTD*AR2

ST#0,*AR2+0

MAC*AR1+0,A STHA,*AR3+ B

FIR .end35MEMORY{PAGE0:RAM:origin=100h,length=100h RAM1:origin=200h,length=200hPAGE1:DARAM1:origin=0500h,length=200h DARAM2:origin=0800h,length=100h}SECTIONS{.data:>RAMPAGE0.text:>RAM1PAGE0.bss:>DARAM1PAGE1x:>DARAM2PAGE1h:>DARAM2PAGE1}链接命令文件:363738

.title "FIR" .global _c_int00 .mmregsDACOUT .set 01hADCIN .set 02hDAC1Addr .set 0f005hADC1Addr .set 0f008h .sect ".vectors"rst: B _c_int00 NOP NOP .data .space 200h*8fir_table .sect “coff_fir” ;201阶低通滤波器的冲激相应系数(Hamming窗)

;通带:<1/20T,衰减<3dB,阻带:>1/8T,衰减>40dB

实验4:FIR滤波器设计与实现-循环缓存区法39

.word -1,-3,-6,-8,-9,-10,-10,-9,-7,-4,0,3,7,11,14,15,16,14 .word 11,6,-1,-8,-15,-22,-27,-30,-31,-28,-22,-12,0,13,26,38,47 .word 53,53,47,36,20,-1,-23,-45,-66,-81,-89,-89,-80,-61,-34,0 .word 36,72,104,128,141,141,125,95,52,-1,-58,-115,-165,-203 .word -224,-223,-198,-151,-84,0,91,182,263,326,361,361,324,249 .word 138,-1,-157,-317,-465,-585,-661,-678,-625,-495,-286,0,351,755 .word 1192,1639,2074,2470,2807,3062,3222,3276,3222,3062,2807 .word 2470,2074,1639,1192,755,351,0,-286,-495,-625,-678,-661,-585 .word -465,-317,-157,-1,138,249,324,361,361,326,263,182,91,0 .word -84,-151,-198,-223,-224,-203,-165,-115,-58,-1,52,95,125,141 .word 141,128,104,72,36,0,-34,-61,-80,-89,-89,-81,-66,-45,-23,-1 .word 20,36,47,53,53,47,38,26,13,0,-12,-22,-28,-31,-30,-27 .word -22,-15,-8,-1,6,11,14,16,15,14,11,7,3,0,-4,-7,-9,-10 .word -10,-9,-8,-6,-3,-1 .space 55*16 data_buff .usect "buff“,20140

.text_c_int00 LD #020h,DP ;置数据页指针

STM #3000h,SP SSBX INTM RSBX SXM stm #0001h,2Bh ;设置软件等待时间

stm #00a8h,PMST ;改变中断向量映射到0x0080 NOP NOP stm #201,BK ;循环寻址缓冲区大小

stm #fir_table,AR3 ;FIR系数区地址指针

stm #data_buff,AR4 ;数据缓冲区地址指针

stm #1,AR041

loop: portr ADC1Addr,ADCIN ;从第一ADC通道读数

ld ADCIN,A stl A,*AR4+% ;存入缓冲区

rptz A,#200 mac *AR4+0%,*AR3+0%,A ;双寻址乘加运算

nop nop nop sfta A,#-15 and #0ffh,A stl A,DACOUT portw DACOUT,DAC1Addr ;结果从第一DAC通道输出

b loop .end42MEMORY{ PAGE0: VECS: origin=0x0080, length=0x80 PROG: origin=0x2000, length=0x1000 PAGE1: DATA: origin=0x1000, length=0x1000 STACK: origin=0x3000,length=0x1000}SECTIONS{ .vectors: {} > VECSPAGE0 .text: {} > PROGPAGE0 .data: {} > DATAPAGE1 .stack: {} > STACKPAGE1 .bss: {} > DATAPAGE1 coff_fir: {} > DATAPAGE1 buff: {} > DATAPAGE1}43用泰勒级数展开式计算一个角度的正弦值sinx=x-x^3/3!+x^5/5!-x^7/7!+x^9/9!=x(1-x^2/2x3(1-x^2/4x5(1-x^2/6x7(1-x^2/8x9))))程序中设x值为45°(π/4)放在sin_vars段的d_x单元;程序执行程序结果为5A81H,放在sin_vars段的d_sinx单元;实验5:正弦波信号发生器设计AR2AR3AR444

.title "sinx.asm" .mmregs .def _c_int00 .ref sin_start,d_x,d_sinxSTACK .usect "STACK",10_c_int00: STM #STACK+10,SP LD #d_x,DP ST #6487H,@d_x ;x=6487H=45度

CALL sin_startend: B endsin_start: .def sin_startd_coeff .usect "coeff",4

.datatable: .word 01c7H ;c1=1/(8*9)*32768=01c7H .word 030bH ;c2=1/(6*7)*32768=030bH .word 0666H ;c3=1/(4*5)*32768=0666H .word 1556H ;c4=1/(2*3)*32768=1556Hd_x .usect "sin_vars",1d_squr_x .usect "sin_vars",1d_temp .usect "sin_vars",1d_sinx .usect "sin_vars",1c_1 .usect "sin_vars",145

.textSSBX FRCTSTM #d_coeff,AR5RPT #3MVPD #table,*AR5+STM #d_coeff,AR3STM #d_x,AR2STM #c_1,AR4ST #7FFFH,@c_1;c_l=#7fffH=1SQUR *AR2+,A

;A=x^2ST A,*AR2

;(AR2)=x^2d_squr_x||LD *AR4,B

;B=1MASR *AR2+,*AR3+,B,A

;A=1-x^2/72,T=x^2MPYA A

;A=T*A=x^2(1-x^2/72)STH A,*AR2 ;(d_temp)=x^2(1-x^2/72)MASR *AR2-,*AR3+,B,A

;A=1-x^2/42(1-x^2/72),T=x^2(1-x^2/72)MPYA *AR2+

;B=x^2(1-x^2/42(1-x^2/72))ST B,*AR2

;(d_temp)=x^2(1-x^2/42(1-x^2/72))||LD *AR4,B

;B=1MASR *AR2-,*AR3+,B,A

;A=1-x^2/20(1-x^2/42(1-x^2/72))MPYA *AR2+

;B=x^2(1-x^2/20(1-x^2/42(1-x^2/72)))ST B,*AR2

;(d_temp)=B||LD *AR4,B

;B=1MASR *AR2-,*AR3+,B,A

;A=1-x^2/6(1-x^2/20(1-x^2/42(1-x^2/72)))MPYA@d_x

;B=x(1-x^2/6(1-x^2/20(1-x^2/42(1-x^2/72))))STH B,@d_sinx ;sin(theta)RET.endsinx=x-x^2/3!+x^5/5!-x^7/7!+x^9/9!=x(1-x^2/2x3(1-x^2/4x5(1-x^2/6x7(1-x^2/8x9))))AR2AR3AR446MEMORY{ PAGE0: EPROM:org=0E00H len=01000H VECS:org=0FF80H len=0080H PAGE1: SPRAM: org=0500H len=0020H DARAM: org=0080H len=0010H }SECTIONS{ .vectors:>VECSPAGE0 .text:> EPROMPAGE0 .data:>EPROMPAGE0 STACK:>SPRAMPAGE1 sin_vars:>DARAMPAGE1coeff:>DARAM PAGE1 } 47用泰勒级数展开式计算一个角度的余弦值cosx=1-x^2/2!+x^4/4!-x^6/6!+x^8/8!=1-x^2/2(1-x^2/3x4(1-x^2/5x6(1-x^2/7x8)))执行程序结果:48

.mmregs .def _c_int00 .ref cos_start,d_x,d_cosxSTACK .usect "STACK",10_c_int00: STM #STACK+10,SP LD #d_x,DP ST #6487H,@d_x ;x=6487H=45度

CALL cos_startend: B endcos_start: .def cos_startd_coeff .usect "coeff",4 .datatable: .word 0249H ;c1=1/(7*8)*32768=0249H .word 0444H ;c2=1/(5*6)*32768=0444H .word 0aabH ;c3=1/(3*4)*32768=0aabH .word 4000H ;c4=1/(1*2)*32768=4000Hd_x .usect "cos_vars",1d_squr_x .usect "cos_vars",1d_temp .usect "cos_vars",1d_cosx .usect "cos_vars",1c_1 .usect "cos_vars",149.textSSBX FRCTSTM #d_coeff,AR5RPT #3MVPD #table,*AR5+STM #d_coeff,AR3STM #d_x,AR2STM #c_1,AR4ST #7FFFH,@c_1SQUR *AR2+,A ;A=x^2ST A,*AR2 ;(AR2)=x^2||LD *AR4,B ;B=1MASR *AR2+,*AR3+,B,A ;A=1-x^2/56,T=x^2MPYA A ;A=T*A=x^2(1-x^2/56)STH A,*AR2 ;(d_temp)=x^2(1-x^2/56)MASR *AR2-,*AR3+,B,A;A=1-x^2/30(1-x^2/56),T=x^2(1-x^2/56)MPYA *AR2+ ;B=x^2(1-x^2/30(1-x^2/56))ST B,*AR2 ;(d_temp)=x^2(1-x^2/30(1-x^2/56))||LD *AR4,B ;B=1MASR *AR2-,*AR3+,B,A ;A=1-x^2/12(1-x^2/30(1-x^2/56))SFTA A,-1,A ;1/2NEG A ;-1/2MPYA *AR2+ ;B=-x^2/2(1-x^2/12(1-x^2/30(1-x^2/56)))MAR *AR2+RETDADD *AR4,16,B ;B=1-x^2(1-x^2/12(1-x^2/30(1-x^2/56)))STH B,*AR2 ;cos(theta)RET.end50产生正弦波程序:先以sin.asm和cos.asm程序计算0~45(间隔为0.5)的正弦和余弦值,再利用Sin(2x)=2sin(x)cos(x)求出0~90的正弦值(间隔为1)然后通过复制,获得0~359的正弦值。重复向PA0口输出,便可得到正弦波。51

.title "sinxwave.asm" .mmregs .def _c_int00 .ref d_xs,d_sinx,d_xc,d_cosx,sinx,cosxsin_x: .usect "sin_x",360STACK.usect "STACK",10Hk_theta.set 286

;theta=pi/360(0.5deg)PA0 .set 0_c_int00: .text STM #STACK+10H,SP

STM k_theta,AR0

;AR0--k_theta(increment)

STM 0,AR1

;AR1=x(rad.) STM #sin_x,AR6 ;AR6--sin_x STM #90,BRC ;fromsin0(deg.)--sin90(deg.) RPTB loop1-1 LDM AR1,A LD #d_xs,DP

CALL sinx

;d_sinx=sin(x)

CALL cosx

;d_cosx=cos(x)52

LD #d_sinx,DP LD @d_sinx,16,A ;A=sin(x)

MPYA@d_cosx

;B=sin(x)*cos(x)

STH B,1,*AR6+

;AR6--2*sin(x)*cos(x)

MAR *AR1+0loop1: STM #sin_x+89,AR7 ;sin91(deg.)--sin179(deg.) STM #88,BRC RPTB loop2-1

LD *AR7-,A STL A,*AR6+loop2: STM #179,BRC ;sin180(deg.)--sin359(deg.) STM #sin_x,AR7 RPTB loop3-1 LD *AR7+,A

NEG A STL A,*AR6+loop3: STM #sinx,AR6 ;generatesinwave STM #1,AR0 STM #360,BKloop4: PORTW*AR6+0%,PA0 B loop453sinx: .def d_xs,d_sinxd_coeff_s .usect "coef_s",4 .datatable_s: .word 01c7H ;c1=1/(8*9)*32768=01c7H .word 030bH ;c2=1/(6*7)*32768=030bH .word 0666H ;c3=1/(4*5)*32768=0666H .word 1556H ;c4=1/(2*3)*32768=1556Hd_xs .usect "sin_vars",1d_squr_xs .usect "sin_vars",1d_temp_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论