/
main.c
728 lines (594 loc) · 14.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
* 23cm JPD transceiver
*
* # change date by
* ---------------------------------------------------------------------------
* 1.0 Initial version 01-01-15 pe1jpd
* 1.5 Software squelch 26-05-15 pe1jpd
* 2.0 PORTC change 01-07-15 pe1jpd
* 2.1 PORTC bug solved 19-08-15 pe1jpd
* 2.2 fref saved/restored from eeprom 02-10-15 pe1jpd
* 3.1 New update, incl memories and scan 01-06-16 pe1jpd
* 3.2 For AO-92, fref=5kHz, menu update 12-02-18 pe1jpd
* 4.0 Support ADF4113 and ADF4153 12-04-18 pe1jpd
* 4.1 Included sequencer koaxrelais PD7 12-04-18 pe1jpd
* 4.2 LCD 4x20 ... 13-07-20 wm DG8WM
* 4.2 Frequency Adjust +-999 KHz 14-07-20 wm
* 4.2 Frequency Adjust +-200 KHz 16-07-20 wm
* 4.2 Display Squelch, Shift, Step 16-07-20 wm
* 4.2 max. Step 1000 KHz 16-07-20 wm
* 4.2 New Encoder Handling 18-07-20 wm
* 4.2 Display RSSI value in dBm 19-07-20 wm
* 4.2 "V"FO Problem cleared 20-07-20 wm
* 4.2 Display reverse mode 20-07-20 wm
* 4.2 Added "Hz" to CTCSS 21-07-20 wm
* 4.2 2-line LCD improved 28-07-20 wm
* 4.2 RSSI Display improved 28-07-20 wm
* 4.2 Update Parameters in Memory Mode 30-07-20 wm
* 4.2 Write only to eeprom, if value has changed 30-07-20 wm
* 4.3 Write eeprom, correction (int)shift 12-08-20 wm
* 4.3 Correction step down 12-08-20 wm
* 4.3 Display in Memory Mode 'CTCSS', VFO Mode 'Step' 12-08-20 wm
* 4.4 Change SEQ from PD7 to PB2 17-08-20 wm
* 4.4 1750 Hz Tone PD7 17-08-20 wm
* 4.4 Double click PTT for 1750 Hz tone 20-08-20 wm
* 4.5 Start in VFO or MEMORY Mode 25-08-20 wm
*/
/*
*
* '#define LCD_20x4' delete this for 2-line LCD in 23nbfm.h wm
* '#define PB2_SEQ' for use PB2 and not PD7 for SEQ, PD7 is used for 1750 Hz wm
* '#define TONE_1750' and '#define PB2_SEQ' 1750 Hz on PD7 wm
*
*/
#include <avr/io.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h> // wm
#include <avr/pgmspace.h> // wm
#include "23nbfm.h"
#include <util/delay.h>
typedef unsigned char u08;
// variables
int squelchlevel;
int mode;
int selectedMemory;
int lastSelectedMemory;
int step, shift;
#ifdef ADF4153 // wm
int frqadj;
#endif
long int freq, lastFreq;
int tx = FALSE;
int rv = FALSE;
int enc = 0;
char str[20]; // wm str[16];
int tick;
int tone; // CTCSS-Tone
long toneCount; // var for Timer1
#ifdef DECODER // wm
volatile int8_t enc_delta; // Drehgeberbewegung zwischen zwei Auslesungen im Hauptprogramm
#endif
#ifdef DECODER // wm
// Dekodertabelle für wackeligen Rastpunkt
// Quelle: https://www.mikrocontroller.net/articles/Drehgeber
// viertel Auflösung // wm
const int8_t table[16] PROGMEM = {0,0,-1,0,0,0,0,1,0,0,0,0,0,0,0,0};
// halbe Auflösung
// const int8_t table[16] PROGMEM = {0,0,-1,0,0,0,0,1,1,0,0,0,0,-1,0,0};
// Dekodertabelle für normale Drehgeber
// volle Auflösung
// const int8_t table[16] PROGMEM = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0};
ISR( TIMER0_COMPA_vect ) // 1ms fuer manuelle Eingabe
{
static int8_t last=0; // alten Wert speichern
last = (last << 2) & 0x0F;
if (PHASE_A) last |=2;
if (PHASE_B) last |=1;
enc_delta += pgm_read_byte(&table[last]);
}
void encode_init( void ) // Timer 0 initialisieren
{
TIFR0 |= (1<<OCF0A); // Clear Interrupt Request
TIMSK0 |= 1<<OCIE0A; // Enable Output Compare A Interrupt
// OCR0A = (uint8_t)(XTAL / 8.0 * 1e-3 - 0.5); // 1ms ==> XTAL/8 * 1e-3 - 0.5 = 124 <<<<<
OCR0A = 125; // 1ms Compare Time
TCNT0 = 0;
TCCR0A |= 1 << WGM01; // CTC Mode 2
TCCR0B |= 1 << CS01; // Prescale XTAL / 8, Timer0 Start
}
#else
// Interrupt service routine INT1
ISR(INT1_vect)
{
if (PIND & (1<<ROT))
enc++;
else
enc--;
_delay_ms(5);
}
#endif
// Interrupt service routine Timer1
ISR(TIMER1_OVF_vect) // CTCSS-Ton
{
TCNT1 = 65535-toneCount; // restart timer 1
tick++;
if (tx && tone>=600)
tbi(PORTB, Beep); // toggle Beep port
}
#ifdef TONE_1750 // wm
// Interrupt service routine Timer2
ISR(TIMER2_OVF_vect) // 1750 Hz
{
if (tx && tone==599)
{
TCNT2 = 225; // restart Timer 2, 1743 Hz
tbi(PORTD, CT); // toggle CT
}
}
#endif
void initInterrupts(void)
{
#ifdef DECODER // wm
encode_init();
#else
EIMSK |= _BV(INT1); // enable INT1
EICRA |= _BV(ISC11); // int1 on falling edge
#endif
// Setup Timer 1 // CTCSS
TCCR1A = 0x00; // Normal Mode
TCCR1B = 0x01; // div/1 clock, F_CPU clock/1
TIMSK1 |= (1 << TOIE1); // Timer1 overflow interrupt
#ifdef TONE_1750 // wm
//Setup Timer 2 // 1750 Hz
TCCR2A = 0X00; // Normal Mode
TCCR2B = 0x00; // (F_CPU clock/8), stop Timer 2
TIMSK2 |= (1 << TOIE2); // Timer2 overflow interrupt
#endif
// enable interrupts
sei();
}
void adcInit(void)
{
ADCSRA = (1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2); // prescaler /128
#ifdef BOARD2
DIDR0 = (1<<ADC5D); // disable digital input cannel 5
ADMUX = (1<<REFS0) + 5; // ADC channel 5, ARef = Vcc
#endif
#ifdef BOARD1
DIDR0 = (1<<ADC0D); // disable digital input
ADMUX = (1<<REFS0) + 1; // ADC channel 1
#endif
}
int getRotaryPush()
{
int t, c = PIND;
// rotary button pushed?
if (!(c & (1<<PUSH))) {
for (t=0; t<5; t++) {
_delay_ms(100);
// button released?
if ((PIND & (1<<PUSH)))
break;
}
if (t>3)
return LONG;
else
return SHORT;
}
return FALSE;
}
#ifdef DECODER //wm
int handleRotary( void ) // Encoder auslesen
{
int8_t val = 0;
// atomarer Variablenzugriff
cli();
if (enc_delta != 0) {
if (enc_delta > 0) val = 1;
else if (enc_delta < 0) val = -1;
enc_delta = 0;
}
sei();
return val;
}
#else
int handleRotary()
{
int count=0;
if (enc != 0) {
if (enc>0) count=1;
else if (enc<0)count=-1;
enc = 0;
}
return count;
}
#endif
void hex2bcd(long int f)
{
long int x = 10000000;
int i;
char d;
for (i=0; i<8; i++) {
d = 0;
for (;;) {
f -= x;
if (f < 0) break;
d++;
}
str[i] = d + 0x30;
f += x;
x = x/10;
}
}
void displayFrequency(long int f)
{
int i;
char c;
hex2bcd(f);
lcdCursor(4,0);
for (i=1; i<8; i++) {
c = str[i];
if (i==5) {
lcdChar('.');
}
lcdChar(c);
}
}
void displayParameter() // wm
{
lcdCursor(0,1);
if (selectedMemory==MAXMEM) // VFO Mode
{
sprintf(str, "Sq %2d Sh %3d St %4d", squelchlevel, shift, step);
}
else // Memory Mode
{
if (tone<599)
{
sprintf(str, "Sq %2d Sh %3d To off", squelchlevel, shift);
}
else if (tone==599)
{
sprintf(str, "Sq %2d Sh %3d To 1750", squelchlevel, shift);
}
else
{
sprintf(str, "Sq %2d Sh %3d To%3d.%1d", squelchlevel, shift, (int)(tone/10), (int)(tone%10));
}
}
lcdStr(str);
}
int TX_ok() // wm
{
// clear smeter
int s = 0;
#ifdef LCD_20x4 // wm Clear RSSI
displayRSSI(s);
#endif
displaySmeter(s); // Clear S-Meter
// sequencer tx on
switch_tx_on();
// force update pll
lastFreq = 0;
tx = TRUE;
return s;
}
int rxtx()
{
int s;
int t_1750; // wm
// read ptt on PORTD
int c = PIND;
// listen reverse?
if (rv) {
if (c & (1<<REVERSE)) {
lastFreq = 0;
rv = FALSE;
lcdCursor(3,0);
lcdData(' ');
}
}
else {
if (!(c & (1<<REVERSE))) {
lastFreq = 0;
rv = TRUE;
lcdCursor(3,0);
lcdData(3);
}
}
if (tx) {
//keep smeter clear
s = 0;
// switch from tx to rx?? // PTT high switch Tx off
if (c & (1<<PTT) )
{
#ifdef TONE_1750
TCCR2B = 0x00; // (F_CPU clock/8), stop Timer 2
cbi(PORTD, CT); // PB07 low
#endif
// sequencer tx off
switch_tx_off();
lastFreq = 0;
tx = FALSE;
}
}
else {
s = readRSSI();
#ifdef LCD_20x4 // wm
displayRSSI(s);
#endif
displaySmeter(s);
// switch from rx to tx?
if (!(c & (1<<PTT))) // PTT low switch Tx on
{
#ifdef TONE_1750 // wm
if (tone == 599) // 1750 Hz is selected
{
tick = 0; // reset counter, 1 tick ~ 8 ms
t_1750 = 0;
do
{
_delay_ms(1); // must be here
if (PIND & (1<<PTT)) // PTT high
{
while ((tick < 20) && (PIND & (1<<PTT))); // wait for PTT low or time out
if (!(PIND & (1<<PTT)))
{
// switch on 1750 Hz
TCCR2B = (1 << CS21); // start Timer 2, F_CPU/8
t_1750 = 1; // PTT low --> 1750 Hz Tone
}
else
t_1750 = -1; // time out PTT high --> break
}
if (t_1750 != 0)
break;
}
while ((tick < 20) && !(tx));
if (t_1750 > -1)
s = TX_ok(); // time out PTT low --> without 1750 Hz Tone
}
else
s = TX_ok(); // 1750 Hz not selected PTT low
#else
s = TX_ok();
#endif
}
}
// calc value for ISR
toneCount = 5*F_CPU/tone; // CTCSS
// freq change or update needed?
if (freq != lastFreq) {
long int f = freq;
if (tx) {
f += (long int) shift*1000;
setFrequency(f);
}
else {
if (rv) f += (long int)shift*1000;
setFrequency(f - IF);
}
displayFrequency(f);
lastFreq = freq;
#ifdef LCD_20x4 // wm
displayParameter();
lcdCursor(18,0);
if (tx)
lcdStr("Tx");
else
lcdStr("Rx");
#else
lcdCursor(15,0);
if (tx)
lcdChar('T');
else
lcdChar('R');
#endif
}
return s;
}
void switch_tx_on()
{
// mute receiver
sbi(PORTC, MUTE);
// switch coax relais to tx
#ifdef PB2_SEQ // wm
sbi(PORTB, SEQ);
#else
sbi(PORTD, SEQ);
#endif
//wait a bit
_delay_ms(200);
// switch on tx
sbi(PORTC, TXON);
}
void switch_tx_off()
{
// switch tx off
cbi(PORTC, TXON);
// wait a bit
_delay_ms(200);
// switch coax relay to rx
#ifdef PB2_SEQ // wm
cbi(PORTB, SEQ);
#else
cbi(PORTD, SEQ);
#endif
}
void Test1() // wm, normaly not used
{
tbi(PORTB, SEQ);
_delay_ms(5);
tbi(PORTB, SEQ);
}
#ifdef ADF4113
void initPLL()
{
long int reg;
cbi(PORTC, DATA);
cbi(PORTC, CLK);
cbi(PORTC, LE);
// set function latch
reg = 0x438086;
setPLL(reg);
// init R-counter
reg = (2UL<<16) + ((F_REF/25)<<2);
setPLL(reg);
unsigned long f = 1170000;
setFrequency(f);
reg = 0x438082;
setPLL(reg);
}
void setFrequency(unsigned long f)
{
long int reg, N, A, B;
N = f/25; // counter N in pll
B = N/16; // split in A and B
A = N%16;
reg = ((B & 0x1fff)<<8) + ((A & 0x3f)<<2) + 1;
setPLL(reg); // set pll
}
#endif
#ifdef ADF4153
void initPLL()
{
unsigned long reg, cntr, R, N, M, F, P1, MUX;
cbi(PORTC, DATA);
cbi(PORTC, CLK);
cbi(PORTC, LE);
// zero noise&spur registers
reg = 0x000+3;
setPLL(reg);
// set lowest noise
reg = 0x3c4+3;
setPLL(reg);
// reset control reg
cntr = 0x001046; //(0x10)+(1<<6)+(1<<2)+2;
setPLL(cntr);
// load MUX, R,M
P1 = 0;
R = 5; // Fpfd = 2.6MHz
M = 2600;
MUX = 1; // lock detect
reg = (MUX<<20)+(P1<<18)+(R<<14)+(M<<2)+1; //0x4168a1
setPLL(reg);
// load N,F for F=1170MHz
N = 450;
F = 0;
reg = (N<<14)+(F<<2); //0x708000
setPLL(reg);
// enable counter
cntr = 0x001042;
setPLL(cntr);
}
void setFrequency(unsigned long f)
{
long int reg, N, F;
N = (f+frqadj)/2600; // wm
F = (f+frqadj)-2600*N; // wm
reg = (N<<14)+(F<<2);
setPLL(reg);
}
#endif
void setPLL(unsigned long r)
{
int i;
for (i=0; i<24; i++) {
if (r & 0x800000)
sbi(PORTC, DATA);
else
cbi(PORTC, DATA);
_delay_us(10);
sbi(PORTC, CLK);
_delay_us(10);
cbi(PORTC, CLK);
r <<= 1;
}
_delay_us(10);
sbi(PORTC, LE);
_delay_us(10);
cbi(PORTC, LE);
}
//=============================================================================
int main()
{
// PORTB output for LCD
DDRB = 0xff;
#ifdef PB2_SEQ // wm
PORTB = 0xfb; // PB2 for SEQ output low
#else
PORTB = 0xff;
#endif
#ifdef BOARD2
// PORTC PC0-4 output, PC5 input
DDRC = 0x1f;
PORTC = 0x00;
sbi(PORTC, MUTE);
#endif
#ifdef BOARD1
// PORTC PC0,2-5 output, PC1 input
DDRC = 0x3d;
PORTC = 0x00;
sbi(PORTC, MUTE);
#endif
// PD0-PD6 input with pullup, PD7 output, low
// wm, PD7 used for 1750 Hz tone --> '#define PB2_SEQ' and '#define TONE_1750'
DDRD = 0x80;
PORTD = 0x7f;
lcdInit();
adcInit();
readGlobalSettings();
if ((mode != VFO) && (mode != MEMORY)) // wm
{
mode = VFO;
}
toneCount = 5*F_CPU/tone; // CTCSS, tick
initInterrupts();
initPLL();
#ifdef LCD_20x4 // wm
sprintf(str, "PE1JPD 23cm-Trx v%s", version);
lcdCursor(0,0);
lcdStr(str);
lcdCursor(0,1);
lcdStr(" and improved by");
lcdCursor(0,2);
lcdStr(" DG8WM");
#else
sprintf(str, "PE1JPD 23cm v%s", version);
lcdCursor(0,0);
lcdStr(str);
lcdCursor(0,1);
lcdStr("Improv. by DG8WM");
#endif
_delay_ms(3000); // wm 1000
#ifdef LCD_20x4
lcdCursor(0,2);
lcdStr(" ");
#endif
for (;;) {
switch(mode) {
case VFO:
mode = Vfo();
writeGlobalSettings();
break;
case MEMORY:
mode = Memory();
writeGlobalSettings();
break;
case SPECTRUM:
mode = Spectrum();
break;
case MENU:
mode = Menu(mode);
break;
case MEMORY_MENU:
mode = MemoryMenu(mode);
break;
default:
mode = VFO;
break;
}
}
}