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