Respuestas a preguntas anteriores de la competencia provincial de la competencia de recursos gratuitos Blue Bridge Cup: la séptima (código provincial más notas)

Respuestas a la VII Competencia Provincial del Grupo Single-chip de la Competencia de la Copa Blue Bridge (código más comentarios)


1. Tema

  He compartido el tema de los concursos provinciales y nacionales anteriores en el artículo anterior ( click para ver ) (recursos de disco en línea), si es necesario, vaya directamente a descargar, no los repetiré aquí.

Dos, archivo hexadecimal

El lector descarga este archivo y lo graba directamente en el microcontrolador con el software de grabación, ¡y está listo para usar!

Enlace: https://pan.baidu.com/s/1i1cHqNFWtSmhXAs-7cAbiw
Código de extracción: dud6

En tercer lugar, la implementación de la función principal

  Consejo: Durante la competición, es posible que solo la modificación de la función principal no sea suficiente. A veces es necesario prestar atención a si el código de cada piloto proporcionado por el oficial de la competición está escrito intacto. Por ejemplo, a veces, no escribe todos estos en el archivo .h, comente deliberadamente, debe averiguar qué funciones se necesitan en el archivo .c correspondiente y completarlas una por una.
Inserte la descripción de la imagen aquí
  Además, mi código está completamente escrito en un archivo, por lo que es más conveniente para los lectores usarlo. Puedes copiar directamente mi archivo .c o copiar y pegar el contenido, y ponerlo donde quieras.

En el código:

# include "reg52.h"
# include "onewire.h"

typedef unsigned char uchar;
typedef unsigned int uint;

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

uchar duanma[18] = {
    
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
uint t_c = 0;				    //定时1s的计数变量
uchar pwm_c = 0;				//脉宽调制的计数变量
uchar pwm_t = 2;				//具体的脉宽调制的值,默认值为2
bit pwm_f = 1;					//pwm输出的标志变量
uint temp = 0;					//用来存放读取的温度
uchar mode = 1;					//标志风扇工作模式,默认为睡眠风
uint rtime = 60;				//剩余工作时间,默认工作时间1min
bit k7 = 0;					    //S7按键的标志位,默认为时间倒计时
uchar k6 = 1;					//S6按键的标志位,默认为1

void BUZZEROutput ();

//========================锁存器的选择========================
void SelectHC573 (uchar n)
{
    
    
	switch (n)
	{
    
    
		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 InitSystem ()
{
    
    
	SelectHC573(4);
	P0 = 0xff;
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(0);	
}
//============================================================

//====================定时器T0 - 100us ========================
void InitTime0 ()
{
    
    
	TMOD = 0x01;

	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;

	TR0 = 1;
	ET0 = 1;
	EA = 1;
}

void ServiceTime0 () interrupt 1
{
    
    
	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;
	
	if (rtime > 0)
	{
    
    
		t_c++;
		if (t_c >= 10000)
		{
    
    
			rtime = rtime - 1;
			t_c = 0;
		}
	}
	
	pwm_c = pwm_c + 1;
	if (pwm_c >= pwm_t)
	{
    
    
		pwm_f = 0;
	}
	if (pwm_c >= 10)
	{
    
    
		pwm_c = 0;
		pwm_f = 1;	
	}	
}
//============================================================

//======================温度的读取============================
void ReadTemp ()
{
    
    
	uchar LSB;
	uchar MSB;

	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);

	LSB = Read_DS18B20();
	MSB = Read_DS18B20();

	temp = (MSB << 8) | LSB;
	if ((temp & 0xf800) == 0x0000)
	{
    
    
		temp = temp >> 4;
	}
}
//============================================================

//==================数码管显示相关函数========================
void Delay_SMG (uint t)
{
    
    
	while (t--);
}

void ShowSMG_Bit (uchar pos,uchar dat)
{
    
    
	SelectHC573(7);
	P0 = 0xff;
	SelectHC573(6);
	P0 = 0x01 << pos - 1;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);	
}

void AllSMG (uchar dat)
{
    
    
	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);
}

void ShowSMG ()
{
    
    
	if(k7 == 0)
	{
    
    
		ShowSMG_Bit(1,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(2,duanma[mode]);
		Delay_SMG (100);
		ShowSMG_Bit(3,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(5,duanma[rtime / 1000]);
		Delay_SMG (100);
		ShowSMG_Bit(6,duanma[(rtime / 100) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(7,duanma[(rtime / 10) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(8,duanma[rtime % 10]);
		Delay_SMG (100);
	}
	else if (k7 == 1)
	{
    
    
		ShowSMG_Bit(1,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(2,duanma[4]);
		Delay_SMG (100);
		ShowSMG_Bit(3,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(6,duanma[(temp / 10) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(7,duanma[temp % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(8,duanma[12]);
		Delay_SMG (100);	
	}
	AllSMG(0xff);	
} 
//============================================================

//=======================浏览按键=============================
void Delay_Key (uchar t)
{
    
    
	while (t--);
}

void ScanKey ()
{
    
    
	if (S7 == 0)
	{
    
    
		Delay_Key(100);
		if (S7 == 0)
		{
    
    
			while (S7 == 0)
			{
    
    
				ShowSMG ();
				if (rtime > 0)
				{
    
    
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
    
    
					SelectHC573(5);
					P0 = 0x00;	
				}	
			}
			if (k7 == 0)
			{
    
    
				k7 = 1;
			}
			else if (k7 == 1)
			{
    
    
				k7 = 0;
			}
		}
	}
	if (S6 == 0)
	{
    
    
		Delay_Key(100);
		if (S6 == 0)
		{
    
    
			while (S6 == 0)
			{
    
    
				ShowSMG ();
				if (rtime > 0)
				{
    
    
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
    
    
					SelectHC573(5);
					P0 = 0x00;	
				}
			}
			rtime = 0;
		}
	}
	if (S5 == 0)
	{
    
    
		Delay_Key(100);
		if (S5 == 0)
		{
    
    
			while (S5 == 0)
			{
    
    
				ShowSMG ();
				if (rtime > 0)
				{
    
    
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
    
    
					SelectHC573(5);
					P0 = 0x00;	
				}
			}
			if(k6 == 1)
			{
    
    
				k6 = 2;
				rtime = 120;
			}
			else if (k6 == 2)
			{
    
    
				k6 = 0;
				rtime = 0;
			}
			else if (k6 == 0)
			{
    
    
				k6 = 1;
				rtime = 60;
			}
		}
	}
	if (S4 == 0)
	{
    
    
		Delay_Key(100);
		if (S4 == 0)
		{
    
    
			while (S4 == 0)
			{
    
    
				ShowSMG ();
				if (rtime > 0)
				{
    
    
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
    
    
					SelectHC573(5);
					P0 = 0x00;	
				}	
			}
			if (mode == 1)			//睡眠风
			{
    
    
				mode = 2;
				pwm_t = 5;
			}
			else if (mode == 2)			//自然风
			{
    
    
				mode = 3;
				pwm_t = 10;
			}
			else if (mode == 3)		    //常风
			{
    
    
				mode = 1;
				pwm_t = 2;
			}
		}
	}	
}
//============================================================

//====================PWM蜂鸣器输出===========================
void BUZZEROutput ()
{
    
    
	SelectHC573(0);
	if (pwm_f == 1)
	{
    
    
		P0 = 0x40;	
	}
	else if (pwm_f == 0)
	{
    
    
		P0 = 0x00;	
	}
	SelectHC573(5);	
}
//============================================================

//=====================LED显示函数============================
void LEDRunning ()
{
    
    
	SelectHC573(0);
	if (rtime > 0)
	{
    
    
		if (mode == 1)
		{
    
    
			P0 = 0xfe;
		}
		else if (mode == 2)
		{
    
    
			P0 = 0xfd;
		}
		else if (mode == 3)
		{
    
    
			P0 = 0xfb;
		}
	}
	else if(rtime == 0)
	{
    
    
		P0 = 0xff;
	}
	SelectHC573(4);		
}
//============================================================

//========================主函数==============================
void main ()
{
    
    
	InitSystem ();
	InitTime0 ();
	while (1)
	{
    
    
		ScanKey ();
		if (k7 == 1)
		{
    
    
		 	ReadTemp ();
		}
		ShowSMG ();
		if (rtime > 0)
		{
    
    
			BUZZEROutput ();
		}
		else if (rtime == 0)
		{
    
    
			SelectHC573(0);
			P0 = 0x00;
			SelectHC573(5);	
		}
		LEDRunning ();				
	}
}
//============================================================

Cuarto, todo el archivo del proyecto

Enlace: https://pan.baidu.com/s/1Qm3icNKZyWl21H2zoB4ZxA
Código de extracción: 80ci

 Si no puede abrir este proyecto directamente, puede deberse a la versión de keil. Yo uso keil3. Si hay un problema, puede copiar directamente el contenido del archivo .c. Como dije antes, mi proceso de implementación es en un .c Realizado en el archivo, ¡conveniente para el uso de los lectores!
Inserte la descripción de la imagen aquí

Al final

Los amigos que lo necesiten pueden comentarme o escribirme en privado en cualquier momento para discutir problemas en el proceso de aprendizaje, y haré todo lo posible para brindar ayuda.


Cálido recordatorio: ¡ prestarme atención no es fácil perder el artículo!

Competencia de Blue Bridge Cup grupo de microcomputadoras de un solo chip anterior competencia provincial preguntas respuestas (código más comentarios) para el resto, ver- https : //blog.csdn.net/weixin_45386875/article/details/114136549

Supongo que te gusta

Origin blog.csdn.net/weixin_45386875/article/details/114192437
Recomendado
Clasificación