STC15系列单片机-环境监控设备


main.c

/**
 *	产品名称:环境监控设备
 *	产品定义:
 *	(1)单片机每隔1秒通过DS18B20温度传感器采集一次温度。
 *	(2)单片机每隔200毫秒采集一次DS1302时钟芯片的时间。
 *	(3)通过数码管实时显示时间,格式为日-小时-分-秒。
 *	(4)通过串口向上位机发送格式提示字符串,设置温度上限和下限;
 *		格式为xx-yy
 *	(5)可以通过串口设置温度上限和下限。
 *	(6)单片机将每次采集到的温度实时通过串口传送给上位机;
 *		如果温度超过阈值,都将给出警告提示。
 *	版本:debug
 *	完成时间:2015.4.5 17:00
 *	作者:宁静致远
 */

#include <stc15f2k60s2.h>
#include <stdio.h>
#include "stdint.h"
#include "DS1302.h"
#include "DS18B20.h"
#include "UART.h"
#include "digitalTube.h"
#include "timer.h"
//#include "main.h"

volatile bit flag1s = 0, flag200ms = 0;

uint8_t serialBuf[10];
volatile uint8_t front = 0, rear = 0;

void main() {
	int16_t temperature;
	uint8_t intPart, decPart;
	uint8_t maxTempInt = 21, maxTempDec = 0;
	uint8_t minTempInt = 20, minTempDec = 0;

	uint8_t intPart0, intPart1, decPart1;
	//bit tempSign;
	sTime_t stime;
	uint8_t preSec;	
	
	configTmr0(2);
	configUART(9600);	//设置正确的波特率
	startDS18B20();	//!!
	DS1302init();

	ES = 0;
	TI = 1;
	printf("input format: xx-yy\n");
	while (TI == 0);
	TI = 0;
	ES = 1;

	while (1) {
		if (flag1s) {	//温度每隔1s采样一次
			flag1s = 0;
			getDS18B20temperature(&temperature);
			startDS18B20();	//!!
			intPart = getIntPart(temperature);
			decPart = getDecPart(temperature);
			
			intPart1 = intPart / 10 + '0';
			intPart0 = intPart % 10 + '0';
			decPart1 = decPart  + '0';

			if (intPart > maxTempInt || (intPart == maxTempInt && decPart > maxTempDec)) {
				ES = 0;
				TI = 1;
				printf("warning: too high\n");
				while (TI == 0);
				TI = 0;
				ES = 1;
			} else if (intPart < minTempInt || (intPart == minTempInt && decPart < minTempDec)) {
				ES = 0;
				TI = 1;
				printf("warning: too low\n");
				while (TI == 0);
				TI = 0;
				ES = 1;	
			} else {
				ES = 0;
				TI = 1;
				//printf("The temp is %u %u\n", intPart, decPart);
				//printf("decPart: %hu\n", decPart);
				printf("The temperature is %c%c.%c\n", intPart1, intPart0, decPart1);
				while (TI == 0);
				TI = 0;
				ES = 1;
			}
		}
		if (flag200ms) {	//时间每隔200ms采样一次
			flag200ms = 0;
			getRealTime(&stime);
			
			if (stime.sec != preSec) {	//BCD码!为16进制!
				dspBuf[0] = stime.day >> 4;
				dspBuf[1] = stime.day & 0x0F;
				dspBuf[2] = stime.hour >> 4;
				dspBuf[3] = stime.hour & 0x0F;
				dspBuf[4] = stime.minute >> 4;
				dspBuf[5] = stime.minute & 0x0F;
				dspBuf[6] = stime.sec >> 4;
				dspBuf[7] = stime.sec & 0x0F;
				preSec = stime.sec;
			}
		}
		if (rear == 5) {
			rear = 0;
			minTempInt = (serialBuf[0] - '0') * 10 + 
						serialBuf[1] - '0';
			maxTempInt = (serialBuf[3] - '0') * 10 + 
						serialBuf[4] - '0';
		}

	}
}

void UARTreceive() {
	serialBuf[rear] = SBUF;
	if (rear == 9)
		rear = 0;
	else 
		r
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值