Nucleo-F767 DCO Test 01

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002    Nucleo F767 DCO Test01
00003    2018.07.22
00004 
00005 */
00006 #include "mbed.h"
00007 
00008 #include "wavetable_12bit_32k.h"
00009 #define COUNT_OF_ENTRIES  (32768)
00010 
00011 #define PIN_CHECK   (1)
00012 #define UART_TRACE  (0)
00013 #define TITLE_STR1  ("Nucleo F767 DCO Test01")
00014 #define TITLE_STR2  ("20180819")
00015 
00016 
00017 // Pin Assign
00018 AnalogOut Dac1(PA_4);
00019 AnalogOut Dac2(PA_5);
00020 
00021 #if (PIN_CHECK)
00022 DigitalOut CheckPin1(D4);
00023 DigitalOut CheckPin2(D5);
00024 #endif
00025 
00026 // Parameter
00027 double drate = 1000.0;         // initial output rate (Hz)
00028 const double refclk = 100000;  // 100kHz
00029 
00030 // Interruput
00031 Ticker ticker;
00032 
00033 // DDS
00034 volatile uint32_t phaccu;
00035 volatile uint32_t tword_m;
00036 
00037 //-------------------------------------------------------------------------------------------------
00038 // Interrupt Service Routine
00039 //
00040 
00041 // param
00042 //   channel: 1 or 2
00043 //   val: 0 .. 4095
00044 void InternalDacWrite(int ch, uint16_t val)
00045 {
00046     //uint16_t v = val << 4;
00047     // avoid distortion of the built-in DAC
00048     uint16_t v = (val + 256) * 14;
00049     
00050     switch(ch) {
00051     case 1:
00052         Dac1.write_u16(v);
00053         break;
00054     case 2:
00055         Dac2.write_u16(v);
00056         break;
00057     }
00058 }
00059 
00060 void update()
00061 {
00062 #if (PIN_CHECK)
00063     CheckPin1.write(1);
00064 #endif
00065 
00066     phaccu = phaccu + tword_m;
00067     uint16_t idx = phaccu >> 17;  // use upper 15 bits
00068     
00069     InternalDacWrite(1, sin_12bit_32k[idx]);
00070     //InternalDacWrite(2, sin_12bit_32k[idx]);
00071 
00072 #if (PIN_CHECK)
00073     CheckPin1.write(0);
00074 #endif
00075 }
00076 
00077 //-------------------------------------------------------------------------------------------------
00078 // Main routine
00079 //
00080 
00081 int main()
00082 {
00083     tword_m = pow(2.0, 32) * drate / refclk;  // calculate DDS tuning word;
00084     
00085     // 1.0s / 1.0us = 1000000.0
00086     float interruptPeriodUs = 1000000.0 / refclk; 
00087     ticker.attach_us(&update, interruptPeriodUs);
00088     
00089     while (1) {
00090 #if (PIN_CHECK)
00091     CheckPin2.write(1);
00092     wait_us(1);
00093     CheckPin2.write(0);
00094     wait_us(1);
00095 #endif
00096     }
00097 }
00098