【蓝桥杯单片机】第十五届模拟题II满分实现

#include "stc15f2k60s2.h"
#include "iic.h"
#include "onewire.h"

code unsigned char SegNoDot_Table[] = {
0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 
0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e };
code unsigned char SegDot_Table[] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

sbit C1 = P4^4;
sbit R3 = P3^2;
sbit R4 = P3^3;

unsigned char smg_mode = 1;
unsigned char mode = 1;
unsigned int param = 0;
float temper = 0;
unsigned int smg_temper = 0;
unsigned char stat_led = 0xff;
unsigned char light_init = 0;
float Volt_out = 0.0;
unsigned char smg_volt = 0;
unsigned char Volt = 0;

void HC573_Choose(unsigned char channel, unsigned char value)
{
	P2 = (P2 & 0x1f) | 0x00;
	P0 = value;
	switch(channel)
	{
		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;
	}
	P2 = (P2 & 0x1f) | 0x00;
	
}

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

void DisplaySMG_bit(unsigned char pos, unsigned char dat)
{
	HC573_Choose(6,0x01 << pos);
	HC573_Choose(7,dat);
	DelaySMG(500);
	HC573_Choose(6,0x01 << pos);
	HC573_Choose(7,0xff);
}

void DisplaySMG_All(unsigned char value)
{
	HC573_Choose(6,0xff);
	HC573_Choose(7,value);
}

void DisplaySMG_Function()
{
	switch(smg_mode)
	{
		case 1:
			if(mode == 1)
			{
				DisplaySMG_bit(7,SegNoDot_Table[smg_temper % 10]);
				DisplaySMG_bit(6,SegDot_Table[smg_temper / 10 % 10]);
				DisplaySMG_bit(5,SegNoDot_Table[smg_temper / 100]);
				DisplaySMG_bit(0,SegNoDot_Table[1]);
			}
			else if(mode == 2)
			{
				DisplaySMG_bit(7,SegNoDot_Table[light_init % 10]);
				if(light_init >= 10)
				{
					DisplaySMG_bit(6,SegNoDot_Table[light_init / 10 % 10]);
				}
				if(light_init >= 100)
				{
					DisplaySMG_bit(5,SegNoDot_Table[light_init / 100]);
				}
				DisplaySMG_bit(0,SegNoDot_Table[2]);
			}
			break;
		case 2:
			DisplaySMG_bit(7,SegNoDot_Table[smg_volt % 10]);
			DisplaySMG_bit(6,SegDot_Table[smg_volt / 10]);
			DisplaySMG_bit(0,0xc1);
			break;
	}
}

void DS18B20_Init()
{
	unsigned char LSB,MSB;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	do
	{
		init_ds18b20();
		Write_DS18B20(0xcc);
		Write_DS18B20(0xbe);
		LSB = Read_DS18B20();
		MSB = Read_DS18B20();
		MSB = (MSB << 4) | (LSB >> 4);
	}while(MSB == 85);
}

void DS18B20_Function()
{
	unsigned char LSB,MSB;
	unsigned int temp = 0;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB = Read_DS18B20();
	MSB = Read_DS18B20();
	temp = MSB;
	temp = (temp << 8) | LSB;
	if((temp & 0xf800) == 0x0000)
	{
		temper = temp * 0.0625;
	}
	smg_temper = temper * 10;
}

unsigned char PCF8591_Read()
{
	unsigned char temp;
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x41);
	I2CWaitAck();
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	temp = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return temp;
}

void PCF8591_Write(unsigned char value)
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x41);
	I2CWaitAck();
	I2CSendByte(value);
	I2CWaitAck();
	I2CStop();
}

void Output_Function()
{
	light_init = PCF8591_Read();
	DS18B20_Function();
	if(mode == 1)
	{
		if(temper < 10)
		{
			Volt_out = 1;
		}
		else if(temper > 40)
		{
			Volt_out = 5;
		}
		else if((temper >= 10) && (temper <= 40))
		{
			Volt_out = (temper * 2.0 / 15.0) - (1.0 / 3.0);
		}
	}
	else if(mode == 2)
	{
		if(light_init < 10)
		{
			Volt_out = 1;
		}
		else if(light_init > 240)
		{
			Volt_out = 5;
		}
		else if((light_init >= 10) && (light_init <= 240))
		{
			Volt_out = (light_init * 2.0 / 115.0) + (19.0 / 23.0);
		}
	}
	Volt = Volt_out * 51;
	smg_volt = Volt_out * 10;
	PCF8591_Write(Volt);
}

void LED_Function()
{
	if(mode == 1)
	{
		stat_led = (stat_led & 0xfc) | 0x02;
	}
	else if(mode == 2)
	{
		stat_led = (stat_led & 0xfc) | 0x01;
	}
	HC573_Choose(4,stat_led);
}

void ScanKeys()
{
	R3 = 1;R4 = 0;
	//S4
	if(C1 == 0)
	{
		DelaySMG(500);
		if(C1 == 0)
		{
			if(smg_mode == 1)
			{
				if(mode == 1)
				{
					mode = 2;
				}
				else
				{
					mode = 1;
				}
			}
			while(C1 == 0)
			{
				DisplaySMG_Function();
			}
		}
	}
	R3 = 0;R4 = 1;
	C1 = 1;
	//S5
	if(C1 == 0)
	{
		DelaySMG(500);
		if(C1 == 0)
		{
			if(smg_mode == 1)
			{
				smg_mode = 2;
			}
			else if(smg_mode == 2)
			{
				smg_mode = 1;
			}
			while(C1 == 0)
			{
				DisplaySMG_Function();
			}
		}
	}
}

void Init_sys()
{
	HC573_Choose(4,0xff);
	HC573_Choose(5,0x00);
	DisplaySMG_All(0xff);
	DS18B20_Init();
}

void main()
{
	Init_sys();
	while(1)
	{
		Output_Function();
		LED_Function();
		ScanKeys();
		DisplaySMG_Function();
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值