Josh Jennings / Mbed 2 deprecated Instrumentation

Dependencies:   mbed

Committer:
joshthepirate
Date:
Fri Jan 31 15:14:42 2020 +0000
Revision:
1:a22687a74345
Parent:
0:bb9aeaaccee4
Child:
2:1d2510530829
fixd something

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joshthepirate 0:bb9aeaaccee4 1 #include "mbed.h"
joshthepirate 0:bb9aeaaccee4 2
joshthepirate 0:bb9aeaaccee4 3 #define SPI_MOSI PB_15
joshthepirate 0:bb9aeaaccee4 4 #define SPI_MISO PB_14
joshthepirate 0:bb9aeaaccee4 5 #define SPI_SCK PB_13
joshthepirate 0:bb9aeaaccee4 6
joshthepirate 0:bb9aeaaccee4 7 #define FSYNC PB_12
joshthepirate 0:bb9aeaaccee4 8
joshthepirate 0:bb9aeaaccee4 9 const int SINE = 0x2000; // AD9833 Waveform register values
joshthepirate 0:bb9aeaaccee4 10 const int SQUARE = 0x2028;
joshthepirate 0:bb9aeaaccee4 11 const int TRIANGLE = 0x2002;
joshthepirate 0:bb9aeaaccee4 12
joshthepirate 0:bb9aeaaccee4 13 const float refFreq = 25000000.0; // On-board crystal reference frequency
joshthepirate 0:bb9aeaaccee4 14
joshthepirate 0:bb9aeaaccee4 15 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
joshthepirate 0:bb9aeaaccee4 16 Serial pc(USBTX, USBRX);
joshthepirate 0:bb9aeaaccee4 17 DigitalOut fsyncpin(FSYNC);
joshthepirate 0:bb9aeaaccee4 18
joshthepirate 0:bb9aeaaccee4 19 unsigned long freq = 1000; // initial freqency
joshthepirate 0:bb9aeaaccee4 20
joshthepirate 0:bb9aeaaccee4 21 void WriteRegister(short dat) {
joshthepirate 0:bb9aeaaccee4 22 fsyncpin = 0; // Set FSYNC low before writing to AD9833 registers
joshthepirate 0:bb9aeaaccee4 23
joshthepirate 0:bb9aeaaccee4 24 wait_us(10); // Give AD9833 time to get ready to receive data.
joshthepirate 0:bb9aeaaccee4 25
joshthepirate 0:bb9aeaaccee4 26 spi.write(dat >> 8); // Each AD9833 register is 32 bits wide and each 16
joshthepirate 0:bb9aeaaccee4 27 spi.write(dat & 0xFF); // bits has to be transferred as 2 x 8-bit bytes.
joshthepirate 0:bb9aeaaccee4 28 fsyncpin = 1; // Write done. Set FSYNC high
joshthepirate 0:bb9aeaaccee4 29 }
joshthepirate 0:bb9aeaaccee4 30
joshthepirate 1:a22687a74345 31 void AD9833reset() {
joshthepirate 1:a22687a74345 32 WriteRegister(0x100); // Write '1' to AD9833 Control register bit D8.
joshthepirate 1:a22687a74345 33 wait_ms(10);
joshthepirate 1:a22687a74345 34 }
joshthepirate 1:a22687a74345 35
joshthepirate 0:bb9aeaaccee4 36 void AD9833setFrequency(long frequency, int Waveform) {
joshthepirate 1:a22687a74345 37
joshthepirate 0:bb9aeaaccee4 38 long FreqWord = (frequency * pow(2, 28.0)) / refFreq;
joshthepirate 0:bb9aeaaccee4 39
joshthepirate 0:bb9aeaaccee4 40 int MSB = (int)((FreqWord & 0xFFFC000) >> 14); //Only lower 14 bits are used for data
joshthepirate 0:bb9aeaaccee4 41 int LSB = (int)(FreqWord & 0x3FFF);
joshthepirate 0:bb9aeaaccee4 42
joshthepirate 0:bb9aeaaccee4 43 //Set control bits 15 ande 14 to 0 and 1, respectively, for frequency register 0
joshthepirate 0:bb9aeaaccee4 44 LSB |= 0x4000;
joshthepirate 0:bb9aeaaccee4 45 MSB |= 0x4000;
joshthepirate 0:bb9aeaaccee4 46
joshthepirate 0:bb9aeaaccee4 47 WriteRegister(0x2100);
joshthepirate 0:bb9aeaaccee4 48 WriteRegister(LSB); // Write lower 16 bits to AD9833 registers
joshthepirate 0:bb9aeaaccee4 49 WriteRegister(MSB); // Write upper 16 bits to AD9833 registers.
joshthepirate 0:bb9aeaaccee4 50 WriteRegister(0xC000); // Phase register
joshthepirate 0:bb9aeaaccee4 51 WriteRegister(Waveform); // Exit & Reset to SINE, SQUARE or TRIANGLE
joshthepirate 0:bb9aeaaccee4 52 }
joshthepirate 0:bb9aeaaccee4 53
joshthepirate 1:a22687a74345 54 void setup() {
joshthepirate 1:a22687a74345 55 spi.format(16, 2); // set to 16 bit data and mode 2
joshthepirate 1:a22687a74345 56 spi.frequency(freq); // set initial fequency
joshthepirate 1:a22687a74345 57
joshthepirate 1:a22687a74345 58 AD9833reset(); // Reset AD9833 module after power-up.
joshthepirate 1:a22687a74345 59
joshthepirate 1:a22687a74345 60 wait_ms(50);
joshthepirate 1:a22687a74345 61
joshthepirate 1:a22687a74345 62 AD9833setFrequency(freq, SINE); // Set the frequency and Sine Wave output
joshthepirate 1:a22687a74345 63 pc.printf("Ready\r\n");
joshthepirate 1:a22687a74345 64 }
joshthepirate 1:a22687a74345 65
joshthepirate 0:bb9aeaaccee4 66 int main() {
joshthepirate 1:a22687a74345 67
joshthepirate 0:bb9aeaaccee4 68 setup();
joshthepirate 0:bb9aeaaccee4 69
joshthepirate 0:bb9aeaaccee4 70 while(1) {
joshthepirate 0:bb9aeaaccee4 71 // put your main code here, to run repeatedly:
joshthepirate 0:bb9aeaaccee4 72
joshthepirate 1:a22687a74345 73 if(pc.readable()){
joshthepirate 0:bb9aeaaccee4 74
joshthepirate 0:bb9aeaaccee4 75 char incomingData[20];
joshthepirate 0:bb9aeaaccee4 76 pc.scanf("%s", incomingData);
joshthepirate 1:a22687a74345 77 pc.printf("Setting Frequency to : %s\r\n", incomingData);
joshthepirate 0:bb9aeaaccee4 78
joshthepirate 0:bb9aeaaccee4 79 freq = (long)atoi(incomingData);
joshthepirate 0:bb9aeaaccee4 80
joshthepirate 0:bb9aeaaccee4 81 AD9833setFrequency(freq, SINE);
joshthepirate 0:bb9aeaaccee4 82 }
joshthepirate 0:bb9aeaaccee4 83 }
joshthepirate 0:bb9aeaaccee4 84 }
joshthepirate 0:bb9aeaaccee4 85