/* ////////////////////////////////////////////////////////////////// ARDUINO/Genuino Project "LEVELMOD", a Broadband RF Power Meter https://www.changpuak.ch/electronics/Arduino-LevelMod.php Software Version 1.0 28.10.2020 by ALEXANDER SSE FRANK 13.06.2022 replaced all that Eeprom Calibration things 22.06.2022 fixed wrong Eeprom addresses 28.06.2022 fixed nan display when CAL ////////////////////////////////////////////////////////////////// */ #include #include #include // DISPLAY #define OLED_MOSI 8 #define OLED_CLK 9 #define OLED_DC 6 #define OLED_CS 5 #define OLED_RESET 7 int MAX_BAR_LENGTH = 127 ; int MIN_BAR_LENGTH = 0 ; int BARY = 45 ; // ROTARY ENCODER const int RotaryEncoder1 = 4 ; // PRESSED const int RotaryEncoder2 = 2 ; const int RotaryEncoder3 = 3 ; volatile boolean LEFT = false ; volatile boolean RIGHT = false ; volatile boolean READY = true ; Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); #if (SH1106_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SH1106.h!"); #endif unsigned int SENSOR_TYPE = 0x0000 ; unsigned int SENSOR_TYPE_OLD = 0x0000 ; unsigned int SERIAL_SENSOR = 0x00 ; int RSSI_ARD_PIN = A0 ; int SUPPLY_ARD_PIN = A7 ; float SUPPLY_ADC = 5.024 ; unsigned int RSSI_ARD_RAW = 0 ; unsigned int MIN_RSSI_ARD_RAW = 20 ; // BELOW : NO SENSOR, MEASURED 70 mV float RSSI_ARD = -19.9 ; float RSSI_ADC = -19.9 ; // FREQUENCIES IN MHz float F_LOW = 1.0 ; // FROM EEPROM float F_HIGH = 500.0 ; // FROM EEPROM // CALCULATED VALUES float F_ARRAY[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13} ; // LEVELS IN dBm float L_LOW = -70.0 ; // FROM EEPROM, LOWEST VALUE float L_HIGH = 10.0 ; // FROM EEPROM, HIGHEST VALUE float SLOPE[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1} ; float INTERCEPT[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1} ; // POINTER TO APPLIED DATASET int F_POINTER = 6 ; // ///////////////////////////////////////////////////////////// // Prints the title block when program first starts. // ///////////////////////////////////////////////////////////// const float Version = 22.06 ; void print_title() { Serial.println("Welcome to Levelmod"); Serial.print("Software Version : "); Serial.println(Version,2); Serial.println("by Alexander C. Frank, (C) 2020"); Serial.println("======================================"); } // ///////////////////////////////////////////////////////////// // Prints a list of Options // ///////////////////////////////////////////////////////////// void print_options() { Serial.println("\n>>> WAS GUCKST DU ???"); Serial.println("----- SENSOR INFORMATION --------------"); Serial.println("[t] Write Sensor Type"); Serial.println("[n] Write Serial Number"); Serial.println("[r] Write Reference Voltage"); Serial.println("----- SENSOR CALIBRATION VALUES -------"); Serial.println("[f] Write minimum Frequency"); Serial.println("[g] Write maximum Frequency"); Serial.println("[i] Write lower Level"); Serial.println("[j] Write upper Level"); Serial.println("[p] Change Pointer to Dataset."); Serial.print("[l] Write Slope ["); Serial.print(F_POINTER,DEC); Serial.println("]"); Serial.print("[m] Write Intercept ["); Serial.print(F_POINTER,DEC); Serial.println("]"); Serial.println("----- OTHER FUNCTIONS -----------------"); Serial.println("[s] Init Sensor"); Serial.println("[h] Hex Dump Eeprom"); Serial.println("[d] Display Eeprom Contents"); Serial.println("---------------------------------------"); } // ///////////////////////////////////////////////////////////// // Serial Communication Routines : USER AND EEPROM // ///////////////////////////////////////////////////////////// #define EEPROM_24C01_I2CADDR 0x50 char Buffy[20] ; // holds User Input from Serial byte byteRead ; // ///////////////////////////////////////////////////////////// void InitBuffy() // ///////////////////////////////////////////////////////////// { for (int i=0; i<20; i++) Buffy[i] = 32 ; // SPACE } void ShowBuffy() { for (int i=0; i<19; i++) Serial.print(char(Buffy[i])); } // ///////////////////////////////////////////////////////////// void ReadUserInput() // ///////////////////////////////////////////////////////////// { int pointer = 0 ; if(Serial.available() > 0) { while(Serial.available()) { Buffy[pointer] = Serial.read(); if ( pointer < 19 ) pointer++ ; } } } // ///////////////////////////////////////////////////////////// void WaitUserInput() // ///////////////////////////////////////////////////////////// { int pointer = 0 ; boolean ende = false ; while (!ende) { if(Serial.available()) { Buffy[pointer] = Serial.read(); if(Buffy[pointer] == 10) ende = true ; if(Buffy[pointer] == 13) ende = true ; if ( pointer < 19 ) pointer++ ; } delay(5) ; } } // ///////////////////////////////////////////////////////////// int ReadInteger(int WoStehtDasImEeprom) // ///////////////////////////////////////////////////////////// { int Value = 0 ; Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(WoStehtDasImEeprom); Wire.endTransmission(); Wire.requestFrom(EEPROM_24C01_I2CADDR,2); delay(5); if(Wire.available() == 2) { Value = Wire.read(); Value = ( Value << 8 ) | (Wire.read()) ; } return Value ; } // ///////////////////////////////////////////////////////////// void ChangeIntegerValue(int WoStehtDasImEeprom) // ///////////////////////////////////////////////////////////// { // READ EXISTING VALUE unsigned int Number = 0 ; unsigned int aux = 0; byte Part[2]; Number = ReadInteger(WoStehtDasImEeprom); Serial.print("\nActual value : "); Serial.print(Number,DEC); Serial.println("\n\nEnter new value. Press ENTER.\n"); InitBuffy(); WaitUserInput(); Number = Buffy[0] - 48 ; aux=Buffy[1]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;} aux=Buffy[2]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;} aux=Buffy[3]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;} aux=Buffy[4]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;} Part[1] = ( Number & 0xFF); Part[0] = ((Number >> 8) & 0xFF); Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(WoStehtDasImEeprom); Wire.write(Part[0]); delay(5); Wire.write(Part[1]); Wire.endTransmission(); delay(5); Serial.println("O.K.\n\n"); } // ///////////////////////////////////////////////////////////// float ReadFloat(int WoStehtDasImEeprom) // ///////////////////////////////////////////////////////////// { float Value = 0.0 ; String StringOne = ""; union MyUnion{ float f; byte b[4]; }; MyUnion myData; Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(WoStehtDasImEeprom); Wire.endTransmission(); Wire.requestFrom(EEPROM_24C01_I2CADDR,4); delay(5); if(Wire.available() == 4) { myData.b[0] = Wire.read(); // MSB myData.b[1] = Wire.read(); myData.b[2] = Wire.read(); myData.b[3] = Wire.read(); } Value = myData.f ; return Value ; } // ///////////////////////////////////////////////////////////// // CHANGE A FLOAT VALUE OF EEPROM // ///////////////////////////////////////////////////////////// void ChangeFloatValue(int WoStehtDasImEeprom) { String StringOne = ""; union MyUnion{ float f; byte b[4]; }; MyUnion myData; float ActualValue = 0.0 ; ActualValue = ReadFloat(WoStehtDasImEeprom) ; Serial.print("Actual value : "); Serial.println(ActualValue,4); Serial.print("\n"); Serial.println(">>> Enter new value. Press ENTER."); WaitUserInput(); for ( int w = 0 ; w < 9 ; w++ ) { StringOne += Buffy[w] ; } myData.f = StringOne.toFloat() ; for (int address = 0; address < 4; address++) { Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(address+WoStehtDasImEeprom); Wire.write(myData.b[address]); Wire.endTransmission(); delay(3); } Serial.println("\nO.K.\n"); } // ///////////////////////////////////////////////////////////// // I2C Communication Routines ADC // ///////////////////////////////////////////////////////////// #define ADC_MCP3221_I2CADDR 0x4E long RSSI_ADC_RAW = 0 ; double RSSI_ADC_VOLT = 0.0 ; void UpDate_ADC_RSSI() { if(SENSOR_TYPE == 0xFFFF) SUPPLY_ADC = 5.0 ; Wire.requestFrom(ADC_MCP3221_I2CADDR, 2) ; // 2 BYTES delay(5); if (Wire.available() == 2) { byte a = Wire.read() ; byte b = Wire.read() ; RSSI_ADC_RAW = ( a & 0x0F ); RSSI_ADC_RAW = (RSSI_ADC_RAW << 8) | b ; RSSI_ADC_VOLT = RSSI_ADC_RAW / 4096.0 ; RSSI_ADC_VOLT *= SUPPLY_ADC ; } else { // NO ANSWER == NO SENSOR SENSOR_TYPE = 0x0000 ; RSSI_ADC_VOLT = 0.003 ; } } void Apply_RSSI_CAL() { RSSI_ADC = RSSI_ADC_VOLT * SLOPE[F_POINTER] + INTERCEPT[F_POINTER] ; delay(50); // Some limit checking ... if(RSSI_ADC < L_LOW) RSSI_ADC = L_LOW; if(RSSI_ADC > L_HIGH) RSSI_ADC = L_HIGH ; } void CheckSensorType() { SENSOR_TYPE = 0x0000 ; // NO SENSOR // UNPROGRAMMED SENSOR : SENSOR_TYPE = 0xFFFF = 65535 RSSI_ARD_RAW = analogRead(RSSI_ARD_PIN) ; if( RSSI_ARD_RAW > MIN_RSSI_ARD_RAW ) { // SOMETHING IS THERE ! SENSOR_TYPE = ReadInteger(0x00) ; } } // ///////////////////////////////////////////////////////////// // READ CAL DATA FROM SENSOR // ///////////////////////////////////////////////////////////// void InitSensor() { SENSOR_TYPE = ReadInteger(0x00) ; SERIAL_SENSOR = ReadInteger(0x02) ; SUPPLY_ADC = ReadFloat(0x04) ; F_LOW = ReadFloat(0x08) ; F_HIGH = ReadFloat(0x0C); // POPULATE FREQUENCY ARRAY float DIFF = (F_HIGH - F_LOW) / 100.0 ; F_ARRAY[0] = F_LOW ; F_ARRAY[1] = F_ARRAY[0] + 10.0 * DIFF ; F_ARRAY[2] = F_ARRAY[1] + 10.0 * DIFF ; F_ARRAY[3] = F_ARRAY[2] + 10.0 * DIFF ; F_ARRAY[4] = F_ARRAY[3] + 10.0 * DIFF ; F_ARRAY[5] = F_ARRAY[4] + 10.0 * DIFF ; F_ARRAY[6] = F_ARRAY[5] + 10.0 * DIFF ; F_ARRAY[7] = F_ARRAY[6] + 10.0 * DIFF ; F_ARRAY[8] = F_ARRAY[7] + 10.0 * DIFF ; F_ARRAY[9] = F_ARRAY[8] + 8.0 * DIFF ; F_ARRAY[10] = F_ARRAY[9] + 6.0 * DIFF ; F_ARRAY[11] = F_ARRAY[10] + 4.0 * DIFF ; F_ARRAY[12] = F_ARRAY[11] + 2.0 * DIFF ; L_LOW = ReadFloat(0x10) ; L_HIGH = ReadFloat(0x14) ; // POPULATE LEVEL ARRAYS for(int i=0; i<13; i++) { SLOPE[i] = ReadFloat(0x18 + i*8) ; // VOLTAGE @ L_LOW INTERCEPT[i] = ReadFloat(0x1C + i*8) ; // VOLTAGE @ L_HIGH } } // //////////////////////////////////// // Read and display contents of Eeprom // //////////////////////////////////// void ReadEeprom() { byte rdata = 0xAF ; for (int address = 0; address < 128; address+=8) { if (address < 16) Serial.print("0"); Serial.print(address,HEX); Serial.print("\t"); // One row, HEX for (int add = 0; add < 8; add++) { Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(address+add); Wire.endTransmission(); Wire.requestFrom(EEPROM_24C01_I2CADDR,1); delay(5); if (Wire.available() == 1) rdata = Wire.read(); if (rdata < 16) Serial.print("0"); Serial.print(rdata, HEX); Serial.print(" "); } Serial.print("\t"); // One row, ASCII for (int add = 0; add < 8; add++) { Wire.beginTransmission(EEPROM_24C01_I2CADDR); Wire.write(address+add); Wire.endTransmission(); Wire.requestFrom(EEPROM_24C01_I2CADDR,1); delay(5); if (Wire.available() == 1) rdata = Wire.read(); if (rdata < 32) { Serial.print("."); } else if (rdata > 126) { Serial.print("."); } else { Serial.print(char(rdata)); } Serial.print(" "); } Serial.println(); } Serial.println("\nO.K.\n\n"); } // ///////////////////////////////////////////////////////////// // ANALOG INPUTS // ///////////////////////////////////////////////////////////// float SUPPLY_ARD_VOLT = 0.0 ; float SUPPLY_ARD_VOLT_MIN = 11.0 ; const float SUPPLY_DIVIDER = 2.4 / (2.4 + 12.0) ; // RESISTOR RATIO void UpDateSupplyVoltage() { unsigned int SUPPLY_ARD_RAW = analogRead(SUPPLY_ARD_PIN) ; SUPPLY_ARD_VOLT = 0.001 * (float)(map(SUPPLY_ARD_RAW,0,1023,0,5000)) / SUPPLY_DIVIDER ; } // ///////////////////////////////////////////////////////////// // SUBROUTINES DISPLAY. // ///////////////////////////////////////////////////////////// void BAR(float value) { int BAR_LENGTH = (int)(120.0 + value) ; if (BAR_LENGTH > MAX_BAR_LENGTH) BAR_LENGTH = MAX_BAR_LENGTH ; if (BAR_LENGTH < MIN_BAR_LENGTH) BAR_LENGTH = MIN_BAR_LENGTH ; display.fillRect(0, BARY, BAR_LENGTH, 7, WHITE); display.fillRect(BAR_LENGTH, BARY+1, MAX_BAR_LENGTH-BAR_LENGTH, 5, BLACK); display.drawRect(0, BARY, 127, 7, WHITE); } void UpDateDisplayRSSI() { int offset = 0 ; float RSSI_DISPLAY = 0.0 ; float FREQ_DISPLAY = 0.0 ; display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.print("****"); display.setCursor(39,0); display.print("LEVELMOD"); display.setCursor(104,0); display.print("****"); display.drawLine(0, 12, 128, 12, WHITE); display.setTextSize(2) ; // LEVEL display.setCursor(0,20); // SIGN if(RSSI_ADC < 0.0) display.print("- ") ; if(RSSI_ADC == 0.0) display.print(" ") ; if(RSSI_ADC > 0.0) display.print("+ ") ; RSSI_DISPLAY = RSSI_ADC ; if(RSSI_ADC < 0.0) RSSI_DISPLAY = -1.0 * RSSI_ADC ; display.setCursor(20,20); if(RSSI_DISPLAY < 10.0) display.print(" ") ; display.print(RSSI_DISPLAY,2) ; display.setCursor(90,20); display.print("dBm") ; // RSSI BAR // DRAW TICKS RSSI BAR display.setTextSize(1) ; // display.setCursor(0,35) ; for (int i=0; i<=6; i++) { display.drawLine(i*20, BARY-4, i*20, BARY+1, WHITE) ; } for (int i=0; i<=25; i++) { display.drawLine(i*5, BARY-2, i*5, BARY+1, WHITE) ; } BAR(RSSI_ADC) ; display.setTextSize(1); display.setCursor(0, 57) ; // SUPPLY VOLTAGE CHECK UpDateSupplyVoltage() ; if(SUPPLY_ARD_VOLT < SUPPLY_ARD_VOLT_MIN) { display.print("SUPPLY LOW"); } if(SUPPLY_ARD_VOLT >= SUPPLY_ARD_VOLT_MIN) { // //////////////////////////////////////////// // DISPLAY SENSOR + SERIALNUMBER // //////////////////////////////////////////// if(SENSOR_TYPE == 8307) display.print("AD8307"); if(SENSOR_TYPE == 5537) display.print("LT5537"); if(SENSOR_TYPE == 8318) display.print("AD8318"); display.print("-"); if(SERIAL_SENSOR < 1000) display.print("0"); if(SERIAL_SENSOR < 100) display.print("0"); if(SERIAL_SENSOR < 10) display.print("0"); display.print(SERIAL_SENSOR,DEC); // //////////////////////////////////////////// // DISPLAY FREQUENCY // //////////////////////////////////////////// display.setCursor(80, 57) ; // > 10 GHz if(F_ARRAY[F_POINTER]>=10000.0) { FREQ_DISPLAY = F_ARRAY[F_POINTER] / 1000.0 ; display.print(FREQ_DISPLAY,1); // FF.F GHz display.print(" GHz"); } // 1 GHz ... 9.999 GHz if((F_ARRAY[F_POINTER]>=1000.0) && (F_ARRAY[F_POINTER]<10000.0)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] / 1000.0 ; display.print(FREQ_DISPLAY,2); // F.FF GHz display.print(" GHz"); } // 100 MHz ... 999.9 MHz if((F_ARRAY[F_POINTER]>=100.0) && (F_ARRAY[F_POINTER]<1000.0)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] ; display.print(FREQ_DISPLAY,0); // FFF MHz display.print(" MHz"); } // 10 MHz ... 99.99 MHz if((F_ARRAY[F_POINTER]>=10.0) && (F_ARRAY[F_POINTER]<100.0)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] ; display.print(FREQ_DISPLAY,1); // FF.F MHz display.print(" MHz"); } // 1 MHz ... 9.999 MHz if((F_ARRAY[F_POINTER]>=1.0) && (F_ARRAY[F_POINTER]<10.0)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] ; display.print(FREQ_DISPLAY,2); // F.FF MHz display.print(" MHz"); } // 0.1 MHz ... 0.999 MHz if((F_ARRAY[F_POINTER]>=0.1) && (F_ARRAY[F_POINTER]<1.0)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ; display.print(FREQ_DISPLAY,0); // FFF kHz display.print(" kHz"); } // 0.01 MHz ... 0.0999 MHz if((F_ARRAY[F_POINTER]>=0.01) && (F_ARRAY[F_POINTER]<0.1)) { FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ; display.print(FREQ_DISPLAY,1); // FF.F kHz display.print(" kHz"); } // < 0.01 MHz if(F_ARRAY[F_POINTER]<0.01) { FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ; display.print(FREQ_DISPLAY,2); // F.FF kHz display.print(" kHz"); } } display.display() ; } void UpDateDisplayCAL() { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.print("****"); display.setCursor(39,0); display.print("LEVELMOD"); display.setCursor(104,0); display.print("****"); display.drawLine(0, 12, 128, 12, WHITE); display.setTextSize(2) ; double RSSI_SUM = 0.0 ; display.setCursor(0,24); display.println("CAL SENSOR") ; display.setCursor(0,50); for(int i=0; i<100; i++) { UpDate_ADC_RSSI() ; RSSI_SUM += RSSI_ADC_VOLT ; delay(19) ; } RSSI_ADC_VOLT = RSSI_SUM / 100.0 ; // SUPPLY VOLTAGE CHECK UpDateSupplyVoltage() ; if(SUPPLY_ARD_VOLT < SUPPLY_ARD_VOLT_MIN) { display.print("LOW BATT") ; } if(SUPPLY_ARD_VOLT >= SUPPLY_ARD_VOLT_MIN) { display.print("+ ") ; display.print(RSSI_ADC_VOLT, 4) ; display.print(" V") ; } display.display() ; } void UpDateDisplayNOHAVE() { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.print("****"); display.setCursor(39,0); display.print("LEVELMOD"); display.setCursor(104,0); display.print("****"); display.drawLine(0, 12, 128, 12, WHITE); display.setTextSize(2) ; display.setCursor(0,17); display.println("ERROR 404") ; display.println("DETECTOR") ; display.print("NOT FOUND") ; display.display() ; } // ///////////////////////////////////////////////////////////// // S E T U P // ///////////////////////////////////////////////////////////// void setup() { Serial.begin(115200) ; Wire.begin() ; // INIT OLED display.begin(SH1106_SWITCHCAPVCC); // SHOW STARTUP SCREEN display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.print("****"); display.setCursor(39,0); display.print("LEVELMOD"); display.setCursor(104,0); display.print("****"); display.drawLine(0, 12, 128, 12, WHITE); display.setTextSize(1); display.setCursor(0, 21); display.println("A DC TO DAYLIGHT "); display.setCursor(0, 33); display.println("MICROWAVE POWERMETER"); display.setCursor(0, 45); display.println("(C) ETH QUANTUMOPTICS"); display.setCursor(0, 57); display.println("BUILT 25.07.2022"); display.display(); delay(999) ; pinMode(RotaryEncoder1, INPUT_PULLUP); pinMode(RotaryEncoder2, INPUT_PULLUP); pinMode(RotaryEncoder3, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(RotaryEncoder2), RotaryEncoderISR2, FALLING); attachInterrupt(digitalPinToInterrupt(RotaryEncoder3), RotaryEncoderISR3, FALLING); print_title() ; print_options() ; pinMode(RSSI_ARD_PIN, INPUT) ; delay(3000); InitSensor() ; } // ///////////////////////////////////////////////////////////// // M A I N L O O P // ///////////////////////////////////////////////////////////// void loop() { // ////////////////////////////////// // EVALUATE ROTARY ENCODER // CHANGE DISPLAYED FREQUENCY // ////////////////////////////////// if (LEFT) { READY = false ; F_POINTER -= 1 ; if (F_POINTER < 0) F_POINTER = 0 ; LEFT = false ; RIGHT = false ; delay(149) ; READY = true ; } if (RIGHT) { READY = false ; F_POINTER += 1 ; if (F_POINTER > 12) F_POINTER = 12 ; LEFT = false ; RIGHT = false ; delay(149) ; READY = true ; } // ////////////////////////////////// // HANDLE SENSORS // ////////////////////////////////// SENSOR_TYPE_OLD = SENSOR_TYPE ; CheckSensorType() ; if(SENSOR_TYPE != SENSOR_TYPE_OLD) InitSensor() ; switch (SENSOR_TYPE) { case 0xFFFF : // UNPROGRAMMED SENSOR // 0xFFFF = 65535 UpDateDisplayCAL() ; delay(99) ; break ; case 0x0000 : // NO SENSOR UpDateDisplayNOHAVE() ; delay(99) ; break ; case 8307 : UpDateSupplyVoltage() ; UpDate_ADC_RSSI() ; Apply_RSSI_CAL() ; UpDateDisplayRSSI() ; break ; case 5537 : UpDateSupplyVoltage() ; UpDate_ADC_RSSI() ; Apply_RSSI_CAL() ; UpDateDisplayRSSI() ; break ; case 8313 : UpDateSupplyVoltage() ; UpDate_ADC_RSSI() ; Apply_RSSI_CAL() ; UpDateDisplayRSSI() ; break ; } // ////////////////////////////////// // HANDLE SERIAL COMMANDS // ////////////////////////////////// InitBuffy(); ReadUserInput(); unsigned int user_command ; // CHARACTER user_command = Buffy[0] ; switch (user_command) { case '*' : // *IDN? if((Buffy[1]=='I')&&(Buffy[2]=='D')&&(Buffy[3]=='N')&&(Buffy[4]=='?')) { Serial.println("Levelmod V2.0 by Changpuak.ch (C) 09/2022") ; } // *FL? = Frequency Low in MHz if((Buffy[1]=='F')&&(Buffy[2]=='L')&&(Buffy[3]=='?')) { Serial.println(F_LOW,1) ; } // *FH? = Frequency High in MHz if((Buffy[1]=='F')&&(Buffy[2]=='H')&&(Buffy[3]=='?')) { Serial.println(F_HIGH,1) ; } // *LL? = Level Low in dBm if((Buffy[1]=='L')&&(Buffy[2]=='L')&&(Buffy[3]=='?')) { Serial.println(L_LOW,4) ; } // *LH? = Level High in dBm if((Buffy[1]=='L')&&(Buffy[2]=='H')&&(Buffy[3]=='?')) { Serial.println(L_HIGH,4) ; } // *SP0! = Set Frequency Pointer to 0 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='0')&&(Buffy[4]=='!')) { F_POINTER = 0 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP1! = Set Frequency Pointer to 1 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='1')&&(Buffy[4]=='!')) { F_POINTER = 1 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP2! = Set Frequency Pointer to 2 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='2')&&(Buffy[4]=='!')) { F_POINTER = 2 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP3! = Set Frequency Pointer to 3 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='3')&&(Buffy[4]=='!')) { F_POINTER = 3 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP4! = Set Frequency Pointer to 4 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='4')&&(Buffy[4]=='!')) { F_POINTER = 4 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP5! = Set Frequency Pointer to 5 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='5')&&(Buffy[4]=='!')) { F_POINTER = 5 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP6! = Set Frequency Pointer to 6 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='6')&&(Buffy[4]=='!')) { F_POINTER = 6 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP7! = Set Frequency Pointer to 7 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='7')&&(Buffy[4]=='!')) { F_POINTER = 7 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP8! = Set Frequency Pointer to 8 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='8')&&(Buffy[4]=='!')) { F_POINTER = 8 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SP9! = Set Frequency Pointer to 9 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='9')&&(Buffy[4]=='!')) { F_POINTER = 9 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SPA! = Set Frequency Pointer to 10 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='A')&&(Buffy[4]=='!')) { F_POINTER = 10 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SPB! = Set Frequency Pointer to 11 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='B')&&(Buffy[4]=='!')) { F_POINTER = 11 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *SPC! = Set Frequency Pointer to 12 if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='C')&&(Buffy[4]=='!')) { F_POINTER = 12 ; Serial.println(F_ARRAY[F_POINTER],1) ; } // *LVL? = Asks for the corrected Level in dBm if((Buffy[1]=='L')&&(Buffy[2]=='V')&&(Buffy[3]=='L')&&(Buffy[4]=='?')) { UpDate_ADC_RSSI() ; // MEASURE FIRST Apply_RSSI_CAL() ; Serial.println(RSSI_ADC,2) ; } // *VOLT? = Asks for the raw voltage measured if((Buffy[1]=='V')&&(Buffy[2]=='O')&&(Buffy[3]=='L')&&(Buffy[4]=='T')&&(Buffy[5]=='?')) { UpDate_ADC_RSSI() ; // MEASURE FIRST Serial.println(RSSI_ADC_VOLT,3) ; } // *REF? = Asks for the Reference Voltage of the Power Sensor if((Buffy[1]=='R')&&(Buffy[2]=='E')&&(Buffy[3]=='F')&&(Buffy[4]=='?')) { Serial.println(SUPPLY_ADC,3) ; } break; case 'd' : // Display Eeprom Contents Serial.println("\n********* EEPROM CONTENTS ********") ; Serial.print("Sensor Type : ") ; Serial.println(ReadInteger(0x00),DEC); Serial.print("Serial No. : ") ; Serial.println(ReadInteger(0x02),DEC); Serial.print("Reference Volt : ") ; Serial.println(ReadFloat(0x04),3); Serial.print("Min. Freq. [MHz] : ") ; Serial.println(ReadFloat(0x08),4); Serial.print("Max. Freq. [MHz] : ") ; Serial.println(ReadFloat(0x0C),4); Serial.print("Min. Level [dBm] : ") ; Serial.println(ReadFloat(0x10),4); Serial.print("Max. Level [dBm] : ") ; Serial.println(ReadFloat(0x14),4); for(int i=0; i<13; i++) { // INDEX if(i < 10 ) Serial.print("0") ; Serial.print(i,DEC) ; // LOW VALUE Serial.print(" : SLOPE = ") ; Serial.print(ReadFloat(0x18 + i*8),3) ; // HIGH VALUE Serial.print(", INTERCEPT = ") ; Serial.print(ReadFloat(0x1C + i*8),3) ; Serial.println(" ") ; } print_options(); break; case 'f' : // Write minimum Frequency Serial.print("\nWrite minimum Frequency :") ; ChangeFloatValue(0x08) ; print_options(); break; case 'g' : // Write maximum Frequency Serial.print("\nWrite minimum Frequency :") ; ChangeFloatValue(0x0C) ; print_options(); break; case 'h' : // Hex Dump Eeprom Serial.println("\nHexdump Eeprom.\n"); ReadEeprom() ; print_options(); break; case 'i' : // Write lower Level Serial.print("\nWrite lower Level :") ; ChangeFloatValue(0x10) ; print_options(); break; case 'j' : // Write upper Level Serial.print("\nWrite upper Level :") ; ChangeFloatValue(0x14) ; print_options(); break; case 'l' : // Write SLOPE Serial.print("\nWrite SLOPE #") ; Serial.println(F_POINTER,DEC) ; ChangeFloatValue(0x18 + F_POINTER * 8) ; print_options(); break; case 'm' : // Write INTERSEPT Serial.print("\nWrite INTERCEPT #") ; Serial.println(F_POINTER,DEC) ; ChangeFloatValue(0x1C + F_POINTER * 8) ; print_options(); break; case 'n' : // Write Serial Number Serial.println("\nWrite Serial Number."); ChangeIntegerValue(0x02); print_options(); break; case 'p' : // Set Pointer to Dataset { unsigned int aux ; Serial.println("\nSet Pointer to Dataset."); Serial.println("\nEnter new value. Press ENTER.\n"); InitBuffy(); WaitUserInput(); F_POINTER = Buffy[0] - 48 ; aux=Buffy[1]-48 ; if((aux >= 0)&&(aux <= 9)){F_POINTER = F_POINTER*10 + aux ;} if(F_POINTER > 12) F_POINTER = 12 ; InitBuffy(); print_options(); } break; case 'r' : // Write Reference Voltage Serial.println("\nWrite Reference Voltage.\n"); ChangeFloatValue(0x04) ; print_options(); break; case 's' : // Init Sensor Serial.println("\nInit Sensor."); InitSensor() ; Serial.print("Sensor Type : "); Serial.println(SENSOR_TYPE,DEC); print_options(); break; case 't' : // Write Sensor Type Serial.println("\nWrite Sensor Type."); ChangeIntegerValue(0x00); print_options(); break; } delay(99) ; } // ///////////////////////////////////////////////////////////// // INTERRUPT SERVICE ROUTINES // ///////////////////////////////////////////////////////////// void RotaryEncoderISR2() { if(READY) { LEFT = false ; RIGHT = false ; byte autre = digitalRead(RotaryEncoder3) ; if (autre > 0) LEFT = true ; if (autre < 1) RIGHT = true ; } } void RotaryEncoderISR3() { if(READY) { LEFT = false ; RIGHT = false ; byte autre = digitalRead(RotaryEncoder2) ; if (autre > 0) RIGHT = true ; if (autre < 1) LEFT = true ; } } // ///////////////////////////////////////////////////////////// // END OF FILE. // /////////////////////////////////////////////////////////////