Lets Make Tech

Signal Generator

I threw this together real quick to test out a signal generator and it is a bit sloppy and written around someone else's code (their code isn't sloppy mine is). This code should just be used as a reference or for testing and credit given to David Mills for the part of the code that I borrowed from him.

This tutorial shows how to make a signal generator using the AD9850 DDS signal generator, an Arduino, and a potentiometer. I used code written by David Mills http://webshed.org/wiki/AD9850_Arduino to interface with the signal generator and added to it to allow you to set the frequency using a potentiometer and change the mode between Hz, kHz, and MHz by pressing a button. As David Mills requested, don't claim the code as your own or try to prevent anyone else using it other than that do with it what you will. 

If you are interested in making music with this check out this previous post.

BOM

-1 Arduino

-1 LCD display (optional)

-1 AD9850 DDS signal generator

-1 Touch button

Connections

Note the connections between the arduino and the AD9850 DDS signal generator. Feel free to change them if you need.

#define CLOCK 2 //pin connections for DDS
#define LOAD 3
#define DATA 7
#define RESET 8

For the LCD I used the following connections LiquidCrystal lcd(5, 6, 9, 10, 11, 12); For additional help connecting the LCD see this tutorial

Note that in this tutorial they use different pins and it uses a potentiometer being used on pin 3, I just used a resistor to ground. The value will vary between screens and it is just used to set contrast. If your screen is all black or white you may need to use a different resistor or adjust the pot. 

 

For the pot and button I used the following pins

#define POTPIN A0
#define MODE 13

 

Use the button to change the mode, then depending on what mode it is, change how you interpret the pot.

Interpreting the pot, convert the reading from the pot to a value between 0 and 1,000 for mode 1, 1,000-1,000,000 for mode 2, and 1,000,000-30,000,000 for mode 3. You will want to limit it to 30,000,000 because past that the hardware doesn't work. You may need to edit the code I used for the potentiometer that you use.

Send this to the AD9850 DDS signal generator using the code that David Mills wrote.

Pro tip: Display the frequency and mode on the LCD so you know what you are doing. 

Alternative inputs: The potentiometer I used wasn't very accurate and the value changed a lot when set all the way up or down. Use buttons or a keypad to set the frequency to get an exact amount and easily set it how you want.   

 

The complete code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(5, 6, 9, 10, 11, 12);


#define DDS_CLOCK 125000000

#define  CLOCK  2  //pin connections for DDS
#define  LOAD 3 
#define  DATA  7
#define  RESET 8
#define  POTPIN A0
#define  MODE 13
float hrz;
float pot;
float temp;
int mode=0;
int moderead;
void setup()
{
  pinMode(DATA, OUTPUT); 
  pinMode(CLOCK, OUTPUT); 
  pinMode(LOAD, OUTPUT); 
  pinMode(RESET, OUTPUT); 
  pinMode(POTPIN, INPUT);
  pinMode(MODE, INPUT);
  AD9850_init();
  AD9850_reset();
  lcd.begin(16, 2);
  Serial.begin(9600);
  
  //SetFrequency(hrz);

}

void loop()
{ 
 moderead=digitalRead(MODE); 
 pot=analogRead(POTPIN);  //14-1000
 hrz=((pot-6)*10000000); 
 if (moderead==LOW)
 {
   mode++;
   delay(500);
   if (mode>2)
   {
     mode=0;
   }
 }
 
 if (mode==0)
 {
   hrz=(hrz/10000000);
    lcd.setCursor(0, 1);
    lcd.print("MODE: Hz");

 }
 
 if (mode==1)
 {
   hrz=(hrz/10000);
    lcd.setCursor(0, 1);
    lcd.print("MODE: KHz");
 }
 
 if (mode==2)
 {
    hrz=(hrz/100);
    lcd.setCursor(0, 1);
    lcd.print("MODE: MHz");
 }
if (mode!=3)
{
 if (hrz<0)
 {
   hrz=0;
   
   if (temp>1000)
   {
     lcd.clear();
   }
   lcd.setCursor(0, 0);
   lcd.print(hrz,1);
   lcd.print("HZ   ");
 }
 
 if (hrz>0 && hrz<1000)
 {
   if (temp>1000)
   {
     lcd.clear();
   }
   lcd.setCursor(0, 0);
   lcd.print(hrz,1);
   lcd.print("HZ   ");
 }
 if (hrz>1000 && hrz<1000000)
 {
   if (temp<1000|| temp>1000000)
   {
     lcd.clear();
   }
   lcd.setCursor(0, 0);
   lcd.print(hrz/1000,1);
   lcd.print("KHZ   ");
 }
  if (hrz>1000000 && hrz<30000000)
 {
   if (temp<1000000)
   {
     lcd.clear();
   }
   lcd.setCursor(0, 0);
   lcd.print(hrz/1000000,1);
   lcd.print("MHZ   ");
 }
 if (hrz>30000000)
 {
   hrz=30000000;
   if (temp<1000000)
   {
     lcd.clear();
   }
   lcd.setCursor(0, 0);
   lcd.print(hrz/1000000,1);
   lcd.print("MHZ   ");
 }
 
 
 SetFrequency(hrz);
 temp=hrz;
} 

}
void SetFrequency(unsigned long frequency)
{
  unsigned long tuning_word = (frequency * pow(2, 32)) / DDS_CLOCK;
  digitalWrite (LOAD, LOW); 

  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 8);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 16);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 24);
  shiftOut(DATA, CLOCK, LSBFIRST, 0x0);
  digitalWrite (LOAD, HIGH); 
}

void AD9850_init()
{

  digitalWrite(RESET, LOW);
  digitalWrite(CLOCK, LOW);
  digitalWrite(LOAD, LOW);
  digitalWrite(DATA, LOW);
}

void AD9850_reset()
{
  //reset sequence is:
  // CLOCK & LOAD = LOW
  //  Pulse RESET high for a few uS (use 5 uS here)
  //  Pulse CLOCK high for a few uS (use 5 uS here)
  //  Set DATA to ZERO and pulse LOAD for a few uS (use 5 uS here)

  // data sheet diagrams show only RESET and CLOCK being used to reset the device, but I see no output unless I also
  // toggle the LOAD line here.

  digitalWrite(CLOCK, LOW);
  digitalWrite(LOAD, LOW);

  digitalWrite(RESET, LOW);
  delay(5);
  digitalWrite(RESET, HIGH);  //pulse RESET
  delay(5);
  digitalWrite(RESET, LOW);
  delay(5);

  digitalWrite(CLOCK, LOW);
  delay(5);
  digitalWrite(CLOCK, HIGH);  //pulse CLOCK
  delay(5);
  digitalWrite(CLOCK, LOW);
  delay(5);
  digitalWrite(DATA, LOW);    //make sure DATA pin is LOW

    digitalWrite(LOAD, LOW);
  delay(5);
  digitalWrite(LOAD, HIGH);  //pulse LOAD
  delay(5);
  digitalWrite(LOAD, LOW);
  // Chip is RESET now
}






 

 

 

 

How To Set Up A PIC 18F4520

First you need to make sure you have everything that you need.

BOM

-1x PIC18f4520

-PICkit-3

-Breadboard

-Jumper wires

-5V power regulator

-9V battery and clip. 

-20 Mhz oscillator

-100 kohh resistor

-2X 22pf capacitors

- 2X 10uf capacitors 

 

Connections

1. First set up your bread board by placing the 5V regulator somewhere on it. Connect the ground pin to the ground rail of the breadboard        and the out pin to the + rail of the breadboard. Connect the 9V positive pin to the voltage regulator input and connect ground to ground. 

2. Check the rails with a multimeter to make sure that you are getting a 5V reading. Once you confirm it is working disconnect the battery.  

3. Now place your pic somewhere else on the bread board. Connect pins 11 and 32 to the positive power rail (5V). Connect pins 12 and 31 to     the ground rail. Place a 10uf capacitor between pins 11 and 12 as well as between pins 31 and 32 (note the polarity of the capacitor when       connecting)

4. Connect a 100 kohm resistor from pin 1 to the 5V rail. 

5. Connect the 20 MHz oscillator to pins 13 and 14. Add one 22pf capacitor from pin 13 to ground and another 22pf capacitor from pin 14 to     ground.  

6. Now its time to connect to your computer with the PICkit 3. Pin 1 of the PICkit 3 designated by a white arrow connects to pin 1 of the PIC.     Pin 2 of the PICkit 3 connects to 5V. Pin 3 of the PICkit-3 connects to ground. Pin 4 of the PICkit-3 connects to pin 40 of the PIC. Pin 5 of       the PICkit-3 connects to Pin 39 of the Pic

7. Configure your PIC: Go to the 'Window' tab, select 'Pic Memory Views'. Configure how you need and get the code, mine looks like this:

// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

 

8. Now you just have to write a program. Here is an example of code that uses an EMG sensor to control a stepper motor. If you are interested in building this check out my previous post

 


// PIC18F4520 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

void setup();
void read();
void motorcontrol();
void LCD();
void forward();
void reverse();
void pause();
int adresult = 0;
int steps=0;
float voltage = 0.0;
int newstep;
void main(void)
{
    setup();
    while(1)
    {
        read();
        motorcontrol();
        LCD();
    }
}

void setup()
{
    TRISC = 0b00000000;
    LATC = 0b00000000;
    ADCON0 = 0b00000001;
    ADCON1 = 0b00001110;
    ADCON2 = 0b00100100;
}

void read()
{
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO == 1);
    adresult = ADRESH;
    voltage = adresult * 5.0 / 255; //8bits 0-255 so 255 steps.
    newstep = voltage*200.0;
}

void motorcontrol()
{
    if (steps<newstep && newstep>50)
    {
        forward();
        steps++;
    }
    if (steps>newstep && newstep < 250)
    {
        reverse();
        steps--;
    }
}

void LCD()
{
    
}

void forward()
{
    LATC = 0b00001001;
    pause();
    LATC = 0b00000011;
    pause();
    LATC = 0b00000110;
    pause();
    LATC = 0b00001100;
    pause();
    LATC = 0b00000000;
}

void reverse()
{
    LATC = 0b00001100;
    pause();
    LATC = 0b00000110;
    pause();
    LATC = 0b00000011;
    pause();
    LATC = 0b00001001;
    pause();
    LATC = 0b00000000;
}

void pause()
{

   _delay(12350); //15000 fastest 100000 slowest
}




 

Home