蓝桥杯单片机第九届省赛试题---彩灯控制器

该代码功能可正常实现,但是数码管显示不太满意。当数码管显示会影响灯的显示,灯会产生闪烁,如果大家有跟好的解决办法,可以留言。希望大家能集思广益一下。

#include "reg52.h"
#include "iic.h"
#include "intrins.h"
#define TSMG 300
sfr T2H = 0xd6;
sfr T2L = 0xd7;
sfr AUXR = 0x8e;
sfr IE2 = 0xaf;
sbit s4 = P3^3;
sbit s5 = P3^2;
sbit s6 = P3^1;
sbit s7 = P3^0;
void led_control();
void DisplaySMG_Info();
void SelectHC573(unsigned char channel,unsigned char dat);
void read_adcvalue();
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
    0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned char code mode_1[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char code mode_2[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char code mode_3[4] = {0x7e,0xbd,0xdb,0xe7};
unsigned char code mode_4[4] = {0xe7,0xdb,0xbd,0x7e};
unsigned char stat_dis = 0;//亮度等级界面显示标志
unsigned char adc_value = 0;//采集到的电压值0~255
unsigned char mode = 0;//灯工作模式
unsigned char run_mode = 0;//灯流转模式调整
unsigned int timer[4] = {400,400,400,400};//灯流转时间
unsigned char level = 1;//亮度等级
unsigned char pwm_duty = 0;//占空比
unsigned char stat_led = 0;//时间间隔标志
unsigned int count = 0;//流转时间计数
unsigned int count_08s = 0;//0.8秒间隔
unsigned char stat_shan = 0;//数码管闪烁标志
unsigned char pwm_num = 0;//占空比计数
unsigned char stop_go = 0;//0-停止 1-启动
unsigned char set_key = 0;//设置按键 0-数码管熄灭 1-模式编号 2-流转间隔
unsigned char addre[4] = {0x01,0x02,0x03,0x04};
void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 59;
	j = 90;
	do
	{
		while (--j);
	} while (--i);
}

void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}
void Init_timer2()//100us*4000 *12000 400ms
{
	AUXR &= 0xfb;
	T2L = 0x9c;
	T2H = 0xff;
	AUXR |= 0x10;
	EA = 1;
	IE2 |= 0x04;
}
void Timer0Init(void)		//10毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0xF0;		//设置定时初值
	TH0 = 0xD8;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	EA = 1;
	ET0 = 1;
}
void Sevice_timer0() interrupt 1
{
	TL0 = 0xF0;		//设置定时初值
	TH0 = 0xD8;		//设置定时初值
	DisplaySMG_Info();
	count++;
	if(count == (timer[mode]/10))
	{
		stat_led = 1;
		count = 0;
	}
}
void Sevice_timer2() interrupt 12
{
	if(set_key != 0)
	{
		count_08s++;
		if(count_08s == 8000)
		{
			count_08s = 0;
			if(stat_shan == 0)
			{
				stat_shan = 1;
			}
			else
			{
				stat_shan = 0;
			}
		}
	}
	if(stop_go == 1)
	{	
		pwm_num++;
		if(pwm_num <= pwm_duty)
		{
			led_control();
		}
		else if(pwm_num <= 8)
		{
			SelectHC573(4,0xff);
		}
		else
		{
			pwm_num = 0;
		}
	}	
}
void read_adcvalue()//读取电压值
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x03);
	I2CWaitAck();
	I2CStop();
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	adc_value = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	
	if(adc_value < 64)//对读取的电压值进行等级划分
	{
		level = 1;
		pwm_duty = 2;
	}
	else if(adc_value < 128)
	{
		level = 2;
		pwm_duty = 4;
	}
	else if(adc_value < 192)
	{
		level = 3;
		pwm_duty = 6;
	}
	else if(adc_value < 256)
	{
		level = 4;
		pwm_duty = 8;
	}
}
unsigned char read_myeeprom(unsigned char addre)//读取24c02的值
{
	unsigned char e_value;
	EA = 0;
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addre);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	e_value = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	EA = 1;
	return e_value;
}
void write_myeeprom(unsigned char addre,unsigned char dat)//写入24c02
{
	EA = 0;
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addre);
	I2CWaitAck();
	I2CSendByte(dat);
	I2CWaitAck();
	I2CStop();
	EA = 1;
}
void SelectHC573(unsigned char channel,unsigned char dat)//锁存器的选择
{
	P2 = (P2 & 0x1f) | 0x00;
	P0 = dat;
	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;
		case 0:
			P2 = (P2 & 0x1f) | 0x00;
		break;
	}
	P2 = (P2 & 0x1f) | 0x00;
}
void DelaySMG(unsigned int t)
{
	while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)//数码管按位显示
{
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,value);
	DelaySMG(TSMG);
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,0xff);
}
void DisplaySMG_All(unsigned char value)//控制所有的数码管
{
	SelectHC573(6,0xff);
	SelectHC573(7,value);
}
void DisplaySMG_Info()//数码管动态显示内容
{
	if(set_key == 1)
	{
		DisplaySMG_Bit(0,SMG_NoDot[16]);
		if(stat_shan == 0)
		DisplaySMG_Bit(1,SMG_NoDot[run_mode+1]);
		DisplaySMG_Bit(2,SMG_NoDot[16]);
		if(timer[run_mode] > 999)
		DisplaySMG_Bit(4,SMG_NoDot[timer[run_mode] / 1000]);
		DisplaySMG_Bit(5,SMG_NoDot[timer[run_mode] / 100 % 10]);
		DisplaySMG_Bit(6,SMG_NoDot[timer[run_mode] / 10 % 10]);
		DisplaySMG_Bit(7,SMG_NoDot[timer[run_mode] % 10]);
	}
	else if(set_key == 2)
	{
		DisplaySMG_Bit(0,SMG_NoDot[16]);
		DisplaySMG_Bit(1,SMG_NoDot[run_mode+1]);
		DisplaySMG_Bit(2,SMG_NoDot[16]);
		if(stat_shan == 0)
		{
			if(timer[run_mode] > 999)
			DisplaySMG_Bit(4,SMG_NoDot[timer[run_mode] / 1000]);
			DisplaySMG_Bit(5,SMG_NoDot[timer[run_mode] / 100 % 10]);
			DisplaySMG_Bit(6,SMG_NoDot[timer[run_mode] / 10 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[timer[run_mode] % 10]);
		}
	}
	if(stat_dis == 1)
	{
		DisplaySMG_Bit(6,SMG_NoDot[16]);
		DisplaySMG_Bit(7,SMG_NoDot[level]);
	}
}
void led_control()//led灯模式控制
{
	static unsigned char i = 0;
	if(mode == 0)
	{
		SelectHC573(4,mode_1[i]);
		if(stat_led == 1)
		{
			i++;
			stat_led = 0;
		}
		
		if(i == 8)
		{
			i = 0;
			mode++;
		}
	}
	else if(mode == 1 )
	{
		SelectHC573(4,mode_2[i]);
		if(stat_led == 1)
		{
			i++;
			stat_led = 0;
		}
		if(i == 8)
		{
			i = 0;
			mode++;
		}
	}
	else if(mode == 2)
	{
		SelectHC573(4,mode_3[i]);
		if(stat_led == 1)
		{
			i++;
			stat_led = 0;
		}
		if(i == 4)
		{
			i = 0;
			mode++;
		}
	}
	else if(mode == 3)
	{
		SelectHC573(4,mode_4[i]);
		if(stat_led == 1)
		{
			i++;
			stat_led = 0;
		}
		if(i == 4)
		{
			i = 0;
			mode = 0;
		}
	}
}
void scan_key()//独立按键扫描
{
	if(s7 == 0)
	{
		Delay10ms();
		if(s7 == 0)
		{
			if(stop_go == 0)
			{
				stop_go = 1;
			}
			else
			{
				stop_go = 0;
			}
			while(s7 == 0)
			{
				DisplaySMG_Info();
			}
		}
	}
	else if(s6 == 0)
	{
		Delay10ms();
		if(s6 == 0)
		{
			if(set_key == 0)
			{
				set_key = 1;
			}
			else if(set_key == 1)
			{
				set_key = 2;
			}
			else
			{
				set_key = 0;
				write_myeeprom(addre[run_mode],(unsigned char)(timer[run_mode]/10));
				Delay5ms();
			}
			while(s6 == 0)
			{
				DisplaySMG_Info();
			}
		}
	}
	else if(s5 == 0)
	{
		Delay10ms();
		if(s5 == 0)
		{
			if(set_key == 1)
			{
				if(run_mode == 3)
				{
					run_mode = 0;
				}
				else
				{
					run_mode++;
				}
			}
			else if(set_key == 2)
			{
				if(timer[run_mode] == 1200)
				{
					timer[run_mode] = 400;
				}
				else
				{
					timer[run_mode] += 100;
				}
			}
			while(s5 == 0)
			{
				DisplaySMG_Info();
			}
		}
	}
	else if(s4 == 0)
	{
		Delay10ms();
		if(s4 == 0)
		{
			if(set_key == 1)
			{
				if(run_mode == 0)
				{
					run_mode = 3;
				}
				else
				{
					run_mode--;
				}
			}
			else if(set_key == 2)
			{
				if(timer[run_mode] == 400)
				{
					timer[run_mode] = 1200;
				}
				else
				{
					timer[run_mode] -= 100;
				}
			}
			while(s4 == 0)
			{
				if(set_key == 0)
				{
					stat_dis = 1;
				}
				DisplaySMG_Info();
			}
			stat_dis = 0;
		}
	}
}
void Init_System()//系统上电初始化
{
	Init_timer2();
	Timer0Init();
	SelectHC573(4,0xff);
	SelectHC573(5,0x00);
	DisplaySMG_All(0xff);
	Delay5ms();
	timer[0] = (int)(read_myeeprom(0x01)*10);
	Delay5ms();
	timer[1] = (int)(read_myeeprom(0x02)*10);
	Delay5ms();
	timer[2] = (int)(read_myeeprom(0x03)*10);
	Delay5ms();
	timer[3] = (int)(read_myeeprom(0x04)*10);
	Delay5ms();
}
void main()
{
	Init_System();
	while(1)
	{
		read_adcvalue();
		scan_key();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值