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

1302,18B20驱动自己添加吧,这里不再单独搞出来。
 
#include<1302.h>
#include<i2c.h>
u16 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
     0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0X40};
u8 yi,er,san,si,wu,liu,qi,ba;
u8 moshi;
u8 fengmingqitiao;
u8 open,close;
u8 shidudingzhi;
u8 shidudingzhitiaozheng=50,shidudingzhitiaozheng1;
u8 i2c;
u16 shidu;
void Delay1ms()  //@11.0592MHz
{
 unsigned char i, j;
 _nop_();
 _nop_();
 _nop_();
 i = 11;
 j = 190;
 do
 {
  while (--j);
 } while (--i);
}
void allinit()
{
  P2=0x80;
 P0=0xff;
 P2=0xa0;
 P0=0x00;
}
void display()
{
  P2=0xc0;
 P0=0x01;
 P2=0xff;
 P0=~duan[yi];
 Delay1ms();
 
   P2=0xc0;
 P0=0x02;
 P2=0xff;
 P0=~duan[er];
 Delay1ms();
 
   P2=0xc0;
 P0=0x04;
 P2=0xff;
 P0=~duan[san];
 Delay1ms();
 
   P2=0xc0;
 P0=0x08;
 P2=0xff;
 P0=~duan[si];
 Delay1ms();
 
   P2=0xc0;
 P0=0x10;
 P2=0xff;
 P0=~duan[wu];
 Delay1ms();
 
   P2=0xc0;
 P0=0x20;
 P2=0xff;
 P0=~duan[liu];
 Delay1ms();
 
   P2=0xc0;
 P0=0x40;
 P2=0xff;
 P0=~duan[qi];
 Delay1ms();
 
   P2=0xc0;
 P0=0x80;
 P2=0xff;
 P0=~duan[ba];
 Delay1ms();
 if(moshi==1)
 {
  P2=0x80;
 
 P0=0xfd;
 Delay1ms();
 }
 else if(moshi==0)
 {
  P2=0x80;
 
 P0=0xfe;
 Delay1ms();
 }
}
void juzhen()//垃圾矩阵
{
 u8 dat,temp;
   P3=0x7f;
 P4=0xef;
 temp=P3&0x0f;
 if(temp!=0x0f)
 {
 Delay1ms();
   P3=0x7f;
 P4=0xef;
 temp=P3&0x0f;
  if(temp!=0x0f)
  {
    temp=P3;
   switch(temp)
   {
    case 0x7e:dat=1;break;
    case 0x7d:dat=2;break;
    case 0x7b:dat=3;break;
    case 0x77:dat=4;break;
   }
    if(dat==1)
 {
   moshi+=1;
 }
 if(moshi==2)
 {
   moshi=0;
 }
    while(temp!=0x0f)
    {
   P3=0x7f;
 P4=0xef;
 temp=P3&0x0f;
    }
  }
 }
 if(moshi==1&&dat==2)
 {
   fengmingqitiao+=1;
  
 }
 if(fengmingqitiao==2)
 {
   fengmingqitiao=0;
 }
 if(moshi==1&&dat==3)
 {
   open=1;
 
  close=0;
 }
 if(moshi==1&&dat==4)
 {
    open=0;
  close=1;
 
 }
 if(shidudingzhitiaozheng1==1)
 {
   if(dat==3)
  {
    shidudingzhitiaozheng+=1;
  }
 }
  if(shidudingzhitiaozheng1==1)
 {
   if(dat==4)
  {
    shidudingzhitiaozheng-=1;
  }
 }
if(moshi==0&&dat==2)
{
  shidudingzhitiaozheng1+=1;
 i2c=0;
}
else if(shidudingzhitiaozheng1==2)
{
   shidudingzhitiaozheng1=0;
 i2c=1;
}
}
void shizhongxianshi()
{
  si=shijian[1]/16;
 wu=shijian[1]%16;
 san=17;
 yi=shijian[2]/16;
 er=shijian[2]%16;
  liu=16;qi=shidu/10;ba=shidu%10;
 
}
void i2cwr(u8 add,u8 dat)
{
IIC_Start();
 IIC_SendByte(0xa0);
 IIC_WaitAck();
 IIC_SendByte(add);
IIC_WaitAck();
 IIC_SendByte(dat);
 IIC_WaitAck();
 IIC_Stop();
}
u8 i2cre(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 DACwr(u8 add,u8 dat)//DAC
{
IIC_Start();
 IIC_SendByte(0x90);
 IIC_WaitAck();
 IIC_SendByte(add);
IIC_WaitAck();
 IIC_SendByte(dat);
 IIC_WaitAck();
 IIC_Stop();
}
u8 DACre()//DAC
{
 unsigned char temp;
 
 IIC_Start();
 IIC_SendByte(0X90);
 IIC_WaitAck();
 IIC_SendByte(0x03);
 IIC_WaitAck();
 IIC_Stop();
 IIC_Start();
 IIC_SendByte(0X91);
 IIC_WaitAck();
 temp=IIC_RecByte1();
 IIC_Stop();
 
 return temp;
}
void quanmie()
{
  yi=16;er=16;san=16;si=16;wu=16;liu=16;qi=16;ba=16;
}
void shiduxianshi()
{
  yi=17;er=17;san=16;si=16;wu=16;liu=16;qi=shidudingzhitiaozheng/10;ba=shidudingzhitiaozheng%10;
}
void main()
{
// u8 dat;
quanmie();
   allinit();
 
 //shidudingzhitiaozheng=i2cre(0x00);
 while(1)
 {
  
  shidu=DACre()*100/256;
  juzhen();
//  dat=DACre(0x00);
//  
//  yi=dat/1000;
//  er=dat%1000/100;
//  san=dat%100/10;
//  si=dat%10;
//  
//  re();
   shizhongxianshi();
if(moshi==0)
{
   if(shidu<shidudingzhitiaozheng)
  {
       P2=0xa0;
 P0=0xf0;
 P2=0x00;
  }
 if (shidu>=shidudingzhitiaozheng)
  {
       P2=0xa0;
 P0=0x00;
 P2=0x00;
  }
  if(shidudingzhitiaozheng1==1)
  {
   shiduxianshi();
  }
}
else if(moshi==1)
{
  if(open==1)
 {
  
   P2=0xa0;
 P0&=0x10;
 P2=0x00;
 }
if(close==1)
 {
 P2=0xa0;
 P0&=0xe0;
 P2=0x00;
 }
 if(shidu<shidudingzhitiaozheng&&fengmingqitiao==0)
{
   P2=0xa0;
 P0|=0xe0;
 P2=0x00;
}
else if(fengmingqitiao==1)
{
   P2=0xa0;
 P0&=0x10;
 P2=0x00;
}
else if(shidu>shidudingzhitiaozheng&&open==0)
{
P2=0xa0;
 P0=0x00;
 P2=0x00;
}
else if(shidu>shidudingzhitiaozheng&&open==1)
{
  P2=0xa0;
 P0=0x10;
 P2=0x00;
}
if(i2c==2)
{
 i2cwr(0x00,shidudingzhitiaozheng);
}
}
  display();
 }
}

猜你喜欢

转载自www.cnblogs.com/a1113775906/p/12098787.html