Код:
/*
* UR7HFO (c)2016 simple ICOM band decoder for Arduino NANO or UNO.
*
* Default COM port speed 9600 8N1 (8 - data, N - parity, 1 - stop bit)
*
* Secondary COM port connected to TRX on pins 8(RX), 9(TX).
* Used for AltSoftSerial on NANO, and UNO boards.
* See: http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
*
* Relay output pins:
*
* 160 band - 2 pin
* 80 band - 3 pin
* 40 band - 4 pin
* 30 band - 5 pin
* 20 band - 6 pin
* 17 band - 7 pin
* 15 band - 10 pin
* 12 band - 11 pin
* 10 band - 12 pin
* 6 band - 13 pin
* 2 band - A0 pin
* 0.7 band - A1 pin
*
* Only if LCD display exist:
* LCD on UNO I2C bus pins - A4(SDA), A5(SCL).
*
* CI-V addresess :
*
* IC-7600 using address 7A hex
* IC-746 PRO using address 66 hex
* IC-751/751A using address 1C hex
* IC-756 using address 50 hex
* IC-7000 using address 70 hex
* IC-7700 using address 74 hex
*
* Other see: http://n1mm.hamdocs.com/tiki-index.php?page=Supported+Radios#Icom
*
*
*/
// Uncomment for I2C_LCD display 20x4
#define I2C_LCD
// Port connected to TRX. ( default - SoftSerial port on pins 8(RX), 9(TX) )
#define TRX_PORT_SOFTSERIAL
// default COM port speed.
unsigned int SERIAL_SPEED = 9600;
// Icom only
const char CIV_ADRESS = 0x88; // CIV input HEX Icom (0x88 for ic-7100)
const char MY_CIV_ADRESS = 0xE1; // MY CIV address.
// Command for read ICOM freq.
const char icom_cmd_read_freq[] = {0xFE, 0xFE, CIV_ADRESS, MY_CIV_ADRESS, 0x03, 0xFD};
#ifdef I2C_LCD
// Add your address here. Find it from I2C Scanner
// i2c 20x4 0x27-address display
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <LCD.h>
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
#endif // I2C_LCD
// Relay pins. Warning , 8,9 used for AltSoftSerial !
#define b160 2
#define b80 3
#define b40 4
#define b30 5
#define b20 6
#define b17 7
#define b15 10
#define b12 11
#define b10 12
#define b6 13
#define b2 A0
#define b430 A1
#define RDWR_CAT_TIMEOUT 100
#include <AltSoftSerial.h>
// Softserial obj.
AltSoftSerial SoftSerial;
// Hardware serial max buffer len 64.
byte tmpCatBufIcom[65];
String Freq = "";
unsigned int band = 0;
unsigned long freq = 0;
unsigned long currentTime = 0;
unsigned long loopTime = 0;
//
// Used procedure.
//
long GetFreqIcom(void);
void DisplayInitDraw(void);
unsigned int NewWriteBand(unsigned long);
void CatRDWR(void);
// Setup procedure
void setup()
{
pinMode(b160, OUTPUT);
pinMode(b80, OUTPUT);
pinMode(b40, OUTPUT);
pinMode(b30, OUTPUT);
pinMode(b20, OUTPUT);
pinMode(b17, OUTPUT);
pinMode(b15, OUTPUT);
pinMode(b12, OUTPUT);
pinMode(b10, OUTPUT);
pinMode(b6, OUTPUT);
pinMode(b2, OUTPUT);
pinMode(b430, OUTPUT);
// China relay module (inverted)
digitalWrite(b160, HIGH);
digitalWrite(b80, HIGH);
digitalWrite(b40, HIGH);
digitalWrite(b30, HIGH);
digitalWrite(b20, HIGH);
digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH);
digitalWrite(b12, HIGH);
digitalWrite(b10, HIGH);
digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH);
digitalWrite(b430, HIGH);
#ifdef I2C_LCD
DisplayInitDraw();
#endif
Serial.begin(SERIAL_SPEED);
Serial.setTimeout(RDWR_CAT_TIMEOUT);
SoftSerial.begin(SERIAL_SPEED);
Freq.reserve(128);
}
//
// Main loop
//
void loop()
{
currentTime = millis();
if( currentTime >= (loopTime + 1000) )
{
freq = GetFreqIcom();
if( (freq >= 1700000) && (freq <= 450000000) )
{
band = NewWriteBand(freq);
#ifdef I2C_LCD
if(band)
{
lcd.setCursor(4,0);
lcd.print(" ");
lcd.setCursor(4,0);
lcd.print(band);
}
if(freq)
{
lcd.setCursor(4,1);
lcd.print(" ");
lcd.setCursor(4,1);
lcd.print(freq);
}
#endif
}
loopTime = currentTime;
}
CatRDWR();
}
//
// Display Init.
//
#ifdef I2C_LCD
void DisplayInitDraw(void)
{
lcd.begin(20,4);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("UR7HFO ICOM");
lcd.setCursor(0,1);
lcd.print("BAND DECODER");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BND");
lcd.setCursor(0,1);
lcd.print("FRQ ");
}
#endif // I2C_LCD
//
// NewWriteBand.
//
unsigned int NewWriteBand(unsigned long freq)
{
// 160 m
if ( (freq >= 1700000) && (freq <= 2100000) )
{
digitalWrite(b160, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 160;
}
// 80 m
if ( (freq >= 3400000) && (freq <= 3900000) )
{
digitalWrite(b80, LOW);
digitalWrite(b160, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 80;
}
// 40 m
if ( (freq >= 6900000) && (freq <= 7300000) )
{
digitalWrite(b40, LOW);
digitalWrite(b80, HIGH); digitalWrite(b160, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 40;
}
// 30 m
if ( (freq >= 9900000) && (freq <= 10200000) )
{
digitalWrite(b30, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b160, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 30;
}
// 20 m
if ( (freq >= 13900000) && (freq <= 14450000) )
{
digitalWrite(b20, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b160, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 20;
}
// 17 m
if ( (freq >= 17900000) && (freq <= 18300000) )
{
digitalWrite(b17, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b160, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 17;
}
// 15 m
if ( (freq >= 20900000) && (freq <= 21600000) )
{
digitalWrite(b15, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b160, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 15;
}
// 12 m
if ( (freq >= 24700000) && (freq <= 25100000) )
{
digitalWrite(b12, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b160, HIGH); digitalWrite(b10, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 12;
}
// 10 m
if ( (freq >= 27000000) && (freq <= 29800000) )
{
digitalWrite(b10, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b160, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 10;
}
// 6 m
if ( (freq >= 49000000) && (freq <= 55000000) )
{
digitalWrite(b6, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b160, HIGH);
digitalWrite(b2, HIGH); digitalWrite(b430, HIGH);
return 6;
}
// 2 m
if ( (freq >= 143500000) && (freq <= 146000000) )
{
digitalWrite(b2, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b160, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b430, HIGH);
return 2;
}
// 0.7 m
if ( (freq >= 420000000) && (freq <= 450000000) )
{
digitalWrite(b430, LOW);
digitalWrite(b80, HIGH); digitalWrite(b40, HIGH); digitalWrite(b30, HIGH); digitalWrite(b20, HIGH); digitalWrite(b17, HIGH);
digitalWrite(b15, HIGH); digitalWrite(b12, HIGH); digitalWrite(b10, HIGH); digitalWrite(b160, HIGH); digitalWrite(b6, HIGH);
digitalWrite(b2, HIGH);
return 430;
}
return 0;
}
//
// Read and write cat data from/to serial int.
//
void CatRDWR()
{
// Read from primary, and send to secondary port.
while ( Serial.available() )
{
SoftSerial.write( Serial.read() );
SoftSerial.flush();
}
// Read from secondary, and send to primary.
while ( SoftSerial.available() )
{
Serial.write( SoftSerial.read() );
Serial.flush();
}
}
//
// Icom GetFreq.
//
long GetFreqIcom()
{
// Response data offset from 5 bytes echo (FE FE 88 E0 03 FD)
int reply_offset = 6;
Freq = "";
#ifdef TRX_PORT_SOFTSERIAL
// Send ICOM freq request.
SoftSerial.write(icom_cmd_read_freq);
SoftSerial.flush();
delay(50);
// Read response. Echo - (FE FE 88 E0 03 FD) Responce - (FE FE E0 88 03 00 60 13 18 00 FD)
while ( SoftSerial.available() )
{
memset(tmpCatBufIcom, 0, sizeof(tmpCatBufIcom));
SoftSerial.readBytes(tmpCatBufIcom, sizeof(tmpCatBufIcom));
}
#else
// Send ICOM freq request.
Serial.write(icom_cmd_read_freq);
Serial.flush();
delay(50);
// Read response. Echo - (FE FE 88 E0 03 FD) Responce - (FE FE E0 88 03 00 60 13 18 00 FD)
while ( Serial.available() )
{
memset(tmpCatBufIcom, 0, sizeof(tmpCatBufIcom));
Serial.readBytes(tmpCatBufIcom, sizeof(tmpCatBufIcom));
}
#endif
// Parse response, if get freq request usd, see ALONE (ptp) moode.
if( tmpCatBufIcom[0 + reply_offset] == byte(0xFE) && // Start ICOM data
tmpCatBufIcom[1 + reply_offset] == byte(0xFE) && // Start ICOM data
tmpCatBufIcom[2 + reply_offset] == byte(MY_CIV_ADRESS) && // MY CIV address
tmpCatBufIcom[3 + reply_offset] == byte(CIV_ADRESS) && // Transiver CIV address
tmpCatBufIcom[4 + reply_offset] == byte(0x03) && // CMD send freq.
tmpCatBufIcom[10 + reply_offset] == byte(0xFD) ) // 10 - byte -> End data.
{
// 9 byte - start freq. 5 - byte - end freq.
for (int i=(9+reply_offset); i >=(5+reply_offset); i-- )
{
if(tmpCatBufIcom[i] <= 0x09)
{
Freq = Freq + String(0);
}
Freq = Freq + String(tmpCatBufIcom[i], HEX);
}
return Freq.toInt();
}
return 0;
}
Социальные закладки