七段数码管模拟演示
码管如下:
用下面的二位数组 每行表示一个数字,每个数字用七段数码管描述,通过控制某几个二极管点亮,拼成一个数字的图像。
int MX[10][7] =
{
{1,1,1,0,1,1,1},//0
{0,0,1,0,0,1,0},//1
{1,0,1,1,1,0,1},//2
{1,0,1,1,0,1,1},//3
{0,1,1,1,0,1,0},//4
{1,1,0,1,0,1,1},//5
{1,1,0,1,1,1,1},//6
{1,0,1,0,0,1,0},//7
{1,1,1,1,1,1,1},//8
{1,1,1,1,0,1,1}//9
};
因为无法驱动真正的数码管,我们把要驱动的数码管所在的位置“描绘”到缓冲区中每个缓冲区只能描绘1个数字,缓冲区“像素”数为:7x4
比如,3号数码管在缓冲区中的描绘为:
....
....
....
@@@@
....
....
....
再比如,5号管描述为:
....
....
....
...@
...@
...@
...@
注意,不同数码管所占据的模拟位置稍有重叠,这并不影响效果。
char BUF1[7][4]; //每个二维数组表示一个数字,三个数组表示一个三位数。
char BUF2[7][4]; //输出时,取每个数组的第一行一起输出,依次类推,因为输出是
char BUF3[7][4]; //从上到下,从左到右进行的,不能倒退
具体代码如下:
#include
#include
#include
int MX[10][7] =
{
{1,1,1,0,1,1,1},//0
{0,0,1,0,0,1,0},//1
{1,0,1,1,1,0,1},//2
{1,0,1,1,0,1,1},//3
{0,1,1,1,0,1,0},//4
{1,1,0,1,0,1,1},//5
{1,1,0,1,1,1,1},//6
{1,0,1,0,0,1,0},//7
{1,1,1,1,1,1,1},//8
{1,1,1,1,0,1,1}//9
};
void init_buf(char p[][4])//初始化
{
for (int i = 0; i < 7; i++)
for (int j = 0; j < 4; j++)
p[i][j] = '.';
}
void Fill(int data[],char buf[7][4])//填充缓冲区
{
int i;
for (i = 0; i < 7; i++)
if (data[i])
{
switch(i)
{
case 0:
buf[0][0] = buf[0][1] = buf[0][2] = buf[0][3] = '@';
break;
case 1:
buf[0][0] = buf[1][0] = buf[2][0] = buf[3][0] = '@';
break;
case 2:
buf[0][3] = buf[1][3] = buf[2][3] = buf[3][3] = '@';
break;
case 3:
buf[3][0] = buf[3][1] = buf[3][2] = buf[3][3] = '@';
break;
case 4:
buf[3][0] = buf[4][0] = buf[5][0] = buf[6][0] = '@';
break;
case 5:
buf[3][3] = buf[4][3] = buf[5][3] = buf[6][3] = '@';
break;
case 6:
buf[6][0] = buf[6][1] = buf[6][2] = buf[6][3] = '@';
break;
}
}
}
int main()
{
char BUF1[7][4],BUF2[7][4],BUF3[7][4];
char can[5];
init_buf(BUF1);
init_buf(BUF2);
init_buf(BUF3);
printf("请输入要显示的3位整数(100-999),输入exit退出:");
gets(can);
while (strcmp(can,"exit"))
{
Fill(MX[can[0] - '0'],BUF1);
Fill(MX[can[1] - '0'],BUF2);
Fill(MX[can[2] - '0'],BUF3);
for (int i = 0; i < 7; i++)//输出
{
int j;
for (j = 0; j < 4; j++)
printf("%c",BUF1[i][j]);
printf(" ");
for (j = 0; j < 4; j++)
printf("%c",BUF2[i][j]);
printf(" ");
for (j = 0; j < 4; j++)
printf("%c",BUF3[i][j]);
printf("
");
}
init_buf(BUF1);
init_buf(BUF2);
init_buf(BUF3);
system("pause");
printf("请输入要显示的3位整数(100-999),输入exit退出:");
gets(can);
}
return 0;
}