suncat0504 发表于 2026-1-15 14:56:19

Ai8051U的TFPU问题 | 已解决

这几天使用擎天柱开发板学习MDU和TFPU,编程并用示波器测试。MDU的使用徐爱国,用示波器观察,尤其是除法上,很明显。但是使用TFPU时出了问题。

首先,在工程中加入了AI8051U_32_TFPU.LIB文件,没做其它特殊设置,


没做什么设置,陈旭代码如下:

<div>
<div>#include "AI8051U.h"
#include "intrins.h"
#include "stdio.h"
#include "math.h"

#define MAIN_Fosc      24000000UL

volatileunsigned long int near uint1, uint2;
volatile unsigned long int near xuint;

volatile long int sint1, sint2;
volatile long int xsint;

unsigned long ultest;
long ltest;

/*****************************************************************************/

sbit TPIN=P1^0;

/*****************************************************************************/

#define Baudrate      115200L
#define TM            (65536 -(MAIN_Fosc/Baudrate/4))
#define PrintUart   1      //1:printf 使用 UART1; 2:printf 使用 UART2

/******************** 串口打印函数 ********************/
void UartInit(void)
{
#if(PrintUart == 1)
    S1_S1 = 0;      //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7, 0xC0: P4.3 P4.4
    S1_S0 = 1;
    SCON = (SCON & 0x3f) | 0x40;
    T1x12 = 1;      //定时器时钟1T模式
    S1BRT = 0;      //串口1选择定时器1为波特率发生器
    TL1= TM;
    TH1= TM>>8;
    TR1 = 1;      //定时器1开始计时

//    SCON = (SCON & 0x3f) | 0x40;
//    T2L= TM;
//    T2H= TM>>8;
//    AUXR |= 0x15;   //串口1选择定时器2为波特率发生器
#else
    S2_S = 1;       //UART2 switch to: 0: P1.2 P1.3,1: P4.2 P4.3
    S2CFG |= 0x01;//使用串口2时,W1位必需设置为1,否则可能会产生不可预期的错误
    S2CON = (S2CON & 0x3f) | 0x40;
    T2L= TM;
    T2H= TM>>8;
    AUXR |= 0x14;   //定时器2时钟1T模式,开始计时
#endif
}

void UartPutc(unsigned char dat)
{
#if(PrintUart == 1)
    SBUF = dat;
    while(TI==0);
    TI = 0;
#else
    S2BUF= dat;
    while(S2TI == 0);
    S2TI = 0;    //Clear Tx flag
#endif
}

char putchar(char c)
{
    UartPutc(c);
    return c;
}

void delay(unsigned char ms)
{
   while(--ms);
}

void PLL_Init() {
//CLKSEL &= ~0x80;      //选择PLL的96M(*8)作为PLL的输出时钟
    CLKSEL |= 0x80;         //选择PLL的144M(*12)作为PLL的输出时钟

    USBCLK &= ~0x60;
//USBCLK |= 0x00;         //PLL输入时钟为12M则选择1分频
//USBCLK |= 0x20;         //PLL输入时钟为24M则选择2分频
    USBCLK |= 0x40;         //PLL输入时钟为48M则选择4分频
//USBCLK |= 0x60;         //PLL输入时钟为96M则选择8分频

    USBCLK |= 0x80;         //启动PLL

    delay(1000);                //等待PLL锁频,建议50us以上

    HSCLKDIV = 0;         //高速外设时钟源不分频
    TFPU_CLKDIV = 0;      //TFPU时钟源不分频

//CLKSEL &= ~0x40;      //选择系统时钟作为高速外设时钟源
    CLKSEL |= 0x40;         //选择PLL时钟作为高速外设时钟源
}

/*****************************************************************************/
void main(void) {   
    float f1=223.57;
    float f2=764.84;
    float f3=0;
   
   
    WTST = 0;//设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
    EAXFR = 1; //扩展寄存器(XFR)访问使能
    CKCON = 0; //提高访问XRAM速度

    P0M1 = 0x00;   P0M0 = 0x00;   //设置为准双向口
    P1M1 = 0x00;   P1M0 = 0x00;   //设置为准双向口
    P2M1 = 0x00;   P2M0 = 0x00;   //设置为准双向口
    P3M1 = 0x00;   P3M0 = 0x00;   //设置为准双向口
    P4M1 = 0x00;   P4M0 = 0x00;   //设置为准双向口
    P5M1 = 0x00;   P5M0 = 0x00;   //设置为准双向口
    P6M1 = 0x00;   P6M0 = 0x00;   //设置为准双向口
    P7M1 = 0x00;   P7M0 = 0x00;   //设置为准双向口

    UartInit();
   
      PLL_Init();
   
//DMAIR = 0x3e;         //TFPU使用系统时钟作为时钟源
    DMAIR = 0x3f;         //TFPU使用高速外设时钟作为时钟源
                            //*** 必须设置此句,TFPU才能使用高速时钟作为时钟源 ***

   
    while(1) {
      //printf("start...\r\n");
      TPIN=1;
      f3 = f1 + f2;
//      f3 = f1 - f2;
//      f3 = f1 * f2;
//      f3 = f1 / f2;
      TPIN=0;
   
    }
}
/*****************************************************************************/</div>
</div>运行后,示波器测试的状况:


47.264纳秒
然后去掉lib,注释掉与TFPU有关的代码:
PLL_Init();

DMAIR = 0x3f;


也就是没有TFPU的支持下,同样的浮点数加法运算,


46.853纳秒
主频设置为24MHz,
从结果来看,好像TPFU没起作用啊。
这是什么原因呢?

suncat0504 发表于 2026-1-15 16:15:41

找到原因了。参与运算的变量,全都改成全局变量就可以了,局部变量场合,确实不起作用,但速度似乎更快。
页: [1]
查看完整版本: Ai8051U的TFPU问题 | 已解决