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@0:bb9aeaaccee4, 2020-01-31 (annotated)
- Committer:
- joshthepirate
- Date:
- Fri Jan 31 14:09:44 2020 +0000
- Revision:
- 0:bb9aeaaccee4
- Child:
- 1:a22687a74345
ported arduino stuff
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 setup() { |
joshthepirate | 0:bb9aeaaccee4 | 22 | spi.format(16, 2); // set to 16 bit data and mode 2 |
joshthepirate | 0:bb9aeaaccee4 | 23 | spi.frequency(freq); // set initial fequency |
joshthepirate | 0:bb9aeaaccee4 | 24 | } |
joshthepirate | 0:bb9aeaaccee4 | 25 | |
joshthepirate | 0:bb9aeaaccee4 | 26 | void WriteRegister(short dat) { |
joshthepirate | 0:bb9aeaaccee4 | 27 | fsyncpin = 0; // Set FSYNC low before writing to AD9833 registers |
joshthepirate | 0:bb9aeaaccee4 | 28 | |
joshthepirate | 0:bb9aeaaccee4 | 29 | wait_us(10); // Give AD9833 time to get ready to receive data. |
joshthepirate | 0:bb9aeaaccee4 | 30 | |
joshthepirate | 0:bb9aeaaccee4 | 31 | spi.write(dat >> 8); // Each AD9833 register is 32 bits wide and each 16 |
joshthepirate | 0:bb9aeaaccee4 | 32 | spi.write(dat & 0xFF); // bits has to be transferred as 2 x 8-bit bytes. |
joshthepirate | 0:bb9aeaaccee4 | 33 | fsyncpin = 1; // Write done. Set FSYNC high |
joshthepirate | 0:bb9aeaaccee4 | 34 | } |
joshthepirate | 0:bb9aeaaccee4 | 35 | |
joshthepirate | 0:bb9aeaaccee4 | 36 | void AD9833setFrequency(long frequency, int Waveform) { |
joshthepirate | 0:bb9aeaaccee4 | 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 | 0:bb9aeaaccee4 | 54 | int main() { |
joshthepirate | 0:bb9aeaaccee4 | 55 | |
joshthepirate | 0:bb9aeaaccee4 | 56 | setup(); |
joshthepirate | 0:bb9aeaaccee4 | 57 | |
joshthepirate | 0:bb9aeaaccee4 | 58 | while(1) { |
joshthepirate | 0:bb9aeaaccee4 | 59 | // put your main code here, to run repeatedly: |
joshthepirate | 0:bb9aeaaccee4 | 60 | |
joshthepirate | 0:bb9aeaaccee4 | 61 | if(pc.readable() > 0){ |
joshthepirate | 0:bb9aeaaccee4 | 62 | |
joshthepirate | 0:bb9aeaaccee4 | 63 | char incomingData[20]; |
joshthepirate | 0:bb9aeaaccee4 | 64 | pc.scanf("%s", incomingData); |
joshthepirate | 0:bb9aeaaccee4 | 65 | pc.printf("Setting Frequency to : %s\n", incomingData); |
joshthepirate | 0:bb9aeaaccee4 | 66 | |
joshthepirate | 0:bb9aeaaccee4 | 67 | freq = (long)atoi(incomingData); |
joshthepirate | 0:bb9aeaaccee4 | 68 | |
joshthepirate | 0:bb9aeaaccee4 | 69 | AD9833setFrequency(freq, SINE); |
joshthepirate | 0:bb9aeaaccee4 | 70 | } |
joshthepirate | 0:bb9aeaaccee4 | 71 | } |
joshthepirate | 0:bb9aeaaccee4 | 72 | } |
joshthepirate | 0:bb9aeaaccee4 | 73 |