第五屆藍橋杯單片機設計與開發項目省賽試題

第五屆藍橋杯單片機設計與開發項目省賽試題:
“簡易溫度採集與控制裝置”設計任務書
在這裏插入圖片描述在這裏插入圖片描述

  • ds18b20.h
#ifndef __DS18B20_H
#define __DS18B20_H
//單總線延時函數
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned char getwendu();
#endif
  • ds18b20.c

#include<reg52.h>
#include<ds18b20.h>
sbit DQ = P1^4;  //單總線接口

//單總線延時函數
void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0; i<8; i++);
	}
}

//通過單總線向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 char getwendu()
{
	unsigned char low,high,temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low=Read_DS18B20();
	high=Read_DS18B20();
	
	temp=high<<4;
	temp=temp|(low>>4);
	
	return temp;
}
  • 主函數
#include<stc15f2k60s2.h>
#include<ds18b20.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;

int yi,er,san,si,wu,liu,qi,ba,a,aa;
int shezhi=0,t=0,tt=0,flag=0,num,biaozhi=0,flagg; //flag 爲閃爍標誌位
uchar code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar temp;int jibie;
uchar tmax=30,tmin=20;//溫度上下限
void shangdian();
void delay1ms(int ms);
void display1();
void display2();
void juzhen();
void Timer0Init(void);		//1毫秒@11.0592MHz
main()
{
	shangdian();

   Timer0Init();
	while(1) 
	{ juzhen();
	temp=getwendu();
		
		while(temp==85)
		{
			temp=getwendu();
		}
			if(flagg==1) //進入設置界面
			{  TR0=0;
	      if(biaozhi==1)
					er=num;
				else if(biaozhi==2)
					san=num;
				else if(biaozhi==3)
					qi=num;
				else if(biaozhi==4)
				{	ba=num;
					tmax=er*10+san;
					tmin=qi*10+ba;
					if(tmax<tmin)
					{
					P2=0X80;P0=~(0X02);
					}
					else
					{	P2=0X80;P0=0xff;
					
					}
				
				}
			
			}
    if(flagg==0) //退出設置界面
		{ TR0=1;
			yi=10;er=jibie;san=10;si=11;
	wu=11;liu=11;qi=temp/10;ba=temp%10;
			if(temp<tmin)//溫度區間
		{ jibie=0;	
			tt=800;	TR0 = 1;
			P2=0XA0;P0=0X00;
		}
		else if((tmin<=temp)&&(temp<=tmax))
		{jibie=1;
     tt=400;TR0 = 1;
		P2=0XA0;P0=0X00;
			
		}
		else if(temp>tmax)
		{	jibie=2;
			tt=200;TR0 = 1;
    	P2=0XA0;P0=0X10;
     }}
		
		display1();
    display2();
	
	}

}
void Timer0Init(void)		//1毫秒@11.0592MHz 
{
	AUXR |= 0x80;		//定時器時鐘1T模式
	TMOD &= 0xF0;		//設置定時器模式
	TL0 = 0xCD;		//設置定時初值
	TH0 = 0xD4;		//設置定時初值
	TF0 = 0;		//清除TF0標誌
	TR0 = 0;		//定時器0開始計時
	ET0=1;
	EA=1;
}
void time0() interrupt 1
{
	t++;
	if(t==tt)
	{ 
		t=0;
		if(flag==0)
		{
		P2=0x80;
		P0=0xfe;
		flag=1;
		}
		else if(flag==1)
	{
		P2=0x80;
		P0=0xff;
		flag=0;
	}
	}
	if(t==900)//防止從800-400-200之間有漏洞
	{t=0;}	
}
void shangdian()
{
	P2=0XA0;P0=0X00;
	P2=0X80;P0=0XFF;
	P2=0XC0;P0=0XFF;
	P2=0XE0;P0=0XFF;
}
void delay1ms(int ms)
{
	int i;
	for(;ms>0;ms--)
	{
		for(i=845;i>0;i--)  ;
	}
}
void display1()
{
	P2=0XC0;P0=0X01;
	P2=0XE0;P0=smg[yi];
	delay1ms(1);
	P2=0XC0;P0=0X02;
	P2=0XE0;P0=smg[er];
	delay1ms(1);
	P2=0XC0;P0=0X04;
	P2=0XE0;P0=smg[san];
	delay1ms(1);
	P2=0XC0;P0=0X08;
	P2=0XE0;P0=smg[si];
	delay1ms(1);
}
void display2()
{
	P2=0XC0;P0=0X10;
	P2=0XE0;P0=smg[wu];
	delay1ms(1);
	P2=0XC0;P0=0X20;
	P2=0XE0;P0=smg[liu];
	delay1ms(1);
	P2=0XC0;P0=0X40;
	P2=0XE0;P0=smg[qi];
	delay1ms(1);
	P2=0XC0;P0=0X80;
	P2=0XE0;P0=smg[ba];
	delay1ms(1);
  P2=0XC0;P0=0X80;
	P2=0XE0;P0=0xff;
}
void juzhen()
{ uchar juzhen;
	P44=0;P42=1;P3=0X7F;
	juzhen=P3;
	juzhen=juzhen&0x0f;
	if(juzhen!=0x0f)
	{
		delay1ms(5);
	  juzhen=P3;
	  juzhen=juzhen&0x0f;		
		if(juzhen!=0x0f)
		{
			juzhen=P3;
			switch(juzhen)
			{
				case 0x7e:num=0;biaozhi++;break;
		     	case 0x7d:num=3;biaozhi++;break;
				case 0x7b:num=6;biaozhi++;break;
				case 0x77:num=9;biaozhi++;break;

			}
			while(juzhen!=0x0f)
			{
			juzhen=P3;
	    juzhen=juzhen&0x0f;
			}
		}
	}
	P44=1;P42=0;P3=0XbF;
	juzhen=P3;
	juzhen=juzhen&0x0f;
	if(juzhen!=0x0f)
	{
		delay1ms(5);
	  juzhen=P3;
	  juzhen=juzhen&0x0f;		
		if(juzhen!=0x0f)
		{
			juzhen=P3;
			switch(juzhen)
			{
				case 0xbe:num=1;biaozhi++;break;
		  	    case 0xbd:num=4;biaozhi++;break;
				case 0xbb:num=7;biaozhi++;break;
				case 0xb7:
					if(flagg==0) {flagg=1;
					yi=10;er=san=si=wu=11;liu=10;qi=ba=11;
					}
				else if(flagg==1) 
				{flagg=0; 					
				biaozhi=0;
				}
             break;//清除
			}
			while(juzhen!=0x0f)
			{
			juzhen=P3;
	    juzhen=juzhen&0x0f;
			}
		}
	}
	P44=1;P42=1;P3=0XdF;
	juzhen=P3;
	juzhen=juzhen&0x0f;
	if(juzhen!=0x0f)
	{
		delay1ms(5);
	  juzhen=P3;
	  juzhen=juzhen&0x0f;		
		if(juzhen!=0x0f)
		{
			juzhen=P3;
			switch(juzhen)
			{
				case 0xde:num=2;biaozhi++;break;
		  	case 0xdd:num=5;biaozhi++;break;
				case 0xdb:num=8;biaozhi++;break;
				case 0xd7:
				if(biaozhi==1)
				{
				er=11;biaozhi=0;
				}
				else if(biaozhi==2)
				{
				san=11;biaozhi=1;num=er;
				}
				else if(biaozhi==3)
				{
				qi=11;biaozhi=2;num=san;
				}
				else if(biaozhi==4)
				{
				ba=11;biaozhi=3;num=qi;
				}
		   break;
			}
			while(juzhen!=0x0f)
			{
			juzhen=P3;
	    juzhen=juzhen&0x0f;
			}
		}
	}	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章