< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Sunday, December 25, 2016

Quickly test a bare esp8266 (ESP12) module with no extra bells and whisles [quick start]



ESP8266 is an extremely popular module among hobbyists for building inexpensive and powerful internet connected projects. The module can connect to a wifi access point and can exchange data. It can act as a wifi access point and can be used to control devices. The GPIO pins on the module can be used for automation and much more (SPI, I2C, I2S).

This post is a log of my quick and dirty test for a fresh bare module from AI-THINKER +25dBm (with 16+6 pins) as shown below

ESP12 module from AI THINKER

To test this we need the following things

1) The module works on 3.3 volt and minimum 300ma (while wifi is active, it draws more current, so needs a regulator 1117 or a battery ( 3-3.4 volt is fine) ). I used a simple regulator as shown below to use the 5 volt supply
ESP12 power supply


2) USB to the Serial module for communicating from the PC to esp8266 ( i used a cp2102). The RX  pin goes to TXD on esp module and vice versa. The ground pin on the usb the o serial module should be connected to ground oin on esp module. This boards rx and tx pins are 3.3 v ( make sure of this, otherwise you need a level shifter)

usb to serial based on CP2102

3) A bit of soldering to add some legs to the module. See figures to sort out how i did in my test to make a simple protoboard

Adding legs to esp12 using some waste leads from resistors

ESP module with leads soldered loks like the one below. The extra six on one side can be avoided as it is for internal eeprom and not necessary for most of the uses

ESP12 "spider"


The next is keep it on  a prototyping board. in addition i placed the regulator (3.3v)and  a capacitor (100uF) fromm vcc to ground. Added some sockets for easy access
A simple ESP12 prototype

ESP12 pin connections

The pins on esp12 are arranged as shown below

ESP12 pins layout


A more useful pin layout is shown below with different pins and their roles (some pins have multiple roles)

ESP12 pin layout, image courtsey: github.com/esp8266

First test of esp12 module


Connect the module to the powersupply (3.3volt) and make sure the module is connected as shown below

ESP12 quick test circuit with a usb to serial module

After connecting the stuffs as above, it is time to check it. I am using a linux computer so, the serial post will be under tty/USB* and in windows use the appropriate COM ports. (Refer to cp2012 manual or www for setting up the usb-serial port). To see the messages , i used an arduino 1.6.5 ide's serial monitor. But any other serial monitor should work (putty, screen etc). Arduino is good as it can be used later for custom firmware development.

Set the serial monitor as shown below ( 115200 baud rate)

See serial settings, baud rate 115200, Both NL & CR

Now leave the GPIO0 unconnected ( see diagram above) and open the serial port. Briefly touch the reset to ground (may use a push to on switch). Serial should show some garbage as below and some messages

AI thinker serial messages from ESP12 module

Now you could use some of the AT commands to see the modules factory firmware features (basic wifi access over serial). Just type AT and press enter

AT mode on ESP12

If this is working you could try various AT commands to see the results. But basically it shows your module is working fine and it is better to update the firmware to something else which does a lot more useful things than At commands and wifi over serial port. There are several ways and if you are an arduino user, i would recommend it as it is much simple and easier.

Common AT commands for esp12 (click to enlarge)


For teaching kids and for simple usage there are some firmwares like ESP8266 basic (very easy) and other useful ones are MICROPYTHON, NODEMCU. All these can help to establish internet connected projects in  a simple and easy manner.

NOTE: One of the main challenge here is the proper wiring and gpio pin connections needed to put the module in to different modes. (e.g BOOT MODE). If it is in wrong mode you wont be able to see anything. Keeping the serial port on and resetting the module should show some garbages on the serial port of  anormal working module.

GPIO modes:

0 Low
1 High
x floating

MODEGPIO15GPIO0GPIO2
SDIO (BootSDCard)1xx
UART (UploadCode)00x or 1
FLASH (NormalRunning)01x or 1

Restoring the ESP8266 module to the factory firmware with AT mode (Restore AT commands on an esp8266 module)


This can be done by restoring the factory firmware. We use esptool.py to do this and flash the factory firmware from AI thinker.

To do this just download this script. Then run /bin/bash factory_reset.sh in  a terminal. This works on a debian/ubuntu based systems. In other cases just go through the commands and install esptool.py and run the following command (Download firmware)

esptool.py --port /dev/ttyUSB0 write_flash -fm dio 0x00000 ai-thinker-v1.1.1.bin

Sunday, December 11, 2016

A simple Si5351 vfo and bfo with S meter for hombrew transceiver- firmware based on arduino/atmega328p

Si5351 vfo cum bfo with S meter for hombrew trx

Here is a Simple vfo+bfo using Si5351 and a 16X2 lcd with an S-meter. It is based on atmega328 as a controller. The connections for LCD, Si5351 and AVR are explained in may previous post at "A Simple Si5351 based vfo (signal generator) for ham radio use [quick start-setting up and general details]" 

The idea is to keep it very basic with some of the essential elements and to use a Rotary encoder for all adjustments.
  • Basic frequency adjustments using an encoder with varying steps
  • Option for Receiver Incremental Tuning (RIT)
  • Option to set a BFO offset and built in bfo signal generation using extra clocks in Si5351
  • A simple S meter using character LCD
  • Setting up for Direct Conversion/Superhet/ x4 time clock for SDR (softrock style)
  • Frequency correction settings for calibrating Si5351+crystal
  • EEPROM save of last frequency
A simple video of the prototype is shown below. In normal operation it defaults as  a vfo. A setup mode (triggered by pressing down the encoder button while switching it on) is used to adjust the parameters




Wiring details can be seen by reading this post

Firmware can be downloaded at this Link

Uploading Firmware to arduino/atmega328  (See: http://www.hobbytronics.co.uk/arduino-xloader)

A reader was asking about the barplot and given below is a snippet to show how to make a simple bar-plot. For S meter, you may use linear or preferably a logarithmic scale (if no op-amps are used to do this at the signal level)

You can customize the characters using this tool Online LCD Character Generator

The code given below is a partial snippet

// include the library code:
#include <LiquidCrystal.h>
int analogInput = A1;

byte graph_simple[8] = {
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
};

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //SET CORRECT PINS IN YOUR CASE

void setup()
{
    pinMode(analogInput, INPUT);
    lcd.begin(16, 2);
    lcd.createChar(0, graph_simple);
}

void loop()
{
    value = analogRead(analogInput);
    barplot(value);
}

void barplot(int value)
{
    //ADC value can range from 0-1023
    value = (value * 16.0) / 1023.0; //Scale it to 16
    lcd.clear(); // clear the old bars
    lcd.setCursor(0, 0); //draw graph starting from 0,0
    for (i = 0; i < value; i++) {
        lcd.write(byte(0));
    }
}


Complete source with an i2c lcd


  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
/*
more details of the project at www.riyas.org
This entire program is taken from Jason Mildrum, NT7S and Przemek Sadowski, SQ9NJE.
There is not enough original code written by me to make it worth mentioning.
http://nt7s.com/
http://sq9nje.pl/
http://ak2b.blogspot.com/
*/

#include <Rotary.h>
#include <si5351.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <avr/eeprom.h>

// S meter glyphs
unsigned long previousMillis = 0;
unsigned long starttime = 0;
        
struct settings_t
{
    int32_t bfo_offset ;
    uint32_t vfo ;
    bool quadruple;
    bool superhet;
    bool direct;
    int fcorrection;
}
settings;


byte bar5[8]={B00000,
B11011,
B11011,
B11011,
B11011,
B11011,
B11011,
B00000};

byte bar1[8]={B10000,
B11000,
B11100,
B11110,
B11110,
B11100,
B11000,
B10000};



#define F_MIN        100000000UL               // Lower frequency limit
#define F_MAX        5000000000UL

#define ENCODER_A    3                      // Encoder pin A
#define ENCODER_B    2                      // Encoder pin B
#define ENCODER_BTN  A0
#define TX_BTN  A2
#define SMETER_IN  A1

// LCD - pin assignement in
// LCD defs
#define I2C_ADDR    0x3f  // Define I2C Address where the PCF8574A is
#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

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
     
Si5351 si5351;
Rotary r = Rotary(ENCODER_A, ENCODER_B);
volatile uint32_t LSB = 899950000ULL;
volatile uint32_t USB = 900150000ULL;
volatile uint32_t bfo = 900150000ULL; //start in usb
//These USB/LSB frequencies are added to or subtracted from the vfo frequency in the "Loop()"
//In this example my start frequency will be 14.20000 plus 9.001500 or clk0 = 23.2015Mhz
volatile uint32_t vfo = 1420000000ULL / SI5351_FREQ_MULT; //start freq - change to suit
volatile uint32_t radix = 100;  //start step size - change to suit
volatile uint32_t ritstep=1;

volatile int32_t menu=0;  //start step size - change to suit
uint16_t menuoption=0;


boolean changed_f = 0;
String tbfo = "";
boolean rit=0;
boolean txon=0;
boolean savetag=0;
boolean setupmenu=0;
volatile int32_t rit_offset=0;

//------------------------------- Set Optional Features here --------------------------------------
//Remove comment (//) from the option you want to use. Pick only one
//#define IF_Offset //Output is the display plus or minus the bfo frequency
#define Direct_conversion //What you see on display is what you get
//#define FreqX4  //output is four times the display frequency
//--------------------------------------------------------------------------------------------------


/*
MULTI-CLICK: One Button, Multiple Events

Oct 12, 2009
Run checkButton() to retrieve a button event:
Click
Double-Click
Hold
Long Hold
*/

// Button timing variables
int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250; // max ms between clicks for a double click event
int holdTime = 2000; // ms hold period: how long to wait for press+hold event
int longHoldTime = 5000; // ms long hold period: how long to wait for press+hold event

// Other button variables
boolean buttonVal = HIGH; // value read from button
boolean buttonLast = HIGH; // buffered value of the button's previous state
boolean DCwaiting = false; // whether we're waiting for a double click (down)
boolean DConUp = false; // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true; // whether it's OK to do a single click
long downTime = -1; // time the button was pressed down
long upTime = -1; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false; // when held, whether to wait for the up event
boolean holdEventPast = false; // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton()
{ 
int event = 0;
// Read the state of the button
buttonVal = digitalRead(ENCODER_BTN);
// Button pressed down
if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce) {
downTime = millis();
ignoreUp = false;
waitForUp = false;
singleOK = true;
holdEventPast = false;
longHoldEventPast = false;
if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
else DConUp = false;
DCwaiting = false;
}
// Button released
else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce) { 
if (not ignoreUp) {
upTime = millis();
if (DConUp == false) DCwaiting = true;
else {
event = 2;
DConUp = false;
DCwaiting = false;
singleOK = false;
}
}
}
// Test for normal click event: DCgap expired
if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true) {
event = 1;
DCwaiting = false;
}
// Test for hold
if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
// Trigger "normal" hold
if (not holdEventPast) {
event = 3;
waitForUp = true;
ignoreUp = true;
DConUp = false;
DCwaiting = false;
//downTime = millis();
holdEventPast = true;
}
// Trigger "long" hold
if ((millis() - downTime) >= longHoldTime) {
if (not longHoldEventPast) {
event = 4;
longHoldEventPast = true;
}
}
}
buttonLast = buttonVal;
return event;
}
/**************************************/
/* Interrupt service routine for      */
/* encoder frequency change           */
/**************************************/
ISR(PCINT2_vect) {
  unsigned char result = r.process();
  if (result == DIR_CW)
  {
    if(!setupmenu)
    set_frequency(1);
    else
    setup_menu(1);
  }
  else if (result == DIR_CCW)
  {
    if(!setupmenu)
    set_frequency(-1);
    else
    setup_menu(-1);    
  }
}
/**************************************/
/* Change the frequency               */
/* dir = 1    Increment               */
/* dir = -1   Decrement               */
/**************************************/
void set_frequency(short dir)
{
  savetag=false;
    if(!txon) //lock on tx
 {   
    
    if (dir == 1)
    {
      if(rit)
      rit_offset+= ritstep ;
      else
      vfo += radix; 
    }  
    if (dir == -1)
    {
      if(rit)
      rit_offset-= ritstep;
      else
      vfo -= radix;
    }

  //    if(vfo > F_MAX)
  //      vfo = F_MAX;
  //    if(vfo < F_MIN)
  //      vfo = F_MIN;

   if(rit)
   rit_offset=constrain(rit_offset,-10000,10000);

  
  changed_f = 1;
 }
}
/**************************************/
/* Read the button with debouncing    */
/**************************************/

boolean get_button()
{
  if (!digitalRead(ENCODER_BTN))
  {
    delay(20);
    if (!digitalRead(ENCODER_BTN))
    {
      while (!digitalRead(ENCODER_BTN));
      return 1;
    }
  }
  return 0;
}

/*****************************/
/* display S meter           */
/****************************/
void display_smeter(int strength)

{
// range is 0 to 1024 of adc  
//meter run from position 3 to 14 (12 )
int scale = (strength*15)/1024;
lcd.setCursor(0, 1);
lcd.print("S");
int smeter=(scale*11)/15;
if(smeter<10)
lcd.print(smeter);
else
lcd.print("9");
lcd.print(">");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
    previousMillis = currentMillis;
//clear all after a small interval
lcd.setCursor(3, 1);
lcd.print("            ");
lcd.noCursor();
}


// write it and show for 500 milli sec
for (int i = 3; i<scale; i++)
  {
      lcd.setCursor(i, 1);
      lcd.write(byte(0));
      if(smeter>9)
      lcd.print("+");
  }

  
}


void display_rit()
{
  lcd.setCursor(0, 0);
  lcd.print("RIT:            ");
  lcd.setCursor(6, 0);
  lcd.print(rit_offset);
  lcd.print("Hz");  

        lcd.setCursor(15, 0);
        switch (ritstep)
          {
              case 1: 
                  lcd.print("1");
                   break;
              case 100:
                  lcd.print("2");
                   break;
              case 2500:
                  lcd.print("3"); 
                  break;
          }
  
}


/**************************************/
/* Displays the frequency             */
/**************************************/
void display_frequency()
{
  uint16_t f, g;

  lcd.setCursor(0, 0);
  lcd.print("VFO:             ");
  lcd.setCursor(4, 0);
  f = vfo / 1000000;  //variable is now vfo instead of 'frequency'
  Serial.println(vfo);
  
  if (f < 10)
    lcd.print(' ');
  lcd.print(f);
  lcd.print('.');
  f = (vfo % 1000000) / 1000;
  if (f < 100)
    lcd.print('0');
  if (f < 10)
    lcd.print('0');
  lcd.print(f);
  lcd.print('.');
  f = vfo % 1000;
  if (f < 100)
    lcd.print('0');
  if (f < 10)
    lcd.print('0');
  lcd.print(f);
  //lcd.print("Hz");
  lcd.setCursor(0, 1);
  //lcd.print(tbfo);
  //Serial.println(vfo + bfo);
  //Serial.println(tbfo);

}


void show_stored()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("vfo=");
  lcd.print(vfo);
  lcd.print("#");
  if(settings.quadruple)
  lcd.print("Q");
  else
  lcd.print("q");
  lcd.setCursor(0, 1);
  if(settings.superhet)
  lcd.print("S");
  else
  lcd.print("s");

  if(settings.direct)
  lcd.print("D");
  else
  lcd.print("d");
  lcd.print("F:");
  lcd.print   (settings.fcorrection);
  lcd.print("B:");
  lcd.print (settings.bfo_offset);
}

void setup_menu(short dir)
{
  //called in setup mode
      if (dir == 1)
    {
      menu += radix; 
    }  
    if (dir == -1)
    {
      menu -= radix;
    }
  menu=constrain(menu,-10000000,10000000);
}

void  show_vfo_setup()
{

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("VFO SETUP MODE"); 
delay(2000);  
lcd.clear();
//lcd.setCursor(0, 1);
//show_stored();
while(1)
    {
      //delay(300);
      //settings.quadruple=false;
      //settings.superhet=false;
      //settings.direct=true;
      //settings.fcorrection=0;
      //settings.bfo_offset=0;
      int b = checkButton();
      if ((b == 1) && (menuoption==0)) vfosteps();
      if (b == 2) menu_option();
      if (b == 3) holdEvent();
      if (b == 4) longHoldEvent();
      
      if(menuoption==0)
      show_bfo();
      if(menuoption==1)
      show_fcorrection();
      if(menuoption==2)
      show_bits();
    } 
}

void show_fcorrection()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("FREQ CORRECTION");  
}

void show_bits()
{
  lcd.clear();
  lcd.setCursor(0, 0);  
  lcd.print("SH  x4  DC"); 
  lcd.setCursor(0, 1);
  lcd.print(settings.superhet);  
}

void menu_option()
{  
menuoption+=1;
if(menuoption>2)
menuoption=0;  
}

void show_bfo(){
  settings.bfo_offset=menu;       
  uint32_t f, g;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" OFFSET:        ");
  lcd.setCursor(9, 0);
  lcd.print(settings.bfo_offset);
  lcd.setCursor(0, 1);
  lcd.print(" STEP:");
  lcd.print(round(log10(radix)+1));
  if((int(log10(radix)) % 2)==0)
  {
  lcd.setCursor(0, 0);
  lcd.write(byte(1));
  }  
  else
  {
  lcd.setCursor(0, 1);
  lcd.write(byte(1));
  }
  
      
}


void setup()
{
  Serial.begin(19200);
  lcd.begin(16, 2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();  
  lcd.clear();
  Wire.begin();

  lcd.createChar(0, bar5); 
  lcd.createChar(1, bar1);   
  lcd.begin(16, 2); 
  lcd.setCursor(5, 0);
  lcd.print("LB7UG");

  lcd.setCursor(4, 1);
  lcd.print("loading");
  delay(1000);
  lcd.clear();
  //read from eeprom
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
  vfo=constrain(settings.vfo,1000000,30000000);
  //vfo=1000000;//remove it after the test run
     if(vfo==1000000|vfo==30000000)
     {
      //dirty hack to fix the first run with eeprom
      settings.quadruple=false;
      settings.superhet=false;
      settings.direct=true;
      settings.fcorrection=0;
      settings.bfo_offset=0;
     }
  bfo= settings.bfo_offset; 

  si5351.set_correction(settings.fcorrection); //**mine. There is a calibration sketch in File/Examples/si5351Arduino-Jason
  //where you can determine the correction by using the serial monitor.

  //initialize the Si5351
  si5351.init(SI5351_CRYSTAL_LOAD_8PF, 27000000); //If you're using a 27Mhz crystal, put in 27000000 instead of 0
  // 0 is the default crystal frequency of 25Mhz.

  si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
  // Set CLK0 to output the starting "vfo" frequency as set above by vfo = ?

  if (settings.superhet)
  {
  si5351.set_freq((vfo * SI5351_FREQ_MULT) + bfo, SI5351_PLL_FIXED, SI5351_CLK0);
  volatile uint32_t vfoT = (vfo * SI5351_FREQ_MULT) + bfo;
  // Set CLK2 to output bfo frequency
  si5351.set_freq( bfo, 0, SI5351_CLK2);
  }
  //si5351.drive_strength(SI5351_CLK0,SI5351_DRIVE_2MA); //you can set this to 2MA, 4MA, 6MA or 8MA
  //si5351.drive_strength(SI5351_CLK1,SI5351_DRIVE_2MA); //be careful though - measure into 50ohms
  //si5351.drive_strength(SI5351_CLK2,SI5351_DRIVE_2MA); //
if(settings.direct)
  si5351.set_freq((vfo * SI5351_FREQ_MULT), SI5351_PLL_FIXED, SI5351_CLK0);

if(settings.quadruple)
  si5351.set_freq((vfo * SI5351_FREQ_MULT) * 4, SI5351_PLL_FIXED, SI5351_CLK0);

  pinMode(ENCODER_BTN, INPUT_PULLUP);
  pinMode(TX_BTN, INPUT_PULLUP);
  PCICR |= (1 << PCIE2);           // Enable pin change interrupt for the encoder
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
  display_frequency();  // Update the display

  
  while(!digitalRead(ENCODER_BTN))
  {
  setupmenu=true;  
  show_vfo_setup();  
  delay(50000);
  }

  
}


void loop()
{

//eeprom timer

unsigned long currentMillis = millis();
if (currentMillis - starttime >= 120000) {
    starttime = currentMillis;
//clear all after a small interval
savetag=true;
}


    
  display_smeter(analogRead(SMETER_IN));
  // Update the display if the frequency has been changed
  
  //tx status
  if(!digitalRead(TX_BTN))  
  txon=true;
  else
  txon=false;
  
  
  if (changed_f)
  {
    if(rit & !txon)
    {
    display_rit();
    }
    else
    {    
    display_frequency();
    //reset rit offset to zero if frequency changed in non rit mode
    rit_offset=0;
    ritstep=1;
    }

    if (settings.superhet)
    {
    if(rit & !txon)  
    si5351.set_freq(((vfo+rit_offset) * SI5351_FREQ_MULT) + bfo, SI5351_PLL_FIXED, SI5351_CLK0);
    else
    si5351.set_freq((vfo * SI5351_FREQ_MULT) + bfo, SI5351_PLL_FIXED, SI5351_CLK0);
    //you can also subtract the bfo to suit your needs
    //si5351.set_freq((vfo * SI5351_FREQ_MULT) - bfo  , SI5351_PLL_FIXED, SI5351_CLK0);
    //set clock2 as bfo
    si5351.set_freq( bfo, 0, SI5351_CLK2);
    }
  
  
  if(settings.direct)
     {
      if(rit & !txon) 
      si5351.set_freq(((vfo+rit_offset) * SI5351_FREQ_MULT), SI5351_PLL_FIXED, SI5351_CLK0);
      else
      si5351.set_freq((vfo * SI5351_FREQ_MULT), SI5351_PLL_FIXED, SI5351_CLK0);

     }
  if(settings.quadruple)
   {
     if(rit & !txon) 
     si5351.set_freq(((vfo+rit_offset) * SI5351_FREQ_MULT) * 4, SI5351_PLL_FIXED, SI5351_CLK0);
     else
     si5351.set_freq((vfo * SI5351_FREQ_MULT) * 4, SI5351_PLL_FIXED, SI5351_CLK0);    
   }
 
     changed_f = 0;
  }

  
if(txon)
{
    display_frequency();
    delay(300);    
}
else
{
    if(rit)
    {
    display_rit();
    delay(100);    
    }    
}    
int b = checkButton();

if (b == 1) clickEvent();
if (b == 2) doubleClickEvent();
if (b == 3) holdEvent();
if (b == 4) longHoldEvent();


if(savetag)
{
//save to eeprom after 30 second of no frequency change
if(vfo !=settings.vfo)
  {
    settings.vfo=vfo;
    eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
    savetag=false;
    lcd.clear();
    delay(2000);
    display_frequency();

  }
}


}

void clickEvent() {

if(!rit)
vfosteps();
else
      {
        //steps for RIT
        lcd.setCursor(15, 0);
        switch (ritstep)
          {
              case 1: 
                  ritstep=100;
                  break;
              case 100:
                  ritstep=2500;
                  break;
              case 2500:
                  ritstep=1 ; 
                  break;
          }
          display_rit();
  
     }
}

void doubleClickEvent() {
  rit=!rit;
 if(rit)
 display_rit();
 else
 display_frequency();
 
}

void holdEvent() {
  lcd.setCursor(0, 0);
  lcd.print("HO ");
}

void longHoldEvent() {
  lcd.setCursor(0, 0);
  lcd.print("LO ");
  settings.vfo=vfo;
  eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));

}


void vfosteps()
{

  
switch (radix)
    {
      case 1:
        radix = 10;
        break;
      case 10:
        radix = 100;
        break;
      case 100:
        radix = 1000;
        break;
      case 1000:
        radix = 10000;
        break;
      case 10000:
        radix = 100000;
        break;
      case 100000:
        radix = 1000000;
        break;
      case 1000000:
        radix = 1;
        break;  
    }//end switch
      //show cursor 
  if(!setupmenu)
  {     
    if(radix<1000)
    lcd.setCursor((13-log10(radix)), 0);
    else if (radix<1000000)
    lcd.setCursor((12-log10(radix)), 0);
    else if (radix=1000000)
    lcd.setCursor(5, 0);
    lcd.cursor(); 
    delay(500);
    lcd.noCursor();
  }
    
}
END