#include
#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit PinLE_Data = P2 ^ 6;
sbit PinLE_Comm = P2 ^ 7;
uchar code ucTabNumber[16] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
// @12.000MHz
void Delay2ms(void) {
uchar i, j;
i = 4;
j = 225;
do
{
while (--j);
} while (--i);
}
#define DISPALY_SPEED 60 // 决定显示速度
#define SEG_LENGTH 6 // 数码管位数
#define SET_74HC573(data, le) {P0 = data; le = 1; _nop_(); le = 0;}
void Display(uchar *p_ucArr) {
uchar i;
uchar ucTemp;
static uchar s_ucPosition; // [0,11] 定位
static uint s_uiCount; // 计数
for (i = 0; i < SEG_LENGTH; i++) {
if ((s_ucPosition + i) < (SEG_LENGTH - 1)) {
ucTemp = 0x00;
} else if ((s_ucPosition + i) > ((SEG_LENGTH << 1) - 2)) {
ucTemp = 0x00;
} else {
// 有效内容
ucTemp = ucTabNumber[*(p_ucArr + ((s_ucPosition + i) - (SEG_LENGTH - 1)))], PinLE_Data;
}
SET_74HC573(0xff, PinLE_Comm); // 先关闭公共端
SET_74HC573(ucTemp, PinLE_Data); // 赋值
SET_74HC573(~(0x01 << i), PinLE_Comm); // 打开公共端
Delay2ms(); // 显示
}
// 计数控制速度
s_uiCount++;
if(s_uiCount >= DISPALY_SPEED) {
s_uiCount = 0;
// 控制有效定位
s_ucPosition++;
if(s_ucPosition >= ((SEG_LENGTH << 1) - 1)) {
s_ucPosition = 0;
}
}
}
/*
* 将数字按位转换成数组
* ulNumber:原数字
* *p_ucArr:数组地址
*/
void Transformation(ulong ulNumber, uchar *p_ucArr) {
*p_ucArr = ulNumber / 100000;
p_ucArr++;
*p_ucArr = ulNumber % 100000 / 10000;
p_ucArr++;
*p_ucArr = ulNumber % 10000 / 1000;
p_ucArr++;
*p_ucArr = ulNumber % 1000 / 100;
p_ucArr++;
*p_ucArr = ulNumber % 100 / 10;
p_ucArr++;
*p_ucArr = ulNumber % 10;
}
void main(void) {
uchar ucArr[6] = {0};
ulong ulNumber = 187732;
// 初始化数码管-关闭显示
SET_74HC573(0xff, PinLE_Comm);
SET_74HC573(0x00, PinLE_Data);
// 将数字按位转换成数组
Transformation(ulNumber, ucArr);
while (1) {
// 显示数组中的数字
Display(ucArr);
}
}