/** ****************************************************************************** * @file DAC/DualModeDMA_SineWave/main.c * @author MCD Application Team * @version V3.4.0 * @date 10/15/2010 * @brief Main program body. ****************************************************************************** * @copy * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * *

© COPYRIGHT 2010 STMicroelectronics

*/ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" #define PI_ARR_N 1000 //#define PI_ARR_N 32 /** @addtogroup STM32F10x_StdPeriph_Examples * @{ */ /** @addtogroup DAC_DualModeDMA_SineWave * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define DAC_DHR12RD_Address 0x40007420 /* Init Structure definition */ DAC_InitTypeDef DAC_InitStructure; DMA_InitTypeDef DMA_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint32_t Idx = 0; /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ //const uint16_t Sine12bit[32] = { // 2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056, // 3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, // 599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647}; const uint16_t Sine12bit[PI_ARR_N] = { 2000,2012,2025,2037,2050,2062,2075,2087,2100,2113, 2125,2138,2150,2163,2175,2188,2200,2213,2225,2238, 2250,2263,2275,2288,2300,2312,2325,2337,2350,2362, 2374,2387,2399,2411,2424,2436,2448,2460,2472,2485, 2497,2509,2521,2533,2545,2557,2570,2582,2594,2606, 2618,2629,2641,2653,2665,2677,2689,2701,2712,2724, 2736,2747,2759,2771,2782,2794,2805,2817,2828,2840, 2851,2862,2874,2885,2896,2907,2919,2930,2941,2952, 2963,2974,2985,2996,3007,3018,3028,3039,3050,3061, 3071,3082,3092,3103,3113,3124,3134,3144,3155,3165, 3175,3185,3195,3205,3215,3225,3235,3245,3255,3265, 3274,3284,3294,3303,3313,3322,3332,3341,3350,3359, 3369,3378,3387,3396,3405,3414,3423,3431,3440,3449, 3457,3466,3475,3483,3491,3500,3508,3516,3524,3532, 3541,3549,3556,3564,3572,3580,3587,3595,3603,3610, 3618,3625,3632,3639,3647,3654,3661,3668,3675,3681, 3688,3695,3701,3708,3715,3721,3727,3734,3740,3746, 3752,3758,3764,3770,3776,3782,3787,3793,3798,3804, 3809,3814,3820,3825,3830,3835,3840,3845,3850,3854, 3859,3864,3868,3873,3877,3881,3885,3890,3894,3898, 3902,3905,3909,3913,3917,3920,3924,3927,3930,3934, 3937,3940,3943,3946,3949,3951,3954,3957,3959,3962, 3964,3966,3969,3971,3973,3975,3977,3979,3980,3982, 3984,3985,3987,3988,3989,3991,3992,3993,3994,3995, 3996,3996,3997,3998,3998,3999,3999,3999,3999,3999, 3999,3999,3999,3999,3999,3999,3998,3998,3997,3996, 3996,3995,3994,3993,3992,3991,3989,3988,3987,3985, 3984,3982,3980,3979,3977,3975,3973,3971,3969,3966, 3964,3962,3959,3957,3954,3951,3949,3946,3943,3940, 3937,3934,3930,3927,3924,3920,3917,3913,3909,3905, 3902,3898,3894,3890,3885,3881,3877,3873,3868,3864, 3859,3854,3850,3845,3840,3835,3830,3825,3820,3814, 3809,3804,3798,3793,3787,3782,3776,3770,3764,3758, 3752,3746,3740,3734,3727,3721,3715,3708,3701,3695, 3688,3681,3675,3668,3661,3654,3647,3639,3632,3625, 3618,3610,3603,3595,3587,3580,3572,3564,3556,3549, 3541,3532,3524,3516,3508,3500,3491,3483,3475,3466, 3457,3449,3440,3431,3423,3414,3405,3396,3387,3378, 3369,3359,3350,3341,3332,3322,3313,3303,3294,3284, 3274,3265,3255,3245,3235,3225,3215,3205,3195,3185, 3175,3165,3155,3144,3134,3124,3113,3103,3092,3082, 3071,3061,3050,3039,3028,3018,3007,2996,2985,2974, 2963,2952,2941,2930,2919,2907,2896,2885,2874,2862, 2851,2840,2828,2817,2805,2794,2782,2771,2759,2747, 2736,2724,2712,2701,2689,2677,2665,2653,2641,2629, 2618,2606,2594,2582,2570,2557,2545,2533,2521,2509, 2497,2485,2472,2460,2448,2436,2424,2411,2399,2387, 2374,2362,2350,2337,2325,2312,2300,2288,2275,2263, 2250,2238,2225,2213,2200,2188,2175,2163,2150,2138, 2125,2113,2100,2087,2075,2062,2050,2037,2025,2012, 2000,1987,1974,1962,1949,1937,1924,1912,1899,1886, 1874,1861,1849,1836,1824,1811,1799,1786,1774,1761, 1749,1736,1724,1711,1699,1687,1674,1662,1649,1637, 1625,1612,1600,1588,1575,1563,1551,1539,1527,1514, 1502,1490,1478,1466,1454,1442,1429,1417,1405,1393, 1381,1370,1358,1346,1334,1322,1310,1298,1287,1275, 1263,1252,1240,1228,1217,1205,1194,1182,1171,1159, 1148,1137,1125,1114,1103,1092,1080,1069,1058,1047, 1036,1025,1014,1003, 992, 981, 971, 960, 949, 938, 928, 917, 907, 896, 886, 875, 865, 855, 844, 834, 824, 814, 804, 794, 784, 774, 764, 754, 744, 734, 725, 715, 705, 696, 686, 677, 667, 658, 649, 640, 630, 621, 612, 603, 594, 585, 576, 568, 559, 550, 542, 533, 524, 516, 508, 499, 491, 483, 475, 467, 458, 450, 443, 435, 427, 419, 412, 404, 396, 389, 381, 374, 367, 360, 352, 345, 338, 331, 324, 318, 311, 304, 298, 291, 284, 278, 272, 265, 259, 253, 247, 241, 235, 229, 223, 217, 212, 206, 201, 195, 190, 185, 179, 174, 169, 164, 159, 154, 149, 145, 140, 135, 131, 126, 122, 118, 114, 109, 105, 101, 97, 94, 90, 86, 82, 79, 75, 72, 69, 65, 62, 59, 56, 53, 50, 48, 45, 42, 40, 37, 35, 33, 30, 28, 26, 24, 22, 20, 19, 17, 15, 14, 12, 11, 10, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8, 10, 11, 12, 14, 15, 17, 19, 20, 22, 24, 26, 28, 30, 33, 35, 37, 40, 42, 45, 48, 50, 53, 56, 59, 62, 65, 69, 72, 75, 79, 82, 86, 90, 94, 97, 101, 105, 109, 114, 118, 122, 126, 131, 135, 140, 145, 149, 154, 159, 164, 169, 174, 179, 185, 190, 195, 201, 206, 212, 217, 223, 229, 235, 241, 247, 253, 259, 265, 272, 278, 284, 291, 298, 304, 311, 318, 324, 331, 338, 345, 352, 360, 367, 374, 381, 389, 396, 404, 412, 419, 427, 435, 443, 450, 458, 467, 475, 483, 491, 499, 508, 516, 524, 533, 542, 550, 559, 568, 576, 585, 594, 603, 612, 621, 630, 640, 649, 658, 667, 677, 686, 696, 705, 715, 725, 734, 744, 754, 764, 774, 784, 794, 804, 814, 824, 834, 844, 855, 865, 875, 886, 896, 907, 917, 928, 938, 949, 960, 971, 981, 992,1003,1014,1025, 1036,1047,1058,1069,1080,1092,1103,1114,1125,1137, 1148,1159,1171,1182,1194,1205,1217,1228,1240,1252, 1263,1275,1287,1298,1310,1322,1334,1346,1358,1370, 1381,1393,1405,1417,1429,1442,1454,1466,1478,1490, 1502,1514,1526,1539,1551,1563,1575,1588,1600,1612, 1625,1637,1649,1662,1674,1687,1699,1711,1724,1736, 1749,1761,1774,1786,1799,1811,1824,1836,1849,1861, 1874,1886,1899,1912,1924,1937,1949,1962,1974,1987}; uint32_t DualSine12bit[PI_ARR_N]; /* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void Delay(__IO uint32_t nCount); /* Private functions ---------------------------------------------------------*/ /** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically connected to the DAC converter. In order to avoid parasitic consumption, the GPIO pin should be configured in analog */ GPIO_Configuration(); /* TIM2 Configuration */ /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0x19; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DAC channel2 Configuration */ DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Fill Sine32bit table */ for (Idx = 0; Idx < PI_ARR_N; Idx++) { DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]); } #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL /* DMA2 channel4 configuration */ DMA_DeInit(DMA2_Channel4); #else /* DMA1 channel4 configuration */ DMA_DeInit(DMA1_Channel4); #endif DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DualSine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = PI_ARR_N; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL DMA_Init(DMA2_Channel4, &DMA_InitStructure); /* Enable DMA2 Channel4 */ DMA_Cmd(DMA2_Channel4, ENABLE); #else DMA_Init(DMA1_Channel4, &DMA_InitStructure); /* Enable DMA1 Channel4 */ DMA_Cmd(DMA1_Channel4, ENABLE); #endif /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); while (1) { } } /** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* Enable peripheral clocks ------------------------------------------------*/ #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL /* DMA2 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); #else /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); #endif /* GPIOA Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* DAC Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* TIM2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); } /** * @brief Configures the different GPIO ports. * @param None * @retval None */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically connected to the DAC converter. In order to avoid parasitic consumption, the GPIO pin should be configured in analog */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); } /** * @brief Inserts a delay time. * @param nCount: specifies the delay time length. * @retval None */ void Delay(__IO uint32_t nCount) { for(; nCount != 0; nCount--); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /** * @} */ /** * @} */ /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/