蓝桥杯之单片机学习(二十一)——自动售水机(附题目和完整代码)

一、题目要求

在这里插入图片描述

二、代码展示

#include <STC15F2K60S2.h>
#include "iic.h"

#define uchar unsigned char
#define uint unsigned int
	
uchar code SMG_duanma[18] = 
	{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
	 0x88,0x80,0xc6,0xc0,0x86,0x8e,
	 0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

//分别是“0.-9.”
uchar code SMG_Dot_AC[10] = 
	 {0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};

uint time_001s = 0;
uchar guang = 0;
	 
//系统初始化
void Initsys();
//配置HC138
void SelectHC138(uchar channel);
//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value);
//数码管8位码显示
void SMG_Display();
//数码管的延时
void Delay_one_ms_SMG();
//独立按键(BTN)
void Alone_Key();
//按键消抖延时
void Delay_five_ms_Key();
//矩阵键盘(KBD)
//void Matrix_Key();
//定时器初始化
void InitTimer0();
//售水机出水
void Mod_1_Strat();
//售水机停水
void Mod_2_Stop();
//PCF8591
uchar IIC_read(uchar add);
//光感处理
void Light_Check();


void main()
{
	Initsys();
	InitTimer0();
	while(1)
	{
		Light_Check();
		//Matrix_Key();
		Alone_Key();
		//SMG_Display();
	}
}

//光感处理
void Light_Check()
{
	guang = IIC_read(0X01);
	if(guang < 64)
	{
		SelectHC138(4);
		P0 = 0XFE;
	}
	else 
	{
		SelectHC138(4);
		P0 = 0XFF;
	}
}

//售水机出水
void Mod_1_Strat()
{
	SelectHC138(5);
	P0 = 0X10;
	while(1)
	{
		Light_Check();
		SMG_Display();
		Alone_Key();
		if(time_001s == 99999)
		{
			Mod_2_Stop();
			break;
		}
	}
}
//售水机停水
void Mod_2_Stop()
{
	EA = 0;
	SelectHC138(5);
	P0 = 0X00;
	time_001s = time_001s / 2;
	while(1)
	{
		Light_Check();
		SMG_Display();
	}
}

//定时器初始化
void InitTimer0()
{
	TMOD = 0x01;
	TH0 = (65535 - 10000) / 256;
	TL0 = (65535 - 10000) % 256;
	
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}

//定时器服务
void ServiceTimer0() interrupt 1
{
	TH0 = (65535 - 10000) / 256;
	TL0 = (65535 - 10000) % 256;
	
	time_001s++;
}


//按键消抖延时
void Delay_five_ms_Key()
{
	uint i,j;
	for(i = 0; i < 5; i++)
		for(j = 845; j > 0; j--);
}

//独立按键(BTN)
void Alone_Key()
{
	//S7按键
	if(P30 == 0)
	{
		Delay_five_ms_Key();
		if(P30 == 0)
		{
			Mod_1_Strat();
		}
		while(!P30)
		{
			Light_Check();
			SMG_Display();
		}
	}
	//S6按键
	else if(P31 == 0)
	{
		Delay_five_ms_Key();
		if(P31 == 0)
		{
			Light_Check();
			Mod_2_Stop();
		}
		while(!P31)
		{
			SMG_Display();
		}
	}
}

//配置HC138
void SelectHC138(uchar channel)
{
	switch(channel)
	{
		case 4:    //LED
			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 Initsys()
{
	SelectHC138(5);
	P0 = 0X00;//关闭蜂鸣器和继电器
	SelectHC138(4);
	P0 = 0XFF;//关闭LED
	
	SelectHC138(6);
	P0 = 0XFF; //选择所有数码管
	SelectHC138(7);
	P0 = 0XFF; //关闭所有数码管
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
	SelectHC138(6);
	P0 = 0X01 << pos;
	SelectHC138(7);
	P0 = value;
}

//数码管8位码显示
void SMG_Display()
{
	DisplaySMG_Bit(1, SMG_Dot_AC[0]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(2, SMG_duanma[5]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(3, SMG_duanma[0]);
	Delay_one_ms_SMG();
	
	DisplaySMG_Bit(4, SMG_duanma[time_001s / 10000]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(5, SMG_Dot_AC[time_001s % 10000 / 1000]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(6, SMG_duanma[time_001s % 1000 / 100]);
	Delay_one_ms_SMG();
	DisplaySMG_Bit(7, SMG_duanma[time_001s % 100 / 10]);
	Delay_one_ms_SMG();
}

//数码管的延时
void Delay_one_ms_SMG()
{
	uint j;
	for(j = 845; j > 0; j--);
}

//PCF8591
uchar IIC_read(uchar add)
{
	uchar temp;
	IIC_Start();
	IIC_SendByte(0X90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0X91);
	IIC_WaitAck();
	temp = IIC_RecByte();
	IIC_Stop();
	return temp;
}
/*
//矩阵键盘(KBD)
void Matrix_Key()
{
	uchar temp;
	//第一列
	P3 = 0X7F;P44 = 0; P42 = 1;
	temp = P3;
	temp = temp & 0X0F;
	if(temp != 0X0F)
	{
		Delay_five_ms_Key();
		temp = P3;
		temp = temp & 0X0F;
		if(temp != 0X0F)
		{
			temp = P3;
			switch(temp)
			{
				case 0X7E:
					DisplaySMG_Bit(0, SMG_duanma[1]);
				break;
				case 0X7D:
					DisplaySMG_Bit(1, SMG_duanma[1]);
				break;
				case 0X7B:
					DisplaySMG_Bit(2, SMG_duanma[1]);
				break;
				case 0X77:
					DisplaySMG_Bit(3, SMG_duanma[1]);
				break;
			}
			while(temp != 0X0F)
			{
				temp = P3;
				temp = temp & 0X0F;
			}
		}
	}
	//第二列
	P3 = 0XBF;P44 = 1; P42 = 0;
	temp = P3;
	temp = temp & 0X0F;
	if(temp != 0X0F)
	{
		Delay_five_ms_Key();
		temp = P3;
		temp = temp & 0X0F;
		if(temp != 0X0F)
		{
			temp = P3;
			switch(temp)
			{
				case 0XBE:
					DisplaySMG_Bit(0, SMG_duanma[2]);
				break;
				case 0XBD:
					DisplaySMG_Bit(1, SMG_duanma[2]);
				break;
				case 0XBB:
					DisplaySMG_Bit(2, SMG_duanma[2]);
				break;
				case 0XB7:
					DisplaySMG_Bit(3, SMG_duanma[2]);
				break;
			}
			while(temp != 0X0F)
			{
				temp = P3;
				temp = temp & 0X0F;
			}
		}
	}
	//第三列
	P3 = 0XDF;P44 = 1; P42 = 1;
	temp = P3;
	temp = temp & 0X0F;
	if(temp != 0X0F)
	{
		Delay_five_ms_Key();
		temp = P3;
		temp = temp & 0X0F;
		if(temp != 0X0F)
		{
			temp = P3;
			switch(temp)
			{
				case 0XDE:
					DisplaySMG_Bit(0, SMG_duanma[3]);
				break;
				case 0XDD:
					DisplaySMG_Bit(1, SMG_duanma[3]);
				break;
				case 0XDB:
					DisplaySMG_Bit(2, SMG_duanma[3]);
				break;
				case 0XD7:
					DisplaySMG_Bit(3, SMG_duanma[3]);
				break;
			}
			while(temp != 0X0F)
			{
				temp = P3;
				temp = temp & 0X0F;
			}
		}
	}
	//第四列
	P3 = 0XEF;P44 = 1; P42 = 1;
	temp = P3;
	temp = temp & 0X0F;
	if(temp != 0X0F)
	{
		Delay_five_ms_Key();
		temp = P3;
		temp = temp & 0X0F;
		if(temp != 0X0F)
		{
			temp = P3;
			switch(temp)
			{
				case 0XEE:
					DisplaySMG_Bit(0, SMG_duanma[4]);
				break;
				case 0XED:
					DisplaySMG_Bit(1, SMG_duanma[4]);
				break;
				case 0XEB:
					DisplaySMG_Bit(2, SMG_duanma[4]);
				break;
				case 0XE7:
					DisplaySMG_Bit(3, SMG_duanma[4]);
				break;
			}
			while(temp != 0X0F)
			{
				temp = P3;
				temp = temp & 0X0F;
			}
		}
	}
}

*/

三、注意事项

  1. define 换行时,要加\
  2. 使用我写的模板DisplaySMG_Bit第二个输入参数,记得要用开头定义的数组
  3. iic,c文件中,要8-12倍延时
  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周末不下雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值