Share My Creation Frequency Generator

Arduino Uno or Nano generates 244-8,000,000 Hz by dividing the 16 MHz crystal oscillator. The pushbuttons set the division from 255 Hz to 8 MHz, holding the button advance the frequency every second.. The high frequency output at pin 9 is 8000000 / (division + 1) , the low frequency output at pin 11 is 62500 / (division + 1). Division is by integer so not all frequencies can be set, for example the High Frequency output can be 8, 4, 2.666, 2, 1.6, 1.333 MHz etc.
The LCD of 16x2 display the High and Low Frequencies. The LCD module has to be HD44780 compatible.
The outputs are generated by using Timer2 and Timer1 in CTC mode using C code.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public lcd As LiquidCrystal
    Public out11 As Pin
    Public out9 As Pin
    Public in2 As Pin
    Public in3 As Pin
    Public set As Byte
End Sub

Private Sub AppStart
    
    'initialize the display(RS As Byte, RW As Byte, Enable As Byte
    'RS pin > Arduino digital pin 12
    'RW pin > 255 means mot used
    'Enable pin > Arduino digital pin 13
    'DataPins: Arduino digital pins 8,7,6,5
    lcd.Initialize(12, 255, 13, Array As Byte(8, 7, 6, 5))    'lcd pins: 4,6,11,12,13,14
    lcd.Begin(16, 2)                        'set the display type 2 * 16 characters
    lcd.SetCursor(6, 0)
    lcd.Write("Hi")            'write in the first line
    
    RunNative ("setTimers",Null)
    
    out11.Initialize(11, out11.MODE_OUTPUT)
    out9.Initialize(9, out9.MODE_OUTPUT)
    in2.Initialize(2, in2.MODE_INPUT_PULLUP)
    in3.Initialize(3, in3.MODE_INPUT_PULLUP)

    main_loop
End Sub

Private Sub main_loop
    Do While True
        If in2.DigitalRead=False Then
            set=set+1
            RunNative ("adjust_freq",Null)
            display
        End If
        If in3.DigitalRead=False Then
            set=set-1
            RunNative ("adjust_freq",Null)
            display
        End If
    Loop
End Sub

Private Sub display
    Dim d(7), dl(5), i As Byte
    Dim freq2, freq As ULong
    Dim freq2L, freqL As UInt
    
    freq = 8000000 / (set + 1)
    freq2= freq

    For i=0 To 6
        d(i)=freq2 Mod 10
        freq2=freq2/10
    Next
    
    freqL = 62500 / (set + 1)
    freq2L=freqL

    For i=0 To 4
        dl(i)=freq2L Mod 10
        freq2L=freq2L/10
    Next
    
    lcd.Clear
    If freq > 999999 Then
        lcd.setCursor(2, 0)
        lcd.Write(d(6))
        lcd.setCursor(3, 0)
        lcd.Write(",")
    End If
    If freq > 99999 Then
        lcd.setCursor(4, 0)
        lcd.Write(d(5))
    End If   
    lcd.setCursor(5, 0)
    lcd.Write(d(4))
    lcd.setCursor(6, 0)
    lcd.Write(d(3))
    lcd.setCursor(7, 0)
    lcd.Write(",")
    lcd.setCursor(8, 0)
    lcd.Write(d(2))
    lcd.setCursor(9, 0)
    lcd.Write(d(1))
    lcd.setCursor(10, 0)
    lcd.Write(d(0))
    lcd.setCursor(12, 0)
    lcd.Write("Hz")
    
    If freqL > 9999 Then
        lcd.setCursor(5, 1)
        lcd.Write(dl(4))
    End If
    If freqL > 999 Then
        lcd.setCursor(6, 1)
        lcd.Write(dl(3))
        lcd.setCursor(7, 1)
        lcd.Write(",")
    End If
    lcd.setCursor(8, 1)
    lcd.Write(dl(2))
    lcd.setCursor(9, 1)
    lcd.Write(dl(1))
    lcd.setCursor(10, 1)
    lcd.Write(dl(0))
    lcd.setCursor(12, 1)
    lcd.Write("Hz")
    
    Delay(1000)
End Sub

#if C
void setTimers(B4R::Object* o)
{
          //set timer2
      OCR2A = 127;    //
      TCCR2A = 0b01000010;  //CTC mode,
      TCCR2B = 0b10000101; //CTC mode, prescale=1:128
    
        //  set timer1
      OCR1A = 127;
      TCCR1A = 0b01000000;  //CTC mode
      TCCR1B = 0b00001001; //CTC mode, prescale=1       
        
     // OCR0A = 127;
     // TCCR0A = 0b01000010;  //CTC mode,
     // TCCR0B = 0b10000001; //CTC mode, prescale=1
}

void adjust_freq(B4R::Object* o)
{   
    OCR2A = b4r_main::_set;
    OCR1A = b4r_main::_set;
}

#End if
 

Attachments

  • freq_gen405.jpg
    freq_gen405.jpg
    15.8 KB · Views: 2,183
  • freq_generator_b4r.gif
    freq_generator_b4r.gif
    4.7 KB · Views: 292
  • frequency_generator.zip
    1.7 KB · Views: 238

stari

Active Member
Licensed User
Longtime User
Arduino Uno or Nano generates 244-8,000,000 Hz by dividing the 16 MHz crystal oscillator. The pushbuttons set the division from 255 Hz to 8 MHz, holding the button advance the frequency every second.. The high frequency output at pin 9 is 8000000 / (division + 1) , the low frequency output at pin 11 is 62500 / (division + 1). Division is by integer so not all frequencies can be set, for example the High Frequency output can be 8, 4, 2.666, 2, 1.6, 1.333 MHz etc.
The LCD of 16x2 display the High and Low Frequencies. The LCD module has to be HD44780 compatible.
The outputs are generated by using Timer2 and Timer1 in CTC mode using C code.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public lcd As LiquidCrystal
    Public out11 As Pin
    Public out9 As Pin
    Public in2 As Pin
    Public in3 As Pin
    Public set As Byte
End Sub

Private Sub AppStart
   
    'initialize the display(RS As Byte, RW As Byte, Enable As Byte
    'RS pin > Arduino digital pin 12
    'RW pin > 255 means mot used
    'Enable pin > Arduino digital pin 13
    'DataPins: Arduino digital pins 8,7,6,5
    lcd.Initialize(12, 255, 13, Array As Byte(8, 7, 6, 5))    'lcd pins: 4,6,11,12,13,14
    lcd.Begin(16, 2)                        'set the display type 2 * 16 characters
    lcd.SetCursor(6, 0)
    lcd.Write("Hi")            'write in the first line
   
    RunNative ("setTimers",Null)
   
    out11.Initialize(11, out11.MODE_OUTPUT)
    out9.Initialize(9, out9.MODE_OUTPUT)
    in2.Initialize(2, in2.MODE_INPUT_PULLUP)
    in3.Initialize(3, in3.MODE_INPUT_PULLUP)

    main_loop
End Sub

Private Sub main_loop
    Do While True
        If in2.DigitalRead=False Then
            set=set+1
            RunNative ("adjust_freq",Null)
            display
        End If
        If in3.DigitalRead=False Then
            set=set-1
            RunNative ("adjust_freq",Null)
            display
        End If
    Loop
End Sub

Private Sub display
    Dim d(7), dl(5), i As Byte
    Dim freq2, freq As ULong
    Dim freq2L, freqL As UInt
   
    freq = 8000000 / (set + 1)
    freq2= freq

    For i=0 To 6
        d(i)=freq2 Mod 10
        freq2=freq2/10
    Next
   
    freqL = 62500 / (set + 1)
    freq2L=freqL

    For i=0 To 4
        dl(i)=freq2L Mod 10
        freq2L=freq2L/10
    Next
   
    lcd.Clear
    If freq > 999999 Then
        lcd.setCursor(2, 0)
        lcd.Write(d(6))
        lcd.setCursor(3, 0)
        lcd.Write(",")
    End If
    If freq > 99999 Then
        lcd.setCursor(4, 0)
        lcd.Write(d(5))
    End If  
    lcd.setCursor(5, 0)
    lcd.Write(d(4))
    lcd.setCursor(6, 0)
    lcd.Write(d(3))
    lcd.setCursor(7, 0)
    lcd.Write(",")
    lcd.setCursor(8, 0)
    lcd.Write(d(2))
    lcd.setCursor(9, 0)
    lcd.Write(d(1))
    lcd.setCursor(10, 0)
    lcd.Write(d(0))
    lcd.setCursor(12, 0)
    lcd.Write("Hz")
   
    If freqL > 9999 Then
        lcd.setCursor(5, 1)
        lcd.Write(dl(4))
    End If
    If freqL > 999 Then
        lcd.setCursor(6, 1)
        lcd.Write(dl(3))
        lcd.setCursor(7, 1)
        lcd.Write(",")
    End If
    lcd.setCursor(8, 1)
    lcd.Write(dl(2))
    lcd.setCursor(9, 1)
    lcd.Write(dl(1))
    lcd.setCursor(10, 1)
    lcd.Write(dl(0))
    lcd.setCursor(12, 1)
    lcd.Write("Hz")
   
    Delay(1000)
End Sub

#if C
void setTimers(B4R::Object* o)
{
          //set timer2
      OCR2A = 127;    //
      TCCR2A = 0b01000010;  //CTC mode,
      TCCR2B = 0b10000101; //CTC mode, prescale=1:128
   
        //  set timer1
      OCR1A = 127;
      TCCR1A = 0b01000000;  //CTC mode
      TCCR1B = 0b00001001; //CTC mode, prescale=1      
       
     // OCR0A = 127;
     // TCCR0A = 0b01000010;  //CTC mode,
     // TCCR0B = 0b10000001; //CTC mode, prescale=1
}

void adjust_freq(B4R::Object* o)
{  
    OCR2A = b4r_main::_set;
    OCR1A = b4r_main::_set;
}

#End if
Nice.
Can you tell me, where you find setting off timers 2 and 1?
 

Beja

Expert
Licensed User
Longtime User
So nice and neat work.. thanks. the Sch is very simple.. just one active component besides the Arduino..
 
Top