用51单片机4位数码管显示"H38C”,"L25C",C语言程序分别怎么写?P1口是段码输出口,P2.7-P2.4口是扫描口。

如题所述

#include<reg52.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//共阳数码管
uchar code table[] = { 0x89,0xb0,0x80,0xc6, //H38C的编码
0xc7,0xa4,0x92,0xc6}; //L25C的编码
//延时函数
void delay(uchar t)
{
uchar i;
while(t--)
{
for(i = 0;i = 50;i++);
}
}

void main()
{
uchar i,j,m,n,a;
P1 = 0xff;
P2 = 0xff;
while(1)
{
for(m = 0;m<10;m++)//显示10次h38c
{
P2 = 0x10;
for(i = 0;i<3;i++)//依次显示h38c 通过控制时间 是他们同时被显示在数码管上
{
P1 = table[i];
delay(2);
P2= _crol_(P2,1);

}
}

for(n = 0;n<10;n++)//显示10次L25C
{
P2 = 0x10;
for(j = 0;j<3;j++)//依次显示L25C 通过控制时间 是他们同时被显示在数码管上
{
P1 = table[j+4];
delay(2);
P2 = _crol_(P2,1);
}
}
}
}追问

程序烧录进单片机没反应啊?不知道是不是我的实验板有问题!

追答

我再给你个程序,这是我以前写的,因为我没用过单片机,不是很了解。
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;

uchar code table[] = {0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8};//- 0xbf

void delay(uchar t)
{
uchar i;
while(t--)
{
for(i = 0;i<100;i++);
}
}

void Opertion ()
{
uchar i,j,k,m,n,a,b,c,d;

//显示---1
for(a = 0;a<20;a++)
{
P2 = 0x08;
for(i = 0;i<3;i++)
{

P0 = 0xbf;
delay(10);
P2 = _cror_(P2,1);
}
//P2 = 0x01
P0 = table[0];
delay(10);
}

//显示--12
for(b = 0;b<20;b++)
{
P2 = 0x08;
for(j = 0;j<2;j++)
{

P0 = 0xbf;
delay(10);
P2 = _cror_(P2,1);
}
P2 = 0x02;
for(k = 0;k<2;k++)
{

P0 = table[k];
delay(10);
P2 = _cror_(P2,1);
}
}

//显示-123
for(c = 0;c<20;c++)
{
P2 = 0x08;
P0 = 0xbf;
delay(10);
for(j = 0;j<3;j++)
{
P2 = _cror_(P2,1);
P0 = table[j];
delay(10);
}
}

//显示1234 2345 3456 4567

for(k = 0;k<4;k++)
{
for(d = 0;d<20;d++)
{
P2 = 0x08;
for(m = 0;m<4;m++)
{
P0 = table[k+m];
//P2 = sel;
delay(10);
P2 = _cror_(P2,1);
}
}
}

d = 0;
//显示567-
for(d = 0;d<20;d++)
{
P2 = 0x08;
for(j = 0;j<3;j++)
{
P0 = table[j+4];
//P2 = sel;
delay(10);
P2 = _cror_(P2,1);
}
P0 = 0xbf;
delay(10);
}
d = 0;
//显示67--
for(d = 0;d<20;d++)
{
P2 = 0x08;
for(j = 0;j<2;j++)
{
P0 = table[j+5];
//P2 = sel;
delay(10);
P2 = _cror_(P2,1);
}
for(k = 0;k<2;k++)
{
P0 = 0xbf;
delay(10);
P2 = _cror_(P2,1);
}
}
d = 0;

//显示7---
for(d = 0;d<20;d++)
{
P2 = 0x08;
//P2 = sel;
P0 = table[6];
delay(10);
for(n = 0;n<3;n++)
{
P2 = _cror_(P2,1);
P0 = 0xbf;
delay(10);
}
}
}

void main()
{
while(1)
{
Opertion();
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-06
共阴还共阳,高电平选通还是低电平?
相似回答
大家正在搜