-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqro_trx_7mhz.c
2152 lines (1826 loc) · 41.3 KB
/
qro_trx_7mhz.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////
// Double DDS (AD9834, AD9835) for 7MHz HiPerformance TRX with //
// ATMega644AP and Line LCD 4x20 //
///////////////////////////////////////////////////////////////////
// //
// Compiler: GCC (GNU AVR C-Compiler) //
// Autor: Peter Rachow //
// Last modification: 2019-01-14 //
///////////////////////////////////////////////////////////////////
////////////////////
// PORT USAGE //
////////////////////
//O U T P U T
//DDS 1 SPI interface
//PD0: FSYNC
//PD1 SCLK
//PD2 DATA
//DDS 2 SPI interface
//PC4 FSYNC
//PC5 SDATA
//PC6 SCLK
//PD3 AGC fast/slow
//LCD 4-Bit parallel interface
//PD4 LCD RS
//PD5 LCD RW
//PD6 LCD E
//PD7 LCD Backlight
//PC0 LCD D0
//PC1 LCD D1
//PC2 LCD D2
//PC3 LCD D3
//I N P U T
//ADC0 Voltage control
//ADC1 S-Meter
//ADC2 PWR-Meter
//ADC3 Keys
//ADC4 Temp sensor 0
//ADC5 TX/RX detect
//ADC6 Temp sensor 1
//PB1, PB2 Rotary encoder
// EEPROM //////////////////////////////////////////////////
// Bytes:
// 0:39: VFO frequencies of VFO 0:9 in blocks by 4 bytes
// 127: Last VFO selected
// 128: Backlight set
// 129: AGC fast=0;slow=1
//////////////////////////////////////////////////////////////
#define F_CPU 8000000
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <util/delay.h>
int main(void);
long menu(long, long);
///////////////////////
// SPI DDS1 AD9834 //
///////////////////////
#define DDS1_PORT PORTD
#define DDS1_DDR DDRD
#define DDS1_FSYNC 1 //green
#define DDS1_SCLK 2 //blue
#define DDS1_SDATA 4 //white
void spi1_start(void);
void spi1_send_bit(int);
void spi1_stop(void);
void set_frequency1(long);
long scan_memories(void);
///////////////////////
// SPI DDS2 AD9835 //
///////////////////////
#define DDS2_PORT PORTC
#define DDS2_DDR DDRC
#define DDS2_FSNYC 16 //green PC4
#define DDS2_SDATA 32 //PC5
#define DDS2_SCLK 64 //PC6
void spi2_send_word(unsigned int);
void set_frequency2(long);
///////////////////
// LCD-Display //
///////////////////
#define LCD_DDR DDRC //DDR for LCD D0:D3
#define LCD_PORT PORTC //Port for LCD D0:D3
#define RS_PORT PORTD //Port for LCD RS line
#define E_PORT PORTD //Port for LCD E line
#define RW_PORT PORTD //Port for LCD RW line
#define LCD_D0 1 //yellow
#define LCD_D1 2 //darkgreen
#define LCD_D2 4 //white
#define LCD_D3 8 //darkblue
#define LCD_RS 16 //darkblue
#define LCD_RW 32 //white
#define LCD_E 64 //green
#define LCD_BL 128 //violet
#define ICONSOLID 0x10
#define ICONOFF 0x00
//LCD hardware based functions
void lcd_write(char, unsigned char);
void lcd_write(char, unsigned char);
void lcd_init(void);
void lcd_cls(void);
void lcd_line_cls(int);
void lcd_putchar(int, int, unsigned char);
void lcd_putstring(int, int, char*);
int lcd_putnumber(int, int, long, int, int, char, char);
void defcustomcharacters(void);
int lcd_check_busy(void);
//LCD display for radio
void s_meter(int);
void show_frequency(long);
void show_txfrequency(long);
void show_pa_temp(int, int);
void show_voltage(int);
void show_vfo(int, int);
void show_tuning_step(int, int);
void show_sideband(int, int);
void lcd_set_icon(int, int);
void lcd_set_batt_icon(int);
void lcd_setbacklight(int);
void show_data(long, int, int, int, int, int, int, int);
void show_msg(char*);
void show_agc(int, int);
//ADC
//ADC Channels
//ADC0: s-meter
int get_adc(int);
int get_keys(void);
int get_temp(int);
//Interfrequency options
#define IFOPTION 0
#if (IFOPTION == 0)
//9MHz Filter 9XMF24D (box73.de)
#define INTERFREQUENCY 9000000
#define IF_LSB 8998500
#define IF_USB 9001500
long fbfo[] = {IF_LSB, IF_USB};
#endif
#if (IFOPTION == 1)
//10.695MHz Filter 10M04DS (ex CB TRX "President Jackson")
#define INTERFREQUENCY 10695000 //fLSB 10692100, fUSB 10697700
#define IF_LSB 10691900
#define IF_USB 10697900
long fbfo[] = {IF_LSB, IF_USB};
#endif
#if (IFOPTION == 2)
//10.7MHz Filter 10MXF24D (box73.de)
#define INTERFREQUENCY 10700000
#define IF_LSB 10697720
#define IF_USB 10702220
long fbfo[] = {IF_LSB, IF_USB};
#endif
//Radio data, tuning, frequencies etc.
#define MAXSTEPS 6
#define STDTSTEP 2
int sideband = 0;
int tuning = 0;
int tuningstep[] = {10, 50, 100, 500, 1000, 5000};
int cur_tstep = STDTSTEP;
int cur_vfo = 0;
long f_vfo[10]; //Max. 10 VFOs
int split = 0;
int agc = 0; //fast
void set_agc(int);
//EEPROM
void store_frequency(long, int);
long load_frequency(int);
void store_last_vfo(int);
int load_last_vfo(void);
int is_mem_freq_ok(long);
//Timer 1
unsigned long runseconds = 0;
//Backlight
int light = 1;
/////////////////
// SPI DDS 1 //
/////////////////
void spi1_start(void)
{
DDS1_PORT |= DDS1_SCLK; //SCLK hi
DDS1_PORT &= ~(DDS1_FSYNC); //FSYNC lo
}
void spi1_stop(void)
{
DDS1_PORT |= DDS1_FSYNC; //FSYNC hi
}
void spi1_send_bit(int sbit)
{
if(sbit)
{
DDS1_PORT |= DDS1_SDATA; //SDATA hi
}
else
{
DDS1_PORT &= ~(DDS1_SDATA); //SDATA lo
}
DDS1_PORT |= DDS1_SCLK; //SCLK hi
DDS1_PORT &= ~(DDS1_SCLK); //SCLK lo
}
void set_frequency1(long f)
{
double fword0;
long fword1, x;
int l[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int m[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, t1;
//x = 268435456 / fClk
//fword0 = (double) 3.579139413 * f; // 75MHz
fword0 = (double) 2.440302361 * (f + INTERFREQUENCY - 1500); //fClk = 110.000.900 MHZ
//fword0 = (double) 5.36870912 * f; //fClk = 50MHz
fword1 = (long) fword0;
//Transfer frequency word to byte array
x = (1 << 13); //2^13
for(t1 = 2; t1 < 16; t1++)
{
if(fword1 & x)
{
l[t1] = 1;
}
x >>= 1;
}
x = (1L << 27); //2^27
for(t1 = 2; t1 < 16; t1++)
{
if(fword1 & x)
{
m[t1] = 1;
}
x >>= 1;
}
////////////////////////////////////////
//Transfer to DDS
//Send start command
spi1_start();
for(t1 = 15; t1 >= 0; t1--)
{
spi1_send_bit(0x2000 & (1 << t1));
}
spi1_stop();
//Transfer frequency word
//L-WORD
spi1_start();
for(t1 = 0; t1 < 16; t1++)
{
spi1_send_bit(l[t1]);
}
spi1_stop();
//M-WORD
spi1_start();
for(t1 = 0; t1 < 16; t1++)
{
spi1_send_bit(m[t1]);
}
spi1_stop();
}
/////////////////
// SPI DDS 2 //
/////////////////
void spi2_send_word(unsigned int sbyte)
{
unsigned int t1, x = 32768;
for(t1 = 0; t1 < 16; t1++)
{
if(sbyte & x)
{
DDS2_PORT |= DDS2_SDATA;
}
else
{
DDS2_PORT &= ~(DDS2_SDATA);
}
DDS2_PORT |= DDS2_SCLK;
DDS2_PORT &= ~(DDS2_SCLK);
x = x >> 1;
}
DDS2_PORT |= DDS2_SDATA;
}
void set_frequency2(long f)
{
long fxtal = 50000000; //fXtal in MHz
double fword0;
long fword1;
int t1;
unsigned char xbyte[] = {0x33, 0x22, 0x31, 0x20};
fword0 = (double) f / fxtal;
fword1 = (long) (fword0 * 0xFFFFFFFF);
//Send 4 * 16 Bit to DDS
for(t1 = 0; t1 < 4; t1++)
{
DDS2_PORT &= ~(DDS2_FSNYC);
spi2_send_word((xbyte[t1] << 8) + (((fword1 >> ((3 - t1) * 8))) & 0xFF));
DDS2_PORT |= DDS2_FSNYC;
}
//End of sequence
DDS2_PORT &= ~(DDS2_FSNYC);
spi2_send_word(0x8000);
DDS2_PORT &= ~(DDS2_FSNYC);
}
long scan_memories(void)
{
int t1;
long ftmp;
int key = 0;
long time1 = 0;
int runsecs2 = 0;
lcd_putstring(3, 3, "(QUIT) (SEL)");
while(!key)
{
for(t1 = 0; t1 < 10; t1++)
{
ftmp = load_frequency(t1);
if(is_mem_freq_ok(ftmp))
{
set_frequency1(ftmp);
show_frequency(ftmp);
lcd_putstring(2, 0, "VFO");
lcd_putchar(2, 3, t1 + 0x80);
time1 = runseconds;
runsecs2 = runseconds;
lcd_putnumber(2, 5, runseconds - time1, -1, -1, 'l', 0);
while(time1 + 3 > runseconds)
{
key = get_keys();
if(runsecs2 != runseconds)
{
lcd_putnumber(2, 5, runseconds - time1, -1, -1, 'l', 0);
runsecs2 = runseconds;
}
switch(key)
{
case 1: return 0;
break;
case 2: cur_vfo = t1;
return cur_vfo;
}
}
}
}
}
return 0;
}
///////////////////////////////
// L C D Module 4x20 //
///////////////////////////////
// Write CMD or DATA to LCD
void lcd_write(char lcdmode, unsigned char value)
{
int t1;
while(lcd_check_busy()); //Check busy flag
LCD_DDR |= 0x0F; //Set DDR data lines as output
RW_PORT &= ~(LCD_RW); //Set RW to write operation, i. e. =0
E_PORT &= ~(LCD_E); //E=0
if(!lcdmode)
{
RS_PORT &= ~(LCD_RS); //CMD
}
else
{
RS_PORT |= LCD_RS; //DATA
}
//HI NIBBLE
E_PORT |= LCD_E; //E = 1
for(t1 = 0; t1 < 4; t1++)
{
if(((value & 0xF0) >> 4) & (1 << t1))
{
LCD_PORT |= (1 << t1);
}
else
{
LCD_PORT &= ~(1 << t1);
}
}
E_PORT &= ~(LCD_E);
//LO NIBBLE
E_PORT |= LCD_E;
for(t1 = 0; t1 < 4; t1++)
{
if(value & (1 << t1))
{
LCD_PORT |= (1 << t1);
}
else
{
LCD_PORT &= ~(1 << t1);
}
}
E_PORT &= ~(LCD_E);
_delay_ms(1);
}
//Send one char to LCD
void lcd_putchar(int row, int col, unsigned char ch)
{
//lcd_write(0, 0x80 + col + row * 0x40);
lcd_write(0, 0x80 + col + row * 0x20);
lcd_write(1, ch);
}
//Print out \0-terminated string on LCD
void lcd_putstring(int row, int col, char *s)
{
unsigned char t1 = col;
while(*(s))
{
lcd_putchar(row, t1++, *(s++));
}
}
//Clear LCD
void lcd_cls(void)
{
lcd_write(0, 1);
}
//Init LCD
void lcd_init(void)
{
int t1;
// Basic settings of LCD
// 4-Bit mode, 2 lines, 5 pixels width matrix
lcd_write(0, 0x28);
//4-line mode
lcd_write(0, 0x2C); //RE=1
lcd_write(0, 0x09);
lcd_write(0, 0x28); //RE=0
//Switch icons off
lcd_write(0, 0x2C); //RE=1
lcd_write(0, 0x40); //Set SEGRAM addr to 0x00
for(t1 = 0; t1 < 15; t1++)
{
lcd_write(1, 0);
}
lcd_write(0, 0x28); //RE=0
// Display on, Cursor off, Blink off
lcd_write(0, 0x0C);
// No display shift, no cursor move
lcd_write(0, 0x04);
}
//Set icon by number, -1 switches allicons off
void lcd_set_icon(int icon, int status)
{
//Set icon
lcd_write(0, 0x2C); //RE=1
lcd_write(0, 0x40); //Set SEGRAM addr to 0x00
lcd_write(0, 0x40 + icon); //Select icon addr
lcd_write(1, status); //Activate icon in given mode
lcd_write(0, 0x28); //RE=0
}
//Set icon by number, -1 switches allicons off
void lcd_set_batt_icon(int volts1)
{
int v[] = {0x10, 0x18, 0x1C, 0x1E, 0x1F};
int t1;
for(t1 = 11; t1 < 15; t1++)
{
if(volts1 >= t1 * 10 && volts1 < (t1 + 1) * 10)
{
//Set icon
lcd_write(0, 0x2C); //RE=1
lcd_write(0, 0x40); //Set SEGRAM addr to 0x00
lcd_write(0, 0x40 + 0x0F); //Select icon addr
lcd_write(1, v[t1 - 10]); //Activate icon in given mode
lcd_write(0, 0x28); //RE=0
}
}
}
int lcd_check_busy(void)
{
unsigned char value;
LCD_DDR &= ~(0x0F); //LCD_PORT data line bits D0:D3 to rx mode
RW_PORT |= LCD_RW; //Read operation => RW=1
RS_PORT &= ~(LCD_RS); //CMD => RS=0: for busy flag
//Read data
//Hi nibble
E_PORT |= LCD_E; //E=1
_delay_us(1);
value = (PIND & 0x0F) << 4;
E_PORT &= ~(LCD_E); //E=0
//Lo nibble
E_PORT |= LCD_E; //E=1
_delay_us(1);
value += (PIND & 0x0F);
E_PORT &= ~(LCD_E); //E=0
LCD_DDR |= 0x0F; //Set DDR 0:3 as output
return (value >> 8) & 1;
}
//Set backlight
void lcd_setbacklight(int duty_cycle)
{
double dc = 255 - (duty_cycle * 2.55);
OCR2A = (int) dc;
}
//Define chars
void defcustomcharacters(void)
{
int i1;
unsigned char adr=64;
unsigned char customchar[]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // #0
0x04, 0x0A, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // #1 ° sign
0x00, 0x00, 0x00, 0x07, 0x04, 0x04, 0x04, 0x04, // #2 ,-
0x00, 0x00, 0x00, 0x1C, 0x04, 0x04, 0x04, 0x04, // #3 -,
0x04, 0x04, 0x04, 0x1C, 0x00, 0x00, 0x00, 0x00, // #4 -'
0x04, 0x04, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, // #5 '-
0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, // #6 -
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}; // #7 |
lcd_write(0, 0);
lcd_write(1, 0);
//Send data to CGRAM in lcd
for (i1 = 0; i1 < 64; i1++)
{
lcd_write(0, adr++);
lcd_write(1, customchar[i1]);
}
}
//Write an n-digit number (int or long) to LCD
int lcd_putnumber(int row, int col, long num, int digits, int dec, char orientation, char showplussign)
{
char cl = col, minusflag = 0;
unsigned char cdigit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, digitcnt = 0;
long t1, t2, n = num, r, x = 1;
if(num < 0)
{
minusflag = 1;
n *= -1;
}
/* Stellenzahl automatisch bestimmen */
if(digits == -1)
{
for(t1 = 1; t1 < 10 && (n / x); t1++)
{
x *= 10;
}
digits = t1 - 1;
}
if(!digits)
{
digits = 1;
}
for(t1 = digits - 1; t1 >= 0; t1--)
{
x = 1;
for(t2 = 0; t2 < t1; t2++)
x *= 10;
r = n / x;
cdigit[digitcnt++] = r + 48;
if(t1 == dec)
cdigit[digitcnt++] = 46;
n -= r * x;
}
digitcnt--;
t1 = 0;
/* Ausgabe */
switch(orientation)
{
case 'l': cl = col;
if(minusflag)
{
lcd_putchar(row, cl++, '-');
digitcnt++;
}
else
{
if(showplussign)
{
lcd_putchar(row, cl++, '+');
digitcnt++;
}
}
while(cl <= col + digitcnt) /* Linksbuendig */
{
lcd_putchar(row, cl++, cdigit[t1++]);
}
break;
case 'r': t1 = digitcnt; /* Rechtsbuendig */
for(cl = col; t1 >= 0; cl--)
{
lcd_putchar(row, cl, cdigit[t1--]);
if(minusflag)
{
lcd_putchar(row, --cl, '-');
}
}
}
if(dec == -1)
{
return digits;
}
else
{
return digits + 1;
}
}
//CLS for one LCD line
void lcd_line_cls(int ln)
{
int t1;
for(t1 = 0; t1 < 15; t1++)
{
lcd_putchar(1, t1, 32);
}
}
///////////////////////////////
// TRX display functions //
///////////////////////////////
//Display current frequency divide by 100 with
//dec. separator
void show_frequency(long f)
{
int row = 2, col = 11;
lcd_putnumber(row, col, f / 100, -1, 1, 'l', 0);
lcd_putstring(row, col + 6, "kHz");
}
void show_txfrequency(long f) //Show TX freq for split mode
{
int row = 2, col = 0;
if(f > 0)
{
lcd_putnumber(row, col, f / 100, -1, 1, 'l', 0);
lcd_putstring(row, col + 6, "kHz");
}
else
{
lcd_putstring(row, col, " ");
}
}
void show_voltage(int v10)
{
int row = 0, col = 15;
lcd_putchar(row, col + lcd_putnumber(row, col, v10, -1, 1, 'l', 0), 'V');
}
void show_vfo(int vfo, int showmode)
{
int row, col;
if(showmode)
{
row = 2, col = 2;
}
else
{
row = 0, col = 10;
}
lcd_putstring(row, col, "VFO");
lcd_putchar(row, col + 3, vfo + 0x80);
}
void show_tuning_step(int ts, int showmode)
{
int row, col;
char *tunstep[] = {"10Hz ", "50Hz ", "100Hz", "500Hz", "1kHz ", "5kHZ "};
if(showmode)
{
row = 2, col = 7;
}
else
{
row = 0, col = 0;
}
lcd_putstring(row, col, " ");
lcd_putstring(row, col, tunstep[ts]);
}
void show_sideband(int sb, int showmode)
{
int row, col;
char *sidebandstr[] = {"LSB", "USB"};
if(showmode)
{
row = 2, col = 9;
}
else
{
row = 0, col = 6;
}
lcd_putstring(row, col, sidebandstr[sb]);
}
void show_pa_temp(int temp, int sensor)
{
int row = 1, col = 0;
char unitstr[] = {1, 'C', 0};
lcd_putstring(row, col + lcd_putnumber(row, col, temp, -1, 1, 'l', 0), unitstr);
lcd_putchar(row, col + 6, sensor + 16);
}
//Show meter value
void s_meter(int value)
{
int c[] = {32, 212, 211, 210, 209, 208};
int v, t1;
int maxs = 35, row = 3, col = 0;
//Clear meter
if(value == -1)
{
for(t1 = 0; t1 < 7; t1++)
{
lcd_putchar(row, col + t1, 32);
}
return;
}
v = value;
if(v > maxs)
{
v = maxs;
}
//Full blocks first
for(t1 = 0; t1 < v / 5; t1++)
{
lcd_putchar(row, col + t1, 208);
}
//Remaining char
if(col + t1 < 20)
{
lcd_putchar(row, col + t1, c[v - (v / 5) * 5]);
lcd_putchar(row, col + t1 + 1, 32);
}
}
void show_msg(char *msgtxt)
{
if(msgtxt[0] == 0)
{
lcd_putstring(2, 0, " ");
return;
}
lcd_putstring(2, 0, msgtxt);
}
void show_agc(int agcset, int showmode)
{
int row, col;
if(showmode)
{
row = 2, col = 8;
}
else
{
row = 1, col = 8;
}
switch(agcset)
{
case 0: lcd_putstring(row, col, "Fast");
break;
case 1: lcd_putstring(row, col, "Slow");
break;
}
}
//Show all radio data at once
void show_data(long freq, int t_step, int side_band, int pa_temp, int sensor, int vfo, int vdd, int agc_set)
{
show_frequency(freq);
show_tuning_step(t_step, 0);
show_sideband(side_band, 0);
show_pa_temp(pa_temp, sensor);
show_vfo(vfo, 0);
show_voltage(vdd);
lcd_set_batt_icon(vdd);
show_agc(agc_set, 0);
}
//////////////////////
// A D C //
//////////////////////
//Read ADC value
int get_adc(int adc_channel)
{
int adc_val = 0;
//ADC config and ADC init
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1); //Activate ADC, Prescaler=64
ADMUX = (1 << REFS0) + adc_channel; //Read ADC channel with Vref=VCC
_delay_ms(3);
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
_delay_ms(3);
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
adc_val = ADCL;
adc_val += ADCH * 256;
ADCSRA &= ~(1<<ADEN); //Deactivate ADC
return adc_val;
}
//Read keys via ADCx
int get_keys(void)
{
int key_value[] = {127, 109};
int t1;
int adcval = get_adc(3);
long ti0, ti1; //Time stamps for detecting push duration
long runsecsold0 = runseconds;
long runsecsold1 = runseconds;
//TEST display of ADC value
//lcd_putstring(2, 0, " ");
//lcd_putnumber(2, 0, adcval, -1, -1, 'l', 0);
ti0 = runseconds;
for(t1 = 0; t1 < 2; t1++)
{
if(adcval > key_value[t1] - 3 && adcval < key_value[t1] + 3)
{
while(adcval < 512)
{
adcval = get_adc(3);
if(runsecsold1 != runseconds)
{
lcd_putnumber(3, 0, runseconds - runsecsold0, -1, -1, 'l', 0);
runsecsold1 = runseconds;
}
}
ti1 = runseconds - ti0;
if(ti1 > 1)
{
return(t1 + 1 + 128);
}
else
{
return t1 + 1;
}
}
}
return 0;
}
//Measure temperature of final amplifier
//Sensor = KTY81-210
int get_temp(int sensor)
{
double temp;
double rt, rv = 5100, ut;
double r0 = 1630; //Resistance of temperature sensor at 0°C
double m = 17.62; //slope of temperature sensor in Ohms/K
//Calculate voltage from ADC value
if(!sensor)
{
ut = (double)get_adc(4) * 5 / 1024;
}
else
{
ut = (double)get_adc(6) * 5 / 1024;
}
//Calculate thermal resistor value from ADC voltage ut
rt = rv / (5 / ut - 1);
//Calculate temperature from rt
temp = 10 * ((rt - r0) / m);
return (int)(temp);
}
//////////////////////
// E E P R O M
//////////////////////
void store_frequency(long f, int memplace)
{
long hiword, loword;