蓝桥杯第十五届电子赛省赛单片机组-代码(包含本次省赛真题压缩包)

这次比赛比较简单,都是基础的模块

有数码管、LED、矩阵键盘、DS1302、频率测量。

频率测量是这次的核心内容

如果把十四届的题目做熟练基本能做出了吧

比赛的时候CPU在燃烧!

P:代码还有漏洞,勉强能看,我之后再完善

代码如下:

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

#define TSMG 500

//sfr AUXR = 0x8e;

sfr P4 = 0xC0;

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


float freq = 0;
unsigned int freq_max = 0;
unsigned int freq_smg = 0;

unsigned char dac;

unsigned char stat_smg = 1;
unsigned char stat_led = 0xff;

unsigned int chaoxian = 2000;
unsigned int jiaozhun = 0;

unsigned char flag_jiaozhun = 0;
unsigned char flag_d_fj = 0;
unsigned char flag_L1 = 0;
unsigned char flag_L2 = 0;
unsigned char flag_warning = 0;

unsigned char hour = 0;
unsigned char min = 0;
unsigned char sec = 0;

unsigned char hour_log = 0;
unsigned char min_log = 0;
unsigned char sec_log = 0;

unsigned char count_t1 = 0;
unsigned char count_t2 = 0;
unsigned int count_f = 5;

unsigned char Write_DS1302[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e};
unsigned char Read_DS1302[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d,0x8f};
unsigned char Time[] = {0x30,0x36,0x10,0x13,0x04,0x06,0x24};

code unsigned char Seg_Table_noDot[] =
{
0xc0,
//0
0xf9,
//1
0xa4,
//2
0xb0,
//3
0x99,
//4
0x92,
//5
0x82,
//6
0xf8,
//7
0x80,
//8
0x90,
//9
0x88,
//A
0x83,
//b
0xc6,
//C
0xa1,
//d
0x86,
//E
0x8e,
//F
0xbf,
//-
0x7f,
//.
0x8c,
//P
0x89,
//H
0xc7
//L
};

code unsigned char Seg_Table_Dot[] =
{
0x40,
//0
0x79,
//1
0x24,
//2
0x30,
//3
0x19,
//4
0x12,
//5
0x02,
//6
0x78,
//7
0x00,
//8
0x10
//9
};

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

void SelectHC573(unsigned char channel,unsigned char dat)
{
	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 Set_Ds1302()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i = 0;i<8;i++)
	{
		Write_Ds1302_Byte(Write_DS1302[i],Time[i]);
	}
	Write_Ds1302_Byte(0x8e,0xff);
	
}


void Read_Ds1302_Time(unsigned char i)
{
	if(i == 0)
	{
	hour = Read_Ds1302_Byte(Read_DS1302[2]);
	min = Read_Ds1302_Byte(Read_DS1302[1]);
	sec = Read_Ds1302_Byte(Read_DS1302[0]);
	}
	else
	{
	hour_log = Read_Ds1302_Byte(Read_DS1302[2]);
	min_log = Read_Ds1302_Byte(Read_DS1302[1]);
	sec_log = Read_Ds1302_Byte(Read_DS1302[0]);
	}
}


void Handle_Freq()
{
	if(flag_jiaozhun == 0)
		{
			if(freq >= jiaozhun)
			{
				flag_d_fj = 0;
				freq_smg = freq-jiaozhun;
			}
			else 
				flag_d_fj = 1;
		}
		else
			freq_smg = freq + jiaozhun;
	
		
	if(freq_smg >freq_max)
	{	
		freq_max=freq_smg;
		Read_Ds1302_Time(1);
	}
	
}

void Handle_DAC()
{
	if(flag_d_fj == 1)
	{
		dac = 0;
	}
	else if(freq_smg > chaoxian)
					dac = 5;
			else if(freq_smg >= 500)
					dac = freq_smg * (4 / (chaoxian - 500)) + 1 - 2000 / (chaoxian - 500);
					else if(freq_smg >= 0)
						dac = 1;
}

void Init_Timer0_Timer1()
{
	TMOD = 0x16;
	
	//AUXR &=0x7f;
	
	TH1 = (65536 - 10000) / 256;
	TL1 = (65536 - 10000) % 256;
	
	TH0 = 0xff;
	TL0 = 0xff;
	
	EA = 1;
	ET0 = 1;
	ET1 = 1;
	
	TR0 = 1;
	TR1 = 1;
}

void Service_Timer0() interrupt 1
{
	count_f++;
					stat_led &= ~0x03;
				SelectHC573(4,stat_led);
}

void Service_Timer1() interrupt 3
{
	TH1 = (65536 - 10000 ) / 256;
	TL1 = (65536 - 10000 ) % 256;
	
	count_t1++;
	count_t2++;
	if(count_t1 == 20)
	{
		count_t1 = 0;
		if(stat_smg == 1 && flag_L1 == 0)
		{
				flag_L1=1;
				stat_led &= ~0x01;
				SelectHC573(4,stat_led);
		}
		else
		{
				flag_L1=0;
				stat_led |= 0x01;
				SelectHC573(4,stat_led);
		}
			
		
		if(flag_warning == 1 && flag_L2 == 0)
		{
				flag_L2=1;
				stat_led &= ~0x02;
				SelectHC573(4,stat_led);
		}
		else
		{
				flag_L2=0;
				stat_led |= 0x02;
				SelectHC573(4,stat_led);
		}
		
	}
	
	if(count_t2 == 100)
	{
		unsigned char dat = 0;
		count_t2 = 0;
		dat = count_f;
		count_f = 0;
		
		freq = dat/(10.0/100.0);

		Handle_Freq();
		Handle_DAC();
	}
	
}



void Write_PCF8591_DAC(unsigned char ain)
{
	I2CStart();
	I2CSendByte(90);
	I2CWaitAck();
	I2CSendByte(ain);
	I2CWaitAck();
	I2CStop();
	
}

unsigned char Read_PCF8591_DAC()
{
	unsigned char tmp;
		
	I2CStart();
	I2CSendByte(91);
	I2CWaitAck();
	tmp = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	
	return tmp;
}


void Scan_Keys()
{
	R3 = 1;
	R4 = 0;
	if(C1 == 0)
	{
		Delay(500);
		if(C1 == 0)
		{
			if(stat_smg == 1)
			{
				stat_smg = 2;
			}
			else if(stat_smg ==2)
			{
				stat_smg =4;
			}
			else if(stat_smg ==4)
			{
				stat_smg =5;
			}
			else if(stat_smg ==5)
			{
				stat_smg =1;
			}
			
			while(C1 == 0);
		}
	}
	if(C2 == 0)
	{
		Delay(500);
		if(C2 == 0)
		{
			if(stat_smg == 2)
			{
				if(chaoxian < 9000)
					chaoxian += 1000;
			}
			
			if(stat_smg ==3)
			{
				if(flag_jiaozhun == 1 && jiaozhun > 0)
				{
					if(jiaozhun == 100)
						flag_jiaozhun = 0;
					jiaozhun -= 100;
				}
				else if(jiaozhun <900)
				{
					flag_jiaozhun = 0;
					jiaozhun += 100;
				}
			}
			while(C2 == 0);
		}
	}
	
	R3 = 0;
	R4 = 1;
	if(C1 == 0)
	{
		Delay(500);
		if(C1 == 0)
		{
			if(stat_smg == 2)
			{
				stat_smg = 3;
			}
			else if(stat_smg ==3)
			{
				stat_smg =2;
			}
			
			if(stat_smg == 5)
			{
				stat_smg = 6;
			}
			else if(stat_smg ==6)
			{
				stat_smg =5;
			}
			while(C1 == 0);
		}
	}
	if(C2 == 0)
	{
		Delay(500);
		if(C2 == 0)
		{
			if(stat_smg == 2)
			{
				if(chaoxian > 1000)
					chaoxian -= 1000;
			}
			
			if(stat_smg ==3)
			{
				if(flag_jiaozhun == 0 && jiaozhun > 0)
					jiaozhun -= 100;
				else if(jiaozhun <900)
				{
					flag_jiaozhun = 1;
					jiaozhun += 100;
				}
			}
			while(C2 == 0);
		}
	}
	
}


void Display_SMG()
{
	switch(stat_smg)
	{
		case 1:
			DisplaySMG_Bit(0,Seg_Table_noDot[15]);
		DisplaySMG_Bit(1,0xff);
		DisplaySMG_Bit(2,0xff);
		DisplaySMG_Bit(3,0xff);
		
		if(flag_d_fj == 0)
		{
		stat_led |= 0x02;
		SelectHC573(4,stat_led);
		
		DisplaySMG_Bit(4,Seg_Table_noDot[freq_smg / 1000]);
		DisplaySMG_Bit(5,Seg_Table_noDot[freq_smg / 100 % 10]);
		DisplaySMG_Bit(6,Seg_Table_noDot[freq_smg / 10 % 10]);
		DisplaySMG_Bit(7,Seg_Table_noDot[freq_smg % 10]);
		}
		else
		{
		stat_led &= ~0x02;
		SelectHC573(4,stat_led);
			
		DisplaySMG_Bit(4,0xff);
		DisplaySMG_Bit(5,0xff);
		DisplaySMG_Bit(6,Seg_Table_noDot[20]);
		DisplaySMG_Bit(7,Seg_Table_noDot[20]);
		}
		break;
		
		case 2:
			DisplaySMG_Bit(0,Seg_Table_noDot[18]);
		DisplaySMG_Bit(1,Seg_Table_noDot[1]);
		DisplaySMG_Bit(2,0xff);
		DisplaySMG_Bit(3,0xff);
		DisplaySMG_Bit(4,Seg_Table_noDot[chaoxian / 1000]);
		DisplaySMG_Bit(5,Seg_Table_noDot[chaoxian / 100 % 10]);
		DisplaySMG_Bit(6,Seg_Table_noDot[chaoxian / 10 % 10]);
		DisplaySMG_Bit(7,Seg_Table_noDot[chaoxian % 10]);
		break;
		
		case 3:
				DisplaySMG_Bit(0,Seg_Table_noDot[18]);
		DisplaySMG_Bit(1,Seg_Table_noDot[2]);
		DisplaySMG_Bit(2,0xff);
		DisplaySMG_Bit(3,0xff);
		
		if(flag_jiaozhun == 1)
			DisplaySMG_Bit(4,Seg_Table_noDot[16]);
		else
			DisplaySMG_Bit(4,0xff);
		
		if(jiaozhun >99)
		{
			DisplaySMG_Bit(5,Seg_Table_noDot[jiaozhun / 100]);
			DisplaySMG_Bit(6,Seg_Table_noDot[jiaozhun / 10 % 10]);
			DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
		}
		else if(jiaozhun > 9)
		{
			DisplaySMG_Bit(5,0xff);
			DisplaySMG_Bit(6,Seg_Table_noDot[jiaozhun / 10 % 10]);
			DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
		}
		else
		{
			DisplaySMG_Bit(5,0xff);
			DisplaySMG_Bit(6,0xff);
			DisplaySMG_Bit(7,Seg_Table_noDot[jiaozhun % 10]);
		}
		
		break;
		
		case 4:
			DisplaySMG_Bit(0,Seg_Table_noDot[hour / 16]);
		DisplaySMG_Bit(1,Seg_Table_noDot[hour % 16]);
		DisplaySMG_Bit(2,Seg_Table_noDot[16]);
		DisplaySMG_Bit(3,Seg_Table_noDot[min / 16]);
		DisplaySMG_Bit(4,Seg_Table_noDot[min % 16]);
		DisplaySMG_Bit(5,Seg_Table_noDot[16]);
		DisplaySMG_Bit(6,Seg_Table_noDot[sec / 16]);
		DisplaySMG_Bit(7,Seg_Table_noDot[sec % 16]);
		break;
		
		case 5:
			DisplaySMG_Bit(0,Seg_Table_noDot[19]);
		DisplaySMG_Bit(1,Seg_Table_noDot[15]);
		DisplaySMG_Bit(2,0xff);
		DisplaySMG_Bit(3,0xff);
		DisplaySMG_Bit(4,Seg_Table_noDot[freq_max / 1000]);
		DisplaySMG_Bit(5,Seg_Table_noDot[freq_max / 100 % 10]);
		DisplaySMG_Bit(6,Seg_Table_noDot[freq_max / 10 % 10]);
		DisplaySMG_Bit(7,Seg_Table_noDot[freq_max % 10]);
		break;
		
		case 6:
			DisplaySMG_Bit(0,Seg_Table_noDot[19]);
		DisplaySMG_Bit(1,Seg_Table_noDot[10]);
		DisplaySMG_Bit(2,Seg_Table_noDot[hour_log / 16]);
		DisplaySMG_Bit(3,Seg_Table_noDot[hour_log % 16]);
		DisplaySMG_Bit(4,Seg_Table_noDot[min_log / 16]);
		DisplaySMG_Bit(5,Seg_Table_noDot[min_log % 16]);
		DisplaySMG_Bit(6,Seg_Table_noDot[sec_log / 16]);
		DisplaySMG_Bit(7,Seg_Table_noDot[sec_log % 16]);
		break;
	}
	DisplaySMG_All(0xff);
}

void main()
{
	SelectHC573(4,0xff);
	SelectHC573(5,0x00);
	Write_PCF8591_DAC(0x09);
	Init_Timer0_Timer1();
	Set_Ds1302();
	freq_smg = Read_PCF8591_DAC();
	Display_SMG();
	while(1)
	{
		Display_SMG();
		Read_Ds1302_Time(0);
		if(freq_smg > chaoxian)
			flag_warning=1;
		else
			flag_warning=0;
		Display_SMG();
		Scan_Keys();
	}
}	

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 蓝桥杯单片机第九省赛试题的hex文件是一种十六进制文件格式,用于将程序代码转换成可供单片机读取的机器码。这个hex文件是由编译器生成的,其中包含了程序的指令、数据和地址信息。 hex文件可以在单片机上烧写,将程序加载到单片机内存中运行。通过烧写hex文件,可以将代码部署到硬件设备上,完成各种功能和任务。 对于蓝桥杯单片机的参选手来说,拿到hex文件后需要将其烧写到指定的单片机上。具体的步骤是:将单片机与计算机通过编程器连接,打开编程软件,选择hex文件并将其下载到单片机中。下载完成后,可以进行功能测试和调试。如果hex文件没有问题,单片机将按照程序设计的逻辑正常运行。 蓝桥杯单片机的hex文件通常包含了多个功能和子程序,选手需要根据竞规定的题目要求进行相应的改动和优化。参选手可以对hex文件进行反汇编,分析其中的机器码,理解程序的运行原理和逻辑。通过对hex文件的修改和调试,选手可以实现各种功能和算法,完成比的要求。 总结来说,蓝桥杯单片机第九省赛试题的hex文件是参选手完成竞过程中重要的载体,通过烧写hex文件到单片机上,选手可以完成编程任务,展示自己的技能和创造力。 ### 回答2: 蓝桥杯单片机第九省赛试题的hex文件是一种用于嵌入式系统的程序文件格式。它包含了由汇编或C语言编写的单片机代码,经过编译和链接之后生成的可执行文件。 hex文件以文本的形式呈现,每一行都包含了十六进制的地址和对应的机器码。这些机器码是单片机执行的指令,用于实现特定的功能。 在单片机中,参选手需要下载并烧录hex文件到单片机上,以便将程序加载到内存中,使得单片机能够按照代码的要求进行运算和控制。 通过阅读hex文件,我们可以了解程序的整体结构、函数的调用关系以及数据的存储方式。这有助于我们分析代码的逻辑和调试可能出现的错误。 蓝桥杯单片机第九省赛试题的hex文件是经过编译的参选手提交的作品。为了保证公平性和保密性,比委会在竞开始前会提供一个统一的下载接口,选手需要将自己的hex文件上传至该接口,由委会进行验证和烧录。 总而言之,hex文件是蓝桥杯单片机中非常重要的一部分,它承载着选手的智慧和努力。通过对hex文件的研究和分析,我们能够更好地理解和掌握单片机编程的技术,提高自己的竞水平。 ### 回答3: 蓝桥杯是一个面向高中生和大学生的计算机竞单片机是其中的一个竞类别。而第九省赛试题中提到的hex文件是单片机程序在编译后生成的二进制文件,该文件存储了程序的机器指令。 hex文件通常以文本格式保存,每一行包含16个字符,其中包括8个十六进制数字、一个标识符和16进制校验和。这些数字表示了相应地址的机器指令或数据。 在蓝桥杯单片机中,参选手需要根据题目要求编写相应的单片机程序,并将其编译生成hex文件。这个文件需要在比中使用,以将程序烧录到实际的单片机芯片中,从而使其运行。 hex文件的生成通常由编译器自动完成,选手只需将生成的文件保存并在比中使用。烧录hex文件到单片机芯片可以使用专门的烧录工具或者调试器进行。 比官方会提供相应的参规则和操作说明,选手需要按照要求正确操作,将hex文件烧录到单片机中,并保证程序的正常运行。 因此,蓝桥杯单片机第九省赛试题中提到的hex文件是指参选手需要编写的单片机程序经过编译后生成的二进制文件。选手需要正确生成和使用这个文件,使得程序能够在实际的单片机上正确运行,以完成竞任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值