40-MDU32–乘法和除法单元,AI8051U实验箱演示程序
40-MDU32–乘法和除法单元,AI8051U实验箱演示程序主程序main.C代码如下(汇编版本在附件中):
/*---------------------------------------------------------------------*/
/* --- Web: www.STCAI.com ---------------------------------------------*/
/* --- BBS: www.STCAIMCU.com-----------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
/*************功能说明 **************
本例程基于AI8051U为主控芯片的实验箱进行编写测试。
使用Keil C51编译器,为了达到比较高的效率,一般建议选择"Small"模式。
当编译器出现 "error C249: 'DATA': SEGMENT TOO LARGE" 错误时,则需要手动将部分比较大的数组通过"xdata" 强制分配到 XDATA 区域(例如: char xdata buffer ;)
通过添加MDU32库文件实现AI8051U硬件乘除法,替换标准算法库算法
串口1(115200,N,8,1)打印计算结果
可通过屏AI8051U_8_MDU32.LIBIB"文件,用示波器测量IO口低电平时间,来对比AI8051U硬件乘除法单元与标准算法库的计算效率
测量计算时间时可将串口打印指令屏蔽,方便查看每条公式的计算时间
下载时, 默认时钟 24MHz (用户可自行修改频率).
******************************************/
#include "..\comm\AI8051U.h"
#include "intrins.h"
#include "stdio.h"
#define MAIN_Fosc 24000000UL
volatileunsigned long intuint1;//keil c51 不认near关键字
volatileunsigned long int uint2;
volatile unsigned long int xuint;
volatile long int sint1, sint2;
volatile long int xsint;
unsigned long ultest;
long ltest;
/*****************************************************************************/
sbit TPIN=P4^2;
/*****************************************************************************/
#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)
P_SW1 &= ~S1_S1; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7, 0xC0: P4.3 P4.4
P_SW1 &= ~S1_S0;
SCON = (SCON & 0x3f) | 0x40;
AUXR |= (1<<6); //定时器时钟1T模式
AUXR &= ~S1BRT; //串口1选择定时器1为波特率发生器
TL1= TM;
TH1= TM>>8;
TR1 = 1; //定时器1开始计时
// SCON = (SCON & 0x3f) | 0x40;
// T2L= TM;
// T2H= TM>>8;
// AUXR |= 0x15; //串口1选择定时器2为波特率发生器
#else
P_SW2 |= S2_S; //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((S2CON & S2TI)==0);
S2CON &= ~S2TI; //Clear Tx flag
#endif
}
char putchar(char c)
{
UartPutc(c);
return c;
}
void delay(unsigned char ms)
{
while(--ms);
}
/*****************************************************************************/
void main(void)
{
WTST = 0;//设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXSFR(); //扩展寄存器(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();
printf("AI8051U MDU32 Test.\r\n");
TPIN = 0;//计算开始同步信号
delay(2);
TPIN = 1;
delay(2);
TPIN = 0;
delay(2);
TPIN = 1;
delay(2);
ultest = 12345678UL;
ltest = 12345678;
ultest = ultest / 12;
ltest = ltest / 12;
sint1 = 0x31030F05;
sint2 = 0x00401350;
TPIN = 0;
xsint = sint1 * sint2;
TPIN = 1;
printf("Result1=0x%lx\r\n",xsint);
uint1 =5;
uint2 =50;
TPIN = 0;
xuint = uint1 * uint2;
TPIN = 1;
printf("Result2=%d\r\n",xuint);
uint1 = 654689;
uint2 = 528;
TPIN = 0;
xuint = uint1 / uint2;
TPIN = 1;
printf("Result3=%u\r\n",xuint);
sint1 = 2134135177;
sint2 = 20000;
TPIN = 0;
xsint = sint1 / sint2;
TPIN = 1;
printf("Result4=0x%lx\r\n",xsint);
sint1 = -2134135177;
sint2 = -20000;
TPIN = 0;
xsint = sint1 / sint2;
TPIN = 1;
printf("Result5=0x%lx\r\n",xsint);
sint1 = 2134135177;
sint2 = -20000;
TPIN = 0;
xsint = sint1 / sint2;
TPIN = 1;
printf("Result6=0x%lx\r\n",xsint);
while(1);
}
页:
[1]