任务描述:
使用蓝桥杯单片机,完成以下操作:
1.关闭蜂鸣器继电器LED等无用设备(初始化系统)
2.8个数码管分别依次显示0-9的值
3.8个数码管同时显示0-F的值
#include "reg52.h"
//定义锁存器口
sbit HC138_A=P2^5;
sbit HC138_B=P2^6;
sbit HC138_C=P2^7;
//数码管段码表
unsigned char code Seg_Table[18] =
{ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,0x82, 0xf8,
0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e,
0xbf, 0x7f};
//延时
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
//锁存器初始化
void InitHC138(unsigned char n)
{
switch(n)
{
case 4:
HC138_C=1;
HC138_B=0;
HC138_A=0; //Y4低电平
break;
case 5:
HC138_C=1;
HC138_B=0;
HC138_A=1; //Y5低电平
break;
case 6:
HC138_C=1;
HC138_B=1;
HC138_A=0; //Y6低电平
break;
case 7:
HC138_C=1;
HC138_B=1;
HC138_A=1; //Y7低电平
break;
}
}
//输出函数
void OutPutP0(unsigned char channel,unsigned char dat)
{
InitHC138(channel);
P0=dat;
}
//数码管显示函数
void ShowSMG_Bit(unsigned char dat,unsigned char pos)
{
OutPutP0(6,(0x01<<pos));
OutPutP0(7,dat);
}
//数码管输出函数
void SMG_static()
{
unsigned char i,j;
for(i=0;i<8;i++) //位移
{
for(j=0;j<10;j++) //数字增加
{
ShowSMG_Bit(Seg_Table[j],i);
Delay(60000);
}
Delay(60000);
}
for(j=0;j<18;j++) //全部数字增加0~f
{
OutPutP0(6,0xff);
OutPutP0(7,Seg_Table[j]);
Delay(60000);
}
}
//初始化
void InitSystem()
{
OutPutP0(5,0x00);
OutPutP0(4,0xff);
}
void main()
{
InitSystem();
while(1)
{
SMG_static();
}
}