📄 mdu_conf.c
字号:
#include "reg24le1.h"
#include "mdutest.h"
/*函数定义部分*/
#define toasc(x) (x+'0') //数字转换成对应的ASC码
#define UPtoLOW(x) (x-'A'+'a') //大写转换成小写
#define LOWtoUP(x) (x-'a'+'A') //小写转换成大写
/*软件延时函数*/
void delay(unsigned int dj)
{
unsigned char di;
for(;dj>0;dj--)
for(di=120;di>0;di--)
{
;
}
}
/*led灯的输出配置*/
void ioconfig()
{
P1DIR&=0XFE; //配置GPIO为输出
P10=0;
}
/*串口初始化化*/
void uart()
{
CLKCTRL = 0x28; // MCU时钟设置16M
CLKLFCTRL = 0x01; // 设置32.768K时钟
P0DIR &= 0xF7; // P03 (TxD) 输出
P0DIR |= 0x10; // P04 (RxD) 输入
P0|=0x18;
S0CON = 0x50;
PCON |= 0x80; //倍增
WDCON |= 0x80; // 选择内部波特率发生器
S0RELL = 0xFB;
S0RELL = 0xF3; //波特率设置为38400
}
/*发送一个字符*/
void putch(char ch)
{
S0BUF=ch;
while(!TI0);
TI0=0;
}
/*发送一个字符串*/
void puts(char *str)
{
while(*str!='\0')
{
putch(*str++);
}
}
/*MDU16bit乘法*/
unsigned int MDUmul_16bit(unsigned int x1,unsigned int x2)
{
unsigned int res=0;
MD0=x1&0xff; //写入低8bit
MD4=x2&0xff;
MD1=((x1&0xff00)>>8);
MD5=((x2&0xff00)>>8);
delay(10);
res+=MD0;
res+=MD3*256; ;
return res;
}
//16bit的除法
void MDUdiv_16bit(unsigned int x1,unsigned int x2,unsigned int *Quotient,unsigned int* Remainder )
{
*Quotient=0;
*Remainder=0;
MD0=x1&0xff; //写入低8bit
MD1=((x1&0xff00)>>8);
MD4=x2&0xff;
MD5=((x2&0xff00)>>8);
delay(10);
*Quotient=MD0+MD1*256;// 商
*Remainder+=MD4;
*Remainder+=MD5*256; //余数
}
//32bit的除法
void MDUdiv_32bit(unsigned int x1,unsigned int x2,unsigned int *Quotient,unsigned int* Remainder )
{
*Quotient=0;
*Remainder=0;
MD0=x1&0xff; //写入低8bit
MD1=(x1>>8)&0xff;
MD2=(x1>>16)&0xff;
MD3=(x1>>24)&0xff; //写入最高的8位
MD4=x2&0xff;
MD5=(x2>>8)&0xff;
delay(10);
*Quotient=MD0+MD3*256;// 商
*Remainder+=MD4;
*Remainder+=MD5*256; //余数
}
//移位函数
unsigned int shiffunc(unsigned int dat,unsigned char bitnum,unsigned char derec)
{
unsigned int res=0;
MD0=dat&0xff; //dat的LSB的提取
MD3=(dat>>8)&0xff;//dat的MSB提取
if(derec==LEFT)
ARCON&=~(0x20);//左移
else
ARCON|=(0x20); //右移
ARCON|=bitnum; //bit0-bit4决定移位的位数
delay(5);
res+=MD0;
res+=MD3*256; //高8位
return res;
}
//显示函数将数字分解成4位的ASC码发送给超级终端
void display(unsigned int x)
{
unsigned char buf[8]; //存储分离的位的数组定义
unsigned char bitw=0;
unsigned char tmph=0,tmpl=0;
while(x)
{
buf[bitw++]=x&0xff; //数据以8位分离出来
x>>=8;
}
if(bitw==0)
{
putch('\n');
puts(" the res=="); //显示数据输出的头
putch(toasc(0));
}
else
{
while(bitw) //有效位宽判断
{
putch('\n');
puts(" the res==0x"); //显示数据输出的头
if((buf[bitw-1]/16)>9) //已经超出10
{tmph=(buf[bitw-1]/16)-10+'a'; } //转成16进制字母显示
else
{putch(toasc(buf[bitw-1]/16)); } //如果没有超出就10进制数字显示
if((buf[bitw-1]%16)>9)
{tmph=(buf[bitw-1]%16)-10+'a'; }
else
{putch(toasc(buf[bitw-1]%16)); }
bitw--;
}
}
putch('\n'); //换行
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -