第九届蓝桥杯单片机类代码

//驱动自己加
#include
#include
#include
#define u8 unsigned char
#define u16 unsigned int
u16 dingshishijian,shi;
u16 value=1,zhankong=10000;
u8 qidong,moshi,cishu,bian;
u8 ling,yi,er,san,si,wu,liu,qi;
void keyboard();
void write(u8 add,u8 dat);
void display2();
void shumaguanguan()
{
   P2=0xc0;
 P0=0xff;
 P2=0xff;
 P0=0xff;
}
void display();
u16 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
     0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
     void Delay5ms()  //@11.0592MHz
{
unsigned char i, j;
 _nop_();
 _nop_();
 _nop_();
 i = 11;
 j = 190;
 do
 {
  while (--j);
 } while (--i);
}
void display()
{
 
 
  
 P2=0xc0;
   P0=0x01;
 P2=0xff;
 P0=~0x40;
 Delay5ms() ;
 P2=0xc0;
   P0=0x02;
 P2=0xff;
 P0=~duan[moshi];
 Delay5ms() ;
 P2=0xc0;
   P0=0x04;
 P2=0xff;
 P0=~0x40;
Delay5ms() ;
 
 
 
 
 P2=0xc0;
   P0=0x08;
 P2=0xff;
 P0=0xff;
  Delay5ms() ;
 P2=0xc0;
   P0=0x10;
 P2=0xff;
 P0=0xff;
  Delay5ms() ;
 P2=0xc0;
   P0=0x20;
 P2=0xff;
 P0=~duan[dingshishijian];
  Delay5ms() ;
 P2=0xc0;
   P0=0x40;
 P2=0xff;
 P0=~0x3f;
 Delay5ms() ;
 P2=0xc0;
   P0=0x80;
 P2=0xff;
 P0=~0x3f;
 Delay5ms() ;
}
 
 
 
//void display2()
//{
// 
// 
//  
// P2=0xc0;
//   P0=0x01;
// P2=0xff;
// P0=0xff;
// Delay5ms() ;
// P2=0xc0;
//   P0=0x02;
// P2=0xff;
// P0=0x00;
// Delay5ms() ;
// P2=0xc0;
//   P0=0x04;
// P2=0xff;
// P0=0x00;
//Delay5ms() ;
// 
// 
// 
// 
// P2=0xc0;
//   P0=0x08;
// P2=0xff;
// P0=0xff;
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x10;
// P2=0xff;
// P0=0xff;
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x20;
// P2=0xff;
// P0=~duan[dingshishijian];
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x40;
// P2=0xff;
// P0=~0x3f;
// Delay5ms() ;
// P2=0xc0;
//   P0=0x80;
// P2=0xff;
// P0=~0x3f;
// Delay5ms() ;
//}

//void display3()
//{
// 
// 
//  
// P2=0xc0;
//   P0=0x01;
// P2=0xff;
// P0=~0x40;
// Delay5ms() ;
// P2=0xc0;
//   P0=0x02;
// P2=0xff;
// P0=~duan[moshi];
// Delay5ms() ;
// P2=0xc0;
//   P0=0x04;
// P2=0xff;
// P0=~0x40;
//Delay5ms() ;
// 
// 
// P2=0xc0;
//   P0=0x08;
// P2=0xff;
// P0=0xff;
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x10;
// P2=0xff;
// P0=0xff;
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x20;
// P2=0xff;
// P0=0xff;
//  Delay5ms() ;
// P2=0xc0;
//   P0=0x40;
// P2=0xff;
// P0=0xff;
// Delay5ms() ;
// P2=0xc0;
//   P0=0x80;
// P2=0xff;
// P0=0xff;
// Delay5ms() ;
//}
// 
void display1()
{
 
 P2=0xc0;
   P0=0x10;
 P2=0xff;
 P0=~duan[5];
shumaguanguan();
}
 
 
 
void Delay100ms()  //@11.0592MHz
{
 unsigned char i, j, k;
 _nop_();
 _nop_();
 i = 5;
 j = 52;
 k = 195;
 do
 {
  do
  {
   while (--k);
  } while (--j);
 } while (--i);
}

void bixu()
{
    EA=1;
 EX0=0;
 IT0=0;
 
  EX1=0;
 IT1=0;
  AUXR |= 0x80;  //?????1T??
 TMOD &= 0xF0;  //???????
 TL0 = 0xCD;  //??????
 TH0 = 0xD4;  //??????
 TF0 = 0;  //??TF0??
 TR0 = 0;  //???0????
EA=1;
 ET0=1;
}
void Delay1ms()  //@11.0592MHz
{
 unsigned char i, j;
 _nop_();
 _nop_();
 _nop_();
 i = 11;
 j = 190;
 do
 {
  while (--j);
 } while (--i);
}
void liang(u8 dat)
{
   u16 value0,zhankong0,shijian0=dingshishijian,l;
 unsigned char i, j, k;
for(l=0;l {
 _nop_();
 _nop_();
 i = 5;
 j = 52;
 k = 195;
  
   while (--k);
   {
   {while (--j);
 }
 while (--i);
 {
  
  keyboard();
if(qidong==1)
  {
     P32=0;
   EX0=1;
  }
  else if(qidong==0)
  {
     P2=0x80;
    P32=1;
   EX0=0;
  }
 
 value0=value;
 zhankong0=zhankong;
  while(zhankong0--)
  {
     P0=0xff;
  }
 while(value0--)
  {
      P0=dat;
  }
}
 
}
   }
  }
void allinit()//初始
{
  P2=0xa0;
 P0=0x00;
 P2=0x80;
 P0=0xff;
}

void keyboard()//垃圾矩阵
{
 u8 Temp,dat=10;
   P3=0x7f;
 P4=0xef;
 Temp=P3;
 Temp&=0x0f;
 if(Temp!=0x0f)
 {
    Delay1ms();
  Temp=P3;
 Temp&=0x0f;
  if(Temp!=0x0f)
  {
   Temp=P3;
     switch(Temp)
    {
      case 0x7e:dat=0;break;
       case 0x7d:dat=1;break;
       case 0x7b:dat=2;break;
       case 0x77:dat=3;break;     
    }
      while(Temp!=0x0f)
   {
    if(dat==2&&cishu==0)
 {
    display1();
 }
    Temp=P3;
    Temp=Temp&0x0f;
   }
  }
 } 
   P3=0xbf;
 P4=0xfb;
 Temp=P3;
 Temp&=0x0f;
 if(Temp!=0x0f)
 {
    Delay1ms();
  Temp=P3;
 Temp&=0x0f;
  if(Temp!=0x0f)
  {
   Temp=P3;
     switch(Temp)
    {
      case 0xbe:dat=4;break;
       case 0xbd:dat=5;break;
       case 0xbb:dat=6;break;
       case 0xb7:dat=7;break;
     
    }
     while(Temp!=0x0f)
   {
    Temp=P3;
    Temp=Temp&0x0f;
   }
  }
 
 }
 
   P3=0xdf;
 Temp=P3;
 Temp&=0x0f;
 if(Temp!=0x0f)
 {
    Delay1ms();
  Temp=P3;
 Temp&=0x0f;
  if(Temp!=0x0f)
  {
   Temp=P3;
     switch(Temp)
    {
      case 0xde:dat=8;break;
       case 0xdd:dat=9;break;
       case 0xdb:dat=10;break;
       case 0xd7:dat=11;break;
     
    }
      while(Temp!=0x0f)
   {
    Temp=P3;
    Temp=Temp&0x0f;
   }
  }
 }
 
    P3=0xef;
 Temp=P3;
 Temp&=0x0f;
 if(Temp!=0x0f)
 {
    Delay1ms();
  Temp=P3;
 Temp&=0x0f;
  if(Temp!=0x0f)
  {
   Temp=P3;
     switch(Temp)
    {
      case 0xee:dat=12;break;
       case 0xed:dat=13;break;
       case 0xeb:dat=14;break;
       case 0xe7:dat=15;break;
     
    }
      while(Temp!=0x0f)
   {
    Temp=P3;
    Temp=Temp&0x0f;
   }
  }
 }
 if(dat==0&&qidong==0)
 {
    qidong=1;
 }
 
else if(dat==0&&qidong==1)
 {
    qidong=0;
 }
 if(dat==1)
 {
  
//  while(1)
//   {
//   P2=0x80;
//    P0=0x00;
//   }
  cishu+=1;
  P33=0;
   EX1=1;
 
   
 }
 if(cishu==4)
 {
    cishu=1;
 }
 if(dat==2&&cishu==1)
 {
    moshi+=1;
 }
 if(dat==3&&cishu==1)
 {
    moshi-=1;
 }
  if(dat==2&&cishu==2)
 {
   dingshishijian+=1;
  //write(0x05,dingshishijian);
 }
 if(dat==3&&cishu==2)
 {
     dingshishijian-=1;
  //write(0x05,dingshishijian);
 }
 
 
}
 
void gongzuoyi()
{
 
 u8 i,dat;
 dat=0x01;
 for(i=0;i<8;i++)
 {
 
  liang(~(dat<  
 }
}
void gongzuoer()
{
 u8 i,dat;
 dat=0x80;
 for(i=0;i<8;i++)
 {
 
  liang(~(dat>>i));
  
 }
}
void gongzuosan()
{
 u8 i,dat=0x81;
 for(i=0;i<4;i++)
 {
 
  liang(~((dat<>i)));
  
 }
}
void gongzuosi()
{
 u8 i,dat0,dat1;
 dat0=0x10;
 dat1=0x08;
 
 for(i=0;i<5;i++)
 {
  liang(~((dat0>>i)|(dat1< }
}
void write(u8 add,u8 dat)
{
IIC_Start();
 IIC_SendByte(0xa0);
 IIC_WaitAck();
  IIC_SendByte(add);
 IIC_WaitAck();
  IIC_SendByte(dat);
 IIC_WaitAck();
 IIC_Stop();
}
u8 read(u8 add)
{
 u8 dat;
   IIC_Start();
 IIC_SendByte(0xa0);
 IIC_WaitAck();
  IIC_SendByte(add);
 IIC_WaitAck();
 IIC_Stop();
   IIC_Start();
  IIC_SendByte(0xa1);
IIC_WaitAck();
 dat=IIC_RecByte();
  IIC_WaitAck();
 IIC_Stop();
 return dat;
}
 
 

void main()
{
//write(0x05,5);
 dingshishijian=read(0x05)%10;
 //write(0xff,dingshishijian+1);
 //moshi=read(0x05)%10; 
 bixu();
   allinit();
 while(1)
 {
 
 
  write(0x05,dingshishijian);
  keyboard();
  switch(moshi)
  {
   case 1: gongzuoyi();break;
    case 2: gongzuoer();break;
  case 3: gongzuosan();break;
  case 4: gongzuosi();break;
 }
  
}
 }
void waibu()interrupt 0
{
  while(1)
 {
  keyboard();
   if(qidong==0)
  {
     break;
  }
 }
}
void waibu1()interrupt 2
{
   while(1)
  {  
   display();       
     keyboard();
   if(cishu==3)
   {
    shumaguanguan();
      break;
   }
  }
  TR0=0;
}
 
 

你可能感兴趣的:(第九届蓝桥杯单片机类代码)