这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » 国产MCU » 【STCAi8051U】Ai8051U在有无TFPU单元时的差别-----单精度

共1条 1/1 1 跳转至

【STCAi8051U】Ai8051U在有无TFPU单元时的差别-----单精度数开方根

专家
2026-01-15 16:51:44     打赏

工程使用24MHz主频。需要引入<math.h>


#include "AI8051U.h"
#include "intrins.h"
#include "stdio.h"
#include "math.h"

5#define MAIN_Fosc        24000000UL

volatile  unsigned 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) {
    int p = ms*2000;
     while(--p);
}

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时钟作为高速外设时钟源
}

/*****************************************************************************/
volatile    float f1=3.9;
volatile    float f2=5.1;
volatile    float f3=0;
void main(void) {    
   
    
    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) {
        TPIN=1;
        f3 = sqrt(f1);
        TPIN=0;
    
    }
}

1、有TFPU

图片9.png

1.373微秒

2、无TFPU    

图片10.png

44.352微秒

这个差距也是很大,使用TFPU的优越性特别明显。




关键词: 懒猫的学习笔记     Ai8051U     TFPU    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]