#include <REGX52.H>
#include <stdlib.h>
unsigned char shumaguan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x48};
unsigned int t=0;
unsigned int x=0,y=0,k=0;
unsigned char keyNum=0;
sbit2_7=2^7;
sbit2_6=2^6;
void Delay(unsigned int xms) //设置延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Timer0_Init() //配置定时器中断
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初始值
TH0 = 0xFC; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
PT0=1;
}
unsigned char key() //检测按键
{
if(P3_0==0)
{
Delay(20);
while(P3_0==0);
Delay(20);
keyNum++;
}
return keyNum;
}
void shuzi(unsigned char location,number) //数码管位数和显示数字
{
P2_7=1;P2_6=0;
switch(location)
{
case 1:P0_0=0;P0_1=1;P0_2=1;P0_3=1;P0_4=1;P0_5=1;P0_6=1;P0_7=1;break;
case 2:P0_0=1;P0_1=0;P0_2=1;P0_3=1;P0_4=1;P0_5=1;P0_6=1;P0_7=1;break;
case 3:P0_0=1;P0_1=1;P0_2=0;P0_3=1;P0_4=1;P0_5=1;P0_6=1;P0_7=1;break;
case 4:P0_0=1;P0_1=1;P0_2=1;P0_3=0;P0_4=1;P0_5=1;P0_6=1;P0_7=1;break;
case 5:P0_0=1;P0_1=1;P0_2=1;P0_3=1;P0_4=0;P0_5=1;P0_6=1;P0_7=1;break;
case 6:P0_0=1;P0_1=1;P0_2=1;P0_3=1;P0_4=1;P0_5=0;P0_6=1;P0_7=1;break;
}
P2_7=0;P2_6=1;
P0=shumaguan[number];
Delay(1);
P0=0x00;
}
void Timer0_Routine() interrupt 1 //中断函数
{
TL0 = 0x18; //设置定时初始值
TH0 = 0xFC; //设置定时初始值
t++;
}
void main()
{
Timer0_Init();
while(1)
{
keyNum=key();
keyNum=keyNum%2;
if(keyNum==1)
{
srand(t);
x=rand()%9+1;
srand(t/x);
y=rand()%9+1;
k=x*y;
while(keyNum==1)
{
shuzi(1,x);
shuzi(3,y);
shuzi(4,10);
shuzi(5,k/10);
shuzi(6,k%10);
keyNum=key();
}
}
}
}