#include <reg52.h>
#define uchar unsigned char //宏定义 #define uint unsigned int //宏定义 #define DELAY_TIME 2 //将常量定义为宏,方便修改代码 sbit we = P2^7; //数码管位选锁存器接口 sbit du = P2^6; //数码管段选锁存器接口 //数码管段选表 uchar code leddata[]={ 0x3F, //"0" 0x06, //"1" 0x5B, //"2" 0x4F, //"3" 0x66, //"4" 0x6D, //"5" 0x7D, //"6" 0x07, //"7" 0x7F, //"8" 0x6F, //"9" 0x77, //"A" 0x7C, //"B" 0x39, //"C" 0x5E, //"D" 0x79, //"E" 0x71, //"F" 0x76, //"H" 0x38, //"L" 0x37, //"n" 0x3E, //"u" 0x73, //"P" 0x5C, //"o" 0x40, //"-" 0x00, //熄灭 0x00 //自定义 }; //毫秒级延时函数 void delay(uint z) { uint x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); }
void main() { while(1) {
P0 = 0xfe;//1111 1110 第一位数码管显示,注意数码管的排序,P0.0对应连接的是左边第一位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[1]; du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME);//延时
P0 = 0xfd; //1111 1101 只选通第二位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[2]; //显示2 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME);//延时
P0 = 0xfb; // 1111 1011只选通第三位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[3]; //显示3 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME); //延时
P0 = 0xf7; //1111 0111 只选通第四位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[4]; //显示4 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME);//延时
P0 = 0xef; //1110 1111 只选通第五位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[5]; //显示5 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME);//延时
P0 = 0xdf; //1101 1111 只选通第六位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[6]; //显示6 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME); //延时
we = 1; //打开位选 P0 = 0xbf; //1011 1111 只选通第七位数码管 we = 0; //关闭位选
P0 = leddata[7]; //显示7 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME); //延时
P0 = 0x7f; //0111 1111 只选通第八位数码管 we = 1; //打开位选 we = 0; //关闭位选
P0 = leddata[8]; //显示8 du = 1; //打开段选 du = 0; //关闭段选 delay(DELAY_TIME); //延时 } }
|