📄 user.h
字号:
// #define _Delay50us
// #define _Delay1ms
// #define _Int2Str
// #define _Int2Hex
// #define _Long2Str
// #define _Bcd2Byte
// #define _Byte2Bcd
// #define _Byte2Bin
// #define _WatchDog
#ifndef USER_C51_DEFINE_2003_7_21
#define USER_C51_DEFINE_2003_7_21
#include <REGX52.H>
#include <intrins.h>
#include <Absacc.h>
#define byte unsigned char
#define word unsigned int
#define uchar unsigned char
#define uint unsigned int
#define Delay5us(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
#define Delay4us(); _nop_(); _nop_(); _nop_(); _nop_();
#define Delay3us(); _nop_(); _nop_(); _nop_();
#define Delay2us(); _nop_(); _nop_();
#define Delay1us(); _nop_();
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif
#ifdef _Delay50us
void Delay50us(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<6;j++);
}
#endif
#ifdef _Delay1ms
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
#endif
#ifdef _Int2Str
void Int2Str(unsigned int t, unsigned char *str, unsigned char n)
{
// 将(0~65535)的整数t转成字符串str, n为输出字
// 符宽度, 若实际宽度大于n,则以实际宽度为准.
unsigned char a[5]; char i, j;
a[0]=(t/10000)%10; //取得整数值到数组
a[1]=(t/1000)%10;
a[2]=(t/100)%10;
a[3]=(t/10)%10;
a[4]=(t/1)%10;
for(i=0; i<5; i++) //转成ASCII码
a[i]=a[i]+'0';
for(i=0; a[i]=='0' && i<=3; i++);
for(j=5-n; j<i; j++) //填充空格
{ *str=' '; str++; }
for(; i<5; i++)
{ *str=a[i]; str++; } //加入有效的数字
*str='\0';
}
#endif
#ifdef _Int2Hex
void Int2Hex(unsigned int value, unsigned char *str, unsigned char n)
{
// 将(0~65535)的整数value转成十六进制……字符串str,
// n为显示十六进制字符个数。
unsigned char a[4];
unsigned char i,j;
a[3] = value&0x000F;
a[2] = (value&0x00F0)>>4;
a[1] = (value&0x0F00)>>8;
a[0] = value>>12;
for(i=0;i<4;i++)
{
if(a[i]>=0x0A) a[i]+=0x37;
else a[i]+='0';
}
*str='0';
*(++str)='x';
str++;
for(i=0; a[i]=='0'&& i<=3;i++);
for(j=4-n; j<i; j++)
{ *str='0'; str++; }
for(; i<4; i++)
{ *str=a[i]; str++; }
*str='\0';
}
#endif
#ifdef _Long2Str
void Long2Str(unsigned long int t, unsigned char *str, unsigned char n)
{
unsigned char a[8]; char i,j;
a[0]= t/10000000;
a[1]=(t/1000000)%10;
a[2]=(t/100000)%10;
a[3]=(t/10000)%10;
a[4]=(t/1000)%10;
a[5]=(t/100)%10;
a[6]=(t/10)%10;
a[7]= t%10;
for(i=0; i<8; i++)
a[i]=a[i]+'0';
for(i=0; a[i]=='0' && i<=6; i++)
for(j=8-n; j<i; j++)
{
*str=' ';
str++;
}
for(; i<8; i++)
{
*str=a[i];
str++;
}
*str='\0';
}
#endif
#ifdef _Bcd2Byte
unsigned char Bcd2Byte(unsigned char value)
{
//压缩型BCD码
return( ((value&0xf0)>>4)*10 + (value&0x0f) );
}
#endif
#ifdef _Byte2Bcd
unsigned int Byte2Bcd(unsigned char value)
{
return((value/100%10<<8)|(value/10%10<<4)|(value%10));
}
#endif
#ifdef _Byte2Bin
void Byte2Bin(unsigned char value, unsigned char *str)
{
char i;
for(i=7;i>=0;i--)
{
*str=((value>>i)&0x01)+'0';
str++;
}
*str='B';
*(str+1)='\0';
}
#endif
#ifdef _WatchDog
sfr WDT = 0xA6;
void FeedDog()
{
WDT=0x1E;
WDT=0xE1;//初始化看门狗。
}
#endif
#ifdef _Stack
#define STACK_MAX_SIZE 12
typedef struct _Stack_
{
unsigned char x[STACK_MAX_SIZE];
char i; //i为最后一个元素的下标, i为-1时为空栈
}Stack;
void SetNULL(Stack *s)
{
s->i=-1;
}
void Push(Stack *s, unsigned char t) //在堆栈中插入数据t
{
if((s->i)==STACK_MAX_SIZE-1)
return; //如果栈满,则不执行任何操作
else
{
s->i++;
s->x[s->i]=t;
}
}
unsigned char Pop(Stack *s)
{
if((s->i)==-1) //当栈为空时返回OxFF;
return 0xFF;
else
{
(s->i)--;
return s->x[(s->i)+1];
}
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -