- P1000606.jpg (187.21 KiB) Viewed 28263 times
I have some sample code ready for the arduino.
With the up and down button you can change the freq in steps of 500 khz.
There are no boundaries for the freq at the moment.
start freq is 1250Mhz
Source is free to use and to adapt.
Menu has to be implemented...
Source code listing here:
Code: Select all
// inslude the SPI library:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <PString.h>
// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 3;
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
unsigned long Reg[3];
float Freq;
float Step;
void setup() {
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
digitalWrite(slaveSelectPin,LOW);
// initialize SPI:
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.begin();
lcd.print("DigiLite VCO");
// SPI.end();
Freq = 1250;
Step = 0.5;
SetFreq(Freq);
}
void loop() {
//lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis()/1000); // display seconds elapsed since power-up
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
break;
}
case btnLEFT:
{
break;
}
case btnUP:
{
Freq = Freq + Step;
SetFreq(Freq);
break;
}
case btnDOWN:
{
Freq = Freq - Step;
SetFreq(Freq);
break;
}
case btnSELECT:
{
//lcd.print("SELECT");
break;
}
case btnNONE:
{
//lcd.print("NONE ");
break;
}
}
}
void SetFreq(float Freq)
{
delay(100);
char buffer[15];
PString mystring(buffer, sizeof(buffer));
mystring.print(Freq);
mystring.print(" Mhz.");
lcd.setCursor(0,1); // move to the begining of the second line
lcd.print(mystring);
delay(100);
ConvertFreq(Freq,Reg);
WriteADF2(1);
delayMicroseconds(2500);
WriteADF2(0);
delayMicroseconds(2500);
WriteADF2(2);
delayMicroseconds(2500);
}
void WriteADF2(int idx)
{
byte buf[5];
for (int i = 0; i < 4; i++) // "i < [Bits per register / 8]"
buf[i] = (byte)(Reg[idx] >> (i * 8));
WriteADF(buf[2],buf[1],buf[0]);
}
int WriteADF(byte a1, byte a2, byte a3) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);
delayMicroseconds(10);
SPI.transfer(a1);
SPI.transfer(a2);
SPI.transfer(a3);
Toggle();
}
int Toggle() {
digitalWrite(slaveSelectPin,HIGH);
delayMicroseconds(1);
digitalWrite(slaveSelectPin,LOW);
}
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void ConvertFreq(float freq,unsigned long R[])
{
int P = 16;
float RFout = freq;
int REFin = 10;
int PFDFreq = 500;
int RSET = 47000;
int InternalD2 = 0;
int OutputD2 = 0;
int MTLD = 1;
int CPGain = 0;
int PDPol = 1;
int CPActive = 0;
int CounterReset = 0;
int LDP = 0;
int Rcounter = REFin * 1000 / PFDFreq;
int N = (Rcounter * RFout) / REFin;
int B = (int)N / P;
int A = (int)N - (B * P);
R[0] = (unsigned long)(0 + 1 * pow(2, 2) + CounterReset * pow(2, 4) + 1 * pow(2,5) + PDPol * pow(2, 8) + CPActive * pow(2, 9) + CPGain * pow(2, 10) + MTLD * pow(2, 11) + 3 * pow(2, 12) + 7 * pow(2, 14) + 7 * pow(2, 17) + 0 * pow(2, 20) + 1 * pow(2, 22));
R[1] = (unsigned long)(1 + Rcounter * pow(2, 2) + 0 * pow(2, 16) + LDP * pow(2, 18) + 3 * pow(2, 20));
R[2] = (unsigned long)(2 + A * pow(2, 2) + B * pow(2, 8) + CPGain * pow(2, 21) + OutputD2 * pow(2, 22) + InternalD2 * pow(2, 23));
//lcd.print(R[2]);
}
Edited: to insert "code" tags 2012-05-02 - gm1mfn.