You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
9.1 KiB
C
278 lines
9.1 KiB
C
/*
|
|
* BJ-Keyer
|
|
*
|
|
* 2023-07-29 Tom, DL7BJ
|
|
*
|
|
*/
|
|
#ifndef BJ-KEYER_H_INCLUDED
|
|
#define BJ-KEYER_H_INCLUDED
|
|
|
|
#define F_CPU 16000000UL
|
|
#define PRESCALER 8
|
|
#define SINEWAVELENGTH 64
|
|
#define F_CPUPRESIN (F_CPU/(PRESCALER*SINEWAVELENGTH))
|
|
#define USART_BAUDRATE 9600
|
|
#define UBRR_VALUE (((F_CPU/(USART_BAUDRATE*16UL)))-1)
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/io.h>
|
|
#include <avr/pgmspace.h>
|
|
#include <avr/wdt.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/eeprom.h>
|
|
#include <util/delay.h>
|
|
#include <util/atomic.h>
|
|
|
|
#include "oled/i2c.h"
|
|
#include "oled/lcd.h"
|
|
#include "oled/font.h"
|
|
|
|
#include "encoder.h"
|
|
#include "functions.h"
|
|
|
|
// Const strings for display
|
|
const char CALL[] = " DL7BJ ";
|
|
const char PRG[] = " BJ-Keyer ";
|
|
const char VER[] = " V1.0 ";
|
|
const char Trx1[] = "TRX 1";
|
|
const char Trx2[] = "TRX 2";
|
|
const char IambicA[] = "Iambic A";
|
|
const char IambicB[] = "Iambic B";
|
|
const char Ratio[] = "Ratio";
|
|
const char ReverseRL[] = " L - R °";
|
|
const char ReverseLR[] = " L ° R -";
|
|
const char SideToneOnOff[] = "Mithörton";
|
|
const char SideToneFreq[] = "Frequenz";
|
|
const char RiseTime[] = "Anstieg";
|
|
const char DebounceTime[] = "Entprl.";
|
|
const char Yes[] = "J";
|
|
const char No[] = "N";
|
|
const char Hz[] = "Hz";
|
|
|
|
// Const strings for VT100 Terminals
|
|
const char CLRSCR[] = "\033[2J";
|
|
|
|
#ifndef EEMEM
|
|
#define EEMEM __attribute__ ((section (".eeprom")))
|
|
#endif
|
|
|
|
#ifndef NOINIT
|
|
#define NOINIT __attribute__ ((section (".noinit")))
|
|
#endif
|
|
|
|
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) // clear bit
|
|
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) // set bit
|
|
|
|
#define L_WAIT 500 // Frequency for debug LED Timer 0
|
|
|
|
// Keying states
|
|
#define NOTHING 0
|
|
#define DIT_DAH_OFF 1
|
|
#define DAH_DIT_OFF 2
|
|
#define DIT_DAH_ON 3
|
|
#define DAH_DIT_ON 4
|
|
// Keyer mode
|
|
#define STRAIGHT 0
|
|
#define IAMBIC_B 1
|
|
#define IAMBIC_A 2
|
|
#define SINGLE_PADDLE 3
|
|
// Paddle Mode
|
|
#define PADDLE_NORMAL 0
|
|
#define PADDLE_REVERSE 1
|
|
// Machine Mode
|
|
#define NORMAL 0
|
|
#define COMMAND 1
|
|
// Sending Mode
|
|
#define SENDING_NOTHING 0
|
|
#define SENDING_DIT 1
|
|
#define SENDING_DAH 2
|
|
#define SENDING_ELEMENT_SPACE 3
|
|
#define SENDING_STRAIGHT 4
|
|
//
|
|
#define AUTOMATIC_SENDING 0
|
|
#define AUTOMATIC 0
|
|
#define MANUAL 1
|
|
#define MANUAL_SENDING 1
|
|
// Side Tone
|
|
#define SIDETONE_OFF 0
|
|
#define SIDETONE_ON 1
|
|
// Ports
|
|
#define LEFT_PADDLE PD2 // Left Paddle Input
|
|
#define RIGHT_PADDLE PD3 // Right Paddle Input
|
|
#define STRAIGHT_KEY PD4 // Straight key Input
|
|
#define MORSE_LED PC3 // LED Morse Output
|
|
#define MEM1 PD5 // Mem 1 Input
|
|
#define MEM2 PD6 // Mem 2 Input
|
|
#define MEM3 PD7 // Mem 3 Input
|
|
#define MEM4 PC0 // Mem 4 Input
|
|
#define MEM5 PB5 // Mem 5 Input
|
|
#define TRX1 PC1 // TRX1 Output
|
|
#define TRX2 PC2 // TRX2 Output
|
|
#define AUDIO PB3 // PWM Audio Output
|
|
#define AUDIO_EN PB4 // Audio PA Enable
|
|
#define SCL PC5 // I²C LC Display
|
|
#define SDA PC4 // I²C LC Display
|
|
// States
|
|
#define ON 1
|
|
#define OFF 0
|
|
// Menue und Drehencoder
|
|
#define M_TRX1 1
|
|
#define M_TRX2 2
|
|
#define M_TON 3
|
|
#define M_TON_FREQ 4
|
|
#define M_IAMBICA 5
|
|
#define M_IAMBICB 6
|
|
#define M_REVERSE 7
|
|
#define M_RATIO 8
|
|
#define M_WPMBPM 9
|
|
#define M_RISETIME 10
|
|
#define M_DEBOUNCE 11
|
|
#define M_MAX 11 // maximale Menuepunke
|
|
// LCD
|
|
#define CLEARLINE " "
|
|
|
|
// Sinustabelle für PWM mit 64 Werten
|
|
const unsigned char sinewave[] PROGMEM = {
|
|
0x00, 0x01, 0x02, 0x05, 0x0a, 0x0f, 0x15, 0x1d, 0x25, 0x2f, 0x39, 0x43, 0x4f, 0x5a, 0x67, 0x73,
|
|
0x80, 0x8c, 0x98, 0xa5, 0xb0, 0xbc, 0xc6, 0xd0, 0xda, 0xe2, 0xea, 0xf0, 0xf5, 0xfa, 0xfd, 0xfe,
|
|
0xff, 0xfe, 0xfd, 0xfa, 0xf5, 0xf0, 0xea, 0xe2, 0xda, 0xd0, 0xc6, 0xbc, 0xb0, 0xa5, 0x98, 0x8c,
|
|
0x80, 0x73, 0x67, 0x5a, 0x4f, 0x43, 0x39, 0x2f, 0x25, 0x1d, 0x15, 0x0f, 0x0a, 0x05, 0x02, 0x01
|
|
};
|
|
|
|
char sdebug[64];
|
|
|
|
// Sidetone generation
|
|
double FrequencySidetone = 600;
|
|
volatile uint8_t icnt;
|
|
volatile uint8_t ocr2a;
|
|
|
|
// Diverse Zähler für Timer 0
|
|
uint16_t StoreEEprom = 0; // Wartezeit bis EEPROM geschrieben wird
|
|
uint16_t MenuCtrlTimer = 0; // Wartezeit bis zur Betriebsanzeige nach Config
|
|
volatile uint16_t ms; // Timer 0 max. 65535ms, all purpose timer variable
|
|
volatile uint16_t mselement; // Timer 0 max. 65535ms, element length of dit or dah
|
|
|
|
int8_t WpM = 12; // Geschwindigkeit in wpm
|
|
uint8_t Weight = 50; // Gewichtung Punkt-Strich Verhältnis
|
|
uint8_t t_element_length;
|
|
|
|
struct Merker
|
|
{
|
|
uint8_t WpMChanged: 1;
|
|
uint8_t WriteWpMEEprom: 1;
|
|
} bMerker;
|
|
|
|
struct MenuCtrl
|
|
{
|
|
uint8_t buttonPressed: 1;
|
|
uint8_t buttonPressedLong: 1;
|
|
uint8_t m_buttonPressed: 1;
|
|
uint8_t m_buttonPressedLong: 1;
|
|
uint8_t ClrScr: 1;
|
|
uint8_t Config: 1;
|
|
uint8_t WriteEEprom: 1;
|
|
uint8_t Update: 1;
|
|
uint8_t CurMenue;
|
|
} bMenuCtrl;
|
|
|
|
// State Machines
|
|
uint8_t MachineMode = NORMAL;
|
|
uint8_t PaddleMode = PADDLE_NORMAL;
|
|
uint8_t KeyerMode = IAMBIC_A;
|
|
uint8_t SidetoneMode = NORMAL;
|
|
uint8_t state_sidetoneoff = 0;
|
|
|
|
volatile uint8_t Mode = NORMAL;
|
|
volatile uint8_t SendStatus = SENDING_NOTHING;
|
|
volatile uint8_t LastSendStatus = MANUAL_SENDING;
|
|
|
|
// History
|
|
uint8_t keyhistory = 0xff;
|
|
|
|
uint8_t KeyTX = 1;
|
|
|
|
uint8_t DahBuffer = 0;
|
|
uint8_t DitBuffer = 0;
|
|
uint8_t Weighting = 50;
|
|
uint8_t IambicFlag = 0;
|
|
uint8_t KeyState = 0;
|
|
uint8_t DitCounter = 0;
|
|
uint8_t DahCounter = 0;
|
|
uint8_t CurrentTRX = TRX1;
|
|
uint8_t StateRiseTime = 0;
|
|
volatile uint8_t StateRiseTimeCounter = 0;
|
|
|
|
// Variablen in Timer 0
|
|
volatile uint16_t l_timer = 0; // counter for LED on
|
|
volatile uint8_t t_timer = 0; // Frequency of audio output
|
|
volatile uint16_t t_wait = 0; // delayms max. 65535ms
|
|
volatile uint8_t t_pwm = 0;
|
|
volatile uint16_t t_wait_led = 50;
|
|
volatile uint8_t lastButton = 0; // Wert der letzten Buttonabfrage
|
|
|
|
// Drehencoder
|
|
volatile uint8_t EncoderTimer = 0; // 10ms Timer for Encoder
|
|
volatile int8_t EncoderCounter = 0; // Vor/Rück Zähler
|
|
volatile int8_t EncoderPos; // Encoderposition für WpM/BpM
|
|
volatile int8_t EncoderPosConfig; // Encoderposition für Einstellungen
|
|
|
|
volatile uint8_t StateStraightKeyPressed = 0; // Merker für StraightKey betätigt
|
|
volatile uint8_t TimerStraightKeyPressed = 0; // Timervariable für Entprellung
|
|
|
|
// EEPROM
|
|
uint8_t Dummy = 0x55;
|
|
uint8_t ee_Dummy EEMEM = 0x55; // Dummy for Address 0
|
|
uint8_t ee_WpM EEMEM = 12; // WpM
|
|
uint8_t ee_Sidetone EEMEM = 0; // Mithörton An (1) oder Aus (0)
|
|
uint16_t ee_FrequencySidetone EEMEM = 600; // Frequenz des Mithörtons
|
|
uint8_t ee_IambicMode EEMEM = 0; // Iambic Mode A oder B
|
|
uint8_t ee_Weight EEMEM = 50; // Dah dit Ratio
|
|
uint8_t ee_TRX EEMEM = 0; // TRX 1 (0), TRX 2 (1), Beide (2)
|
|
|
|
struct Config
|
|
{
|
|
uint8_t TRX; // TRX 1 oder 2 oder beide
|
|
uint8_t IambicMode; // Iambic A oder Iambic B Mode
|
|
uint8_t Ratio; // Punkt/Strich Verhältnis
|
|
uint16_t FrequencySidetone; // Frequenz des Mithörtons
|
|
uint8_t SidetoneEnabled; // Mithörton eingeschaltet
|
|
uint8_t WpMBpM; // WpM oder BpM Anzeige
|
|
uint8_t WpM; // WpM
|
|
uint8_t Reverse; // linkes/rechtes Paddle vertauschen
|
|
uint8_t Weight; // ???
|
|
uint8_t RiseTime; // Anstiegszeit Sinuston
|
|
uint8_t RiseTimeCounter; // Anzahl Sinusschwingungen für den Anstieg
|
|
uint8_t DebounceTime; // Entprellzeit für Straight Key Eingang
|
|
uint8_t WinkeyerEnabled; // Winkeyer2 Emulation
|
|
} bConfig;
|
|
// Function prototypes
|
|
void Init(void);
|
|
void InitTimer(void);
|
|
|
|
void CheckDahPaddle(void);
|
|
void CheckDitPaddle(void);
|
|
void CheckPaddle(void);
|
|
void CheckStraightKey(void);
|
|
void SendDit(uint8_t SendingType);
|
|
void SendDah(uint8_t SendingType);
|
|
void PTTKey(uint8_t State);
|
|
void SideToneOff(void);
|
|
void SideToneOn(void);
|
|
void TXSidetoneKey(uint8_t State, uint8_t SendingType);
|
|
void SendChar(uint8_t);
|
|
void ChangeSpeed(void);
|
|
|
|
void dah(void);
|
|
void dit(void);
|
|
void DoMorse(void);
|
|
|
|
void SetFrequency(uint16_t f);
|
|
|
|
#endif // BJ-KEYER_H_INCLUDED
|