Ryo Od
/
Nucleo-F767_DCO_Test01
Nucleo-F767 DCO Test 01
main.cpp@4:b242f4206894, 2018-08-19 (annotated)
- Committer:
- ryood
- Date:
- Sun Aug 19 01:07:58 2018 +0000
- Revision:
- 4:b242f4206894
- Parent:
- 3:19bb7c30c354
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:000c6506ff90 | 1 | /* |
ryood | 4:b242f4206894 | 2 | Nucleo F767 DCO Test01 |
ryood | 0:000c6506ff90 | 3 | 2018.07.22 |
ryood | 0:000c6506ff90 | 4 | |
ryood | 0:000c6506ff90 | 5 | */ |
ryood | 0:000c6506ff90 | 6 | #include "mbed.h" |
ryood | 0:000c6506ff90 | 7 | |
ryood | 2:4834d4483ea6 | 8 | #include "wavetable_12bit_32k.h" |
ryood | 2:4834d4483ea6 | 9 | #define COUNT_OF_ENTRIES (32768) |
ryood | 0:000c6506ff90 | 10 | |
ryood | 0:000c6506ff90 | 11 | #define PIN_CHECK (1) |
ryood | 0:000c6506ff90 | 12 | #define UART_TRACE (0) |
ryood | 4:b242f4206894 | 13 | #define TITLE_STR1 ("Nucleo F767 DCO Test01") |
ryood | 4:b242f4206894 | 14 | #define TITLE_STR2 ("20180819") |
ryood | 4:b242f4206894 | 15 | |
ryood | 0:000c6506ff90 | 16 | |
ryood | 0:000c6506ff90 | 17 | // Pin Assign |
ryood | 0:000c6506ff90 | 18 | AnalogOut Dac1(PA_4); |
ryood | 0:000c6506ff90 | 19 | AnalogOut Dac2(PA_5); |
ryood | 0:000c6506ff90 | 20 | |
ryood | 0:000c6506ff90 | 21 | #if (PIN_CHECK) |
ryood | 4:b242f4206894 | 22 | DigitalOut CheckPin1(D4); |
ryood | 4:b242f4206894 | 23 | DigitalOut CheckPin2(D5); |
ryood | 0:000c6506ff90 | 24 | #endif |
ryood | 0:000c6506ff90 | 25 | |
ryood | 0:000c6506ff90 | 26 | // Parameter |
ryood | 4:b242f4206894 | 27 | double drate = 1000.0; // initial output rate (Hz) |
ryood | 4:b242f4206894 | 28 | const double refclk = 100000; // 100kHz |
ryood | 0:000c6506ff90 | 29 | |
ryood | 0:000c6506ff90 | 30 | // Interruput |
ryood | 0:000c6506ff90 | 31 | Ticker ticker; |
ryood | 0:000c6506ff90 | 32 | |
ryood | 0:000c6506ff90 | 33 | // DDS |
ryood | 0:000c6506ff90 | 34 | volatile uint32_t phaccu; |
ryood | 0:000c6506ff90 | 35 | volatile uint32_t tword_m; |
ryood | 0:000c6506ff90 | 36 | |
ryood | 0:000c6506ff90 | 37 | //------------------------------------------------------------------------------------------------- |
ryood | 0:000c6506ff90 | 38 | // Interrupt Service Routine |
ryood | 0:000c6506ff90 | 39 | // |
ryood | 0:000c6506ff90 | 40 | |
ryood | 0:000c6506ff90 | 41 | // param |
ryood | 3:19bb7c30c354 | 42 | // channel: 1 or 2 |
ryood | 0:000c6506ff90 | 43 | // val: 0 .. 4095 |
ryood | 3:19bb7c30c354 | 44 | void InternalDacWrite(int ch, uint16_t val) |
ryood | 0:000c6506ff90 | 45 | { |
ryood | 4:b242f4206894 | 46 | //uint16_t v = val << 4; |
ryood | 4:b242f4206894 | 47 | // avoid distortion of the built-in DAC |
ryood | 4:b242f4206894 | 48 | uint16_t v = (val + 256) * 14; |
ryood | 0:000c6506ff90 | 49 | |
ryood | 3:19bb7c30c354 | 50 | switch(ch) { |
ryood | 3:19bb7c30c354 | 51 | case 1: |
ryood | 3:19bb7c30c354 | 52 | Dac1.write_u16(v); |
ryood | 3:19bb7c30c354 | 53 | break; |
ryood | 3:19bb7c30c354 | 54 | case 2: |
ryood | 3:19bb7c30c354 | 55 | Dac2.write_u16(v); |
ryood | 3:19bb7c30c354 | 56 | break; |
ryood | 3:19bb7c30c354 | 57 | } |
ryood | 0:000c6506ff90 | 58 | } |
ryood | 0:000c6506ff90 | 59 | |
ryood | 0:000c6506ff90 | 60 | void update() |
ryood | 0:000c6506ff90 | 61 | { |
ryood | 0:000c6506ff90 | 62 | #if (PIN_CHECK) |
ryood | 4:b242f4206894 | 63 | CheckPin1.write(1); |
ryood | 0:000c6506ff90 | 64 | #endif |
ryood | 0:000c6506ff90 | 65 | |
ryood | 0:000c6506ff90 | 66 | phaccu = phaccu + tword_m; |
ryood | 2:4834d4483ea6 | 67 | uint16_t idx = phaccu >> 17; // use upper 15 bits |
ryood | 0:000c6506ff90 | 68 | |
ryood | 3:19bb7c30c354 | 69 | InternalDacWrite(1, sin_12bit_32k[idx]); |
ryood | 4:b242f4206894 | 70 | //InternalDacWrite(2, sin_12bit_32k[idx]); |
ryood | 0:000c6506ff90 | 71 | |
ryood | 0:000c6506ff90 | 72 | #if (PIN_CHECK) |
ryood | 4:b242f4206894 | 73 | CheckPin1.write(0); |
ryood | 0:000c6506ff90 | 74 | #endif |
ryood | 0:000c6506ff90 | 75 | } |
ryood | 0:000c6506ff90 | 76 | |
ryood | 0:000c6506ff90 | 77 | //------------------------------------------------------------------------------------------------- |
ryood | 0:000c6506ff90 | 78 | // Main routine |
ryood | 0:000c6506ff90 | 79 | // |
ryood | 0:000c6506ff90 | 80 | |
ryood | 0:000c6506ff90 | 81 | int main() |
ryood | 0:000c6506ff90 | 82 | { |
ryood | 0:000c6506ff90 | 83 | tword_m = pow(2.0, 32) * drate / refclk; // calculate DDS tuning word; |
ryood | 0:000c6506ff90 | 84 | |
ryood | 4:b242f4206894 | 85 | // 1.0s / 1.0us = 1000000.0 |
ryood | 0:000c6506ff90 | 86 | float interruptPeriodUs = 1000000.0 / refclk; |
ryood | 0:000c6506ff90 | 87 | ticker.attach_us(&update, interruptPeriodUs); |
ryood | 0:000c6506ff90 | 88 | |
ryood | 0:000c6506ff90 | 89 | while (1) { |
ryood | 4:b242f4206894 | 90 | #if (PIN_CHECK) |
ryood | 4:b242f4206894 | 91 | CheckPin2.write(1); |
ryood | 4:b242f4206894 | 92 | wait_us(1); |
ryood | 4:b242f4206894 | 93 | CheckPin2.write(0); |
ryood | 4:b242f4206894 | 94 | wait_us(1); |
ryood | 4:b242f4206894 | 95 | #endif |
ryood | 0:000c6506ff90 | 96 | } |
ryood | 0:000c6506ff90 | 97 | } |
ryood | 0:000c6506ff90 | 98 |