关于单片机io口驱动问题
#define uchar unsigned char
#define ON 0
#define OFF 1
sbit FM=P2^3;
sbit d=P2^6;
sbit w=P2^7;
uchar code sz[27]={
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, //熄灭
0xfc,//红灯亮 25
0xf3,//绿灯亮 26
0xcf,//黄灯亮 27
};
void delay(unsigned int t) //延迟函数
{
while(--t);
}
void main()
{
while(1)
{
P0=sz[1];
d=1;
d=0; //数字1
P0=0x7f;
w=1;
w=0;
P0=0xff; //
w=1; // 所有位都为高电压,位选关闭锁存,以免产生人眼看成乱码
w=0; //
P0=sz[2];
d=1;
d=0;
P0=0xbf; //数字2
w=1;
w=0;
P0=0xff;
w=1;
w=0;
P0=sz[4];
d=1;
d=0;
P0=0xdf; //数字4
w=1;
w=0;
P0=0xff;
w=1;
w=0;
P0=sz[0];
d=1;
d=0;
P0=0xef;
w=1;
w=0;
P0=0xff; //数字0
w=1;
w=0;
P0=sz[7];
d=1;
d=0;
P0=0xf7;
w=1;
w=0;
P0=0xff; //数字7
w=1;
w=0;
P0=sz[1];
d=1;
d=0;
P0=0xfb;
w=1;
w=0; //数字1
P0=0xff;
w=1;
w=0;
P0=sz[0];
d=1;
d=0;
P0=0xfd;
w=1;
w=0; //数字0
P0=0xff;
w=1;
w=0;
P0=sz[2];
d=1;
d=0;
P0=0xfe;
w=1;
w=0; //数字2
P0=0XFF;
w=1;
w=0;
FM=ON; //蜂鸣器报警
delay(50000); //延迟
FM=OFF; //蜂鸣器关闭
delay(50000); //延迟
}
}
而我把延迟函数50000改成500时,数码管显示很弱,当改成5时数码管就有显示了。
是蜂鸣器延时占用数码管动态显示时间,更改如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
//#define ON 0
//#define OFF 1
sbit FM=P2^3;
sbit d=P2^6;
sbit w=P2^7;
uint count=0;
uchar code sz[27]=
{
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, //熄灭
0xfc,//红灯亮 25
0xf3,//绿灯亮 26
0xcf,//黄灯亮 27
};
/*void delay(unsigned int t)//延迟函数
{
while(--t);
}*/
void main()
{
while(1)
{
P0=sz[1];
d=1;
d=0; //数字1
P0=0x7f;
w=1;
w=0;
P0=0xff; //
w=1; //所有位都为高电压,位选关闭锁存,以免产生人眼看成乱码
w=0; //
P0=sz[2];
d=1;
d=0;
P0=0xbf; //数字2
w=1;
w=0;
P0=0xff;
w=1;
w=0;
P0=sz[4];
d=1;
d=0;
P0=0xdf; //数字4
w=1;
w=0;
P0=0xff;
w=1;
w=0;
P0=sz[0];
d=1;
d=0;
P0=0xef;
w=1;
w=0;
P0=0xff; //数字0
w=1;
w=0;
P0=sz[7];
d=1;
d=0;
P0=0xf7;
w=1;
w=0;
P0=0xff; //数字7
w=1;
w=0;
P0=sz[1];
d=1;
d=0;
P0=0xfb;
w=1;
w=0; //数字1
P0=0xff;
w=1;
w=0;
P0=sz[0];
d=1;
d=0;
P0=0xfd;
w=1;
w=0; //数字0
P0=0xff;
w=1;
w=0;
P0=sz[2];
d=1;
d=0;
P0=0xfe;
w=1;
w=0; //数字2
P0=0XFF;
w=1;
w=0;
/* FM=ON; //蜂鸣器报警
delay(50000); //延迟
FM=OFF; //蜂鸣器关闭
delay(50000); */ //延迟
if(FM==1)
{
count++;
if(count>=5000)
{
FM=0;
count=0;
}
}
else
{
count++;
if(count>=5000)
{
FM=1;
count=0;
}
}
}
}