初学LPC176X,发现这个东西的串口波特率设置不是一般的麻烦,还要去查表,写了个函数计算波特率初值,恳请各位高手指正
//typedef unsigned char BOOLEAN; /* 布尔变量 */
//typedef unsigned char INT8U; /* 无符号8位整型变量 */
//typedef signed char INT8S; /* 有符号8位整型变量 */
//typedef unsigned short INT16U; /* 无符号16位整型变量 */
//typedef signed short INT16S; /* 有符号16位整型变量 */
//typedef unsigned long INT32U; /* 无符号32位整型变量 */
//typedef signed long INT32S; /* 有符号32位整型变量 */
//typedef float FP32; /* 单精度浮点数(32位长度) */
//typedef double FP64;
FP32 BandRateCalc(INT32U band,INT32U clk)
{
double Fpclk = clk; //50,000,000
FP32 DL; //原始
FP32 RDL;
INT32U temp;
FP32 fr;
INT8U x,y;
FP32 ntp; //偏移量
INT8U tx,ty;
FP32 tdl;
FP32 tp; //偏移量
FP32 band1;
INT8U flag = 0;
DL = (Fpclk/16/band);
temp = (INT32U)DL;
if(DL == temp)
{
tdl = DL;
tx = 1;
ty = 0;
}
else
{
for(x = 2; x < 16; x++)
{
for(y = 1; y < x; y++)
{
fr = y;
fr /= x;
fr += 1;
RDL = DL/fr;
temp = (INT32U)RDL;
ntp = (RDL - temp);
if(ntp == 0)
{
flag = 2;
tx = x;
ty = y;
tdl = temp;
break;
}
if(flag)
{
if(ntp < tp) //找到一个最小的就把当前的保存起来。
{
tx = x;
ty = y;
tp = ntp;
tdl = temp;
}
}
else //第一次保存初值
{
tx = x;
ty = y;
tp = ntp;
tdl = temp;
}
flag = 1;
}
if(flag == 2)
break;
}
}
RDL = ty;
RDL /= tx;
RDL += 1;
band1 = ((Fpclk)/((tdl*RDL)*(16)));
return band1;
}
|