/********************************************************************
汇诚科技
网址:http://www.ourhc.cn
产品有售淘宝店:http://shop36330473.taobao.com
*********************************************************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar duan[10]={0xc0,0Xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //所需的段的位码
//uchar wei[4]={0XEf,0XDf,0XBf,0X7f}; //位的控制端 (开发板)
uchar wei[4]={0X80,0X40,0X20,0X10}; //位的控制端 (仿真)
uint z,x,c,v, date; //定义数据类型
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
/******************************************************************
延时函数
******************************************************************/
void delay(uchar t)
{
uchar i,j;
for(i=0;i<t;i++)
{
for(j=13;j>0;j--);
{ ;
}
}
}
/**********************************************************************
数码管动态扫描
*********************************************************************/
void xianshi()
{
/*****************数据转换*****************************/
z=date/1000; //求千位
x=date%1000/100; //求百位
c=date%100/10; //求十位
v=date%10; //求个位
P2=wei[0];
P0=duan[z];
delay(50);
P2=wei[1];
P0=duan[x];
delay(50);
P2=wei[2];
P0=duan[c];
delay(50);
P2=wei[3];
P0=duan[v];
delay(50);
}
/*************************************************************************
串口初始化,波特率9600,方式1
**************************************************************************/
void Init_Com(void)
{
TMOD = 0x20; //16位定时计数器
PCON = 0x00; //电源控制寄存器
SCON = 0x50; //串口寄存器
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
/*************************************************************************
主函数
**************************************************************************/
void main()
{
Init_Com();//串口初始化
// date=1234;
P1=0X0A;
while(1)
{
if(P2_0==0)P1=0XAA;
if(P2_1==0)P1=0X55;
if(P2_2==0)P1=0X0F;
if(P2_3==0)P1=0XF0;
SBUF=P1;
while(!TI);
TI=0;
}
}