当前位置:文档之家› 数码管动态显示驱动

数码管动态显示驱动

/*****************************************************************************
******************************** LED.H ***********************
******************************** 数码管显示 ***********************
**************************** 袁东华 2012/7/15 ***********************
*****************************************************************************
******************************** 使用端口P0口 ************************
***************************** 段选为P1.0,位选为P1.1 ****************
******************************************************************************/
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif

#ifndef ulong
#define ulong unsigned long
#endif
/*******************************************************
************** 使用方法 **************************
1.调用displayDigit();显示整数。注意有两个参数。
2.调用displayDotDigit();显示浮点型数据。
3.注意因为这是动态显示,一定要把语句放在死循环中。而且每次都能扫描一次、

/******************************************************/
/********** 函数声明 **************/
void SetLEDsingle(uchar pesition,uint number,uint dot);
//显示一个数字可以显示小数点。pesition为显示数字的位置,number为显示的数字或负号(number==-1),
//Ldot可取0x00和0xff.表示没有小数点和有小数点。
void displayFloatDigit(float floatDigit);
//显示一个浮点数值,可正可负,保留两位整数和两位小数
void displayDigit(ulong digit,uchar mode);
//显示一个数值,digit为显示的数值,mode是显示的长度,mode可取4和8分别表示显示4位和8位。
void leddelay(uchar time);
//延时函数。
/******************************************************/


/********************************************************************************
******************** 编码表 *************************************
***********************************************************************************/
uchar code numTable[]={ //数字表0-9
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,
};
uchar code weiMa[]={ //位码表
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f,
};
uchar code charTable[]={ //abcdef,H,L,-,
0x77,0x7c,0x39,0x5e,
0x79,0x71,0x76,0x38,
0x40,
};
/*****************************************************/
/********** 端口定义 *************/
#define LEDPort P0 //定义LED显示端口,换的话修改此处。
sbit dula=P2^0;
sbit wela=P2^1;
/*****************函数部分**********************/
void displayDigit(ulong digit,uchar mode){
uchar arry[8],i,temp=0xfe,delayTime;
if(mode==4){
arry[3]=digit%10;
arry[2]=digit/10%10;
arry[1]=digit/100%10;
arry[0]=digit/1000%10;
delayTime=4;
}else{
arry[7]=digit%10;

arry[6]=digit/10%10;
arry[5]=digit/100%10;
arry[4]=digit/1000%10;
arry[3]=digit/10000%10;
arry[2]=digit/100000%10;
arry[1]=digit/1000000%10;
arry[0]=digit/10000000%10;
delayTime=2;
}
for(i=0;i//temp=pow(2,i); //耗时太多大概是1ms,8个数码管显示的是时候会闪烁。
dula=0;
wela=1;
LEDPort=temp;//送段选信号。由于是共阴的接法所以,选位是低电平有效。
wela=0;

dula=1;
LEDPort=numTable[arry[i]];//送位选信号。由于是共阴的接法所以,选段是高电平有效。
dula=0;
temp=temp<<1|0x01;
if(temp==0xff){
temp=0xfe;
}
leddelay(delayTime);//如果不加的话就会因为一闪而过,而都显示8.
}
}
void SetLEDsingle(uchar position,uint number,uint dot){
uchar temp;
if(dot==0xff){
temp=numTable[number]+128;//给数字加小数点。位码最高位为1.
}else{
temp=numTable[number];
}
if(number==-1){
temp=charTable[8];//负号
}
dula=0;
wela=1;
LEDPort=weiMa[position];
wela=0;

dula=1;
LEDPort=temp;
dula=0;
leddelay(5);
}
void displayFloatDigit(float floatDigit){
if(floatDigit<0){
SetLEDsingle(0,-1,0x00);
SetLEDsingle(1,-(int)(floatDigit)/10,0x00);
SetLEDsingle(2,-(int)(floatDigit)%10,0xff);
SetLEDsingle(3,-(int)(floatDigit*10)%10,0x00);
SetLEDsingle(4,-(int)(floatDigit*100)%10,0x00);
}else{
SetLEDsingle(0,(int)(floatDigit)/10,0x00);
SetLEDsingle(1,(int)(floatDigit)%10,0xff);
SetLEDsingle(2,(int)(floatDigit*10)%10,0x00);
SetLEDsingle(3,(int)(floatDigit*100)%10,0x00);
}
}
void leddelay(uchar time){
uchar t1,y;
for(t1=time;t1>0;t1--)
for(y=110;y>0;y--);
}



相关主题
文本预览
相关文档 最新文档