首页 分享 温度记录器设计

温度记录器设计

来源:花匠小妙招 时间:2024-12-26 11:45

 温度记录器

/**********

温度记录器

**********/

#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

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;

}

个人见解,感谢阅读。

相关知识

土壤温湿度监测
鲜花保鲜冷库设计的温度要求
基于PLC大棚温度自动调节设计
基于花房的温度湿度光照控制系统设计
基于单片机的花房温度、光照控制系统设计
ECO D3植物生理生态监测系统
基于单片机花房温度控制系统的设计与实现
基于单片机的花房温度、光照控制系统设计.pdf
基于单片机的智能温度监测系统设计(电路图+程序)
土壤湿度监测仪是什么

网址: 温度记录器设计 https://www.huajiangbk.com/newsview1303818.html

所属分类:花卉
上一篇: 冷水茭白种植技术要点
下一篇: 高温天气养花,暴晒、施浓肥、乱浇

推荐分享