栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【蓝桥杯】第十一届蓝桥杯单片机国赛 代码程序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【蓝桥杯】第十一届蓝桥杯单片机国赛 代码程序

第十一届蓝桥杯单片机国赛 程序
  • 题目
  • hex文件
  • 代码
  • 工程文件
  • B站视频
  • 更多资料

题目

历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这里就不再赘述了。

hex文件

读者下载这个文件然后用烧录软件直接烧入单片机就可以用了!

有问题直接找我哟。

链接: https://pan.baidu.com/s/1ZQqYBVVsYjuThCBdRgdCZw?pwd=bxt8
提取码: bxt8

代码

main.c

#include "stc15.h"
#include "ds1302.h"
#include "onewire.h"
#include "iic.h"
#define u8 unsigned char
#define u16 unsigned int
#define get() (P3&0x3f)|((P4&0x10)<<3)|((P4&0x04)<<4)|0x33


//	C:12			E:13		P:14
u8 code Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xff,0xc6,0x86,0x8c};
u8 smg_cnt=0;
u8 disbuff[9]={11,11,11,11,11,11,11,11,11};


extern u8 TIME[];

u16 T;
u16 T_tt=0;
bit T_flag=0;

u8	 key_buf=0;


u8 mode=1;


bit rd1_flag=0;

u16  dat_rd1=0;

u8 T_canshu=25;
u8 hour_canshu=17;
u8 led_canshu=4;


u8 T_canshu_temp=25;
u8 hour_canshu_temp=17;
u8 led_canshu_temp=4;


u8 led_state=0xff;


bit L1_flag=0;

bit count_first1=0;
bit count_flag1=0;
bit count_flag2=0;


u16 flag1_tt=0;
u16 flag2_tt=0;

bit L3_flag=0;
void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

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

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x20;		//设置定时初值
	T2H = 0xD1;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	IE2|=0x04;
	EA=1;
}
void Sevice_Timer2() interrupt 12
{
	 P2&=0x1f;

	 P0=0xff;
	P2|=0xe0;
	P2&=0x1f;

	P0=1<=8)
	smg_cnt=0;

	if(++T_tt>=300)
	{
		T_tt=0;
		T_flag=1;
	}

	 if(count_flag1&&++flag1_tt>=3000)
	 {
	 	L3_flag=0;
		
	 }
	

	 if(count_flag2&&++flag2_tt>=3000)
	 {
	 	L3_flag=1;
		
	 }
	 
}
void Init_System()
{
	P2&=0x1f;
	P0=0xff;
	P2|=0x80;
	P2&=0x1f;

   	P0=0x00;
	P2|=0xa0;
	P2&=0x1f;

	P0=0xff;
	P2|=0xc0;
	P2&=0x1f;

	P0=0xff;
	P2|=0xe0;
	P2&=0x1f;

}

void Show_SMG_T()
{
	disbuff[1]=TIME[2]/16;
	disbuff[2]=TIME[2]%16;
	disbuff[3]=10;
	disbuff[4]=TIME[1]/16;
	disbuff[5]=TIME[1]%16;
	disbuff[6]=10;
	disbuff[7]=TIME[0]/16;
	disbuff[8]=TIME[0]%16;
}
void Show_SMG_C()
{
	disbuff[1]=12;
	disbuff[2]=11;
	disbuff[3]=11;
	disbuff[4]=11;
	disbuff[5]=11;
	disbuff[6]=T/100%10;
	disbuff[7]=T/10%10;
	disbuff[8]=T%10;
}
void Show_SMG_E()
{
	disbuff[1]=13;
	disbuff[2]=11;
	disbuff[3]=dat_rd1/100%10;
	disbuff[4]=dat_rd1/10%10;
	disbuff[5]=dat_rd1%10;
	disbuff[6]=11;
	disbuff[7]=11;

	disbuff[8]=count_flag2;
}
void Show_SMG_P()
{
	disbuff[1]=14;
	disbuff[2]=mode-3;
	disbuff[3]=11;
	disbuff[4]=11;
	disbuff[5]=11;
	disbuff[6]=11;
	if(mode==4)
	{
	if(hour_canshu>=10)
		disbuff[7]=hour_canshu/10%10;
	else
		disbuff[7]=11;
			
	disbuff[8]=hour_canshu%10;
	}
	else  if(mode==5)
	{
	if(T_canshu>=10)
		disbuff[7]=T_canshu/10%10;
	else
		disbuff[7]=11;
			
	disbuff[8]=T_canshu%10;
	}
	else  if(mode==6)
	{
	
		disbuff[7]=11;
			
	disbuff[8]=led_canshu%10;
	}
}
void Show_SMG()
{
	if(mode==1)
	{
		Show_SMG_T();
	}
	else if(mode==2)
	{
		Show_SMG_C();
	}
	else if(mode==3)
	{
		Show_SMG_E();
	}
	else if(mode<=6)
	{
		Show_SMG_P();
	}
}
void Show_SMG_11()
{
	disbuff[1]=11;
	disbuff[2]=11;
	disbuff[3]=11;
	disbuff[4]=11;
	disbuff[5]=11;
	disbuff[6]=11;
	disbuff[7]=11;
	disbuff[8]=11;
}
//************************ 矩阵按键程序 ********************************************
void Scan_Key_B()
{
		u8 key_temp;
		P44=1;P42=1;P33=0;P32=0;
		key_temp=get();
		P44=0;P42=0;P33=1;P32=1;
		key_temp|=get();
		if(key_temp!=0xff&&!key_buf)
		{
			Delay5ms();
			P44=1;P42=1;P33=0;P32=0;
		key_temp=get();
		P44=0;P42=0;P33=1;P32=1;
		key_temp|=get();
			if(key_temp!=0xff&&!key_buf)
			{
				key_buf=key_temp;
			}
				
		}
		else if(key_temp==0xff&&key_buf)
		{
			Delay5ms();
			P44=1;P42=1;P33=0;P32=0;
		key_temp=get();
		P44=0;P42=0;P33=1;P32=1;
		key_temp|=get();
			if(key_temp==0xff&&key_buf)
			{
				switch(key_buf)
				{
				


					case 0x77:
					if(mode<=3)
					{
						mode=4;
					}
					else if(mode<=6)
					{
						mode=1;
						T_canshu_temp= T_canshu;
						hour_canshu_temp= hour_canshu;;
						led_canshu_temp= led_canshu;
					}
					break;//S4
					case 0x7b:
					if(mode<=3)
					{
						if(++mode>=4)
						{
							mode=1;
						}
					}
					else if(mode<=6)
					{
						if(++mode>=7)
							mode=4;
					}
					break;//S5
					case 0xb7:
					if(mode==4)
					{
						if(hour_canshu>=1)
						{
							hour_canshu-=1;
						}
					}
					else if(mode==5)
					{
						if(T_canshu>=1)
						{
							T_canshu-=1;
						}
					}
					else if(mode==6)
					{
						if(led_canshu>=5)
						{
							led_canshu-=1;
						}
					}
					
					break;//S8
					case 0xbb:
					if(mode==4)
					{
						if(hour_canshu<=22)
						{
							hour_canshu+=1;
						}
					}
					else if(mode==5)
					{
						if(T_canshu<=98)
						{
							T_canshu+=1;
						}
					}
					else if(mode==6)
					{
						if(led_canshu<=7)
						{
							led_canshu+=1;
						}
					}

					break;//S9
					

				}
				key_buf=0;
			}
				
		}

}
void Delay750ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 35;
	j = 51;
	k = 182;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
	Init_System();
	Timer2Init();
	DS1302_Config();
	Read_Temp();
	Delay750ms();
	T=(u16)(Read_Temp()*10);
	while(1)
	{
	
	   	Read_Time();
		if(T_flag)
		{
			T_flag=0;
			T=(u16)(Read_Temp()*10) ;
		}

		dat_rd1=Read_PCF8591(1);
		dat_rd1=Read_PCF8591(1);

		Scan_Key_B();
		Show_SMG();


		if(!count_first1&&(dat_rd1<50))
		{
			 		 count_first1=1;

					 count_flag1=0;
					 count_flag2=1;
		}
		else if(!count_first1&&(dat_rd1>50))
		{
			 		 count_first1=1;

					 count_flag1=1;
					 count_flag2=0;	//1:亮		2:暗
		}


		if(!count_flag2&&(dat_rd1<50))
		{
			 		  flag2_tt=0;
					   flag1_tt=0;
					 count_flag1=0;
					 count_flag2=1;
		}
		else if(!count_flag1&&(dat_rd1>50))
		{
					 flag2_tt=0;
					   flag1_tt=0;
					 count_flag1=1;
					 count_flag2=0;
		}

		led_state=0xff;

		if(hour_canshu_temp<8)
		{
				if(((TIME[2]/16*10+TIME[2]%16)>=hour_canshu_temp)&&((TIME[2]/16*10+TIME[2]%16)<8))
				{
					L1_flag=1;
				}
				else
					L1_flag=0;	
		}
		else
		{
			   if(((TIME[2]/16*10+TIME[2]%16)>=hour_canshu_temp)||((TIME[2]/16*10+TIME[2]%16)<8))
				{
					L1_flag=1;
				}
				else
					L1_flag=0;	
		}
		
		if(L1_flag)
		{
			 led_state&=0xfe;
		}
		if(T
			 led_state&=0xfd;
		}
		if(L3_flag)
		{
			 led_state&=0xfb;
		}
		if(count_flag2)
		{
			switch(led_canshu_temp)
			{
				case 4:led_state&=0xf7;break;
				case 5:led_state&=0xef;break;
				case 6:led_state&=0xdf;break;
				case 7:led_state&=0xbf;break;
			    case 8:led_state&=0x7f;break;
			}
		}
		P0=led_state;P2|=0x80;P0=led_state;P2&=0x1f;


	}
}

iic.h

#ifndef _IIC_H
#define _IIC_H

#include "stc15.h"
#include "intrins.h"


void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 
unsigned char Read_PCF8591(unsigned char channel);
#endif

iic.c

#include "iic.h"
sbit SDA = P2^1;
sbit SCL = P2^0;

#define DELAY_TIME 5

//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}

//I2C总线启动信号
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//I2C总线停止信号
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//I2C总线发送一个字节数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//I2C总线接收一个字节数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}
unsigned char Read_PCF8591(unsigned char channel)
{
	unsigned char temp;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40|channel);
	IIC_WaitAck();

	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();



	return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include "stc15.h"

sbit DQ = P1^4;  

float Read_Temp();

#endif

onewire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t);  

//单总线写操作
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//单总线读操作
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}
float Read_Temp()
{
	unsigned int temp;
	unsigned char LSB,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;

	return temp*0.0625;

		
}
void Delay_OneWire(unsigned int t)  
{
	t*=12;
	while(t--);
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include 
#include 

sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3; 

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void DS1302_Config();
void Read_Time();
#endif

ds1302.c

#include "ds1302.h"  									
unsigned char TIME[3]={0x50,0x59,0x16};

unsigned char read_addr[3]={0x81,0x83,0x85};
unsigned char write_addr[3]={0x80,0x82,0x84};
//写字节
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}

void DS1302_Config()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);	
	for(i=0;i<=2;i++)
	{
		  Write_Ds1302_Byte(write_addr[i],TIME[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);	
}

void Read_Time()
{
	unsigned char i;
	
	for(i=0;i<=2;i++)
	{
		  TIME[i]=Read_Ds1302_Byte(read_addr[i]);
	}
	
}
工程文件

链接: https://pan.baidu.com/s/1KaxCTAtTRJDzrxnbZAqQfw?pwd=bh6c
提取码: bh6c

B站视频

我在B站发布了自己完成代码的过程,想要了解的朋友可以点击呀,第十一届蓝桥杯单片机国赛。

更多资料

需要更多蓝桥杯资料的小可爱,可以关注公众号【玛德花】,回复 蓝桥杯 即可,也随时欢迎需要交流的小伙伴一起讨论呀。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号