书接上回 51单片机-数码管显示单个
http://t.csdnimg.cn/Ii6x0
一.简单全亮
ps:有好几次没电亮,是没加阻值,看了这方面的基础要好好补补了.
这种是不需要控制哪个亮,全部接了电.直接用上一篇的代码,就可以每个都动态变化
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
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 //熄灭
};
void DelayMS(uint x)
{
uchar t;
while(x--)
for(t=120;t>0;t--);
}
void main()
{
uchar i=0;
P0=0x00;
while(i<24)
{
P0=leddata[i];
i++;
DelayMS(400);
}
}
二.控制单个变化
就是分别控制 1234引脚了
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
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 //熄灭
};
void DelayMS(uint x)
{
uchar t;
while(x--)
for(t=120;t>0;t--);
}
void main()
{
uchar i=0;
P0=0x00;
P2 =0xfe;
while(i<24)
{
P0=leddata[i];
i++;
DelayMS(400);
}
}
这样就可以做很多小显示了,比如2024;
三.2024
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
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 //熄灭
};
void DelayMS(uint x)
{
uchar t;
while(x--)
for(t=120;t>0;t--);
}
void delay(unsigned int t)
{
unsigned int i, j;
for (i = 0; i < t; i++) {
for (j = 0; j < 1275; j++) {
; // 空循环,等待时间过去
}
}
}
void main()
{
uchar i=0;
P0=0x00;
while(1)
{
for (i = 0; i < 5; i++)
{
switch (i)
{
case 0:
P0 = 0x5B;
P2 =0xFE; //1111 1110
break;
case 1:
P0 = 0x3F;
P2 =0xFD;//1111 1101
break;
case 2:
P0 =0x5B;
P2 =0xFB;//1111 1011
break;
case 3:
P0 =0x66;
P2 =0xF7;//1111 0111
break;
default:
break;
}
delay(1);
P0=0x00;
}
}
}