#include"reg52.h"
#include"delay.h"
#define uchar unsigned char
#define uint unsigned int
uchar code SMG_position[9] = {0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar code SMG_duanma[15] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6,0x8c,0x88};
uchar yi,er,san,si,wu,liu,qi,ba;
void InitHC138(uchar channel)
{
switch(channel)
{
case 4:P2 = (P2&0x1f)|0x80;break;
case 5:P2 = (P2&0x1f)|0xa0;break;
case 6:P2 = (P2&0x1f)|0xc0;break;
case 7:P2 = (P2&0x1f)|0xe0;break;
}
}
void ShowSMG_Bit(uchar dat,uchar pos)
{
InitHC138(7);P0 = 0xff;//俗称“消影”
InitHC138(6);P0 = SMG_position[pos];
InitHC138(7);P0 = SMG_duanma[dat];
}
void InitSystem()
{
InitHC138(4);P0 = 0xff;
InitHC138(5);P0 = 0x00;
InitHC138(6);P0 = 0xff;
InitHC138(7);P0 = 0xff;
}
void All_SMG()
{
InitHC138(6);P0 = 0xff;
InitHC138(7);P0 = 0xff;
}
void Display_SMG()//单片机运行速度非常快,只要时间小于10ms,通过人的眼睛是识别不了变化的
{
ShowSMG_Bit(yi,1);Delay2ms();
ShowSMG_Bit(er,2);Delay2ms();
ShowSMG_Bit(san,3);Delay2ms();
ShowSMG_Bit(si,4);Delay2ms();
ShowSMG_Bit(wu,5);Delay2ms();
ShowSMG_Bit(qi,7);Delay2ms();
ShowSMG_Bit(ba,8);Delay2ms();
All_SMG();Delay2ms();
}
void main()
{
while(1)
{
}
}
delay.c
#inlcude"delay.h"
#include"intrins.h"
void Delay2ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
delay.h
#ifndef _delay_h
#define _delay_h
void Delay2ms();
void Delay10ms();
#endif