📄 sine.c
字号:
/******************************************************************************
; Module: Sine
; Filename: Sine.c
; Project: DSP library for XC166 microcontroller
;------------------------------------------------------------------------------
; Compiler: Keil
;
; Version: V1.2
;
; Description: Implementation of sine function
;
; Date: April 2003
;
; History:
;******************************************************************************/
/******************************************************************************
; DataS Sine( DataS x );
;
; INPUT:
; x By pi normalized input value between [-1,1] in 1Q15 format
; x = xrad/pi, where xrad contains the angle in radians
; [-pi,pi].
;
; OUTPUT:
; y Output value in 1Q15 format
;
; ALGORITHM:
; Using Taylor series to compute sin(x) with xrad in 1rd quadrant
; (0-pi/2)
; sin(x) = 3.140625*x + 0.02026367*x^2 - 5.325196*x^3 +
; 0.5446778*x^4 + 1.800293*x^5
;
; REGISTER USAGE:
; 1. From .c file to .asm file:
; definded by compiler
;
; 2. From .asm file to .c file:
; (R4) = y
;
; Assumption:
;
;*****************************************************************************/
#include "DspLib_Keil.h"
DataS Sine(DataS x)
{
DataS sdata coeff[5] = {0x1CCE,0x08B7,0x0AACC, 0x0053,0x3240};
//hex values in 4Q12 format
__asm
{
//MAC registers initialization
MOV MCW,#0600h ; MP=1, with left shift
;cheaking sign of input
MOV R5,x ;(R5)=x
SHR R5,#15 ;(R5)>>15
;compuetr abs value of input
MOV R4,#0
CoABS R4,x ;(ACC)=abs(x)
CoSTORE x,MAS ;(R12)=abs(x)
;cheak if abs(x)>0.5
CMP x,#4000h
JMPR cc_SLE, loop ;if abs(x)<0.5(pi/2), x is in 1rd and 4th quadrant
SUB x,#7FFFh ;if abs(x)>0.5(pi/2), x is in 2nd and 3rd quadrant
NEG x
loop:
MOV R6,#coeff ;(R6)=pointer of data field
CoMUL x,[R6+] ;(ACC)=1.800293*x
CoADD R4,[R6+] ;(ACC)=1.800293*x + 0.5446778
CoSTORE R7,MAS ;(R7)=limited(ACC)
CoMUL x,R7
CoADD R4,[R6+] ;(ACC)=(1.800293*x + 0.5446778)*x - 5.325196
CoSTORE R7,MAS
CoMUL x,R7
CoADD R4,[R6+] ;(ACC)=((1.800293*x + 0.5446778)*x - 5.325196)*x + 0.02026367
CoSTORE R7,MAS
CoMUL x,R7
CoADD R4,[R6] ;(ACC)=(((1.800293*x + 0.5446778)*x - 5.325196)*x + 0.02026367)*x
; + 3.140625
CoSTORE R7,MAS
CoMUL x,R7 ;(ACC)=((((1.800293*x + 0.5446778)*x - 5.325196)*x + 0.02026367)*x
; + 3.140625)*x
CoSHL #3 ;(ACC)<<3, change format to 1Q15
CoSTORE R4,MAS ;(R4)=sin(x)
CMP R5,#0
JMPR cc_EQ,return
NEG R4 ;(R4)=sin(-x)
return:
RET
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -