蓝桥杯——第六届省赛_温度器

一、使用到的模块

DS1302,DS18B20,数码管,按键,LED

题目要求

蓝桥杯——第六届省赛_温度器_第1张图片

二、代码部分

实验平台:CT107D
实验芯片:stc15f2k60s2
实验现象:完成题目要求
代码如下

onewire.c

#include "stc15f2k60s2.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 duwendu()
{
     
	 unsigned char high,low;
	 unsigned int temp;
	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0x44);
	 Delay_OneWire(200);

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

	 low =	Read_DS18B20();
	 high = Read_DS18B20();
	 temp = high<<8;
	 temp |= low;
	 temp = temp*0.0625;
	 return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char rd_temperature(void);  //; ;
unsigned int read_temp();

#endif

DS1302.c

#include 
#include 

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位												

void Write_Ds1302(unsigned  char temp) 
{
     
	unsigned char i;
	for (i=0;i<8;i++)     	
	{
      
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

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; 
}

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 set_sfm(unsigned char shi,unsigned char fen,unsigned char miao)
{
     
	Write_Ds1302_Byte(0x8e,0x00);//关闭保护位
	Write_Ds1302_Byte(0x80,(miao/10)*16+miao%10);//设置秒
	Write_Ds1302_Byte(0x82,(fen/10)*16+fen%10);//设置分
	Write_Ds1302_Byte(0x84,(shi/10)*16+shi%10);//设置小时
	Write_Ds1302_Byte(0x8e,0x80);//打开保护位
}

DS1302.h

#ifndef __DS1302_H
#define __DS1302_H

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 set_sfm(unsigned char shi,unsigned char fen,unsigned char miao);
#endif

main.c

#include
#include
#include

#define uchar unsigned char
#define uint unsigned int

sbit buzz = P0^6;

uchar code duan[]={
     0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};//定义段码数组
uchar disbuff[8];//定义显示数字
uchar temp_val[10];
uchar trg,cont;
uchar gap=1;
uchar jishu=0;
uchar shi,fen,miao;
uchar mode = 1;
count_temp = 0;
uchar temp;
bit shot_flag = 1;
bit read_temp_flag = 0;
bit led_flag = 0;
bit rec_temp = 0;
bit open_flag = 0;


void closebuzz()
{
     
	P2 = 0xa0;
	buzz = 0;
	P2 = 0x00;
}
void keyscan()
{
     
	uchar readdata = P3^0xff;
	trg = readdata&(readdata^cont);
	cont = readdata;	
}
void keyfun()
{
     
	 if(trg == 0x01&&mode == 3)
	 mode =1;

	 if(trg == 0x02)
	 {
     
	 	led_flag = 0;
	 }

	 if(trg == 0x04&&mode == 1)
	 {
     
	 	mode = 2;
		open_flag = 1;
		count_temp = 0;
	 }

	if(trg == 0x08 && mode == 1)
	 {
     
		if(jishu%4 == 0)
			gap =1; 
		if(jishu%4 == 1)
			gap =5; 
		if(jishu%4 == 2)
			gap =30; 
		if(jishu%4 == 3)
			gap =60;
		jishu++;
		jishu=jishu%4;
	 }

}

void shownum()//显示数字函数
{
     
	if(mode == 1)
	{
     
	disbuff[0]=10;
	disbuff[1]=10;
	disbuff[2]=10;
	disbuff[3]=10;
	disbuff[4]=10;
	disbuff[5]=11;
	disbuff[6]=gap/10;
	disbuff[7]=gap%10;
	}

	if(mode == 2)
	{
     
		if(shot_flag == 1)
		{
     
			disbuff[0]=shi/10;
			disbuff[1]=shi%10;
			disbuff[2]=11;
			disbuff[3]=fen/10;
			disbuff[4]=fen%10;
			disbuff[5]=11;
			disbuff[6]=miao/10;
			disbuff[7]=miao%10;
		}

		if(shot_flag == 0)
		{
     
			disbuff[0]=shi/10;
			disbuff[1]=shi%10;
			disbuff[2]=10;
			disbuff[3]=fen/10;
			disbuff[4]=fen%10;
			disbuff[5]=10;
			disbuff[6]=miao/10;
			disbuff[7]=miao%10;
		}
	}
	if(mode == 3)
		{
     
		disbuff[0]=11;
		disbuff[1]=count_temp/10;
		disbuff[2]=count_temp%10;
		disbuff[3]=10;
		disbuff[4]=10;
		disbuff[5]=11;
		disbuff[6]=temp_val[count_temp]/10;
		disbuff[7]=temp_val[count_temp]%10;
		}
}

void display()//数码管扫描函数
{
     
	static uchar index = 0;
	P2 = P2&0x1f|0xe0;
	P0 = 0xff;
	P2 = P2&0x1f|0x00;

	P2 = P2&0x1f|0xc0;
	P0 = 1<<index;
	P2 = P2&0x1f|0x00;

	P2 = P2&0x1f|0xe0;
	P0 = ~duan[disbuff[index]];
	P2 = P2&0x1f|0x00;

	index++;
	index &= 0x07;

}

void Timer0Init(void)		//2毫秒@12.000MHz
{
     
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x40;		//设置定时初值
	TH0 = 0xA2;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
	EA = 1 ; 
}

void time0() interrupt 1
{
     
	static uint count=0;
	static uchar sec = 0;
	if(++count == 500)
	{
     
		shot_flag=~shot_flag;
		count = 0;
		read_temp_flag = 1;
		if(open_flag == 0&&mode == 3)
		if(++count_temp == 10)
		count_temp = 0 ;
		if(open_flag)		
		sec++;	
	}
	
	if(sec >= gap&&open_flag)
	{
     
		sec = 0;
		if(++count_temp==10)
		{
     
		count_temp = 0;
		open_flag = 0;	
		led_flag = 1;
		mode = 3;
		}
	}	

	display();
	keyscan();
	keyfun();		
}

void set_led()
{
     
	if(led_flag == 1)
	{
     
		if(shot_flag == 1)
		{
     
		P2 = P2&0x1f|0x80;
		P0 = ~0x01;
		P2 = P2&0x1f|0x00;	
		}

		if(shot_flag == 0)
		{
     
		P2 = P2&0x1f|0x80;
		P0 = ~0x00;
		P2 = P2&0x1f|0x00;	
		}	
	}

	if(led_flag == 0)
	{
     
	P2 = P2&0x1f|0x80;
	P0 = ~0x00;
	P2 = P2&0x1f|0x00;	
	}
}
	
void main()
{
     
	uchar shi_tp,fen_tp,miao_tp;
	closebuzz();
	Timer0Init();
	set_sfm(23,59,50);
	while(1)
	{
     

		shi_tp=Read_Ds1302_Byte(0x85);
		fen_tp=Read_Ds1302_Byte(0x83);
		miao_tp=Read_Ds1302_Byte(0x81);

		shi = shi_tp/16*10+shi_tp%16;
		fen = fen_tp/16*10+fen_tp%16;
		miao = miao_tp/16*10+miao_tp%16;

		if(read_temp_flag == 1&&open_flag == 1)
		{
     
			temp = duwendu();
			read_temp_flag = 0;
			temp_val[count_temp]=temp;
		}

		shownum();
		set_led();
	}
}

你可能感兴趣的:(蓝桥杯,单片机)