/*=================================================================================*/ /* Project : Icom CI-V Tune Button */ /* Description : Starts a Tune Sequence via CI-V */ /* */ /* File : IC_Tune.ino */ /* Version : 0.2 */ /* Date : 07/04/2024 */ /* Author : Bjorn Pasteuning - PD5DJ */ /* */ /* Version history */ /* 07/04/2024 : V0.1 : First release. */ /* 11/04/2024 : V0.2 : Added SPLIT read/write */ /* : Added Status LED on pin D13 */ /*=================================================================================*/ /* .---------------------------------------. | | | VCC | | | | | .-. | | | | | | | | 10K | | '-' | | | | | | | | ARDUINO PIN 9 TX ----|<---o----> CIV | | 1N4148 | | | | | | .-. | | | | | | | | 4K7 | | '-' | | | | | ARDUINO PIN 8 RX ---------- | | | | | | TUNE BUTTON | | _/ | | ARDUINO PIN 2 ---o/ o-. | | | | | | | | === | | GND | | ____ | | ARDUINO PIN 13 ---|____|---->|----. | | \\ | | | 470R LED | | | === | | GND | | | '---------------------------------------' */ //Uncomment to enable serial debug output //#define DEBUG #include #define CIVBAUD_RATE 19200 // CI-V speed #define TRX_address ((byte)00) // HEX 0x70 = Icom IC-7000 , HEX 0x94 = Icom IC-7300, (byte)00 works for all radios #define TUNE_BUTTON 2 // Pin used for TUNE Button #define STATUS_LED 13 // Pin used for Status LED SoftwareSerial CIV = SoftwareSerial(8, 9); // (RX, TX) uint16_t readCounter; // CI-V bytes counter // Led Blinking parameters const unsigned long blinkInterval = 100; unsigned long previousMillis = 0; int ledState = LOW; // Global Variables for storing Settings and Modes. uint16_t OPERATING_MODE; uint16_t FILTER; uint16_t DATA_MODE; uint16_t POWER; uint16_t PowerByte1; uint16_t PowerByte2; uint16_t SPLIT; // Reads Operating mode in first byte, 0 = LSB, 1 = USB, 2 = AM, 3 = CW, 4 = RTTY, 5 = FM, 7 = CW-R, 8 = RTTY-R // Reads Filter in second byte, 1 = Filter 1, 2 = Filter 2, 3 = Filter 3 uint16_t read_operating_filter_mode() { CIV.flush(); // start sequence: send "read mode" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x04); CIV.write(0xFD); delay(20); // now read info from radio uint16_t nbChar = CIV.available(); uint16_t Byte1, Byte2; if (nbChar > 0) { for (uint16_t readCounter = 0; readCounter < nbChar; readCounter++) { byte byteRead = CIV.read(); if (readCounter == 5) { Byte1 = (byteRead); // First byte: convert from BCD to decimal. } if (readCounter == 6) { Byte2 = (byteRead); // First byte: convert from BCD to decimal. delay(20); } } OPERATING_MODE = Byte1; FILTER = Byte2; } } // Reads Operating mode, 0 = LSB, 1 = USB, 2 = AM, 3 = CW, 4 = RTTY, 5 = FM, 7 = CW-R, 8 = RTTY-R uint16_t read_operating_mode() { CIV.flush(); // start sequence: send "read mode" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x04); CIV.write(0xFD); delay(20); // now read info from radio uint16_t nbChar = CIV.available(); uint16_t Byte1; uint16_t val; if (nbChar > 0) { for (uint16_t readCounter = 0; readCounter < nbChar; readCounter++) { byte byteRead = CIV.read(); if (readCounter == 5) { Byte1 = (byteRead); // First byte: convert from BCD to decimal. delay(20); } } val = Byte1; return val; } } // Reads Data mode, 0 = OFF, 1 = ON uint16_t read_data_mode() { CIV.flush(); // start sequence: send "read data" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x1A); CIV.write(0x06); CIV.write(0xFD); delay(20); // now read info from radio uint16_t nbChar = CIV.available(); uint16_t Byte1; uint16_t val; if (nbChar > 0) { for (uint16_t readCounter = 0; readCounter < nbChar; readCounter++) { byte byteRead = CIV.read(); if (readCounter == 6) { Byte1 = (byteRead); // First byte: convert from BCD to decimal. delay(20); } } val = Byte1; return val; } } uint16_t read_power() { CIV.flush(); // start sequence: send "read power" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x14); CIV.write(0x0A); CIV.write(0xFD); delay(20); // now read info from radio uint16_t nbChar = CIV.available(); uint16_t Byte1, Byte2; uint16_t val; if (nbChar > 0) { for (uint16_t readCounter = 0; readCounter < nbChar; readCounter++) { byte byteRead = CIV.read(); if (readCounter == 6) { #ifdef DEBUG Byte1 = ((byteRead / 16 * 10) + (byteRead % 16)); // First byte: convert from BCD to decimal. #endif PowerByte1 = byteRead; } if (readCounter == 7) { #ifdef DEBUG Byte2 = ((byteRead / 16 * 10) + (byteRead % 16)); // Second byte: convert from BCD to decimal. #endif PowerByte2 = byteRead; delay(20); } } val = (Byte1 * 100) + Byte2; // Calculate and write the S-meter value on the S-meter output pin. return val; } } // Reads Split mode, 0 = OFF, 1 = ON uint16_t read_split() { CIV.flush(); // start sequence: send "read Split state" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x0F); CIV.write(0xFD); delay(20); // now read info from radio uint16_t nbChar = CIV.available(); uint16_t Byte1; uint16_t val; if (nbChar > 0) { for (uint16_t readCounter = 0; readCounter < nbChar; readCounter++) { byte byteRead = CIV.read(); if (readCounter == 5) { Byte1 = (byteRead); // First byte: convert from BCD to decimal. delay(20); } } val = Byte1; return val; } } // Sets Operating mode in first byte, 0 = LSB, 1 = USB, 2 = AM, 3 = CW, 4 = RTTY, 5 = FM, 7 = CW-R, 8 = RTTY-R // Sets Filter in second byte, 1 = Filter 1, 2 = Filter 2, 3 = Filter 3 void Set_Operating_Filter_Mode(uint16_t opmode, uint16_t filtmode) { CIV.flush(); CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x01); CIV.write(opmode); CIV.write(filtmode); CIV.write(0xFD); } // Sets Operating mode, 0 = LSB, 1 = USB, 2 = AM, 3 = CW, 4 = RTTY, 5 = FM, 7 = CW-R, 8 = RTTY-R void Set_Operating_Mode(uint16_t opmode) { CIV.flush(); CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x06); CIV.write(opmode); CIV.write(0xFD); } // Sets Data mode, 0 = OFF, 1 = ON void Set_Data_Mode(uint16_t datamode) { CIV.flush(); CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x1A); CIV.write(0x06); CIV.write(datamode); CIV.write(0xFD); } // Sets Split, 0 = OFF, 1 = ON void Set_Split(uint16_t split) { CIV.flush(); CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x0F); CIV.write(split); CIV.write(0xFD); } // Sets Power Level void Set_Power(uint16_t Byte1, uint16_t Byte2) { CIV.flush(); // start sequence: send "TX" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x14); CIV.write(0x0A); CIV.write(Byte1); CIV.write(Byte2); CIV.write(0xFD); } // Sets TX/RX state of transceiver, false = OFF, true = ON void Set_TX(bool onoff) { CIV.flush(); // start sequence: send "TX" command to radio. CIV.write(0xFE); CIV.write(0xFE); CIV.write(TRX_address); CIV.write(0xE0); CIV.write(0x1C); CIV.write((byte)00); if (onoff) { CIV.write(0x01); } else { CIV.write((byte)00); } CIV.write(0xFD); } //--------------------------------------------------------------------------------------------- void setup() { // Starts CI-V Communication CIV.begin(CIVBAUD_RATE); CIV.listen(); while (CIV.available()) CIV.read(); // clean buffer #ifdef DEBUG // Serial Debugging Serial.begin(115200); #endif // Configures TUNE Button pinMode(TUNE_BUTTON, INPUT_PULLUP); pinMode(STATUS_LED, OUTPUT); digitalWrite(STATUS_LED, LOW); } //--------------------------------------------------------------------------------------------- void loop() { // Read CAT data continuously DATA_MODE = read_data_mode(); POWER = read_power(); SPLIT = read_split(); read_operating_filter_mode(); unsigned long currentMillis = millis(); // Dirty delay to slow down CIV data polling. delay(200); #ifdef DEBUG Serial.print("Mode: "); Serial.print(OPERATING_MODE); Serial.print(", Filter: "); Serial.print(FILTER); Serial.print(", Data: "); Serial.print(DATA_MODE); Serial.print(", SPLIT: "); Serial.print(SPLIT); Serial.print(", Power: "); Serial.println(POWER); #endif if (digitalRead(TUNE_BUTTON) == HIGH) digitalWrite(STATUS_LED, LOW); // When TUNE button is pressed, sequence starts. if (digitalRead(TUNE_BUTTON) == LOW) { if (SPLIT == 1) { // Blink Status led if (currentMillis - previousMillis >= blinkInterval) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(STATUS_LED, ledState); } } else { //Set_Split((byte)00); Set_Operating_Mode(4); Set_Power((byte)00, 0x26); // 0x26 = 10% of Power Set_TX(true); digitalWrite(STATUS_LED,HIGH); //Loop waits for the button to be released, sequence then continues to restore all settings and modes. while (digitalRead(2) == LOW) {} Set_TX(false); digitalWrite(STATUS_LED, LOW); //Set_Split(SPLIT); Set_Operating_Filter_Mode(OPERATING_MODE, FILTER); // Recover Operating mode + filter Set_Data_Mode(DATA_MODE); // Recover Data Mode Set_Power(PowerByte1, PowerByte2); // Recover Power Level } } }