commit 138867439fed4bd6d95b49d4730c6457b4cdc34a Author: HoshinoSuzumi Date: Thu Nov 28 00:54:11 2024 +0800 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..226ca24 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,580 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fff42b8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/PlatformIO.xml b/.idea/runConfigurations/PlatformIO.xml new file mode 100644 index 0000000..e555943 --- /dev/null +++ b/.idea/runConfigurations/PlatformIO.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/serial_command.h b/include/serial_command.h new file mode 100644 index 0000000..0290316 --- /dev/null +++ b/include/serial_command.h @@ -0,0 +1,52 @@ +#ifndef SERIAL_COMMAND_H +#define SERIAL_COMMAND_H + +// Command send to MCU +enum CTRL_CODE_ { + // System mode + CTRL_CODE_SYSMODE_Disable = 0x01, + CTRL_CODE_SYSMODE_Telemetry = 0x02, + CTRL_CODE_SYSMODE_Security = 0x03, + + // Tmperature telemetry + CTRL_CODE_TEMP_Disable = 0x10, + CTRL_CODE_TEMP_Enable = 0x11, + + // Body alarming + CTRL_CODE_BODY_Disable = 0x21, + CTRL_CODE_BODY_Buzzer = 0x22, + CTRL_CODE_BODY_Light = 0x23, + + // Gas telemetry + CTRL_CODE_GAS_Disable = 0x31, + CTRL_CODE_GAS_State = 0x32, + CTRL_CODE_GAS_Data = 0x33, + + // Collection cycle + CTRL_CODE_CYCLE_500ms = 0x41, + CTRL_CODE_CYCLE_1s = 0x42, + CTRL_CODE_CYCLE_2s = 0x43, + CTRL_CODE_CYCLE_5s = 0x44 +}; + +// Command code from MCU +enum MCU_CMD_ { + MCU_CMD_TEMP = 0x01, + MCU_CMD_BODY = 0x02, + MCU_CMD_GAS_STATE = 0x03, + MCU_CMD_GAS_DATA = 0x04, +}; + +// Body sensor status code +enum COMMAND_BODY { + MCU_CMD_BODY_YES = 0x01, + MCU_CMD_BODY_NO = 0x02, +}; + +// Gas sensor status code +enum { + MCU_CMD_GAS_STATE_NORMAL = 0x01, + MCU_CMD_GAS_STATE_ALARMING = 0x02, +}; + +#endif // SERIAL_COMMAND_H diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/lib/peripheral/temp.c b/lib/peripheral/temp.c new file mode 100644 index 0000000..152b8c1 --- /dev/null +++ b/lib/peripheral/temp.c @@ -0,0 +1,78 @@ +#include "temp.h" + +void Delay1ms(uint y) { + uint x; + for(; y > 0; y--) { + for(x = 110; x > 0; x--); + } +} + +uchar Ds18b20Init(void) { + uchar i; + DSPORT = 0; // Pull down bus + i = 70; + while(i--); + DSPORT = 1; // Pull up bus + i = 0; + while(DSPORT) { + Delay1ms(1); + i++; + if(i > 5) return 0; + } + return 1; +} + +void Ds18b20WriteByte(uchar payload) { + uint i, j; + for(j = 0; j < 8; j++) { + DSPORT = 0; + i++; + DSPORT = payload & 0x01; + i = 6; + while(i--); + DSPORT = 1; + payload >>= 1; + } +} + +uchar Ds18b20ReadByte(void) { + uchar byte, bi; + uint i, j; + for(j = 8; j > 0; j--) { + DSPORT = 0; + i++; + DSPORT = 1; + i++; + i++; + bi = DSPORT; + byte = (byte >> 1) | (bi << 7); + i = 4; + while(i--); + } + return byte; +} + +void Ds18b20ChangTemp(void) { + Ds18b20Init(); + Delay1ms(1); + Ds18b20WriteByte(0xCC); + Ds18b20WriteByte(0x44); +} + +void Ds18b20ReadTempCom() { + Ds18b20Init(); + Delay1ms(1); + Ds18b20WriteByte(0xCC); + Ds18b20WriteByte(0xBE); +} + +int Ds18b20ReadTemp() { + int temp = 0; + uchar th, tl; + Ds18b20ChangTemp(); + Ds18b20ReadTempCom(); + temp = th; + temp <<= 8; + temp |= tl; + return temp; +} diff --git a/lib/peripheral/temp.h b/lib/peripheral/temp.h new file mode 100644 index 0000000..916b541 --- /dev/null +++ b/lib/peripheral/temp.h @@ -0,0 +1,24 @@ +#ifndef __TEMP_H +#define __TEMP_H + +#include <8052.h> + +#ifndef uchar +#define uchar unsigned char +#endif + +#ifndef uint +#define uint unsigned int +#endif + +#define DSPORT P3_7 + +void Delay1ms(uint y); +uchar Ds18b20Init(void); +void Ds18b20WriteByte(uchar com); +uchar Ds18b20ReadByte(void); +void Ds18b20ChangTemp(void); +void Ds18b20ReadTempCom(void); +int Ds18b20ReadTemp(void); + +#endif diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d2b930d --- /dev/null +++ b/platformio.ini @@ -0,0 +1,16 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:STC89C52RC] +platform = intel_mcs51 +board = STC89C52RC + +lib_deps = + C:\Users\5ANK41\.platformio\packages\toolchain-sdcc\include\mcs51 diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..8fbb8f4 --- /dev/null +++ b/src/main.c @@ -0,0 +1,249 @@ +#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(); + } + } + } +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html