projecter2
Published

Frequency Generator

Arduino generates 244-8, 000, 000 Hz by dividing the 16 MHz crystal oscillator.

IntermediateFull instructions provided2,639
Frequency Generator

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
RGB LCD Shield Kit, 16x2 Character Display
RGB LCD Shield Kit, 16x2 Character Display
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Pushbutton Switch, Pushbutton
Pushbutton Switch, Pushbutton
×2

Story

Read more

Schematics

fg

Code

fg

Arduino
/*
 *  LCD Frequency Generator
 * Created: 10/08/22
 *  Author: moty22.co.uk
 */ 
#include <LiquidCrystal.h>

unsigned char d[7], set=1, dl[5];    //
unsigned long freq ;
unsigned int freqL;

   // initialize the library with the numbers of the interface pins
  LiquidCrystal lcd(12, 13, 8, 7, 6, 5);
  
void setup() {
  lcd.begin(16, 2);   // set up the LCD's number of columns and rows:

    //timer2 settings
  OCR2A = 127;    //
  TCCR2A = 0b01000010;  //CTC mode, 
  TCCR2B = 0b10000101; //CTC mode, prescale=1:128
  pinMode(11, OUTPUT);

  OCR1A = 127;
  TCCR1A = 0b01000000;  //CTC mode
  TCCR1B = 0b00001001; //CTC mode, prescale=1
  pinMode(9, OUTPUT);  
  
  pinMode(2, INPUT_PULLUP); //+ sw
  pinMode(3, INPUT_PULLUP); //- sw
  
  lcd.setCursor(7, 0); // top middle
  lcd.print("hi");
}

void loop()
{
  if(!digitalRead(2)){
    ++set;
    OCR2A=set;
    OCR1A=set;
    display();
  }
  if(!digitalRead(3)){
    --set;
    OCR2A=set;
    OCR1A=set;
    display();
  }
}

void display(){
    freq = 8000000 / ((unsigned long)set + 1);
    
    d[6]=(freq/1000000) %10;    //1MHz digit  
    d[5]=(freq/100000) %10;
    d[4]=(freq/10000) %10;
    d[3]=(freq/1000) %10;
    d[2]=(freq/100) %10;
    d[1]=(freq/10) %10;    //10Hz digit  
    d[0]=freq %10;

    freqL = 62500 / ((unsigned long)set + 1);
    
    dl[4]=(freqL/10000) %10;
    dl[3]=(freqL/1000) %10;
    dl[2]=(freqL/100) %10;
    dl[1]=(freqL/10) %10;    //10Hz digit  
    dl[0]=freqL %10;
    
    lcd.clear();

    if(freq > 999999) {lcd.setCursor(2, 0); lcd.print(d[6],DEC); lcd.setCursor(3, 0); lcd.print(",");}
    if(freq >  99999) {lcd.setCursor(4, 0); lcd.print(d[5],DEC);}
    lcd.setCursor(5, 0); lcd.print(d[4],DEC);
    lcd.setCursor(6, 0); lcd.print(d[3],DEC); lcd.setCursor(7, 0); lcd.print(",");
    lcd.setCursor(8, 0); lcd.print(d[2],DEC);
    lcd.setCursor(9, 0); lcd.print(d[1],DEC);
    lcd.setCursor(10, 0); lcd.print(d[0],DEC);
    lcd.setCursor(12, 0); lcd.print("Hz");
    
    if(freqL >   9999) {lcd.setCursor(5, 1); lcd.print(dl[4],DEC);}
    if(freqL >    999) {lcd.setCursor(6, 1); lcd.print(dl[3],DEC); lcd.setCursor(7, 1); lcd.print(",");}
    lcd.setCursor(8, 1); lcd.print(dl[2],DEC);
    lcd.setCursor(9, 1); lcd.print(dl[1],DEC);
    lcd.setCursor(10, 1); lcd.print(dl[0],DEC);
    lcd.setCursor(12, 1); lcd.print("Hz");
    
    delay(1000);
}

Credits

projecter2

projecter2

4 projects • 0 followers

Comments