Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:1d2510530829, 2020-01-31 (annotated)
- Committer:
- joshthepirate
- Date:
- Fri Jan 31 15:55:14 2020 +0000
- Revision:
- 2:1d2510530829
- Parent:
- 1:a22687a74345
removed pow
Who changed what in which revision?
| User | Revision | Line number | New 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 | 2:1d2510530829 | 24 | // printf("writing register with %i.\r\n"); |
| joshthepirate | 2:1d2510530829 | 25 | |
| joshthepirate | 0:bb9aeaaccee4 | 26 | wait_us(10); // Give AD9833 time to get ready to receive data. |
| joshthepirate | 0:bb9aeaaccee4 | 27 | |
| joshthepirate | 0:bb9aeaaccee4 | 28 | spi.write(dat >> 8); // Each AD9833 register is 32 bits wide and each 16 |
| joshthepirate | 0:bb9aeaaccee4 | 29 | spi.write(dat & 0xFF); // bits has to be transferred as 2 x 8-bit bytes. |
| joshthepirate | 0:bb9aeaaccee4 | 30 | fsyncpin = 1; // Write done. Set FSYNC high |
| joshthepirate | 0:bb9aeaaccee4 | 31 | } |
| joshthepirate | 0:bb9aeaaccee4 | 32 | |
| joshthepirate | 1:a22687a74345 | 33 | void AD9833reset() { |
| joshthepirate | 1:a22687a74345 | 34 | WriteRegister(0x100); // Write '1' to AD9833 Control register bit D8. |
| joshthepirate | 1:a22687a74345 | 35 | wait_ms(10); |
| joshthepirate | 1:a22687a74345 | 36 | } |
| joshthepirate | 1:a22687a74345 | 37 | |
| joshthepirate | 0:bb9aeaaccee4 | 38 | void AD9833setFrequency(long frequency, int Waveform) { |
| joshthepirate | 1:a22687a74345 | 39 | |
| joshthepirate | 2:1d2510530829 | 40 | long FreqWord = (frequency * 0x1000000) / refFreq; |
| joshthepirate | 2:1d2510530829 | 41 | |
| joshthepirate | 2:1d2510530829 | 42 | printf("freq word: %ld\r\n", FreqWord); |
| joshthepirate | 0:bb9aeaaccee4 | 43 | |
| joshthepirate | 0:bb9aeaaccee4 | 44 | int MSB = (int)((FreqWord & 0xFFFC000) >> 14); //Only lower 14 bits are used for data |
| joshthepirate | 0:bb9aeaaccee4 | 45 | int LSB = (int)(FreqWord & 0x3FFF); |
| joshthepirate | 0:bb9aeaaccee4 | 46 | |
| joshthepirate | 0:bb9aeaaccee4 | 47 | //Set control bits 15 ande 14 to 0 and 1, respectively, for frequency register 0 |
| joshthepirate | 0:bb9aeaaccee4 | 48 | LSB |= 0x4000; |
| joshthepirate | 0:bb9aeaaccee4 | 49 | MSB |= 0x4000; |
| joshthepirate | 0:bb9aeaaccee4 | 50 | |
| joshthepirate | 0:bb9aeaaccee4 | 51 | WriteRegister(0x2100); |
| joshthepirate | 0:bb9aeaaccee4 | 52 | WriteRegister(LSB); // Write lower 16 bits to AD9833 registers |
| joshthepirate | 0:bb9aeaaccee4 | 53 | WriteRegister(MSB); // Write upper 16 bits to AD9833 registers. |
| joshthepirate | 0:bb9aeaaccee4 | 54 | WriteRegister(0xC000); // Phase register |
| joshthepirate | 0:bb9aeaaccee4 | 55 | WriteRegister(Waveform); // Exit & Reset to SINE, SQUARE or TRIANGLE |
| joshthepirate | 0:bb9aeaaccee4 | 56 | } |
| joshthepirate | 0:bb9aeaaccee4 | 57 | |
| joshthepirate | 1:a22687a74345 | 58 | void setup() { |
| joshthepirate | 2:1d2510530829 | 59 | fssyncpin = 1; |
| joshthepirate | 2:1d2510530829 | 60 | spi.format(8, 2); // set to 16 bit data and mode 2 |
| joshthepirate | 2:1d2510530829 | 61 | spi.frequency(1000000); // set initial fequency |
| joshthepirate | 1:a22687a74345 | 62 | |
| joshthepirate | 1:a22687a74345 | 63 | AD9833reset(); // Reset AD9833 module after power-up. |
| joshthepirate | 1:a22687a74345 | 64 | |
| joshthepirate | 1:a22687a74345 | 65 | wait_ms(50); |
| joshthepirate | 1:a22687a74345 | 66 | |
| joshthepirate | 1:a22687a74345 | 67 | AD9833setFrequency(freq, SINE); // Set the frequency and Sine Wave output |
| joshthepirate | 1:a22687a74345 | 68 | pc.printf("Ready\r\n"); |
| joshthepirate | 1:a22687a74345 | 69 | } |
| joshthepirate | 1:a22687a74345 | 70 | |
| joshthepirate | 0:bb9aeaaccee4 | 71 | int main() { |
| joshthepirate | 1:a22687a74345 | 72 | |
| joshthepirate | 0:bb9aeaaccee4 | 73 | setup(); |
| joshthepirate | 0:bb9aeaaccee4 | 74 | |
| joshthepirate | 0:bb9aeaaccee4 | 75 | while(1) { |
| joshthepirate | 0:bb9aeaaccee4 | 76 | // put your main code here, to run repeatedly: |
| joshthepirate | 0:bb9aeaaccee4 | 77 | |
| joshthepirate | 1:a22687a74345 | 78 | if(pc.readable()){ |
| joshthepirate | 0:bb9aeaaccee4 | 79 | |
| joshthepirate | 0:bb9aeaaccee4 | 80 | char incomingData[20]; |
| joshthepirate | 0:bb9aeaaccee4 | 81 | pc.scanf("%s", incomingData); |
| joshthepirate | 1:a22687a74345 | 82 | pc.printf("Setting Frequency to : %s\r\n", incomingData); |
| joshthepirate | 0:bb9aeaaccee4 | 83 | |
| joshthepirate | 0:bb9aeaaccee4 | 84 | freq = (long)atoi(incomingData); |
| joshthepirate | 0:bb9aeaaccee4 | 85 | |
| joshthepirate | 0:bb9aeaaccee4 | 86 | AD9833setFrequency(freq, SINE); |
| joshthepirate | 0:bb9aeaaccee4 | 87 | } |
| joshthepirate | 0:bb9aeaaccee4 | 88 | } |
| joshthepirate | 0:bb9aeaaccee4 | 89 | } |
| joshthepirate | 0:bb9aeaaccee4 | 90 |