serialicon8051_next/src/main.c
2024-11-28 00:54:11 +08:00

250 lines
5.2 KiB
C

#include <8052.h>
#include "serial_command.h"
#include "temp.h"
typedef unsigned int u16;
typedef unsigned char u8;
u8 cmdCode = 0;
u8 lightCmdCode = 0x0;
u8 tempCmdCode = 0x0;
u8 newCmdFlag = 0;
u16 tim0countAll = 2000;
u16 tim0count = 0;
u8 tim0flag = 0;
char g_mode = 0x01;
char g_bodyMode = 0x00;
#define person = P1_0;
char g_gasMode = 0x00;
#define gasState = P1_1;
char FAS_LIGHT_BLINK = 0x50;
#define beep = P1_5;
u8 m_flag = 0;
// LcdDisplay
#define LSA P2_2
#define LSB P2_3
#define LSC P2_4
u8 DisplayData[8];
// u8 DisplayData[8] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07};
u8 LcdSegment[10] = {
0x3F,
0x06,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x07,
0x7F,
0x6F
};
#define CLASS_3
#define CLASS_4
void delay(u16 i) {
while (i--);
}
void Uart1Send(u16 sendData) {
TI = 0;
SBUF = (char) (sendData >> 8);
while (TI == 0) {
}
TI = 0;
SBUF = (char) (sendData & 0xFF);
while (TI == 0) {
}
TI = 0;
delay(50);
}
void UsartInit(void) {
SCON = 0x50;
TMOD = 0x22;
PCON = 0x80;
TH1 = 0xF4;
TL1 = 0xF4;
ES = 1;
EA = 1;
TR1 = 1;
PS = 1;
}
void Usart(void) __interrupt(4) {
cmdCode = SBUF;
newCmdFlag = 1;
RI = 0;
}
void ProcessCommand(void) {
if (newCmdFlag) {
#ifdef CLASS_3
u16 recvData;
recvData = (cmdCode << 8) + cmdCode;
Uart1Send(recvData);
#endif
newCmdFlag = 0;
switch (cmdCode) {
case CTRL_CODE_CYCLE_500ms:
tim0countAll = 2000;
break;
case CTRL_CODE_CYCLE_1s:
tim0countAll = 4000;
break;
case CTRL_CODE_CYCLE_2s:
tim0countAll = 8000;
break;
case CTRL_CODE_CYCLE_5s:
tim0countAll = 20000;
break;
case CTRL_CODE_TEMP_Enable:
case CTRL_CODE_TEMP_Disable:
tempCmdCode = cmdCode;
break;
case CTRL_CODE_BODY_Disable:
case CTRL_CODE_BODY_Buzzer:
case CTRL_CODE_BODY_Light:
g_bodyMode = cmdCode;
break;
case CTRL_CODE_SYSMODE_Disable:
case CTRL_CODE_SYSMODE_Telemetry:
case CTRL_CODE_SYSMODE_Security:
g_mode = cmdCode;
break;
case CTRL_CODE_GAS_Disable:
case CTRL_CODE_GAS_State:
case CTRL_CODE_GAS_Data:
g_gasMode = cmdCode;
break;
default:
break;
}
}
}
void LcdDisplay(void) {
u8 i;
for (i = 0; i < 6; i++) {
switch (i) {
case 0:
LSA = 0;
LSB = 0;
LSC = 0;
break;
case 1:
LSA = 1;
LSB = 0;
LSC = 0;
break;
case 2:
LSA = 0;
LSB = 1;
LSC = 0;
break;
case 3:
LSA = 1;
LSB = 1;
LSC = 0;
break;
case 4:
LSA = 0;
LSB = 0;
LSC = 1;
break;
case 5:
LSA = 1;
LSB = 0;
LSC = 1;
break;
}
P0 = DisplayData[5 - i];
delay(100);
P0 = 0x00;
}
}
void TempProc(int temp) {
float tp;
u8 i = 0;
if (temp < 0) {
DisplayData[0] = 0x40;
temp = temp - 1;
temp = ~temp;
tp = temp;
temp = tp * 0.0625 * 100 + 0.5;
} else {
DisplayData[0] = 0x00;
tp = temp;
temp = tp * 0.0625 * 100 + 0.5;
}
DisplayData[1] = LcdSegment[temp / 10000];
DisplayData[2] = LcdSegment[temp % 10000 / 1000];
DisplayData[3] = LcdSegment[temp % 1000 / 100] | 0x80;
DisplayData[4] = LcdSegment[temp % 100 / 10];
DisplayData[5] = LcdSegment[temp % 10];
}
void ProcessTempCmd(void) {
u16 t_send = 0, tc = 0, td = 0;
int temp;
#ifdef CLASS_4
tempCmdCode = CTRL_CODE_TEMP_Enable;
#endif
if (tempCmdCode == CTRL_CODE_TEMP_Enable) {
EA = 0;
temp = Ds18b20ReadTemp();
EA = 1;
tc = MCU_CMD_TEMP << 12;
td = (u16) temp & 0xFFF;
//t_send = td + tc;
t_send = temp;
Uart1Send(t_send);
TempProc(temp);
}
}
int ProcessBodyCmd(void) {
return 0;
}
void ProcessLightCmd(void) {
}
int ProcessGasCmd(void) {
return 0;
}
void main(void) {
int gasFlag, bodyFlag;
UsartInit();
while (1) {
ProcessCommand();
#ifdef CLASS_4
ProcessTempCmd();
LcdDisplay();
#endif
if (tim0flag) {
tim0flag = 0;
if (g_mode == CTRL_CODE_SYSMODE_Security) {
bodyFlag = ProcessBodyCmd();
}
if ((g_mode == CTRL_CODE_SYSMODE_Security) || (g_mode == CTRL_CODE_SYSMODE_Telemetry)) {
gasFlag = ProcessGasCmd();
}
ProcessLightCmd();
if (tempCmdCode != CTRL_CODE_TEMP_Disable) {
ProcessTempCmd();
}
}
}
}