Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.   ******************************************************************************
  3.   * @file    DAC_SignalsGeneration/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    19-September-2011
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************  
  20.   */
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f4_discovery.h"
  24.  
  25. /** @addtogroup STM32F4_Discovery_Peripheral_Examples
  26.   * @{
  27.   */
  28.  
  29. /** @addtogroup DAC_SignalsGeneration
  30.   * @{
  31.   */
  32.  
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. #define DAC_DHR12R2_ADDRESS    0x40007414
  36. #define DAC_DHR8R1_ADDRESS     0x40007410
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* Private variables ---------------------------------------------------------*/
  40. DAC_InitTypeDef  DAC_InitStructure;
  41.  
  42. const uint16_t Sine12bit[32] = {
  43.                       2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
  44.                       3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,
  45.                       599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
  46. const uint8_t Escalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};
  47.  
  48. __IO uint8_t SelectedWavesForm = 1;
  49. __IO uint8_t KeyPressed = RESET;
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void TIM6_Config(void);
  53.  
  54. void DAC_Ch1_EscalatorConfig(void);
  55. void DAC_Ch2_SineWaveConfig(void);
  56.  
  57. void DAC_Ch1_NoiseConfig(void);
  58. void DAC_Ch2_TriangleConfig(void);
  59.  
  60. /* Private functions ---------------------------------------------------------*/
  61.  
  62. /**
  63.   * @brief   Main program
  64.   * @param  None
  65.   * @retval None
  66.   */
  67. int main(void)
  68. {
  69.   /*!< At this stage the microcontroller clock setting is already configured,
  70.        this is done through SystemInit() function which is called from startup
  71.        file (startup_stm32f4xx.s) before to branch to application main.
  72.        To reconfigure the default setting of SystemInit() function, refer to
  73.        system_stm32f4xx.c file
  74.      */    
  75.  
  76.   /* Preconfiguration before using DAC----------------------------------------*/
  77.   GPIO_InitTypeDef GPIO_InitStructure;
  78.  
  79.   /* DMA1 clock and GPIOA clock enable (to be used with DAC) */
  80.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);
  81.  
  82.   /* DAC Periph clock enable */
  83.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
  84.  
  85.   /* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
  86.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  87.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  88.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  89.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  90.  
  91.   /* TIM6 Configuration ------------------------------------------------------*/
  92.   TIM6_Config();  
  93.  
  94.   /* Configures User Button */
  95.   STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
  96.  
  97.   while (1)
  98.   {
  99.     /* If the User Button is pressed */
  100.     if (KeyPressed == RESET)
  101.     {            
  102.       DAC_DeInit();
  103.  
  104.       /* select waves forms according to the Key Button status */
  105.       if (SelectedWavesForm == 1)
  106.       {
  107.         /* The sine wave and the escalator wave has been selected */
  108.  
  109.         /* Escalator Wave generator ------------------------------------------*/
  110.         DAC_Ch1_EscalatorConfig();
  111.  
  112.         /* Sine Wave generator -----------------------------------------------*/
  113.         DAC_Ch2_SineWaveConfig();
  114.          
  115.       }
  116.       else
  117.       {
  118.         /* The triangle wave and the noise wave has been selected */
  119.  
  120.         /* Noise Wave generator ----------------------------------------------*/
  121.         DAC_Ch1_NoiseConfig();
  122.  
  123.         /* Triangle Wave generator -------------------------------------------*/
  124.         DAC_Ch2_TriangleConfig();
  125.       }
  126.      
  127.       KeyPressed = RESET;
  128.     }
  129.   }
  130. }
  131.  
  132. /**
  133.   * @brief  TIM6 Configuration
  134.   * @note   TIM6 configuration is based on CPU @168MHz and APB1 @42MHz
  135.   * @note   TIM6 Update event occurs each 37.5MHz/256 = 16.406 KHz    
  136.   * @param  None
  137.   * @retval None
  138.   */
  139. void TIM6_Config(void)
  140. {
  141.   TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
  142.   /* TIM6 Periph clock enable */
  143.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
  144.  
  145.   /* Time base configuration */
  146.   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  147.   TIM_TimeBaseStructure.TIM_Period = 0xFF;
  148.   TIM_TimeBaseStructure.TIM_Prescaler = 0;
  149.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  150.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  151.   TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
  152.  
  153.   /* TIM6 TRGO selection */
  154.   TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
  155.  
  156.   /* TIM6 enable counter */
  157.   TIM_Cmd(TIM6, ENABLE);
  158. }
  159.  
  160. /**
  161.   * @brief  DAC  Channel2 SineWave Configuration
  162.   * @param  None
  163.   * @retval None
  164.   */
  165. void DAC_Ch2_SineWaveConfig(void)
  166. {
  167.   DMA_InitTypeDef DMA_InitStructure;
  168.  
  169.   /* DAC channel2 Configuration */
  170.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
  171.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  172.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  173.   DAC_Init(DAC_Channel_2, &DAC_InitStructure);
  174.  
  175.   /* DMA1_Stream5 channel7 configuration **************************************/
  176.   DMA_DeInit(DMA1_Stream5);
  177.   DMA_InitStructure.DMA_Channel = DMA_Channel_7;  
  178.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R2_ADDRESS;
  179.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Sine12bit;
  180.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  181.   DMA_InitStructure.DMA_BufferSize = 32;
  182.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  183.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  184.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  185.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  186.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  187.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  188.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  189.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  190.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  191.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  192.   DMA_Init(DMA1_Stream5, &DMA_InitStructure);
  193.  
  194.   /* Enable DMA1_Stream5 */
  195.   DMA_Cmd(DMA1_Stream5, ENABLE);
  196.  
  197.   /* Enable DAC Channel2 */
  198.   DAC_Cmd(DAC_Channel_2, ENABLE);
  199.  
  200.   /* Enable DMA for DAC Channel2 */
  201.   DAC_DMACmd(DAC_Channel_2, ENABLE);
  202. }
  203.  
  204. /**
  205.   * @brief  DAC Channel1 Escalator Configuration
  206.   * @param  None
  207.   * @retval None
  208.   */
  209. void DAC_Ch1_EscalatorConfig(void)
  210. {
  211.   DMA_InitTypeDef DMA_InitStructure;
  212.  
  213.   /* DAC channel1 Configuration */
  214.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
  215.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  216.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  217.   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  218.  
  219.   /* DMA1_Stream6 channel7 configuration **************************************/  
  220.   DMA_DeInit(DMA1_Stream6);
  221.   DMA_InitStructure.DMA_Channel = DMA_Channel_7;  
  222.   DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS;
  223.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Escalator8bit;
  224.   DMA_InitStructure.DMA_BufferSize = 6;
  225.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  226.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  227.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  228.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  229.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  230.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  231.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  232.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;        
  233.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  234.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  235.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  236.   DMA_Init(DMA1_Stream6, &DMA_InitStructure);    
  237.  
  238.   /* Enable DMA1_Stream6 */
  239.   DMA_Cmd(DMA1_Stream6, ENABLE);
  240.  
  241.   /* Enable DAC Channel1 */
  242.   DAC_Cmd(DAC_Channel_1, ENABLE);
  243.  
  244.   /* Enable DMA for DAC Channel1 */
  245.   DAC_DMACmd(DAC_Channel_1, ENABLE);
  246. }
  247.  
  248. /**
  249.   * @brief  DAC Channel2 Triangle Configuration
  250.   * @param  None
  251.   * @retval None
  252.   */
  253. void DAC_Ch2_TriangleConfig(void)
  254. {
  255.  /* DAC channel2 Configuration */
  256.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
  257.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
  258.   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
  259.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  260.   DAC_Init(DAC_Channel_2, &DAC_InitStructure);
  261.  
  262.   /* Enable DAC Channel2 */
  263.   DAC_Cmd(DAC_Channel_2, ENABLE);
  264.  
  265.   /* Set DAC channel2 DHR12RD register */
  266.   DAC_SetChannel2Data(DAC_Align_12b_R, 0x100);
  267. }
  268.  
  269. /**
  270.   * @brief  DAC  Channel1 Noise Configuration
  271.   * @param  None
  272.   * @retval None
  273.   */
  274. void DAC_Ch1_NoiseConfig(void)
  275. {
  276.  /* DAC channel1 Configuration */
  277.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
  278.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
  279.   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits10_0;
  280.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  281.   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  282.  
  283.   /* Enable DAC Channel1 */
  284.   DAC_Cmd(DAC_Channel_1, ENABLE);
  285.  
  286.   /* Set DAC Channel1 DHR12L register */
  287.   DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0);
  288. }
  289.  
  290. #ifdef  USE_FULL_ASSERT
  291.  
  292. /**
  293.   * @brief  Reports the name of the source file and the source line number
  294.   *         where the assert_param error has occurred.
  295.   * @param  file: pointer to the source file name
  296.   * @param  line: assert_param error line source number
  297.   * @retval None
  298.   */
  299. void assert_failed(uint8_t* file, uint32_t line)
  300. {
  301.   /* User can add his own implementation to report the file name and line number,
  302.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  303.  
  304.   /* Infinite loop */
  305.   while (1)
  306.   {
  307.   }
  308. }
  309. #endif
  310.  
  311. /**
  312.   * @}
  313.   */
  314.  
  315. /**
  316.   * @}
  317.   */
  318.  
  319. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement