feat: ppt6
This commit is contained in:
parent
c49d40b8b9
commit
216ae7930c
276
src/main.c
276
src/main.c
@ -3,6 +3,8 @@
|
||||
|
||||
#include "ds18b20.h"
|
||||
|
||||
// #define YELLOW_BOARD
|
||||
|
||||
typedef unsigned int u16;
|
||||
typedef unsigned char u8;
|
||||
|
||||
@ -15,14 +17,19 @@ u16 tim0countAll = 2000;
|
||||
u16 tim0count = 0;
|
||||
u8 tim0flag = 0;
|
||||
|
||||
#define person P1_0
|
||||
#define gasState P1_1
|
||||
#define beep P1_5
|
||||
#define PERSON P1_0
|
||||
#define GAS_STATE P1_1
|
||||
|
||||
#ifdef YELLOW_BOARD
|
||||
#define BEEP P1_5
|
||||
#else
|
||||
#define BEEP P1_6
|
||||
#endif
|
||||
|
||||
char g_mode = 0x01;
|
||||
char g_bodyMode = 0x00;
|
||||
char g_gasMode = 0x00;
|
||||
char FAS_LIGHT_BLINK = 0x50;
|
||||
char GAS_LIGHT_BLINK = 0x50;
|
||||
|
||||
u8 m_flag = 0;
|
||||
|
||||
@ -48,23 +55,26 @@ u8 LcdSegment[10] = {
|
||||
// #define CLASS_3
|
||||
// #define CLASS_4
|
||||
|
||||
void delay(u16 i) {
|
||||
void delay(u16 i)
|
||||
{
|
||||
while (i--);
|
||||
}
|
||||
|
||||
void Uart1Send(u16 sendData) {
|
||||
void Uart1Send(u16 sendData)
|
||||
{
|
||||
TI = 0;
|
||||
SBUF = (char) (sendData >> 8);
|
||||
SBUF = (char)(sendData >> 8);
|
||||
while (TI == 0);
|
||||
TI = 0;
|
||||
|
||||
SBUF = (char) (sendData & 0xFF);
|
||||
SBUF = (char)(sendData & 0xFF);
|
||||
while (TI == 0);
|
||||
TI = 0;
|
||||
delay(50);
|
||||
}
|
||||
|
||||
void UsartInit(void) {
|
||||
void UsartInit(void)
|
||||
{
|
||||
SCON = 0x50;
|
||||
TMOD = 0x22;
|
||||
PCON = 0x80;
|
||||
@ -89,91 +99,104 @@ void Usart_ISR(void) __interrupt(4) {
|
||||
|
||||
void Timer0_ISR(void) __interrupt(1) {
|
||||
tim0count++;
|
||||
if (tim0count >= tim0countAll) {
|
||||
if (tim0count >= tim0countAll)
|
||||
{
|
||||
tim0count = 0;
|
||||
tim0flag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessCommand(void) {
|
||||
if (newCmdFlag) {
|
||||
void beep(int duration)
|
||||
{
|
||||
BEEP = ~BEEP;
|
||||
delay(duration);
|
||||
}
|
||||
|
||||
void ProcessCommand(void)
|
||||
{
|
||||
if (newCmdFlag)
|
||||
{
|
||||
#ifdef CLASS_3
|
||||
u16 recv_data = (cmdCode << 8) + cmdCode;
|
||||
Uart1Send(recv_data);
|
||||
#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;
|
||||
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) {
|
||||
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;
|
||||
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(200);
|
||||
@ -181,16 +204,20 @@ void LcdDisplay(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void TempProc(int temp) {
|
||||
void TempProc(int temp)
|
||||
{
|
||||
float tp;
|
||||
u8 i = 0;
|
||||
if (temp < 0) {
|
||||
if (temp < 0)
|
||||
{
|
||||
DisplayData[0] = 0x40;
|
||||
temp = temp - 1;
|
||||
temp = ~temp;
|
||||
tp = temp;
|
||||
temp = tp * 0.0625 * 100 + 0.5;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
DisplayData[0] = 0x00;
|
||||
tp = temp;
|
||||
temp = tp * 0.0625 * 100 + 0.5;
|
||||
@ -202,62 +229,105 @@ void TempProc(int temp) {
|
||||
DisplayData[5] = LcdSegment[temp % 10];
|
||||
}
|
||||
|
||||
void ProcessTempCmd(void) {
|
||||
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) {
|
||||
if (tempCmdCode == CTRL_CODE_TEMP_Enable)
|
||||
{
|
||||
EA = 0;
|
||||
DS18B20_startConvert();
|
||||
temp = DS18B20_readTemperature();
|
||||
EA = 1;
|
||||
tc = MCU_CMD_TEMP << 12;
|
||||
td = (u16) temp & 0xFFF;
|
||||
td = (u16)temp & 0xFFF;
|
||||
t_send = td + tc;
|
||||
Uart1Send(t_send);
|
||||
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;
|
||||
}
|
||||
|
||||
void ProcessLightCmd(void) {
|
||||
}
|
||||
|
||||
int ProcessGasCmd(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
int gasFlag, bodyFlag;
|
||||
void main(void)
|
||||
{
|
||||
int gasFlag, bodyFlag = 0;
|
||||
DS18B20_startConvert();
|
||||
delay(100);
|
||||
UsartInit();
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
ProcessCommand();
|
||||
LcdDisplay();
|
||||
|
||||
#ifdef CLASS_4
|
||||
ProcessTempCmd();
|
||||
LcdDisplay();
|
||||
ProcessTempCmd();
|
||||
LcdDisplay();
|
||||
#endif
|
||||
|
||||
if (tim0flag) {
|
||||
if (tim0flag)
|
||||
{
|
||||
tim0flag = 0;
|
||||
if (g_mode == CTRL_CODE_SYSMODE_Security) {
|
||||
if (g_mode == CTRL_CODE_SYSMODE_Security)
|
||||
{
|
||||
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();
|
||||
}
|
||||
ProcessLightCmd();
|
||||
if (tempCmdCode != CTRL_CODE_TEMP_Disable) {
|
||||
if (tempCmdCode != CTRL_CODE_TEMP_Disable)
|
||||
{
|
||||
ProcessTempCmd();
|
||||
}
|
||||
}
|
||||
if (bodyFlag)
|
||||
{
|
||||
beep(2);
|
||||
}
|
||||
LcdDisplay();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user