十四届蓝桥杯单片机模拟赛第二套题 自撸程序

#include 
#include 
#include 
#include 
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C1=P4^4;
sbit C2=P4^2;
sbit C3=P3^5;
sbit C4=P3^4;
unsigned char F_SMG=1; //数码管标志位 1温度界面 2电压读取界面
unsigned char command; //获取下位机命令
unsigned int SMG_vlot;  //数码管电压显示参数
unsigned int SMG_T;     //数码管温度显示参数
unsigned int adc_value; //电压处理参数
unsigned char count_t;  //T0 定时50ms 中断计数
float adc_vlot;					//电压实际值
float b20_temp;					//温度实际值
unsigned char code Nodat[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code Isdat[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};


//=============================控制函数====================================
void SelctHC573(unsigned char n)										//P2控制位HC573
{
	switch(n)
	{
		case 0: P2=(P2 & 0x1f) |0x00; break; 
		case 4: P2=(P2 & 0x1f) |0x80; break; 
		case 5: P2=(P2 & 0x1f) |0xa0; break; 
		case 6: P2=(P2 & 0x1f) |0xc0; break; 
		case 7: P2=(P2 & 0x1f) |0xe0; break; 
	}
}
//========= P0输出控制位
void Output_P0(unsigned char pos,unsigned char value)  
{
	SelctHC573(pos);
	P0=value;				        //控制完成后,取消位选,P0赋值为0x00
	SelctHC573(0);   																
	P0=0x00;
}
//======== 数码管专用延时 1-3ms
void Delay2ms()		
{
	unsigned char i, j;
	i= 24;
	j = 85;
	do
	{
		while (--j);
	} while (--i);
}
//======== 单个数码控制
void SMGBit(unsigned char pos,unsigned char value)  
{
 
	Output_P0(6,0x01<>4;
		medium=medium*10;
		medium=(medium+(LSB&0x0f)*0.625);
	}
	return medium;  //返回值  为实际温度的10倍
}

//======== 电压读取
unsigned char Read_AD()
{
	 unsigned char medium;
	 IIC_Start();
	 IIC_SendByte(0x90); 
	 IIC_WaitAck(); 
    
	 IIC_SendByte(0x03);	
	 IIC_WaitAck(); 
	 IIC_Stop(); 
	
	 DisplaySMG(); //****几毫秒足够*******
	
	 IIC_Start();
	 IIC_SendByte(0x91); 
	 IIC_WaitAck(); 

	 medium=IIC_RecByte(); 
	 IIC_SendAck(1);
	 IIC_Stop();
	 return medium;   //返回值  为未处理的AD值
}
//=============================温度电压数据处理====================================
	void Date_mange()
{
	 char i;
	 SMG_T=Read_temp();
	
	
	 adc_value=0;
	 for(i=0;i<3;i++)
	{
	adc_value +=Read_AD();  
	}
	 adc_value=adc_value/3;					//三次采样取平均的电压值
	 adc_vlot=adc_value*(5.0/255);  //实际电压值
	 SMG_vlot=adc_vlot*100;         //数码管电压值
	
//======== 温度电压判断	
	if(SMG_T>=240) 					
	{
		Output_P0(5,(P0&0x40)|0x10);  // 温度电压判断
		DisplaySMG();
	}
	if(adc_vlot>=3.6)
	{
		Output_P0(5,(P0&0x10)|0x40);
	}
  else{Output_P0(5,0x00);}
}
//=============================键盘扫描====================================
void Scankey()
{
	  R4 = 0;
    R2 = R3 = R1 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0) //S4
{
		   Delay1ms(1);
		if(C1 == 0)
			 {
			 ES = 0; //关闭串行中断
		   }
		while(C1==0)
	  {
		 DisplaySMG();
		}
}

	 else if(C3 == 0) //S12
{
			Delay1ms(1);
	 if(C3 == 0)
		{
		 if(F_SMG==1)					 //判断当前界面是
		  {
		   b20_temp=SMG_T*0.1; //实际温度
			 printf("TEMP: %.1f℃ \r\n",b20_temp);
		   }
		 if(F_SMG==2)
			 {
				printf("Vlotage:%.2fV\r\n",adc_vlot);
			 }
		}
	 while(C3==0)
	{
		DisplaySMG();
	}
}
	  R3 = 0;
    R4 = R2 = R1 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0) //S4
    {
		   Delay1ms(1);
		if(C1 == 0)
			 {
			 ES = 1;  //打开串行中断
		   }
		while(C1==0)
	  {
		 DisplaySMG();
		}
}
}
//=============================主函数====================================

void main()
{
 Initsystem();
 Timer0Init();
 IintUart();
	while(1)
	{
		 Command_mange ();
		 Scankey();
		 Date_mange();
     DisplaySMG();
	}
}

十四届蓝桥杯单片机模拟赛第二套题 自撸程序_第1张图片

你可能感兴趣的:(蓝桥杯单片机,蓝桥杯,单片机,嵌入式硬件)