【发错区了,求斑竹删除了吧】

2020-02-08 12:27发布

只用了一个数码管屏幕,当然用段式液晶也可以,不过显示程序要改
k1按键是选手1下完子按的按钮,k2按键是选手2下完子按的按钮,k3是重启计时键,k4是在调整计时时间的
系统上电后可以调时间,然后任意选手按一下自己的按键即可开始计时

#include<reg51.h>
#include<stdio.h>
#include <intrins.h>
typedef unsigned int  uint;
typedef unsigned char uchar;
sfr AUXR=0x8e;

sfr P0M1=0x93;
sfr P0M0=0x94;
sfr P1M1=0x91;
sfr P1M0=0x92;
sfr P2M1=0x95;
sfr P2M0=0x96;
sfr P3M1=0xB1;
sfr P3M0=0xB2;
sfr P4=0xC0;
sfr P4M1=0xB3;
sfr P4M0=0xB4;
sfr P4SW=0xBB;
sfr P5=0xC8;
sfr P5M1=0xC9;
sfr P5M0=0xCA;
sfr AUXR1=0xA2;

sfr WDT_CONTR=0xC1;
sfr WAKE_CLK0=0x8F;
sfr ADC_CONTR=0xBC;
sfr ADC_RES=0xBD;                   //AD转换结果寄存器高8位
sfr ADC_RESL=0xBE;                   //AD转换低2位
sfr P1ASF=0x9D;
sfr CLK_DIV=0x97;

sfr CCON=0xD8;
sfr CMOD=0xD9;
sfr CCAPM0=0xDA;
sfr CCAPM1=0xDB;
sfr CL=0xE9;
sfr CH=0xF9;
sfr CCAP0L=0xEA;
sfr CCAP0H=0xFA;
sfr CCAP1L=0xEB;
sfr CCAP1H=0xFB;
sfr PCA_PWM0=0xF2;
sfr PCA_PWM1=0xF3;
sbit CR=CCON^6;
sbit CF=CCON^7;
sbit CCF0=CCON^0;
sbit CCF1=CCON^1;

sfr IAP_DATA=0xC2;
sfr IAP_ADDRH=0xC3;
sfr IAP_ADDRL=0xC4;
sfr IAP_CMD=0xC5;  
sfr IAP_TRIG=0xC6;
sfr IAP_CONTR=0xC7;

sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit ML=P3^4;
sbit AL=P3^5;

sbit hourH=P2^7;
sbit hourL=P2^6;
sbit miniH=P2^5;
sbit miniL=P2^4;

uchar MiniH;
uchar MiniL;
uchar HourH;
uchar HourL;
uchar key1_flag;
uchar key2_flag;
uchar key3_flag;
uchar key4_flag;

uchar INR_5ms1;
uchar INR_5ms2;
uchar INR_5ms3;
uchar INR_5ms4;
uchar counter;
uchar key_counter1;
uchar key_counter2;
uchar key_counter3;
uchar key_counter4;
uchar keycode;
int time_counter;
int time1_counter;
int time2_counter;

uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//共阴极
uint code time[]={3600,3000,2400,1800,1200,600,1200,1800,2400,3000,3600};
void sys_init(void);
void TimeC(void);
void sec_t1(void);                       //interrupt 3                          //中断产生1s程序
void KEY1(void);
void KEY2(void);
void KEY3(void);
void KEY4(void);
void process(void);
void CTT(void);                                                        //计数转时间程序
void Display(void);                                                //数码管显示程序(电量剩余时间)

void main(void)
{
sys_init();
TimeC();
while(1)
{
KEY1();
KEY2();
KEY3();
KEY4();
process();
CTT();
Display();
}
}


void delay(uint x)
{
uint i;
for(i=0;i<x;i++);
}

void sys_init(void)
{
P0M1=0x00;
P0M0=0xff;
keycode=0;
key1_flag=0;
key2_flag=0;
key3_flag=0;
key4_flag=0;
//time1_counter=3600;
//time2_counter=3600;
counter=0;
}

/*time*/
void TimeC(void)                                                   //定时器1计数初始化,产生5ms程序
{
counter=0;
TR1=0;
TMOD|=0x10;
TH1=0xEC;
TL1=0x78;
EA=1;
ET1=1;
TR1=1;
}

void sec_t1(void) interrupt 3                          //中断产生1s程序
{
TH1=0xEC;
TL1=0x78;
INR_5ms1=1;
INR_5ms2=1;;
INR_5ms3=1;
INR_5ms4=1;
counter++;
if(counter==200)
{
  counter=0;
  ML=!ML;                                                                                                          
  if(key1_flag)
  {
  time1_counter--;
  }
  if(key2_flag)
  {
   time2_counter--;
  }
}
}

void KEY1(void)
{
if(!k1)
{
  if(INR_5ms1)
  {
   key_counter1++;
   if(key_counter1==10)
   {
        key_counter1=0;
        key1_flag=1;
        key2_flag=0;
        key3_flag=0;
        AL=0;
   }
   INR_5ms1=0;
  }
}
}

void KEY2(void)
{
if(!k2)
{
  if(INR_5ms2)
  {
   key_counter2++;
   if(key_counter2==10)
   {
        key_counter2=0;
        key1_flag=0;
        key2_flag=1;
        key3_flag=0;
        AL=1;
   }
   INR_5ms2=0;
  }
}
}

void KEY3(void)
{
if(!k3)
{
  if(INR_5ms3)
  {
   key_counter3++;
   if(key_counter3==10)
   {
        key_counter3=0;
        key1_flag=0;
        key2_flag=0;
        key3_flag=1;
        AL=1;
   }
   INR_5ms3=0;
  }
}
}

void KEY4(void)
{
if(!k4)
{
  if(INR_5ms4)
  {
   key_counter4++;
   if(key_counter4==25)
   {
        key_counter4=0;
        key4_flag=1;
        keycode++;
        if(keycode>10)
        {
          keycode=0;
        }
   }
   INR_5ms4=0;
  }
}
}

void process(void)
{

if(key1_flag)
{
  time_counter=time1_counter;
}

if(key2_flag)
{
  time_counter=time2_counter;
}

if(key3_flag)
{
  IAP_CONTR=0x60;
}

if(key4_flag)
{
  
  key4_flag=0;
  if((key1_flag==0)&&(key2_flag==0))
  {
  time1_counter=time[keycode];
  time2_counter=time[keycode];
  time_counter=time1_counter;
  time_counter=time2_counter;
  }
}

if(time_counter<0)
{
  time_counter=0;
}
}


void CTT(void)                                                        //计数转时间程序
{
uint i,j;
i=time_counter/60;
j=time_counter%60;
HourH=i/10;
HourL=i%10;
MiniH=j/10;
MiniL=j%10;
}

void Display(void)                                                //数码管显示程序(电量剩余时间)
{

P0=0x00;
P0=tab[HourH];
hourH=0;
delay(50);
P0=0x00;
hourH=1;

delay(10);

P0=0x00;
P0=tab[HourL];
hourL=0;
delay(50);
P0=0x00;
hourL=1;

delay(10);

P0=0x00;
P0=tab[MiniH];
miniH=0;
delay(50);
P0=0x00;
miniH=1;

delay(10);

P0=0x00;
P0=tab[MiniL];
miniL=0;
delay(50);
P0=0x00;
miniL=1;
}
0条回答

一周热门 更多>