diff --git a/.github/workflows/arduino_ci.yml b/.github/workflows/arduino_ci.yml index 95451d9..30d33c0 100644 --- a/.github/workflows/arduino_ci.yml +++ b/.github/workflows/arduino_ci.yml @@ -82,7 +82,7 @@ jobs: with: github_token: ${{ secrets.github_token }} reporter: github-pr-review - flags: --extensions=h,hpp,c,cpp,cc,cu,hh,ipp,ino + flags: --extensions=h,hpp,c,cpp,cc,cu,hh,ipp,ino --exclude=./test/Nose/ filter: "-readability/braces\ ,-whitespace/braces\ ,-whitespace/comments\ @@ -111,6 +111,11 @@ jobs: version: latest - name: Adafruit BNO055 version: latest + - source-url: https://github.com/uChip/MCP342X.git + - source-url: https://github.com/core-rocket/CCP.git + - source-url: https://github.com/coryjfowler/MCP_CAN_lib.git + - source-url: https://github.com/771-8bit/W25Q512.git + - source-url: https://github.com/sdesalas/Arduino-Queue.h.git strategy: fail-fast: false diff --git a/.gitignore b/.gitignore index 600d2d3..722d5e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.vscode \ No newline at end of file +.vscode diff --git a/Nose/Nose.ino b/Nose/Nose.ino new file mode 100644 index 0000000..79a0529 --- /dev/null +++ b/Nose/Nose.ino @@ -0,0 +1,139 @@ +#include +#include +#include +#include + +#define CAN_AVAIRABLE + +#define CAN0_CS 0 +#define CAN0_INT 1 +#define LED_YELLOW LED_BUILTIN +#define LED_BLUE PIN_LED_RXL + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; +MCP342X myADC; +CCP_MCP2515 CCP(CAN0_CS, CAN0_INT); //CAN + +const int clockFrequency = 400000; //I2C bus speed +bool timer100Hz = false; +bool sleep_sensors = false; +bool can_checkerflag = false; +struct repeating_timer st_timer; + +void setup() { + Serial.begin(1843200); + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + bme.begin(0x76); + myADC.configure(MCP342X_MODE_CONTINUOUS | MCP342X_CHANNEL_1 | MCP342X_SIZE_18BIT | MCP342X_GAIN_1X); + add_repeating_timer_us(10000, TimerIsr, NULL, &st_timer); //100Hz +#ifdef CAN_AVAIRABLE + CCP.begin(); +#endif +} + +void loop() { + static int32_t result; + static float temperature; + static float barometic_pressure; + static char adc_bytes[3]; + static double voltage; + if (timer100Hz) { + timer100Hz = false; + if (!sleep_sensors) { + //差圧センサ関連 + myADC.startConversion(); + myADC.getResult(&result); + DevideBytes(&result, adc_bytes); + ConvertToVoltage(adc_bytes, &voltage); //3つのバイトを電圧に変換 + //BME280関連 + GetBME280Data(&temperature, &barometic_pressure); +//CAN送信 +#ifdef CAN_AVAIRABLE + CCP.uint32_to_device(CCP_nose_adc, voltage); + CCP.float_to_device(CCP_nose_temperature, temperature); + CCP.float_to_device(CCP_nose_barometic_pressure, barometic_pressure); + if (can_checkerflag) { + CCP.string_to_device(CCP_nose_status, "OK"); + can_checkerflag = false; + } +#endif + + //シリアル出力 + SerialPrintSensors(adc_bytes, temperature, barometic_pressure, voltage); + } + } +#ifdef CAN_AVAIRABLE + CCP.read_device(); + switch (CCP.id) { + case CCP_EMST_mesure: + if (CCP.str_match("STOP", 4)) { + sleep_sensors = true; + } else if (CCP.str_match("CLEAR", 5)) { + sleep_sensors = false; + } + break; + case CCP_nose_adc: + if (CCP.str_match("CHECK", 5)) { + can_checkerflag = true; + } + if (CCP.str_match("KILL", 4)) { + sleep_sensors = true; + } + break; + default: + break; + } +#endif +} + +void GetBME280Data(float* temperature, float* barometic_pressure) { + *temperature = bme.readTemperature(); + *barometic_pressure = bme.readPressure(); +} + +void DevideBytes(int32_t* _result, char* bytes) { + bytes[2] = static_cast(*_result & 0xFF); + bytes[1] = static_cast((*_result >> 8) & 0xFF); + bytes[0] = static_cast((*_result >> 16) & 0xFF); +} + +void ConvertToVoltage(char* bytes, double* voltage) { + double pga = 1; + double lsb = 2 * 2.048 / pow(2, 18); + + byte msb = (bytes[0] >> 6) & 0x01; + uint32_t outputcode = bytes[2] | (bytes[1] << 8) | ((bytes[0] * 0x01) << 16); + if (msb == 0x00) { //正の値 + *voltage = static_cast(outputcode)*lsb / pga; + } else { //負の値 + outputcode = ((~outputcode) & 0x01FFFF) + 1; //2の補数 + *voltage = -static_cast(outputcode)*lsb / pga; + } +} + +void SerialPrintSensors(char* adc_bytes, float temperature, float barometic_pressure, double voltage) { + // if(timer100Hz) Serial.println("overrun"); + Serial.print("time:"); + Serial.print(micros()); + Serial.print(",adc_bytes:"); + Serial.print(adc_bytes[0], HEX); + Serial.print(adc_bytes[1], HEX); + Serial.print(adc_bytes[2], HEX); + Serial.print(",temperature:"); + Serial.print(temperature, 10); + Serial.print(","); + Serial.print(",barometic_pressure:"); + Serial.print(barometic_pressure, 10); + Serial.print(",voltage:"); + Serial.println(voltage, 10); +} + +bool TimerIsr(struct repeating_timer* t) { + timer100Hz = true; + return true; +} \ No newline at end of file diff --git a/Nose/rp2040reset/adafruit-circuitpython-raspberry_pi_pico-ja-8.2.9.uf2 b/Nose/rp2040reset/adafruit-circuitpython-raspberry_pi_pico-ja-8.2.9.uf2 new file mode 100644 index 0000000..fdcc9bf Binary files /dev/null and b/Nose/rp2040reset/adafruit-circuitpython-raspberry_pi_pico-ja-8.2.9.uf2 differ diff --git a/Nose/dataqsheets/MPX5050.pdf b/docs/Nose/datasheets/MPX5050.pdf similarity index 100% rename from Nose/dataqsheets/MPX5050.pdf rename to docs/Nose/datasheets/MPX5050.pdf diff --git a/Nose/dataqsheets/mcp3421.pdf b/docs/Nose/datasheets/mcp3421.pdf similarity index 100% rename from Nose/dataqsheets/mcp3421.pdf rename to docs/Nose/datasheets/mcp3421.pdf diff --git a/Nose/memo.ipynb b/docs/Nose/memo.ipynb similarity index 100% rename from Nose/memo.ipynb rename to docs/Nose/memo.ipynb diff --git a/docs/Nose/softwarememo.ipynb b/docs/Nose/softwarememo.ipynb new file mode 100644 index 0000000..c78e661 --- /dev/null +++ b/docs/Nose/softwarememo.ipynb @@ -0,0 +1,59 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ノーズソフトメモ" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 使用ライブラリ\n", + "- [MCP3421](https://github.com/uChip/MCP342X)\n", + "- " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## rp2040を使う上での注意点\n", + "\n", + "- USB認識をしなくなったら,BOOTボタンを押しながらUSB接続し,RP2040のドライブにリセット用のuf2ファイルをドラック&ドロップ\n", + "- Wire.hを使う前に,rp2040はI2Cがいくつもあるので,どのGPIOを使うか宣言する必要がある.\n", + "```\n", + "#define PIN1_SDA 6\n", + "#define PIN1_SCL 7\n", + "Wire.setSDA(PIN1_SDA);\n", + "Wire.setSCL(PIN1_SCL);\n", + "Wire.begin(); \n", + "```\n", + "といったように" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/test/Nose/BME_high_rate/BME_high_rate.ino b/test/Nose/BME_high_rate/BME_high_rate.ino new file mode 100644 index 0000000..c96de5c --- /dev/null +++ b/test/Nose/BME_high_rate/BME_high_rate.ino @@ -0,0 +1,48 @@ +#include +#include + + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; + +unsigned long delayTime; +const int clockFrequency = 400000; //I2C bus speed + +void setup() { + Serial.begin(115200); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + status = bme.begin(0x76); +} + + +void loop() { + printValues(); + delay(5); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" °C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} diff --git a/test/Nose/BME_mcp3421/BME_mcp3421.ino b/test/Nose/BME_mcp3421/BME_mcp3421.ino new file mode 100644 index 0000000..08c6503 --- /dev/null +++ b/test/Nose/BME_mcp3421/BME_mcp3421.ino @@ -0,0 +1,78 @@ +#include +#include +#include + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; +MCP342X myADC; + +const int clockFrequency = 400000; //I2C bus speed + +double voltage; + +void setup() { + Serial.begin(115200); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + status = bme.begin(0x76); + myADC.configure(MCP342X_MODE_CONTINUOUS | MCP342X_CHANNEL_1 | MCP342X_SIZE_18BIT | MCP342X_GAIN_1X); +} + + +void loop() { + printValues(); + + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result, &voltage); + Serial.print("voltage:"); + Serial.println(voltage, 10); + delay(5); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" °C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} + +void ConvertToVoltage(int32_t* _result, double* voltage) { + byte bytes[4]; + bytes[3] = static_cast(*_result & 0xFF); + bytes[2] = static_cast((*_result >> 8) & 0xFF); + bytes[1] = static_cast((*_result >> 16) & 0xFF); + bytes[0] = static_cast((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga = 1; + double lsb = 2 * 2.048 / pow(2, 18); + + byte msb = (bytes[1] >> 6) & 0x01; + uint32_t outputcode = bytes[3] | (bytes[2] << 8) | ((bytes[1] * 0x01) << 16); + if (msb == 0x00) { //正の値 + *voltage = static_cast(outputcode)*lsb / pga; + } else { //負の値 + outputcode = ((~outputcode) & 0x01FFFF) + 1; //2の補数 + *voltage = -static_cast(outputcode)*lsb / pga; + } +} diff --git a/test/Nose/bme280test/bme280test.ino b/test/Nose/bme280test/bme280test.ino new file mode 100644 index 0000000..db87e97 --- /dev/null +++ b/test/Nose/bme280test/bme280test.ino @@ -0,0 +1,42 @@ +#include +#include + + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; + +void setup() { + Serial.begin(9600); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.begin(); + status = bme.begin(0x76); +} + +void loop() { + printValues(); + delay(1000); +} + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" °C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} diff --git a/test/Nose/mcp3421_2/README.md b/test/Nose/mcp3421_2/README.md new file mode 100644 index 0000000..c495f1b --- /dev/null +++ b/test/Nose/mcp3421_2/README.md @@ -0,0 +1,3 @@ +# mcp3421_2.ino + +xiao samd21でmcp3421から電圧値を取得・計算するためのプログラム. \ No newline at end of file diff --git a/test/Nose/mcp3421_2/mcp3421_2.ino b/test/Nose/mcp3421_2/mcp3421_2.ino new file mode 100644 index 0000000..3bf0a94 --- /dev/null +++ b/test/Nose/mcp3421_2/mcp3421_2.ino @@ -0,0 +1,42 @@ + +#include +#include + +MCP342X myADC; +double voltage; +void setup() { + Wire.begin(); // join I2C bus + // TWBR = 12; // 400 kHz (maximum) + Serial.begin(9600); // Open serial connection to send info to the host + myADC.configure(MCP342X_MODE_CONTINUOUS | MCP342X_CHANNEL_1 | MCP342X_SIZE_18BIT | MCP342X_GAIN_1X); + delay(300); +} + +void loop() { + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result, &voltage); + + Serial.print("voltage:"); + Serial.println(voltage); +} +void ConvertToVoltage(int32_t* _result, double* voltage) { + byte bytes[4]; + bytes[3] = static_cast(*_result & 0xFF); + bytes[2] = static_cast((*_result >> 8) & 0xFF); + bytes[1] = static_cast((*_result >> 16) & 0xFF); + bytes[0] = static_cast((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga = 1; + double lsb = 2 * 2.048 / pow(2, 18); + + byte msb = (bytes[1] >> 6) & 0x01; + uint32_t outputcode = bytes[3] | (bytes[2] << 8) | ((bytes[1] * 0x01) << 16); + if (msb == 0x00) { //正の値 + *voltage = static_cast(outputcode)*lsb / pga; + } else { //負の値 + outputcode = ((~outputcode) & 0x01FFFF) + 1; //2の補数 + *voltage = -static_cast(outputcode)*lsb / pga; + } +} \ No newline at end of file diff --git a/test/Nose/mcp3421_pico/README.md b/test/Nose/mcp3421_pico/README.md new file mode 100644 index 0000000..ea1de7e --- /dev/null +++ b/test/Nose/mcp3421_pico/README.md @@ -0,0 +1,3 @@ +# mcp3421_pico.ino + +xiao rp2040でmcp3421から電圧値を取得・計算するためのプログラム. \ No newline at end of file diff --git a/test/Nose/mcp3421_pico/mcp3421_pico.ino b/test/Nose/mcp3421_pico/mcp3421_pico.ino new file mode 100644 index 0000000..d302b7c --- /dev/null +++ b/test/Nose/mcp3421_pico/mcp3421_pico.ino @@ -0,0 +1,47 @@ + +#include +#include +#define PIN1_SDA 6 +#define PIN1_SCL 7 + + +MCP342X myADC; +double voltage; +void setup() { + Wire.setSDA(PIN1_SDA); + Wire.setSCL(PIN1_SCL); + Wire.begin(); // join I2C bus + // TWBR = 12; // 400 kHz (maximum) + Serial.begin(9600); // Open serial connection to send info to the host + myADC.configure(MCP342X_MODE_CONTINUOUS | MCP342X_CHANNEL_1 | MCP342X_SIZE_18BIT | MCP342X_GAIN_1X); + delay(300); +} + +void loop() { + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result, &voltage); + + Serial.print("voltage:"); + Serial.println(voltage, 10); +} +void ConvertToVoltage(int32_t* _result, double* voltage) { + byte bytes[4]; + bytes[3] = static_cast(*_result & 0xFF); + bytes[2] = static_cast((*_result >> 8) & 0xFF); + bytes[1] = static_cast((*_result >> 16) & 0xFF); + bytes[0] = static_cast((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga = 1; + double lsb = 2 * 2.048 / pow(2, 18); + + byte msb = (bytes[1] >> 6) & 0x01; + uint32_t outputcode = bytes[3] | (bytes[2] << 8) | ((bytes[1] * 0x01) << 16); + if (msb == 0x00) { //正の値 + *voltage = static_cast(outputcode)*lsb / pga; + } else { //負の値 + outputcode = ((~outputcode) & 0x01FFFF) + 1; //2の補数 + *voltage = -static_cast(outputcode)*lsb / pga; + } +} \ No newline at end of file diff --git a/test/Nose/mcp3421check_samd/mcp3421check_samd.ino b/test/Nose/mcp3421check_samd/mcp3421check_samd.ino new file mode 100644 index 0000000..f927701 --- /dev/null +++ b/test/Nose/mcp3421check_samd/mcp3421check_samd.ino @@ -0,0 +1,29 @@ + +// Include libraries this sketch will use +#include +#include + +// Instantiate objects used in this project +MCP342X myADC; + +void setup() { + Wire.begin(); // join I2C bus + // TWBR = 12; // 400 kHz (maximum) + + Serial.begin(9600); // Open serial connection to send info to the host + while (!Serial) {} // wait for Serial comms to become ready + Serial.println("Starting up"); + Serial.println("Testing device connection..."); + Serial.println(myADC.testConnection() ? "MCP342X connection successful" : "MCP342X connection failed"); + + myADC.configure(MCP342X_MODE_CONTINUOUS | MCP342X_CHANNEL_1 | MCP342X_SIZE_16BIT | MCP342X_GAIN_1X); + + Serial.println(myADC.getConfigRegShdw(), HEX); +} // End of setup() + +void loop() { + static int16_t result; + myADC.startConversion(); + myADC.getResult(&result); + Serial.println(result, HEX); +} // End of loop() diff --git a/test/Nose/trash/BME_high_rate/BME_high_rate.ino b/test/Nose/trash/BME_high_rate/BME_high_rate.ino new file mode 100644 index 0000000..f457cab --- /dev/null +++ b/test/Nose/trash/BME_high_rate/BME_high_rate.ino @@ -0,0 +1,48 @@ +#include +#include + + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; + +unsigned long delayTime; +const int clockFrequency = 400000;//I2C bus speed + +void setup() { + Serial.begin(115200); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + status = bme.begin(0x76); +} + + +void loop() { + printValues(); + delay(5); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" °C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} diff --git a/test/Nose/trash/BME_mcp3421/BME_mcp3421.ino b/test/Nose/trash/BME_mcp3421/BME_mcp3421.ino new file mode 100644 index 0000000..5b0b9b9 --- /dev/null +++ b/test/Nose/trash/BME_mcp3421/BME_mcp3421.ino @@ -0,0 +1,83 @@ +#include +#include +#include + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; +MCP342X myADC; + +unsigned long delayTime; +const int clockFrequency = 400000;//I2C bus speed + +double voltage; + +void setup() { + Serial.begin(115200); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + status = bme.begin(0x76); + myADC.configure( MCP342X_MODE_CONTINUOUS | + MCP342X_CHANNEL_1 | + MCP342X_SIZE_18BIT | + MCP342X_GAIN_1X + ); +} + + +void loop() { + printValues(); + + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result,&voltage); + Serial.print("voltage:"); + Serial.println(voltage,10); + delay(5); +} + + +void printValues() { + Serial.print("Temperature = "); + Serial.print(bme.readTemperature()); + Serial.println(" °C"); + + Serial.print("Pressure = "); + + Serial.print(bme.readPressure() / 100.0F); + Serial.println(" hPa"); + + Serial.print("Approx. Altitude = "); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(" m"); + + Serial.print("Humidity = "); + Serial.print(bme.readHumidity()); + Serial.println(" %"); + + Serial.println(); +} + +void ConvertToVoltage(int32_t* _result,double* voltage){ + byte bytes[4]; + bytes[3] = (char)(*_result & 0xFF); + bytes[2] = (char)((*_result >> 8) & 0xFF); + bytes[1] = (char)((*_result >> 16) & 0xFF); + bytes[0] = (char)((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga=1; + double lsb=2*2.048/pow(2,18); + + byte msb=(bytes[1]>>6)&0x01; + uint32_t outputcode=bytes[3]|(bytes[2]<<8)|((bytes[1]*0x01)<<16); + if(msb==0x00){//正の値 + *voltage=(double)(outputcode)*lsb/pga; + }else{//負の値 + outputcode=((~outputcode)&0x01FFFF)+1;//2の補数 + *voltage=-(double)(outputcode)*lsb/pga; + } +} diff --git a/test/Nose/trash/mcp3421_2/README.md b/test/Nose/trash/mcp3421_2/README.md new file mode 100644 index 0000000..c495f1b --- /dev/null +++ b/test/Nose/trash/mcp3421_2/README.md @@ -0,0 +1,3 @@ +# mcp3421_2.ino + +xiao samd21でmcp3421から電圧値を取得・計算するためのプログラム. \ No newline at end of file diff --git a/test/Nose/trash/mcp3421_2/mcp3421_2.ino b/test/Nose/trash/mcp3421_2/mcp3421_2.ino new file mode 100644 index 0000000..c909d45 --- /dev/null +++ b/test/Nose/trash/mcp3421_2/mcp3421_2.ino @@ -0,0 +1,46 @@ + +#include +#include + +MCP342X myADC; +double voltage; +void setup() { + Wire.begin(); // join I2C bus +// TWBR = 12; // 400 kHz (maximum) + Serial.begin(9600); // Open serial connection to send info to the host + myADC.configure( MCP342X_MODE_CONTINUOUS | + MCP342X_CHANNEL_1 | + MCP342X_SIZE_18BIT | + MCP342X_GAIN_1X + ); + delay(300); +} + +void loop() { + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result,&voltage); + + Serial.print("voltage:"); + Serial.println(voltage); +} +void ConvertToVoltage(int32_t* _result,double* voltage){ + byte bytes[4]; + bytes[3] = (char)(*_result & 0xFF); + bytes[2] = (char)((*_result >> 8) & 0xFF); + bytes[1] = (char)((*_result >> 16) & 0xFF); + bytes[0] = (char)((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga=1; + double lsb=2*2.048/pow(2,18); + + byte msb=(bytes[1]>>6)&0x01; + uint32_t outputcode=bytes[3]|(bytes[2]<<8)|((bytes[1]*0x01)<<16); + if(msb==0x00){//正の値 + *voltage=(double)(outputcode)*lsb/pga; + }else{//負の値 + outputcode=((~outputcode)&0x01FFFF)+1;//2の補数 + *voltage=-(double)(outputcode)*lsb/pga; + } +} \ No newline at end of file diff --git a/test/Nose/trash/mcp3421_pico/README.md b/test/Nose/trash/mcp3421_pico/README.md new file mode 100644 index 0000000..ea1de7e --- /dev/null +++ b/test/Nose/trash/mcp3421_pico/README.md @@ -0,0 +1,3 @@ +# mcp3421_pico.ino + +xiao rp2040でmcp3421から電圧値を取得・計算するためのプログラム. \ No newline at end of file diff --git a/test/Nose/trash/mcp3421_pico/mcp3421_pico.ino b/test/Nose/trash/mcp3421_pico/mcp3421_pico.ino new file mode 100644 index 0000000..4f52ac2 --- /dev/null +++ b/test/Nose/trash/mcp3421_pico/mcp3421_pico.ino @@ -0,0 +1,51 @@ + +#include +#include +#define PIN1_SDA 6 +#define PIN1_SCL 7 + + +MCP342X myADC; +double voltage; +void setup() { + Wire.setSDA(PIN1_SDA); + Wire.setSCL(PIN1_SCL); + Wire.begin(); // join I2C bus +// TWBR = 12; // 400 kHz (maximum) + Serial.begin(9600); // Open serial connection to send info to the host + myADC.configure( MCP342X_MODE_CONTINUOUS | + MCP342X_CHANNEL_1 | + MCP342X_SIZE_18BIT | + MCP342X_GAIN_1X + ); + delay(300); +} + +void loop() { + static int32_t result; + byte bytes[4]; + myADC.startConversion(); + myADC.getResult(&result); + ConvertToVoltage(&result,&voltage); + + Serial.print("voltage:"); + Serial.println(voltage,10); +} +void ConvertToVoltage(int32_t* _result,double* voltage){ + byte bytes[4]; + bytes[3] = (char)(*_result & 0xFF); + bytes[2] = (char)((*_result >> 8) & 0xFF); + bytes[1] = (char)((*_result >> 16) & 0xFF); + bytes[0] = (char)((*_result >> 24) & 0xFF); //ライブラリの関数内で8bit右シフトしたときに発生したものなので無視 + double pga=1; + double lsb=2*2.048/pow(2,18); + + byte msb=(bytes[1]>>6)&0x01; + uint32_t outputcode=bytes[3]|(bytes[2]<<8)|((bytes[1]*0x01)<<16); + if(msb==0x00){//正の値 + *voltage=(double)(outputcode)*lsb/pga; + }else{//負の値 + outputcode=((~outputcode)&0x01FFFF)+1;//2の補数 + *voltage=-(double)(outputcode)*lsb/pga; + } +} \ No newline at end of file diff --git a/test/Nose/trash/nose1/nose1.ino b/test/Nose/trash/nose1/nose1.ino new file mode 100644 index 0000000..96e5cbb --- /dev/null +++ b/test/Nose/trash/nose1/nose1.ino @@ -0,0 +1,83 @@ +#include +#include +#include + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; +MCP342X myADC; + +unsigned long delayTime; +const int clockFrequency = 400000;//I2C bus speed + +void setup() { + Serial.begin(115200); + unsigned status; + Wire.setSDA(6); + Wire.setSCL(7); + Wire.setClock(clockFrequency); + Wire.begin(); + status = bme.begin(0x76); + myADC.configure( MCP342X_MODE_CONTINUOUS | + MCP342X_CHANNEL_1 | + MCP342X_SIZE_18BIT | + MCP342X_GAIN_1X + ); +} + + +void loop() { + static int32_t result; + static float temperature; + static float barometic_pressure; + static char adc_bytes[3]; + static double voltage; + + //差圧センサ関連 + myADC.startConversion(); + myADC.getResult(&result); + DevideBytes(&result, adc_bytes); //CAN送信用 + ConvertToVoltage(adc_bytes, &voltage); //3つのバイトを電圧に変換 + //BME280関連 + GetBME280Data(&temperature, &barometic_pressure); + + Serial.print("adc_bytes:"); + Serial.print(adc_bytes[0],HEX); + Serial.print(adc_bytes[1],HEX); + Serial.println(adc_bytes[2],HEX); + Serial.print("temperature:"); + Serial.println(temperature,10); + Serial.print("barometic_pressure:"); + Serial.println(barometic_pressure,10); + Serial.print("voltage:"); + Serial.println(voltage,10); + + delay(5); +} + +void GetBME280Data(float* temperature,float* barometic_pressure){ + *temperature=bme.readTemperature(); + *barometic_pressure=bme.readPressure(); +} + +void DevideBytes(int32_t* _result,char* bytes){ + bytes[2] = (char)(*_result & 0xFF); + bytes[1] = (char)((*_result >> 8) & 0xFF); + bytes[0] = (char)((*_result >> 16) & 0xFF); +} + +void ConvertToVoltage(char* bytes,double* voltage){ + double pga=1; + double lsb=2*2.048/pow(2,18); + + byte msb=(bytes[0]>>6)&0x01; + uint32_t outputcode=bytes[2]|(bytes[1]<<8)|((bytes[0]*0x01)<<16); + if(msb==0x00){//正の値 + *voltage=(double)(outputcode)*lsb/pga; + }else{//負の値 + outputcode=((~outputcode)&0x01FFFF)+1;//2の補数 + *voltage=-(double)(outputcode)*lsb/pga; + } +} + + diff --git a/test/Nose/trash/test1.csv b/test/Nose/trash/test1.csv new file mode 100644 index 0000000..08ca713 --- /dev/null +++ b/test/Nose/trash/test1.csv @@ -0,0 +1,11 @@ +3483886,101846.1406250000,-0.0010468750 +3717104,101845.0000000000,-0.0012500000 +3950401,101845.1640625000,-0.0011093750 +4183577,101844.1796875000,-0.0009843750 +4416920,101843.5781250000,-0.0010625000 +4650168,101846.8515625000,-0.0010781250 +4883384,101843.0781250000,-0.0010625000 +5116670,101842.2109375000,-0.0011562500 +5349882,101847.2343750000,-0.0011250000 +5583204,101845.4375000000,-0.0010625000 +5816445,101845.1093750000,-0.0011093750 diff --git a/test/Nose/trash/test1.xlsx b/test/Nose/trash/test1.xlsx new file mode 100644 index 0000000..88d9227 Binary files /dev/null and b/test/Nose/trash/test1.xlsx differ diff --git a/test/Nose/xiaotest/xiaotest.ino b/test/Nose/xiaotest/xiaotest.ino new file mode 100644 index 0000000..e6c651a --- /dev/null +++ b/test/Nose/xiaotest/xiaotest.ino @@ -0,0 +1,9 @@ +void setup() { + pinMode(13, OUTPUT); +} +void loop() { + digitalWrite(13, HIGH); + delay(1000); + digitalWrite(13, LOW); + delay(1000); +}