feat: ppt6

This commit is contained in:
Timothy Yin 2024-11-28 09:38:37 +08:00
parent c49d40b8b9
commit 216ae7930c

View File

@ -3,6 +3,8 @@
#include "ds18b20.h" #include "ds18b20.h"
// #define YELLOW_BOARD
typedef unsigned int u16; typedef unsigned int u16;
typedef unsigned char u8; typedef unsigned char u8;
@ -15,14 +17,19 @@ u16 tim0countAll = 2000;
u16 tim0count = 0; u16 tim0count = 0;
u8 tim0flag = 0; u8 tim0flag = 0;
#define person P1_0 #define PERSON P1_0
#define gasState P1_1 #define GAS_STATE P1_1
#define beep P1_5
#ifdef YELLOW_BOARD
#define BEEP P1_5
#else
#define BEEP P1_6
#endif
char g_mode = 0x01; char g_mode = 0x01;
char g_bodyMode = 0x00; char g_bodyMode = 0x00;
char g_gasMode = 0x00; char g_gasMode = 0x00;
char FAS_LIGHT_BLINK = 0x50; char GAS_LIGHT_BLINK = 0x50;
u8 m_flag = 0; u8 m_flag = 0;
@ -48,23 +55,26 @@ u8 LcdSegment[10] = {
// #define CLASS_3 // #define CLASS_3
// #define CLASS_4 // #define CLASS_4
void delay(u16 i) { void delay(u16 i)
{
while (i--); while (i--);
} }
void Uart1Send(u16 sendData) { void Uart1Send(u16 sendData)
{
TI = 0; TI = 0;
SBUF = (char) (sendData >> 8); SBUF = (char)(sendData >> 8);
while (TI == 0); while (TI == 0);
TI = 0; TI = 0;
SBUF = (char) (sendData & 0xFF); SBUF = (char)(sendData & 0xFF);
while (TI == 0); while (TI == 0);
TI = 0; TI = 0;
delay(50); delay(50);
} }
void UsartInit(void) { void UsartInit(void)
{
SCON = 0x50; SCON = 0x50;
TMOD = 0x22; TMOD = 0x22;
PCON = 0x80; PCON = 0x80;
@ -89,91 +99,104 @@ void Usart_ISR(void) __interrupt(4) {
void Timer0_ISR(void) __interrupt(1) { void Timer0_ISR(void) __interrupt(1) {
tim0count++; tim0count++;
if (tim0count >= tim0countAll) { if (tim0count >= tim0countAll)
{
tim0count = 0; tim0count = 0;
tim0flag = 1; tim0flag = 1;
} }
} }
void ProcessCommand(void) { void beep(int duration)
if (newCmdFlag) { {
BEEP = ~BEEP;
delay(duration);
}
void ProcessCommand(void)
{
if (newCmdFlag)
{
#ifdef CLASS_3 #ifdef CLASS_3
u16 recv_data = (cmdCode << 8) + cmdCode; u16 recv_data = (cmdCode << 8) + cmdCode;
Uart1Send(recv_data); Uart1Send(recv_data);
#endif #endif
newCmdFlag = 0; newCmdFlag = 0;
switch (cmdCode) { switch (cmdCode)
case CTRL_CODE_CYCLE_500ms: {
tim0countAll = 2000; case CTRL_CODE_CYCLE_500ms:
break; tim0countAll = 2000;
case CTRL_CODE_CYCLE_1s: break;
tim0countAll = 4000; case CTRL_CODE_CYCLE_1s:
break; tim0countAll = 4000;
case CTRL_CODE_CYCLE_2s: break;
tim0countAll = 8000; case CTRL_CODE_CYCLE_2s:
break; tim0countAll = 8000;
case CTRL_CODE_CYCLE_5s: break;
tim0countAll = 20000; case CTRL_CODE_CYCLE_5s:
break; tim0countAll = 20000;
case CTRL_CODE_TEMP_Enable: break;
case CTRL_CODE_TEMP_Disable: case CTRL_CODE_TEMP_Enable:
tempCmdCode = cmdCode; case CTRL_CODE_TEMP_Disable:
break; tempCmdCode = cmdCode;
case CTRL_CODE_BODY_Disable: break;
case CTRL_CODE_BODY_Buzzer: case CTRL_CODE_BODY_Disable:
case CTRL_CODE_BODY_Light: case CTRL_CODE_BODY_Buzzer:
g_bodyMode = cmdCode; case CTRL_CODE_BODY_Light:
break; g_bodyMode = cmdCode;
case CTRL_CODE_SYSMODE_Disable: break;
case CTRL_CODE_SYSMODE_Telemetry: case CTRL_CODE_SYSMODE_Disable:
case CTRL_CODE_SYSMODE_Security: case CTRL_CODE_SYSMODE_Telemetry:
g_mode = cmdCode; case CTRL_CODE_SYSMODE_Security:
break; g_mode = cmdCode;
case CTRL_CODE_GAS_Disable: break;
case CTRL_CODE_GAS_State: case CTRL_CODE_GAS_Disable:
case CTRL_CODE_GAS_Data: case CTRL_CODE_GAS_State:
g_gasMode = cmdCode; case CTRL_CODE_GAS_Data:
break; g_gasMode = cmdCode;
default: break;
break; default:
break;
} }
} }
} }
void LcdDisplay(void) { void LcdDisplay(void)
{
u8 i; u8 i;
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++)
switch (i) { {
case 0: switch (i)
LSA = 0; {
LSB = 0; case 0:
LSC = 0; LSA = 0;
break; LSB = 0;
case 1: LSC = 0;
LSA = 1; break;
LSB = 0; case 1:
LSC = 0; LSA = 1;
break; LSB = 0;
case 2: LSC = 0;
LSA = 0; break;
LSB = 1; case 2:
LSC = 0; LSA = 0;
break; LSB = 1;
case 3: LSC = 0;
LSA = 1; break;
LSB = 1; case 3:
LSC = 0; LSA = 1;
break; LSB = 1;
case 4: LSC = 0;
LSA = 0; break;
LSB = 0; case 4:
LSC = 1; LSA = 0;
break; LSB = 0;
case 5: LSC = 1;
LSA = 1; break;
LSB = 0; case 5:
LSC = 1; LSA = 1;
break; LSB = 0;
LSC = 1;
break;
} }
P0 = DisplayData[5 - i]; P0 = DisplayData[5 - i];
delay(200); delay(200);
@ -181,16 +204,20 @@ void LcdDisplay(void) {
} }
} }
void TempProc(int temp) { void TempProc(int temp)
{
float tp; float tp;
u8 i = 0; u8 i = 0;
if (temp < 0) { if (temp < 0)
{
DisplayData[0] = 0x40; DisplayData[0] = 0x40;
temp = temp - 1; temp = temp - 1;
temp = ~temp; temp = ~temp;
tp = temp; tp = temp;
temp = tp * 0.0625 * 100 + 0.5; temp = tp * 0.0625 * 100 + 0.5;
} else { }
else
{
DisplayData[0] = 0x00; DisplayData[0] = 0x00;
tp = temp; tp = temp;
temp = tp * 0.0625 * 100 + 0.5; temp = tp * 0.0625 * 100 + 0.5;
@ -202,62 +229,105 @@ void TempProc(int temp) {
DisplayData[5] = LcdSegment[temp % 10]; DisplayData[5] = LcdSegment[temp % 10];
} }
void ProcessTempCmd(void) { void ProcessTempCmd(void)
{
u16 t_send = 0, tc = 0, td = 0; u16 t_send = 0, tc = 0, td = 0;
int temp; int temp;
#ifdef CLASS_4 #ifdef CLASS_4
tempCmdCode = CTRL_CODE_TEMP_Enable; tempCmdCode = CTRL_CODE_TEMP_Enable;
#endif #endif
if (tempCmdCode == CTRL_CODE_TEMP_Enable) { if (tempCmdCode == CTRL_CODE_TEMP_Enable)
{
EA = 0; EA = 0;
DS18B20_startConvert(); DS18B20_startConvert();
temp = DS18B20_readTemperature(); temp = DS18B20_readTemperature();
EA = 1; EA = 1;
tc = MCU_CMD_TEMP << 12; tc = MCU_CMD_TEMP << 12;
td = (u16) temp & 0xFFF; td = (u16)temp & 0xFFF;
t_send = td + tc; t_send = td + tc;
Uart1Send(t_send); Uart1Send(t_send);
TempProc(temp); TempProc(temp);
} }
} }
int ProcessBodyCmd(void) { int ProcessBodyCmd(void)
{
int r_flag = 0;
u8 state = 0;
u16 send_buff = 0;
PERSON = 1;
delay(1);
state = PERSON;
if (state == 0)
{
if (g_bodyMode == CTRL_CODE_BODY_Light)
{
lightCmdCode = GAS_LIGHT_BLINK;
}
if (g_bodyMode == CTRL_CODE_BODY_Buzzer)
{
r_flag = 1;
}
send_buff = (MCU_CMD_BODY << 12) + MCU_CMD_BODY_NO;
}
else
{
lightCmdCode = 0;
send_buff = (MCU_CMD_BODY << 12) + MCU_CMD_BODY_NO;
}
Uart1Send(send_buff);
return r_flag;
}
void ProcessLightCmd(void)
{
if (lightCmdCode == GAS_LIGHT_BLINK)
{
P2 = ~P2;
}
}
int ProcessGasCmd(void)
{
return 0; return 0;
} }
void ProcessLightCmd(void) { void main(void)
} {
int gasFlag, bodyFlag = 0;
int ProcessGasCmd(void) {
return 0;
}
void main(void) {
int gasFlag, bodyFlag;
DS18B20_startConvert(); DS18B20_startConvert();
delay(100); delay(100);
UsartInit(); UsartInit();
while (1) { while (1)
{
ProcessCommand(); ProcessCommand();
LcdDisplay();
#ifdef CLASS_4 #ifdef CLASS_4
ProcessTempCmd(); ProcessTempCmd();
LcdDisplay(); LcdDisplay();
#endif #endif
if (tim0flag) { if (tim0flag)
{
tim0flag = 0; tim0flag = 0;
if (g_mode == CTRL_CODE_SYSMODE_Security) { if (g_mode == CTRL_CODE_SYSMODE_Security)
{
bodyFlag = ProcessBodyCmd(); bodyFlag = ProcessBodyCmd();
} }
if ((g_mode == CTRL_CODE_SYSMODE_Security) || (g_mode == CTRL_CODE_SYSMODE_Telemetry)) { if ((g_mode == CTRL_CODE_SYSMODE_Security) || (g_mode == CTRL_CODE_SYSMODE_Telemetry))
{
gasFlag = ProcessGasCmd(); gasFlag = ProcessGasCmd();
} }
ProcessLightCmd(); ProcessLightCmd();
if (tempCmdCode != CTRL_CODE_TEMP_Disable) { if (tempCmdCode != CTRL_CODE_TEMP_Disable)
{
ProcessTempCmd(); ProcessTempCmd();
} }
} }
if (bodyFlag)
{
beep(2);
}
LcdDisplay();
} }
} }