单片机模块学习之数码管

数码管是led的升级版,用到段选位选,其实都差不多

位选就是选某个数码管com,段选就是数码管送入数据

还有转码表
共阴
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
共阳
0xc0,xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e

驱动思路就是打开位选,然后送段选

蓝桥杯数码管驱动代码

void LedScan()
{
	static u8 index=0;

	P2=(P2&0x1f)|0xe0;
	P0=0xff;
	P2=P2&0x1f;

	P2=(P2&0x1f)|0xc0;
	P0=0x80>>index;
	P2=P2&0x1f;

	P2=(P2&0x1f)|0xe0;
	P0=LedBuff[index];
	P2=P2&0x1f;

	index++;
	index&=7;
}

另外两个数组数码管转码LedChar[]和数码管缓冲LedBuff[]
u8 code LedChar[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
u8 LedBuff[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

数码管缓冲显示

void ShowNumber(u16 dat)
{
	char i;
	u8 buff[8];

	for(i=0;i<8;i++)
	{
		buff[i]=dat%10;
		dat/=10;
	}

	for(i=7;i>0;i--)
	{
		if(buff[i]==0)
			LedBuff[i]=0xff;
		else
			break;
	}

	for(;i>=0;i--)
	{
		LedBuff[i]=LedChar[buff[i]];
	}
}

你可能感兴趣的:(单片机模块学习之数码管)