#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
uchar t0;
uchar code SEG_DAT[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code BIT_CS[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
sbit SH_CP = P0^0; //移位串行时钟脉冲
sbit DS = P0^1; //串行数据输入
sbit ST_CP = P0^2; //数据并行输出锁存器控制脉冲
//串行输入子程序
void Serial_Input_595(uchar dat)
{
uchar i;
for (i = 0; i < 8; i++)
{
if (dat&0x80) //发送高位
{
DS = 1;
}
else
{
DS = 0;
}
dat <<= 1; //次高位左移到高位
SH_CP = 0; //移位时钟线拉低
nop();
nop();
SH_CP = 1; //DS位上的0或1在移位脉冲上升沿被移入595
nop();
nop();
}
}
//并行输出子程序
void Parallel_Output_595()
{
ST_CP = 0;
nop();
nop();
ST_CP = 1; //上升沿将数据送到输出锁存器
nop();
nop();
}
void display(uint count)
{
uchar i=0;
uchar a[6];
uchar temp;
a[0]=0;
a[1]=count/10000;
a[2]=(count%10000)/1000;
a[3]=(count%10000%1000)/100;
a[4]=(count%10000%1000%100)/10;
a[5]=count%10;
for(i = 0; i <= 5; i++)
{
Serial_Input_595(BIT_CS[i]);
temp=a[i];
Serial_Input_595(SEG_DAT[temp]);
Parallel_Output_595();
}
}
void timer0_init(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
}
void main(void)
{
uint i=0;
timer0_init();
while(1)
{
if(t0==5)
{
t0=0;
i++;
}
display(i);
if(i==65535) i=0;
}
}
评论0