数码管动态显示
数码管静态显示
#include "reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(u16 i)
{
while(i--);
}
void select_bit(u8 numbit)
{
switch(numbit)
{
case(0):
LSA=1;LSB=1;LSC=1;break;
case(1):
LSA=0;LSB=1;LSC=1;break;
case(2):
LSA=1;LSB=0;LSC=1;break;
case(3):
LSA=0;LSB=0;LSC=1;break;
case(4):
LSA=1;LSB=1;LSC=0;break;
case(5):
LSA=0;LSB=1;LSC=0;break;
case(6):
LSA=1;LSB=0;LSC=0;break;
case(7):
LSA=0;LSB=0;LSC=0;break;
}
}
void NumDis(u8 i)
{
P0 = smgduan[i];
}
void main()
{
u8 i = 3;
u8 numbit = 2;
NumDis(i);
select_bit(numbit);
while (1)
{
}
}
数码管动态显示
#include "reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(u16 i)
{
while(i--);
}
void NumDis()
{
u8 i;
for(i = 0; i < 8; i++)
{
switch(i)
{
case(0):
LSA=1;LSB=1;LSC=1;break;
case(1):
LSA=0;LSB=1;LSC=1;break;
case(2):
LSA=1;LSB=0;LSC=1;break;
case(3):
LSA=0;LSB=0;LSC=1;break;
case(4):
LSA=1;LSB=1;LSC=0;break;
case(5):
LSA=0;LSB=1;LSC=0;break;
case(6):
LSA=1;LSB=0;LSC=0;break;
case(7):
LSA=0;LSB=0;LSC=0;break;
}
P0 = smgduan[i];
delay(100);
P0=0X00;
}
}
void main()
{
while (1)
{
NumDis();
}
}
利用位操作优化
#include "reg52.h"
#include <intrins.h>
typedef unsigned int u16;
typedef unsigned char u8;
#define led P2
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(u16 i)
{
while(i--);
}
void NumDis()
{
u8 i;
for(i = 0; i < 8; i++)
{
led = 0xFF;
led = _crol_((~i)&led, 2);
P0 = smgduan[i];
delay(100);
P0=0X00;
}
}
void main()
{
while (1)
{
NumDis();
}
}