Skip to content

Instantly share code, notes, and snippets.

@bd1es
Created August 29, 2021 11:54
Show Gist options
  • Save bd1es/9865e8a18d930145b3936de5cb936ee8 to your computer and use it in GitHub Desktop.
Save bd1es/9865e8a18d930145b3936de5cb936ee8 to your computer and use it in GitHub Desktop.
K3-2KL band decoder suitable for ATtiny2313. (This code is under review.)
/*
-- A K3-2KL band decoder suitable for ATtiny2313.
-- (C) 2011 B1Z.
-- Currently, nothing about licensing is considered.
-- Author: BD1ES.
-- Comments:
-- The chip should be configured to enable the internal "RC clock div 8."
-- That is, the main frequency of the CPU is 1 MHz.
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
// Define D/A values corresponding to 2KL input voltages.
#define BAND_28 BAND_VALUE(2.25)
#define BAND_24 BAND_VALUE(2.25)
#define BAND_21 BAND_VALUE(3.25)
#define BAND_18 BAND_VALUE(3.25)
#define BAND_14 BAND_VALUE(4.25)
#define BAND_10 BAND_VALUE(0.6)
#define BAND_7 BAND_VALUE(5.25)
#define BAND_3M5 BAND_VALUE(6.25)
#define BAND_1M8 BAND_VALUE(7.5)
#define BAND_UNDEF BAND_VALUE(2.25)
// Since
// Vout = (DAout/128)*Vref*(1+Rf/Ra),
// DAout = 128*Vout*Ra/(Vref*(Ra+Rf)),
// and Rf = 6k, Ra = 10k, Vref = 5.0,
// therefore:
#define BAND_VALUE(band_volt) (128 * band_volt * 10 / (5.0 * (10 + 6)) + 0.5)
// Define a table for mapping bands from K3 to 2KL.
static uint8_t PROGMEM band_value[16] = {
BAND_UNDEF, BAND_1M8, BAND_3M5, BAND_7,
BAND_10, BAND_14, BAND_18, BAND_21,
BAND_24, BAND_28, BAND_UNDEF, BAND_UNDEF,
BAND_UNDEF, BAND_UNDEF, BAND_UNDEF, BAND_UNDEF
};
// Define a table for preventing invalid band combination input.
static uint8_t PROGMEM band_valid[16] = {
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0
};
// Hardware dependent routines.
static void init_io(void)
{
// Port A(1..0) pull up.
DDRA = 0;
PORTA = (1<<PA1) | (1<<PA0);
// Port D(5..2) pull up.
DDRD = 0;
PORTD = (1<<PD5) | (1<<PD4) | (1<<PD3) | (1<<PD2);
// Port B output.
PORTB = 0;
DDRB = 0xff;
}
static uint8_t read_k3_on(void)
{
return PINA & (1<<PA0);
}
static uint8_t read_key_out_lp(void)
{
return PINA & (1<<PA1);
}
static uint8_t read_band(void)
{
uint8_t value = 0;
if(PIND & (1<<PD2)) value |= 0x08;
if(PIND & (1<<PD3)) value |= 0x04;
if(PIND & (1<<PD4)) value |= 0x02;
if(PIND & (1<<PD5)) value |= 0x01;
return value;
}
static void write_port_b(uint8_t value)
{
PORTB = value;
}
// The main program.
int main()
{
// Init I/O.
init_io();
// Main loop.
while(1){
// Read signals representing the band and the PTT from the input.
uint8_t band = read_band();
// Forward the PTT signal and the converted D/A value to the output.
if(pgm_read_byte(&band_valid[band]) && read_k3_on() &&
(read_key_out_lp() == 0)){
write_port_b(pgm_read_byte(&band_value[band]) | 0x80);
}else{
write_port_b(pgm_read_byte(&band_value[band]) & 0x7f);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment