📄 main.c
字号:
/*------------------------------------------------------------------------------
main.C:
------------------------------------------------------------------------------*/
static char code menu[] =
"\n"
"+******************** REMOTE THERMOMETER **********************+\n"
"| This program is a simple Thermometer. It is based on the |\n"
"| STC89C52RC CPU. coded by tary. |\n"
"+ command -+ syntax -----+ function ---------------------------+\n"
"| ID | I | display ID |\n"
"| Read | R | read |\n"
"| Display | D | display current measurement values |\n"
"| Time | T hh:mm:ss | set time |\n"
"+----------+-------------+-------------------------------------+\n";
#include <stdio.h> /* standard I/O .h-file */
#include <stdlib.h> /* standard library .h-file */
#include <reg52.h> /* special function register 8052 */
#include <ctype.h> /* character functions */
#include "type.h"
#include "timer.h"
#include "measure.h" /* global project definition file */
#include "ds18b20.h"
#include "getline.h"
#ifdef MONITOR51 /* Debugging with Monitor-51 needs */
char code reserve[3] _at_ 0x23; /* space for serial interrupt if */
#endif /* Stop Exection with Serial Intr. */
/* is enabled */
#define TEMPER_STATE_NONE 0
#define TEMPER_STATE_WORKING 1
#define TEMPER_STATE_ID_WORKING 2
#define TEMPER_STATE_DONE 3
static uchar temper_state = TEMPER_STATE_NONE; /* measurement process state */
#define UART_STATE_INIT 0
#define UART_STATE_RECEIVE 1
#define UART_STATE_CMD_PARSE 2
#define UART_STATE_CMD_DONE 3
#define UART_STATE_WAIT_ESC 4
#define UART_STATE_DONE 5
static uchar uart_state = UART_STATE_INIT;
uchar cmdidx, cmdbuf[15]; /* command input buffer */
char code ERROR[] = "\n*** 错误: %s\n"; /* ERROR message string in code */
data DS18B20_ID id;
data DS18B20_SCRATCH scrch;
void init_temper(void) {
if(!DS18B20_DeviceInit(DS18B20_PRECISION_9bit)) {
while (true)
printf(ERROR, "INIT TEMPER");
}
return;
}
bool start_temper(void) {
if (!DS18B20_GetScratchStart()) {
printf(ERROR, "START TEMPER");
return false;
}
temper_state = TEMPER_STATE_WORKING;
return true;
}
bool check_temper(void) {
bool b;
b = false;
if ((temper_state == TEMPER_STATE_ID_WORKING)) {
b = true;
} else if ((temper_state == TEMPER_STATE_WORKING)) { /* process measurement */
b = DS18B20_GetScratchReady();
}
return b;
}
void do_temper(void) {
if ((temper_state == TEMPER_STATE_ID_WORKING)) {
if(DS18B20_GetDeviceID(&id)) {
uchar i;
printf("\n设备ID 为: ");
for (i = 0; i < DS18B20_DEVICE_ID_LENGTH; i++)
printf("%02bX ", id.buf[i]);
printf("\n");
} else {
printf(ERROR, "GET ID");
}
temper_state = TEMPER_STATE_NONE;
uart_state = UART_STATE_INIT;
} else if ((temper_state == TEMPER_STATE_WORKING)) { /* process measurement */
if (DS18B20_GetScratch(&scrch)) {
DS18B20_GetTemperature(current.temper, &scrch);
measure_display(current); /* display values */
if (uart_state != UART_STATE_WAIT_ESC)
printf("\n");
} else {
printf(ERROR, "GET SCRATCH");
return;
}
temper_state = TEMPER_STATE_NONE;
if (uart_state != UART_STATE_WAIT_ESC)
uart_state = UART_STATE_INIT;
}
}
void init_uart(void) {
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xF3; /* TH1: reload value for 2400 baud @ 12MHz */
PCON |= 0x80; /* set to 4800 Baud @ 12 MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
}
bool check_uart(void) {
if (uart_state == UART_STATE_INIT) {
printf("\n输入命令: ");
uart_state = UART_STATE_RECEIVE;
} else if (uart_state == UART_STATE_RECEIVE) {
return RI; // if there is a char
} else if (uart_state == UART_STATE_CMD_PARSE) {
return true;
} else if (uart_state == UART_STATE_WAIT_ESC) {
if (temper_state == TEMPER_STATE_NONE)
start_temper();
return RI;
}
return false;
}
void do_uart(void) {
uchar i;
if (uart_state == UART_STATE_RECEIVE || uart_state == UART_STATE_WAIT_ESC) {
getline(&cmdbuf[0], sizeof(cmdbuf)); /* input command line */
for (i = 0; cmdbuf[i] != 0; i++) { /* convert to upper characters */
cmdbuf[i] = toupper(cmdbuf[i]);
}
for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks */
cmdidx = i;
if (uart_state != UART_STATE_WAIT_ESC) {
uart_state = UART_STATE_CMD_PARSE;
return;
}
}
if (uart_state == UART_STATE_WAIT_ESC && cmdbuf[cmdidx] == ESCAPE) {
printf("\n");
temper_state = TEMPER_STATE_NONE;
uart_state = UART_STATE_INIT;
} else if (uart_state == UART_STATE_CMD_PARSE) {
switch (cmdbuf[cmdidx]) { /* proceed to command function */
case 'R': /* Read circular Buffer */
if (start_temper()) {
uart_state = UART_STATE_CMD_DONE;
} else {
uart_state = UART_STATE_INIT;
}
break;
case 'I':
temper_state = TEMPER_STATE_ID_WORKING;
uart_state = UART_STATE_CMD_DONE;
break;
case 'T': /* Enter Current Time */
set_time(&cmdbuf[cmdidx + 1]);
uart_state = UART_STATE_INIT;
break;
case 'D': /* Display Command */
printf("\n连续显示当前测量值: (按ESC中断)\n");
uart_state = UART_STATE_WAIT_ESC;
break;
default: /* Error Handling */
printf(ERROR, "UNKNOWN COMMAND");
printf("char=0x%02bX idx=%bU\n", cmdbuf[cmdidx], cmdidx);
printf(menu); /* display command menu */
uart_state = UART_STATE_INIT;
break;
}
cmdbuf[0] = '\0';
}
}
/*************************** MAIN PROGRAM ***************************/
void main(void) { /* main entry for program */
init_uart();
init_temper();
init_timer0();
ENABLE(); /* global interrupt enable */
printf(menu); /* display command menu */
while (true) { /* loop forever */
if (check_timer0())
do_timer0();
if (check_temper())
do_temper();
if (check_uart())
do_uart();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -