蓝桥杯单片机第十二届省赛

如果有用请点赞,还会继续更新的

题目:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路:

还是千篇一律的定时器中断,简直就是yyds。这届和第十一届的题可以说是有很多相似的地方,我感觉难点可能还是LED的那个部分。废话少说,直接上代码。

main.c

#include"reg52.h"
#include"intrins.h"
#include"onewire.h"
#include"iic.h"
sfr AUXR=0x8e;

sfr P4=0xC0;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;

char code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc6,0x7f,0x8c,0x88};
int smg_mode=0;
int wen_mode=0;
int set_temp=2500;
int te_temp;
int led_temp=0xff;
int output_v;
void get_v();

void get_temperature()
{	
	te_temp=rd_temperature();
}
void choose_573(int n)
{
	switch(n)
	{
		case(0):P2=(P2&0x1f)|0x00;break;
		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;
	}
}
void init_system()
{
	choose_573(4);
	P0=0xff;
	choose_573(5);
	P0=0x00;
	choose_573(0);
	P0=0xff;
}
//==================================中断
void Timer0Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初始值
	TH0 = 0x28;		//设置定时初始值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
}
int count_1=200;
int count_2=0;
void Timer0Init_server() interrupt 1
{
	count_1++;
	count_2++;
	if(count_1>=200)
	{
		get_temperature();
		count_1=0;	
	}
	if(count_2>=100)
	{
		get_v();
		count_2=0;
	}
}
//==================================
//==================================按键
void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}
void key_board()
{
	R3=0;R1=R2=R4=1;
	C1=C2=C3=C4=1;
	if(C1==0&&smg_mode==2)
	{
		Delay5ms();Delay5ms();
		if(C1==0&&smg_mode==2)
		{
			wen_mode++;
			if(wen_mode>=2)
			{
				wen_mode=0;
			}	
		}
		while(!C1);
	}
	if(C2==0&&smg_mode==1)
	{
		Delay5ms();Delay5ms();
		if(C2==0&&smg_mode==1)
		{
			set_temp=set_temp+100;
		}
		while(!C2);
	}

	R4=0;R1=R2=R3=1;
	C1=C2=C3=C4=1;
	if(C1==0)
	{
		Delay5ms();Delay5ms();
		if(C1==0)
		{
			smg_mode++;
			if(smg_mode>=3)
			{
				smg_mode=0;
			}
		}
		while(!C1);
	}
	if(C2==0&&smg_mode==1)
	{
		Delay5ms();Delay5ms();
		if(C2==0&&smg_mode==1)
		{
			set_temp=set_temp-100;
		}
		while(!C2);
	}
}
//==================================
//================================== DAC

void get_v()
{
	if(wen_mode==0)
	{
		if(te_temp<set_temp)
		{
			output(0);
			output_v=0;
		}
		else
		{
			output(255);
			output_v=500;
		}
	}
	if(wen_mode==1)
	{
		if(te_temp<=2000)
		{
			output(51);
			output_v=100;
		}
		else if(te_temp>=4000)
		{
			output(204);
			output_v=400;
		}
		else if(te_temp>2000&&te_temp<4000)
		{
			int te_temp1;
			te_temp1=te_temp/100;
			output((1+(0.15*(te_temp-20)))*51);
			output_v=100*(1+(0.15*(te_temp1-20)));	
		}	
	}
}

void putout_v()
{
	if(wen_mode==0)
	{
		if(te_temp<set_temp)
		{
			output(0);
		//	output_v=0;
		}
		else
		{
			output(255);
		//	output_v=500;
		}
	}
	if(wen_mode==1)
	{
		if(te_temp<=2000)
		{
			output(51);
		//	output_v=100;
		}
		else if(te_temp>=4000)
		{
			output(204);
		//	output_v=400;
		}
		else if(te_temp>2000&&te_temp<4000)
		{
			int te_temp1;
			te_temp1=te_temp/100;
			output((1+(0.15*(te_temp-20)))*51);
		//	output_v=100*(1+(0.15*(te_temp1-20)));	
		}	
	}
}
//==================================
//==================================数码管
void Delay400us()		//@11.0592MHz
{
	unsigned char i, j;

	i = 5;
	j = 74;
	do
	{
		while (--j);
	} while (--i);
}
void SMG(int wei,int dat)
{
	choose_573(6);
	P0=0x80>>(wei-1);
	choose_573(7);
	P0=xianshi[dat];
	choose_573(0);
	P0=0xff;
}
void smg_display()
{
	if(smg_mode==0)
	{
		SMG(1,te_temp%10);
		Delay400us();
		SMG(2,(te_temp%100)/10);
		Delay400us();
		SMG(3,(te_temp%1000)/100);
		Delay400us();
		SMG(3,12);
		Delay400us();
		if(te_temp>=1000)
		{
			SMG(4,(te_temp%10000)/1000);
			Delay400us();
		}
		else
		{
			SMG(4,10);
			Delay400us();
		}
		SMG(5,10);
		Delay400us();
		SMG(6,10);
		Delay400us();
		SMG(7,10);
		Delay400us();
		SMG(8,11);
		Delay400us();
	}
	if(smg_mode==1)
	{
		SMG(1,set_temp%10);
		Delay400us();
		SMG(2,(set_temp%100)/10);
		Delay400us();
		SMG(3,(set_temp%1000)/100);
		Delay400us();
		SMG(3,12);
		Delay400us();
		if(set_temp>=1000)
		{
			SMG(4,(set_temp%10000)/1000);
			Delay400us();
		}
		else
		{
			SMG(4,10);
			Delay400us();
		}
		SMG(5,10);
		Delay400us();
		SMG(6,10);
		Delay400us();
		SMG(7,10);
		Delay400us();
		SMG(8,13);
		Delay400us();
	}
	if(smg_mode==2)
	{
		SMG(1,output_v%10);
		Delay400us();
		SMG(2,(output_v%100)/10);
		Delay400us();
		SMG(3,(output_v%1000)/100);
		Delay400us();
		SMG(3,12);
		Delay400us();
		SMG(4,10);
		Delay400us();
		SMG(5,10);
		Delay400us();
		SMG(6,10);
		Delay400us();
		SMG(7,10);
		Delay400us();
		SMG(8,14);
		Delay400us();	
	}	
}
//==================================
//==================================led
void led_work()
{
	if(wen_mode==1)
	{
		led_temp=(led_temp&0xfe)|0x00;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(wen_mode==0)
	{
		led_temp=(led_temp&0xfe)|0x01;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode==0)
	{
		led_temp=(led_temp&0xfd)|0x00;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode!=0)
	{
		led_temp=(led_temp&0xfd)|0x02;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode==1)
	{
		led_temp=(led_temp&0xfb)|0x00;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode!=1)
	{
		led_temp=(led_temp&0xfb)|0x04;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode==2)
	{
		led_temp=(led_temp&0xf7)|0x00;
		choose_573(4);
		P0=led_temp;
		choose_573(0);
	}
	if(smg_mode!=2)
	{
		led_temp=(led_temp&0xf7)|0x08;
		choose_573(4);
		P0=led_temp;
		choose_573(0);	
	}
}
//==================================
void main()
{
	init_system();
	Timer0Init();
	while(1)
	{
		smg_display();
		key_board();
		led_work();
		putout_v();
	}
}

onewire.c

/*
  程序说明: 单总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
  日    期: 2011-8-9
*/
#include "reg52.h"

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	t=t*12;
	while(t--);

}

//通过单总线向DS18B20写一个字节
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);
}

//从DS18B20读取一个字节
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;
}
unsigned int temper;
unsigned int temp;
unsigned int rd_temperature(void)
{
	int HBS;int LBS;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LBS=Read_DS18B20();
	HBS=Read_DS18B20();
	temper=HBS<<8;
	temper=temper|LBS;
	temper=temper*6.25;	  //两位小数

	temp=HBS<<4;
	temp=temp|(LBS>>4);	   //整数


	return temper;
	

}

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
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;  					// 0:应答,1:非应答
    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;    
}
void output(int dat)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned int rd_temperature(void);  //; ;

void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif

iic.h

#ifndef _IIC_H
#define _IIC_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); 
void output(int dat);
#endif

**

码字不容易,点个赞再走!!!!

**

版权声明:本文为CSDN博主「Miseñor」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_52465629/article/details/122731773

生成海报
点赞 0

Miseñor

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐