蓝桥杯-温度记录器

本文介绍了一个基于STC15F2K60S2单片机的温度记录器设计,该记录器能够按照用户设定的时间间隔采集温度,并利用DS18B20温度传感器和DS1302实时时钟模块实现数据记录与显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 温度记录器

 


/**********

温度记录器

**********/

#include <stc15f2k60s2.h>
#include "onewire.h"
#include "ds1302.h"
#define uint  unsigned int
#define uchar unsigned char 
	
uint  count;
uchar model,time,aa,i,xx;
uchar yi,er,san,si,wu,liu,qi,ba;
uint  wendu[10];
uchar shuma[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
void keyscan();
void display();
void jilu_wendu();
//
void allinit()
{
	P2=0x80;P0=0xff;
	P2=0xa0;P0=0x00;
	P2=0xc0;P0=0xff;
	P2=0xff;P0=0xff;
}
//
void Delay_Ms(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
	  for(y=845;y>0;y--);
}
//
void Timer0Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x28;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}
//
void main()
{
	xx = 0;
	time = 1;
	allinit();
	Timer0Init();
	while(1)
	{
		keyscan();
		display();
	}
}
//
void exter0() interrupt 1
{
	count++;
}
//
void keyscan()
{
	if(model==0)   //采集时间设置界面。
	{
		if(P33==0)
		{
			     if(time==1)  {time=5;}
			else if(time==5)  {time=30;}
			else if(time==30) {time=60;}
			else if(time==60) {time=1;}
			while(P33==0);
		}
		yi=11;er=11;san=11;
		si=11;wu=11;liu=10;
		qi=time/10;ba=time%10;
		
		if(P32==0) {model=1;while(P32==0);}
	}
	//
	else if(model==1)
	{
		EA  = 1;
		ET0 = 1;
		if(xx==0) {ds1302_write_time();xx=1;}   //查看是否为第一次写入时间。
		
		jilu_wendu();
		
		ds1302_read_time();
		yi=ds1302_time[2]/10;er=ds1302_time[2]%10;san=10;
		si=ds1302_time[1]/10;wu=ds1302_time[1]%10;liu=10;
		qi=ds1302_time[0]/10;ba=ds1302_time[0]%10;
		
		if(aa==10) {model=2;aa=0;}
	}
	else if(model==2)
	{
		if(P30==0) {model=0;while(P31==0);}
		if(count>=50)
		{
			P2=0x80;P0=0xfe;
			if(count>100) {count=0;}
		}
		else {P2=0x80;P0=0xff;}
		
		if(P31==0) {EA=0;ET0=0;count=0;i++;while(P31==0);}
		if(i==10) {i=0;}
		
		yi=i/10;er=i%10;san=11;
		si=11;wu=11;liu=11;
		qi=wendu[i]/10;ba=wendu[i]%10;
	}
}
//
void jilu_wendu()
{
	if(time==1)    //200 * 5 = 1000ms  = 1s
	{
		     if(aa==0&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==1&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==2&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==3&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==4&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==5&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==6&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==7&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==8&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==9&&count>=200) {wendu[aa]=get_wendu();count=0;aa++;}
	}
	//
	if(time==5)   //1000 * 5 = 5000ms = 5s
	{
		     if(aa==0&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==1&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==2&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==3&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==4&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==5&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==6&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==7&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==8&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==9&&count>=1000) {wendu[aa]=get_wendu();count=0;aa++;}
	}
	//
	if(time==30)    //6000 * 5 = 30000ms = 30s
	{
		     if(aa==0&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==1&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==2&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==3&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==4&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==5&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==6&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==7&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==8&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==9&&count>=6000) {wendu[aa]=get_wendu();count=0;aa++;}
	}
	//
	if(time==60)    //12000 * 5 = 60000ms = 60s
	{
		     if(aa==0&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==1&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==2&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==3&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==4&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==5&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==6&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==7&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==8&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
		else if(aa==9&&count>=12000) {wendu[aa]=get_wendu();count=0;aa++;}
	}
}
//
void display()
{
	P2=0xc0;
	P0=0x01;
	P2=0xff;
	P0=shuma[yi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x02;
	P2=0xff;
	P0=shuma[er];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x04;
	P2=0xff;
	P0=shuma[san];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x08;
	P2=0xff;
	P0=shuma[si];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x10;
	P2=0xff;
	P0=shuma[wu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x20;
	P2=0xff;
	P0=shuma[liu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x40;
	P2=0xff;
	P0=shuma[qi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x80;
	P2=0xff;
	P0=shuma[ba];
	Delay_Ms(1);
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include<STC15F2K60S2.H>
#include<intrins.h>
	
sbit SDA=P1^3;
sbit RST=P2^3;
sbit SCK=P1^7;

unsigned char read(unsigned char add);
void writebyte(unsigned char dat);
void write(unsigned char add,unsigned char dat);

extern unsigned char set_TIME[7];	//加入全局变量
extern unsigned char ds1302_time[7];

void ds1302_write_time();
void ds1302_read_time();

#endif

ds1302.c

#include "DS1302.h"

unsigned char code write_add[7]={0X80,0X82,0X84,0X86,0X88,0X8A,0X8C};

unsigned char code read_add[7]={0X81,0X83,0X85,0X87,0X89,0X8b,0X8d};

unsigned char set_TIME[7] = {50,59,23,26,2,2,19};

unsigned char ds1302_time[7];

unsigned char read(unsigned char add)
{
	unsigned char i,temp,dat1,dat2;
	SDA=0;
	SCK=0;
	SDA=1;
	writebyte(add);
	for(i=0;i<8;i++)
	{
		SCK=0;
		temp>>=1;
		if(RST) temp|=0x80;
		SCK=1;
	}
	
	RST=0;
	
	dat1 = temp/16;
	dat2 = temp%16;
	temp = dat1*10+dat2;
	
	return temp;
}

void writebyte(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		SCK=0;
		RST=dat&0x01;
		SCK=1;
		dat>>=1;
	}
}

void write(unsigned char add,unsigned char dat)
{
	unsigned char num;
	SDA=0;
	SCK=0;
	SDA=1;
	writebyte(add);
	num = ((dat/10)<<4) | (dat%10);
	writebyte(num);
	SDA=0;
}

//************************
void ds1302_write_time()
{
	unsigned char i;
	write(0x8e,0x00);
	
	for(i=0;i<8;i++)
	{
		write(write_add[i],set_TIME[i]);
	}
	
	write(0x8e,0x80);
}
//
void ds1302_read_time()
{
	unsigned char i;
	write(0x8e,0x00);
	
	for(i=0;i<8;i++)
	{
		ds1302_time[i] = read(read_add[i]);
	}
	
	write(0x8e,0x80);
}
//************************

onewire.h

#ifndef _ONEWIRE_H
#define _ONEWIRE_H

#include<STC15F2K60S2.H>
#include<intrins.h>

#define OW_SKIP_ROM 0xcc
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xbe

//IC引脚定义
sbit DQ = P1^4;

//函数声明
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
bit Init_DS18B20(void);
unsigned char Read_DS18B20(void);

unsigned int get_wendu();
#endif

onewire.c

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

#include "onewire.h"

//单总线延时函数
void Delay_OneWire(unsigned int t)
{
	unsigned char i;
  while(t--)
		for(i=0;i<8;i++);
}

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

//通过单总线向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;
}

//******************************
unsigned int get_wendu()
{
	unsigned int  date;
	unsigned char low,high;
	
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low = Read_DS18B20();
	high	= Read_DS18B20();
	
	date = high;
	date <<= 8;
	date = date | low;
	date = date*0.0625;
	return date;
}

个人见解,感谢阅读。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值