蓝桥杯单片机组第十一届省赛——电压测量

实现代码:

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

sfr P4=0xc0;
sbit H1=P3^0;
sbit H2=P3^1;
sbit H3=P3^2;
sbit H4=P3^3;
sbit L1=P3^4;
sbit L2=P3^5;
sbit L3=P4^2;
sbit L4=P4^4;

sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^2;
sbit LED4=P0^3;

unsigned char Rb2=0,Vp=200,count=0;//Rb2上的电压值,电压参数,刷新计数
unsigned char interface=0;//模式切换
unsigned char invalid_count;//无效按键计数
unsigned char count_flag=0,LED_flag = 0,V_flag = 0;

unsigned char code SMG_duanma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char SMG_Dot[] = {0x40,0x79,0x34,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; 

void delay(unsigned char t)
{
	while(t--);
}

void HC138(unsigned char m)
{
	switch(m)
	{
		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;
		case 0:P2=(P2&0x1f)|0x00;break;
	}
}

void Init_system()
{
	HC138(4);
	P0=0xff;
	HC138(5);
	P0=0x00;
	HC138(0);
}

void SMG_display_bit(unsigned char pos,unsigned char dat)
{
	HC138(6);
	P0=(0x01<<pos);
	HC138(7);
	P0=dat;
	HC138(0);
}

void SMG_Close()
{
  	HC138(6);
	P0=0xff;
	HC138(7);
	P0=0xff;
	HC138(0);
}

void SMG_display_data()
{
	SMG_display_bit(0,0xc1);//U标识符
	delay(200);
	SMG_display_bit(1,0xff);
	delay(200);
	SMG_display_bit(2,0xff);
	delay(200);
	SMG_display_bit(3,0xff);
	delay(200);
	SMG_display_bit(4,0xff);
	delay(200);
	SMG_display_bit(5,SMG_Dot[Rb2/100]);
	delay(200);
	SMG_display_bit(6,SMG_duanma[Rb2/10%10]);
	delay(200);
	SMG_display_bit(7,SMG_duanma[Rb2%10]);
	delay(200);
	SMG_Close();
	delay(200);
}

void SMG_display_para()
{
	SMG_display_bit(0,0x8c);//U标识符
	delay(200);
	SMG_display_bit(1,0xff);
	delay(200);
	SMG_display_bit(2,0xff);
	delay(200);
	SMG_display_bit(3,0xff);
	delay(200);
	SMG_display_bit(4,0xff);
	delay(200);
	SMG_display_bit(5,SMG_Dot[Vp/100]);
	delay(200);
	SMG_display_bit(6,SMG_duanma[Vp/10%10]);
	delay(200);
	SMG_display_bit(7,SMG_duanma[Vp%10]);
	delay(200);	
	SMG_Close();
	delay(200);
}

void SMG_display_count()
{
	SMG_display_bit(0,0x9b);//U标识符
	delay(200);
	SMG_display_bit(1,0xff);
	delay(200);
	SMG_display_bit(2,0xff);
	delay(200);
	SMG_display_bit(3,0xff);
	delay(200);
	SMG_display_bit(4,0xff);
	delay(200);
	if(count>99)
	{
		SMG_display_bit(5,SMG_duanma[count/100]);
		delay(200);
	}
	else 
	{
		SMG_display_bit(5,0xff);
		delay(200);
	}

	if(count>9)
	{
		SMG_display_bit(5,SMG_duanma[count/10%10]);
		delay(200);
	}
	else 
	{
		SMG_display_bit(5,0xff);
		delay(200);
	}	
	SMG_display_bit(7,SMG_duanma[count%10]);
	delay(200);	
	SMG_Close();
	delay(200);
}

unsigned read_Rb2_data()
{
	unsigned char temp;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck(); 
	IIC_SendByte(0x43);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91); 
	IIC_WaitAck();
	temp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return temp;
}

unsigned write_AT24C02(unsigned char addr,unsigned char dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}  

unsigned read_AT24C02(unsigned char addr)
{
	unsigned char temp;	
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();

	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	temp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return temp;
}

void keyscan()
{
	H3=0;
	H1=H2=H4=1;
	L1=L2=L3=L4=1;
	if(L1==0)
	{
		delay(20);
		if(L1==0)
		{
			while(L1==0)
			{
				if(interface==1)
				{
					Vp=Vp-50;
					if(Vp<0)
					{
						Vp=500;
					}
					invalid_count=0;
				}
				else invalid_count++;
			}
		}
	}
	
	if(L2==0)
	{
		delay(20);
		if(L2==0)
		{
			while(L2==0)
			{
			 	count=0;
			}
		}
	}

	H4=0;
	H1=H2=H3=0;
	L1=L2=L3=L4=0;
	if(L1==0)
	{
		delay(200);
		if(L1==0)
		{
			while(L1==0)
			{
				if(interface==1)
				{
					Vp=Vp+50;
					if(Vp<500)
					{
						Vp=0;
					}
					invalid_count=0;
				}
				else
				{
					invalid_count++;
				}
			}
		}
	}
	if(L2==0)
	{
		delay(200);
		if(L2==0)
		{
			while(L2==0)
			{
				if(interface==0)
				{
					interface=1;
					SMG_display_data();
				}
				else if(interface==1)
				{
					write_AT24C02(0x00,Vp*10);
					delay(200);
					interface=2;
					SMG_display_para();
				}
				else if(interface==2)
				{
					interface=0;
					SMG_display_count();
				}
			}
		}
	}
}

void Init_Timer0()
{
	TMOD=0x01;
	TH0=(65535-50000)/256;
   	TL0=(65535-50000)%256;
	TR0=1;
	ET0=1;
	EA=1;
}

void serviceTimer0() interrupt 1
{
	TH0=(65535-50000)/256;
   	TL0=(65535-50000)%256;
	count++;
	if(count_flag>500)
	{
		count_flag=0;
		LED_flag=0;
		TR0=0;
	}
}	

void LED()
{
	if(Rb2<Vp)
	{
		TR0=1;
	}
	else
	{
		TR0=0;
		count_flag=0;
		LED_flag=0;
	}
	HC138(4);
	if(LED_flag==1)
	{
		LED2=1;
	}
	if(count%2==1)
	{
		LED2=0;
	}
	else
	{
		LED2=1;
	}
	if(invalid_count>= 3)
	{
	 	LED3 = 0;
	}
	else
	{
	 LED3 = 1;
	}
	HC138(0);
}

void main()
{
	Init_system();
	Init_Timer0();
	while(1)
	{
		Rb2 = read_Rb2_data();// 计算转换电压
		Rb2 = (Rb2/256.0*5)*100;
		switch(interface)
		{
		 	case 0:SMG_display_data();break;
			case 1:SMG_display_para();delay(2000);break;
			case 2:SMG_display_count();break;
		}
		if(Rb2 > Vp)
		{
		 	V_flag = 1;
		}
		else if(Rb2 < Vp)
		{
			if(V_flag == 1)
			{
			 	count++;
				V_flag = 0;
			}
		}
			keyscan();
		LED();
	}
}	

		

 但是我的unsigned write_AT24C02() 出现了上面图片里的错误,希望能有大佬指点一下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值