DISCO-F746NGのAudioOutから正弦波をスイープとして出力します。ダブルバッファリングを使って、リアルタイムにデータ生成をしています。 This program outputs sine sweep from AudioOut on DISCO-F746NG. The program uses double buffering when generates data signals.

Dependencies:   BSP_DISCO_F746NG_patch_fixed LCD_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 
00003 LCD_DISCO_F746NG lcd;
00004 int16_t sweep_buffer[BufferSize];
00005 double sweep_phase = 0.0;
00006 double sweep_freq = MinFreq;
00007 
00008 void init_audio()
00009 {
00010     if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, AudioVolume, SamplingFreq) == AUDIO_ERROR)
00011         error_trap();
00012 
00013     NVIC_SetVector(AUDIO_OUT_SAIx_DMAx_IRQ, (uint32_t)&AUDIO_OUT_SAIx_DMAx_IRQHandler);
00014     BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
00015 }
00016 
00017 void play_audio()
00018 {
00019     if (BSP_AUDIO_OUT_Play((uint16_t*)&sweep_buffer, BufferByteSize) == AUDIO_ERROR)
00020         error_trap();
00021 }
00022 
00023 void fill_buffer(uint32_t offset, uint32_t size)
00024 {
00025     static double sweep_freq_delta = 1.0 + SweepSpeed;
00026     double sweep_phase_delta = 1.0 / SamplingFreq;
00027     double new_freq;
00028     
00029     for (int i = 0, j = offset; i < size / 2; i++ ) {
00030         int16_t value = (int16_t)(Amplifier * sin(2.0 * PI * sweep_freq * sweep_phase) * 32767.5);
00031         sweep_buffer[j++] = value;
00032         sweep_buffer[j++] = value;
00033 
00034         sweep_phase += sweep_phase_delta;
00035     }
00036     
00037     new_freq = sweep_freq * sweep_freq_delta;
00038     sweep_phase = sweep_phase * sweep_freq / new_freq;
00039     sweep_freq = new_freq;
00040 
00041     if (sweep_freq > MaxFreq)
00042         sweep_freq_delta = 1.0 - SweepSpeed;
00043     else if (sweep_freq < MinFreq)
00044         sweep_freq_delta = 1.0 + SweepSpeed;
00045 }
00046 
00047 int main()
00048 {
00049     char strbuf[64];
00050 
00051     lcd.Clear(LCD_COLOR_BLACK);
00052     lcd.SetBackColor(LCD_COLOR_BLACK);
00053     lcd.SetTextColor(LCD_COLOR_WHITE);
00054     lcd.SetFont(&Font12);
00055 
00056     init_audio();
00057     play_audio();
00058 
00059     while(1) {
00060         sprintf(strbuf, "Freq: %.2f Hz, Phase: %.2f  ", sweep_freq, sweep_phase);
00061         printlcdAt(strbuf, 0);
00062         wait_ms(100);
00063     }
00064 }