蓝桥杯单片机省赛题目《全集》之第十届省赛

蓝桥杯从第十届开始程序题就很简单了,直接就很简单

注意好每届的一点点小问题就可以了

加油

 

#include"reg52.h"
#include"iic.h"

unsigned char code SmgNodot[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code Smgdot[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;

void SelectHC573(unsigned char channel)
{
  switch(channel)
  {
    case 4:
		   P2 = (0x1f & P2) | 0x80;
	break;

	case 5:
		   P2 = (0x1f & P2) | 0xa0;
	break;

	case 6:
		   P2 = (0x1f & P2) | 0xc0;
	break;

	case 7:
		   P2 = (0x1f & P2) | 0xe0;
	break;

	case 0:
		   P2 = (0x1f & P2) | 0x00;
	break;
  }
}

void InitSystem()
{
  SelectHC573(4);
  P0 = 0xff;
  SelectHC573(5);
  P0 = 0x00;
  SelectHC573(6);
  P0 = 0x00;
  SelectHC573(0);
}

void InitTimer()
{
  TMOD = 0x16;
  TH0 = 255;
  TL0 = 255;
  TH1 = (2^16 - 50000) / 256;
  TL1 = (2^16 - 50000) % 256;
  TR1 = 1;
  TR0 = 1;
  EA = 1;
  ET1 = 1;
  ET0 = 1;
}

unsigned int U = 0;
unsigned char dat_v = 0;

void Read_RB2()
{
  IIC_Start();
  IIC_SendByte(0x90);
  IIC_WaitAck();
  IIC_SendByte(0x03);
  IIC_WaitAck();
  IIC_Stop();

  IIC_Start();
  IIC_SendByte(0x91);
  IIC_WaitAck();
  dat_v = IIC_RecByte();
  IIC_SendAck(1);
  IIC_Stop();

  U = dat_v * 250 / 255 * 2;
  //U = dat_v * 2;
}

void SetData_DAC(unsigned char dat)
{
  IIC_Start();
  IIC_SendByte(0x90);
  IIC_WaitAck();
  IIC_SendByte(0x40);
  IIC_WaitAck();
  IIC_SendByte(dat);
  IIC_SendAck(1);
  IIC_Stop();
}

unsigned int count = 0;
unsigned int count_f = 0;
unsigned int Fre =0;

void Timer0() interrupt 1
{
  count_f++;
}

void Timer1() interrupt 3
{
  TH1 = (2^16 - 50000) / 256;
  TL1 = (2^16 - 50000) % 256;
  
  count++;
  if(count % 10 == 0)
  {
    Read_RB2();
  }
  if(count == 20)
  {
    count = 0 ;
	Fre = count_f;
	count_f = 0;
  }
}

bit stat_s4 = 0;
bit stat_s5 = 0;
bit stat_s6 = 0;
bit stat_s7 = 0;

void DisplayL1L2()
{
  SelectHC573(0);
  if(stat_s4 == 0)
  {
	P0 = (P0 | 0x02) & 0xfe;
  } 
  else if(stat_s4 == 1)
  {
	P0 = (P0 | 0x01) & 0xfd;;
  }
}

void DisplayL3()
{
  SelectHC573(0);
  if(U < 150)
  {
    P0 = P0 | 0x04;
  }
  else if(U < 250)
  {
    P0 = P0 & 0xfb;
  }
  else if(U < 350)
  {
    P0 = P0 | 0x04;
  }
  else
  {
    P0 = P0 & 0xfb;
  }
}

void DisplayL4()
{
  SelectHC573(0);
  if(Fre < 1000)
  {
    P0 = P0 | 0x08;
  }
  else if(Fre < 5000)
  {
    P0 = P0 & 0xf7;
  }
  else if(Fre < 10000)
  {
    P0 = P0 | 0x08;
  }
  else
  {
    P0 = P0 & 0xf7;
  }
}

void DisplayL5()
{
  SelectHC573(0);
  if(stat_s5 == 0)
  {
    P0 = P0 | 0x10;
  }
  else if(stat_s5 == 1)
  {
    P0 = P0 & 0xef;
  }
}
void DisplayLed()
{
  if(stat_s6 == 0)
  {
	DisplayL1L2();
    DisplayL3();
    DisplayL4();
	DisplayL5();
	SelectHC573(4); 
  }
  else if(stat_s6 == 1)
  {
    SelectHC573(4);
	P0 = 0xff;
  }
}

void Delay(unsigned int t)
{
  while(t--);
}

void DisplaySmg_Bit(unsigned char pos, unsigned char dat)
{
  P0 = 0xff;
  SelectHC573(6);
  P0 = 0x01 << pos;
  SelectHC573(7);
  P0 = dat;
}

void DisplayFre()
{
  DisplaySmg_Bit(0,0x8e);
  Delay(500);

  if(Fre > 99999)
  {
    DisplaySmg_Bit(2,SmgNodot[Fre / 100000]);
    Delay(500);
  }

  if(Fre > 9999)
  {
    DisplaySmg_Bit(3,SmgNodot[(Fre / 10000) % 10]);
    Delay(500);
  }

  if(Fre > 999)
  {
    DisplaySmg_Bit(4,SmgNodot[(Fre / 1000) % 10]);
    Delay(500);
  }

  if(Fre > 99)
  {
    DisplaySmg_Bit(5,SmgNodot[(Fre / 100) % 10]);
    Delay(500);
  }

  if(Fre > 9)
  {
    DisplaySmg_Bit(6,SmgNodot[(Fre / 10) % 10]);
    Delay(500);
  }

  DisplaySmg_Bit(7,SmgNodot[Fre % 10]);
  Delay(500);
  DisplaySmg_Bit(7,0xff);
}


void DisplayU()
{
  DisplaySmg_Bit(0,0xc1);
  Delay(500);

  DisplaySmg_Bit(5,Smgdot[U / 100]);
  Delay(500);
  DisplaySmg_Bit(6,SmgNodot[(U / 10) % 10]);
  Delay(500);
  DisplaySmg_Bit(7,SmgNodot[U % 10]);
  Delay(500);
  DisplaySmg_Bit(7,0xff);
}

void Display()
{
  if(stat_s7 == 0)
  {
    if(stat_s4 == 0)
	{
	  DisplayU();
	}
	else
	  DisplayFre();
  }
  else
  {
    SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = 0xff;
    SelectHC573(0);
  }
  DisplayLed();
  Delay(1000);//提高LED指示灯亮度
}

void ScanKey()
{
  if(S4 == 0)
  {
    Delay(100);
	if(S4 == 0)
	{
	 stat_s4 = ~stat_s4;
	 while(S4 == 0)
	 {
	   Display();
	 }
	}
  }

  if(S5 == 0)
  {
    Delay(100);
	if(S5 == 0)
	{
	  stat_s5 = ~stat_s5;
	  while(S5 == 0)
	  {
	    Display();
	  }
	}
  }

  if(S6 == 0)
  {
    Delay(100);
	if(S6 == 0)
	{
	  stat_s6 = ~stat_s6;
	  while(S6 == 0)
	  {
	    Display();
	  }
	}
  }

  if(S7 == 0)
  {
    Delay(100);
	if(S7 == 0)
	{
	  stat_s7 = ~stat_s7;
	  while(S7 == 0)
	  {
	    Display();
	  }
	}
  }
}

void main()
{
  InitSystem();
  InitTimer();
  while(1)
  {
	ScanKey();
	if(stat_s5 == 0)
	{
	  SetData_DAC(102);
	}
	else
	{
	  SetData_DAC(U);
	}
	Display();
  }
}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值