Close

Arduino Uno code for the phase frequency detector

A project log for NXP Phase Frequency Detector

This is a phase frequency detector based using NXP 74aup2g57gm.

shane kirkbrideshane kirkbride 06/08/2015 at 17:170 Comments
int ledPin = 9;      // LED connected to digital pin 9
int digitalPin7 =7;
int digitalPin6 =6;
 
int analogPin3 = 3;   // potentiometer connected to analog pin 3
int analogPin0 = 0;   // potentiometer connected to analog pin 0
int val3 = 0;         // variable to store the read value
int val0 = 0;         // variable to store the read value
 
float duration0 = 0.1;  // in millis  -- 100, 200, 1000, 2000, 4000 == 10Hz, 5Hz, 1Hz, .5Hz, and .125Hz 
float duration1 = 0.01;  // in millis  -- 100, 200, 1000, 2000, 4000 == 10Hz, 5Hz, 1Hz, .5Hz, and .125Hz 
 
unsigned long lastFlip0=0;
unsigned long lastFlip1=0;
 
int state0 = LOW;
int state1 = LOW;
int led = 13;
 
void setup()
 
{
  pinMode(digitalPin7, OUTPUT);
  pinMode(digitalPin6, OUTPUT);
  pinMode(led,OUTPUT);
  
  digitalWrite(digitalPin7, state0);
  digitalWrite(digitalPin6, state1);
  Serial.begin(9600);          //  setup serial
  
}
 
void loop()
{
 
  val3 = analogRead(analogPin3);    // read the input pin
  val0 = analogRead(analogPin0);    // read the input pin
  
  unsigned long now = millis();
  
  //This is XO 0
  if (now - lastFlip0 >= duration0)
  {
    lastFlip0 = now;
    state0 = !state0;
    digitalWrite(digitalPin7, state0);
  }
  
  //This is XO 1
  if (now - lastFlip1 >= duration1)
  {
    lastFlip1 = now;
    state1 = !state1;
    digitalWrite(digitalPin6, state1);
  }
  
  if(val0 > val3)
  {
    digitalWrite(led, HIGH);
    duration0 = duration0 - random(1, 10);
    duration1 = duration1 + random(1, 10);
  }
  else
  {
    digitalWrite(led, LOW);
    duration1 = duration1 - random(1, 10);
    duration0 = duration0 + random(1, 10);
  }
  
  
    Serial.write("Val3: ");
    Serial.println(val3);     
    Serial.write("Val0: ");
    Serial.println(val0);  
    Serial.write("Time: ");
    Serial.println(now);
 
}

Discussions