MSP430G2553单片机sin()、cos()函数计算–错误笔记

之前一直没有用MSP430G2553计算过三角函数值,一直以为MSP430单片机计算三角函数的速度应该与linux嵌入式开发板的计算速度不会有太大差别,一直以为MSP430内部会有特殊硬件辅助计算。一直觉得,既然MSP430单片机支持C语言标准库math.h,就一定可以进行sqrt(),sin(),cos()等计算。
谁知,我需要紧急使用MSP430G2553计算一个比较复杂的公式的时候,才发现MSP430G2553这款神器居然计算不了cos(float)、sin(float)等。(博主使用CSS V5开发套件,也有可能MSP430G2553是可以计算的,但是博主搞错了,所以此片文章就先假设MSP430G2553计算不了三角函数吧。)

一、测试现象

编辑一下代码并进行编译:

  float tmp;
  tmp=1.0f;
  tmp=cos(tmp);

编译器成功计算,但是将tmp换为比较复杂的公式的时候,CCS就会编译报错。

二、解决办法

既然MSP430G2553无法计算,那么就只能祭出大招来计算了。简单思维后就可以发现,cos()、sin()、可以通过简单计算相互转换、而且通过弧度值(0,PI/2)的cos()、sin()计算结果可以计算出其他弧度值得cos()、sin()值。

第一招:查表法

使用EXCLE计算出(0,PI/2)范围内sin()、cos()值,保存到二位数组内。
我从搞数学的同学那里乞讨来的cos()、sin()表。
此表将(0,PI/2)等分为238等分,使用程序计算cos()、sin(),并将计算激活存储于此const double数组中。
查询办法:cos(Angle)= Cos_Table[Angle/90*238][0],sin()异同

此表具体出处不明,如果朋友知道此表出处,我定加上转载链接或者删除。
const double Cos_Table[238][2]={
 {0.999999999,0},
 {0.999978128,0.006613831},
 {0.999912514,0.013227373},
 {0.999803161,0.019840336},
 {0.999650073,0.026452431},
 {0.999453257,0.033063369},
 {0.999212722,0.039672861},
 {0.998928478,0.046280618},
 {0.998600538,0.05288635},
 {0.998228915,0.059489768},
 {0.997813627,0.066090584},
 {0.997354691,0.07268851},
 {0.996852128,0.079283255},
 {0.996305959,0.085874533},
 {0.995716209,0.092462054},
 {0.995082903,0.09904553},
 {0.994406068,0.105624674},
 {0.993685735,0.112199198},
 {0.992921935,0.118768813},
 {0.992114701,0.125333234},
 {0.991264069,0.131892171},
 {0.990370076,0.13844534},
 {0.989432761,0.144992452},
 {0.988452165,0.151533222},
 {0.987428331,0.158067363},
 {0.986361303,0.16459459},
 {0.985251129,0.171114617},
 {0.984097857,0.177627159},
 {0.982901537,0.184131931},
 {0.981662222,0.190628649},
 {0.980379966,0.197117027},
 {0.979054825,0.203596784},
 {0.977686856,0.210067634},
 {0.976276121,0.216529295},
 {0.97482268,0.222981485},
 {0.973326597,0.22942392},
 {0.971787938,0.23585632},
 {0.97020677,0.242278403},
 {0.968583161,0.248689887},
 {0.966917184,0.255090493},
 {0.96520891,0.261479941},
 {0.963458415,0.267857951},
 {0.961665776,0.274224243},
 {0.95983107,0.280578541},
 {0.957954378,0.286920564},
 {0.956035782,0.293250037},
 {0.954075365,0.299566683},
 {0.952073215,0.305870224},
 {0.950029417,0.312160385},
 {0.947944063,0.318436892},
 {0.945817242,0.324699469},
 {0.943649048,0.330947843},
 {0.941439575,0.33718174},
 {0.939188922,0.343400888},
 {0.936897185,0.349605014},
 {0.934564465,0.355793847},
 {0.932190864,0.361967117},
 {0.929776486,0.368124553},
 {0.927321437,0.374265886},
 {0.924825823,0.380390847},
 {0.922289755,0.386499169},
 {0.919713343,0.392590585},
 {0.917096699,0.398664827},
 {0.914439939,0.40472163},
 {0.911743178,0.410760729},
 {0.909006535,0.41678186},
 {0.906230129,0.42278476},
 {0.903414081,0.428769166},
 {0.900558515,0.434734817},
 {0.897663556,0.44068145},
 {0.89472933,0.446608807},
 {0.891755965,0.452516627},
 {0.888743593,0.458404653},
 {0.885692344,0.464272627},
 {0.882602351,0.470120292},
 {0.879473751,0.475947393},
 {0.87630668,0.481753674},
 {0.873101276,0.487538882},
 {0.86985768,0.493302763},
 {0.866576034,0.499045065},
 {0.863256481,0.504765538},
 {0.859899166,0.51046393},
 {0.856504237,0.516139993},
 {0.853071841,0.521793479},
 {0.849602129,0.527424139},
 {0.846095252,0.533031729},
 {0.842551365,0.538616002},
 {0.838970622,0.544176713},
 {0.835353179,0.549713621},
 {0.831699196,0.555226483},
 {0.828008831,0.560715057},
 {0.824282247,0.566179104},
 {0.820519605,0.571618384},
 {0.816721072,0.57703266},
 {0.812886812,0.582421695},
 {0.809016994,0.587785252},
 {0.805111788,0.593123098},
 {0.801171362,0.598434999},
 {0.797195892,0.603720722},
 {0.793185549,0.608980037},
 {0.789140509,0.614212713},
 {0.785060951,0.619418521},
 {0.780947051,0.624597234},
 {0.776798989,0.629748625},
 {0.772616948,0.634872468},
 {0.768401111,0.639968541},
 {0.764151661,0.645036618},
 {0.759868784,0.65007648},
 {0.755552669,0.655087906},
 {0.751203503,0.660070676},
 {0.746821477,0.665024572},
 {0.742406783,0.669949378},
 {0.737959613,0.674844878},
 {0.733480163,0.679710858},
 {0.728968627,0.684547106},
 {0.724425205,0.689353409},
 {0.719850093,0.694129558},
 {0.715243494,0.698875343},
 {0.710605607,0.703590557},
 {0.705936635,0.708274994},
 {0.701236784,0.712928448},
 {0.696506259,0.717550717},
 {0.691745266,0.722141598},
 {0.686954014,0.72670089},
 {0.682132712,0.731228393},
 {0.677281572,0.735723911},
 {0.672400805,0.740187245},
 {0.667490625,0.744618201},
 {0.662551247,0.749016586},
 {0.657582887,0.753382205},
 {0.652585762,0.75771487},
 {0.64756009,0.762014389},
 {0.642506093,0.766280576},
 {0.63742399,0.770513243},
 {0.632314004,0.774712205},
 {0.627176358,0.778877279},
 {0.622011278,0.783008282},
 {0.616818989,0.787105034},
 {0.611599719,0.791167355},
 {0.606353695,0.795195068},
 {0.601081147,0.799187997},
 {0.595782306,0.803145967},
 {0.590457404,0.807068804},
 {0.585106673,0.810956338},
 {0.579730347,0.814808397},
 {0.574328663,0.818624815},
 {0.568901855,0.822405423},
 {0.563450162,0.826150056},
 {0.557973821,0.829858551},
 {0.552473073,0.833530745},
 {0.546948158,0.837166478},
 {0.541399318,0.840765591},
 {0.535826795,0.844327926},
 {0.530230833,0.847853327},
 {0.524611678,0.85134164},
 {0.518969574,0.854792713},
 {0.513304768,0.858206394},
 {0.50761751,0.861582535},
 {0.501908046,0.864920987},
 {0.496176627,0.868221605},
 {0.490423504,0.871484244},
 {0.484648928,0.874708761},
 {0.478853152,0.877895016},
 {0.473036429,0.881042869},
 {0.467199015,0.884152182},
 {0.461341163,0.887222819},
 {0.455463131,0.890254647},
 {0.449565175,0.893247532},
 {0.443647554,0.896201343},
 {0.437710527,0.899115952},
 {0.431754352,0.90199123},
 {0.425779292,0.904827052},
 {0.419785606,0.907623295},
 {0.413773557,0.910379835},
 {0.407743409,0.913096552},
 {0.401695425,0.915773327},
 {0.395629869,0.918410043},
 {0.389547007,0.921006585},
 {0.383447105,0.923562839},
 {0.37733043,0.926078694},
 {0.371197249,0.928554038},
 {0.365047831,0.930988765},
 {0.358882444,0.933382768},
 {0.352701359,0.935735941},
 {0.346504845,0.938048182},
 {0.340293174,0.94031939},
 {0.334066618,0.942549465},
 {0.327825449,0.94473831},
 {0.321569939,0.94688583},
 {0.315300363,0.948991929},
 {0.309016994,0.951056516},
 {0.302720109,0.953079501},
 {0.296409981,0.955060796},
 {0.290086887,0.957000312},
 {0.283751104,0.958897967},
 {0.277402909,0.960753676},
 {0.271042579,0.962567359},
 {0.264670393,0.964338936},
 {0.258286629,0.966068329},
 {0.251891568,0.967755464},
 {0.245485487,0.969400266},
 {0.239068668,0.971002663},
 {0.232641392,0.972562586},
 {0.226203939,0.974079965},
 {0.219756591,0.975554735},
 {0.213299631,0.976986831},
 {0.20683334,0.97837619},
 {0.200358001,0.979722752},
 {0.193873898,0.981026458},
 {0.187381315,0.982287251},
 {0.180880534,0.983505075},
 {0.174371842,0.984679877},
 {0.167855522,0.985811607},
 {0.161331859,0.986900213},
 {0.154801139,0.98794565},
 {0.148263648,0.988947871},
 {0.141719671,0.989906831},
 {0.135169495,0.990822491},
 {0.128613406,0.991694808},
 {0.122051691,0.992523745},
 {0.115484637,0.993309266},
 {0.108912531,0.994051337},
 {0.102335662,0.994749925},
 {0.095754316,0.995404999},
 {0.089168781,0.99601653},
 {0.082579345,0.996584493},
 {0.075986298,0.997108862},
 {0.069389926,0.997589614},
 {0.06279052,0.998026728},
 {0.056188366,0.998420186},
 {0.049583755,0.998769969},
 {0.042976974,0.999076063},
 {0.036368314,0.999338454},
 {0.029758063,0.999557131},
 {0.02314651,0.999732084},
 {0.016533945,0.999863305},
 {0.009920656,0.999950789},
 {0.003306934,0.999994532}
 };

第二招:快速傅里叶级数分解法

学过高等数学的朋友应该会会有所了解,使用傅里叶级数知识变换后可以得到正弦级数和余弦级数,那么通过编程就可以借助简单数值计算三角函数。具体理论知识大家可以问一下度娘。

原创作者:百度ID:占有≠拥有 
以cos()为例:
泰勒展式:cos(x) = x^0/0!-x^2/2!+x^4/4!-x^6/6!+…+(-1)^(n-1)*x^(2n)/(2n)!+…

double cos(double x)
{
 double cos=0,item=1.0,temp;
 int n=0,sign=1;
 do 
 {
  temp=item;
  for(int i=1;i<=2*n;i++)temp/=i;   cos +=sign*temp;   item*=x*x;//item=x^(2n);   sign *=-1;//sign=-sign;   n++;  } while (temp>E);
 return cos;
}

u=2879855201,4085649907&fm=21&gp=0

分享到:更多 ()