-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOPENLABS_FGEN_CODE.ino
105 lines (70 loc) · 2.29 KB
/
OPENLABS_FGEN_CODE.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <SoftwareSerial.h>
SoftwareSerial RCV(7, 8);
String msg = "";
#define W_CLK 12 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD 11 // Pin 9 - connect to freq update pin (FQ)
#define DATA 10 // Pin 10 - connect to serial data load pin (DATA)
#define RESET 9 // Pin 11 - connect to reset pin (RST).
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
int FgenPin = 13;
int WaveSwitch = 2;
// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
for (int i=0; i<8; i++, data>>=1) {
digitalWrite(DATA, data & 0x01);
pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
}
}
// frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
for (int b=0; b<4; b++, freq>>=8) {
tfr_byte(freq & 0xFF);
}
tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
pulseHigh(FQ_UD); // Done! Should see output
}
void setup() {
// configure arduino data pins for output
RCV.begin(115200);
Serial.begin(115200);
pinMode(FQ_UD, OUTPUT);
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(FgenPin, OUTPUT);
pinMode(WaveSwitch, OUTPUT);
pulseHigh(RESET);
pulseHigh(W_CLK);
pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10
}
void loop() {
//digitalWrite(FgenPin, 1);
delay(1000);
sendFrequency(100); // freq
while(1){
if(RCV.available()){
while(RCV.available() > 0){
char incoming_char = RCV.read();
Serial.println(incoming_char);
//Serial.println("\r");
msg = msg + incoming_char;
}
Serial.println(msg);
String wave = msg.substring(0, 3);
String freq = msg.substring(3);
Serial.println(wave);
Serial.println(freq);
if(wave == "SIN"){
digitalWrite(WaveSwitch, 1);
}else if(wave == "SQU"){
digitalWrite(WaveSwitch, 0);
}
double new_freq = freq.toInt();
Serial.println(new_freq);
sendFrequency(new_freq);
msg = "";
}
}
}