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
00001 #include "mbed.h" 00002 #include "analogout_api.h" 00003 #include "mbed-os/targets/TARGET_STM/TARGET_STM32L4/device/stm32l4xx_ll_dac.h" 00004 00005 #if !DEVICE_ANALOGOUT 00006 #error You cannot use this example as the AnalogOut is not supported on this device. 00007 #else 00008 AnalogOut sinOut( PA_4 ); // channel 1 00009 #endif 00010 00011 #define PI (3.141592653589793238462f) 00012 #define AMPLITUDE (0.3) // x * 3.3V 00013 #define PHASE (PI * 1.0f) // 2*pi is one period 00014 #define OFFSET (0.5f) 00015 00016 // Configuration for sinewave output 00017 /* #define BUFFER_SIZE (540U) */ 00018 #define BUFFER_SIZE (1024U) 00019 unsigned int buffer[BUFFER_SIZE]; 00020 00021 void calculate_sinewave( void ); 00022 00023 int main() 00024 { 00025 calculate_sinewave(); 00026 00027 const int stepBuffN = 12; 00028 float stepBuff[stepBuffN] = {}; 00029 AnalogOut stepOut( PA_5 ); 00030 00031 for( int i = 0; i < stepBuffN; i += 3 ) 00032 { 00033 stepBuff[i] = 3.0f / 3.3f; 00034 stepBuff[i + 1] = 2.0f / 3.3f; 00035 stepBuff[i + 2] = 1.0f / 3.3f; 00036 }; 00037 00038 stepOut = stepBuff[0]; 00039 00040 core_util_critical_section_enter(); 00041 00042 HAL_SuspendTick(); 00043 00044 core_util_critical_section_exit(); 00045 00046 dac_t* obj = &sinOut._dac; 00047 00048 DAC_HandleTypeDef dacHandle = obj->handle; 00049 00050 uint32_t dacChannel = obj->channel; 00051 00052 for( unsigned int i = 0; i < BUFFER_SIZE; i++ ) 00053 { 00054 buffer[i] *= 0xFFF; 00055 } 00056 00057 while( 1 ) 00058 { 00059 // sinewave output 00060 00061 for( int i = 0; i < BUFFER_SIZE; ++i ) 00062 { 00063 HAL_DAC_SetValue( &dacHandle, dacChannel, DAC_ALIGN_12B_R, buffer[i] ); 00064 HAL_DAC_Start( &dacHandle, dacChannel ); 00065 } 00066 } 00067 } 00068 00069 // Create the sinewave buffer 00070 void calculate_sinewave( void ) 00071 { 00072 for( unsigned int i = 0; i < BUFFER_SIZE; i++ ) 00073 { 00074 float rads = 2.0f * ( PI * i ) / ( ( float )BUFFER_SIZE ); // Convert degree in radian 00075 buffer[i] = ( unsigned int )( 0xFFF * AMPLITUDE * cos( rads + PHASE ) + 0xFFF * OFFSET ); 00076 } 00077 }
Generated on Tue Jul 12 2022 13:24:53 by
1.7.2