コード例 #1
0
ファイル: main.c プロジェクト: shjere/common
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32L0xx HAL library initialization:
       - Configure the Flash prefetch, Flash preread and Buffer caches
       - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure LED2 */
  BSP_LED_Init(LED2);

  /* Configure the system clock to 32 Mhz */
  SystemClock_Config();
  
  /* Configure DAC */
  DAC_Config();
  
  /* Configure COMP */
  COMP_Config(); 
  
  /* TIM2 Configuration in input capture mode */
  TIM_Config();
  
  /* Infinite loop */
  while (1)
  {}
}
コード例 #2
0
void start_configration(void)
{
	uint8_t sdadc_cali_flag=0;//SDADC配置成功与否标志,0-成功,1-INITRDY未置位,2-EOCAL未置位
	
	Init_flash();//初始化一些参数
		
	delay_init();//延时函数初始化,可以使用delay_ms(u16 nms),delay_us(u32 nus)
	NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	HT1621_Init();
	lcd_clr();
	lcd_full_test();
	Init_Keyboard_Interrupt();//内含TIM3-按键10ms检测中断、EXTI9_5-按键外部中断
	BUZZER_LED_Init();		//TIM4驱动
	uart_init(115200);//内含USART2与上位机通信串口中断9600
	delay_ms(500);
	sdadc_cali_flag=SDADC1_Config();//采样配置,内含DMA2-SDADC数据DMA中断
	TIM19_Config();//采样触发、采样频率配置
	TIM5_2_Compare_Capture_Init();//COMP比较器、定时器配置,用于基频捕获,内含TIM5比较器捕获中断
	DTA0660_Init();//内含Usart1-DTA0660通信串口中断,用于单片机与DTA通信
	TIM12_Config_1s();//1s定时器,内含TIM12-1s中断:秒表显示闪烁
	TIM14_Config_0_5s();//0.5s定时器,内含TIM14-0.5s中断:相序测量闪烁

	PowerControl_Init();
	
	DAC_Config();//新增DAC输出驱动钳头
	Dac1_Set_Vol(1800);
	
	TIM13_Config_1s_Standby();//内含1s中断,计数1800次为30min,进入Standby模式
	
	
}
コード例 #3
0
ファイル: main.c プロジェクト: RTOS-Developers/TRTOS
/**
  * @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_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */     

  /* Fill Sine12bit table */
  for (Idx = 0; Idx < 32; Idx++)
  {
    DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
  }

  /* DMA1 channel3 configuration: DualSine12bit is used as memory base address */
  DMA_Config();

  /* DAC configuration ------------------------------------------------------*/
  DAC_Config();

  /* TIM2 configuration ------------------------------------------------------*/
  TIM_Config();

  while (1)
  {
  }
}
コード例 #4
0
ファイル: main.c プロジェクト: AuCl3/P14345_Software
int main(void)
{
	
	uint16_t out = 0;
	
	
	/* Configure ADC and DAC */
	ADC_Config();
	DAC_Config();
	

	
	/* Loop forever */
	while(1)
	{
		
		
		
		/* Test EOC flag */
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
    
    /* Get ADC1 converted data */
    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
		
		Data = ADC1ConvertedValue;
		
		/*
		if( Data > 0x555 )
		{
			if( out < 0x0FFF )
			{
				out = out + 50;
			}

		}
		else
		{
			out = 0;
		
		}
		
		*/
			/* Output to DAC */
		DAC_SetChannel1Data(DAC_Align_12b_R, Data);
		
		
		
		
		
		
	} //end while
	
} //end main
コード例 #5
0
/**
  * @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_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */ 

  /* DAC configuration ---------------------------------------------*/
  DAC_Config();

  /* ADC configuration ---------------------------------------------*/
  ADC_Config();

  /* Infinite loop */
  while (1)
  {
  }
}
コード例 #6
0
ファイル: main.c プロジェクト: zegervdv/PS2-controller
/**
  * @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_stm32f0xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f0xx.c file
  */ 
  
  /* Preconfiguration before using DAC----------------------------------------*/
  DAC_Config();
  
  /* TIM2 configuration to trigger DAC */
  TIM2_Config();
  
  /* Configures User Button */
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
  
  while (1)
  {
    /* If the User Button is pressed */
    if (KeyPressed == RESET)
    {            
      DAC_DeInit(); 
      
      /* select waves forms according to the Key Button status */
      if (SelectedWavesForm == 1)
      {
        /* The sine wave and the escalator wave has been selected */
        DAC_Sine_EscalatorConfig();        
      }
      else
      {
        /* The triangle wave and the noise wave has been selected */
        DAC_Noise_TriangleConfig();
      }
      
      KeyPressed = SET; 
    }
  }
}
コード例 #7
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void COMP_PulseWidthMeasurement(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f30x.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */

  /* Initialize the TFT-LCD */
  STM32303C_LCD_Init();
  
  /* Clear the TFT-LCD */
  LCD_Clear(LCD_COLOR_WHITE);
  
  /* DAC Channel1 configuration */
  DAC_Config();
  
  /* COMP1 Configuration */
  COMP_Config();
  
  /* TIM2 Configuration in input capture mode */
  TIM_Config();
  
  /* Displays PulseWidthMeasurement message on line 0 */
  LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);

  /* Infinite loop */
  while (1)
  {
    if (DisplayActive != 0)
    {
      /* Compute the pulse width in us */
      MeasuredPulse = (uint32_t)(((uint64_t) Capture * 1000000) / ((uint32_t)SystemCoreClock));
      
      /* Display measured pulse width on LCD */
      LCD_Display(MeasuredPulse);  
      DisplayActive = 0;
    }
  }
}
コード例 #8
0
/**
  * @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_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */ 

    /* Initialize the TFT-LCD */
    STM320518_LCD_Init();
    
    /* Clear the TFT-LCD */
    LCD_Clear(LCD_COLOR_WHITE);
      
    /* DAC Channel1 configuration */
    DAC_Config();
    
    /* COMP1 Configuration */
    COMP_Config();
    
    /* TIM2 Configuration in input capture mode */
    TIM_Config();
   

  /* Infinite loop */
  while (1)
  {
    if (DisplayActive != 0)
    {
      /* Compute the pulse width in us */
      MeasuredPulse = (uint32_t)(((uint64_t) Capture * 1000000) / ((uint32_t)SystemCoreClock));
      
      /* Display measured pulse width on Glass LCD and color LCD */
      DisplayOnLCD(MeasuredPulse);  
      DisplayActive = 0;
    }
  }
}
コード例 #9
0
ファイル: main.c プロジェクト: avr-master/Healty_Beck
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
    /* CLK configuration -------------------------------------------*/
    CLK_Config();

    /* GPIO configuration -------------------------------------------*/
    GPIO_Config();

    /* TIM1 configuration -------------------------------------------*/
    TIM1_Config();

    /* DAC configuration -------------------------------------------*/
    DAC_Config();

    /* COMP configuration -------------------------------------------*/
    COMP_Config();

    /* TIM1 counter enable */
    TIM1_Cmd(ENABLE);

    /* Infinite loop */
    while (1)
    {}
}
コード例 #10
0
ファイル: DAC_SignalsGeneration.c プロジェクト: coocox/Mplib
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void DAC_SignalsGeneration(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_stm32f30x.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f30x.c file
       */

    /* Preconfiguration before using DAC----------------------------------------*/
    DAC_Config();

    /* TIM2 configuration to trigger DAC */
    TIM_Config();

    /* Configures Key Button EXTI Line */
    STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);

    /* Infinite loop */
    while (1)
    {
        /* If the wave form is changed */
        if (WaveChange == 1)
        {
            /* Switch the selected waves forms according the Button status */
            if (SelectedWavesForm == 1)
            {
                /* The sine wave and the escalator wave has been selected */
                /* Sine Wave generator ---------------------------------------------*/

                /* DAC channel2 Configuration */
                DAC_DeInit();
                DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
                DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
                DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;

                /* DAC Channel2 Init */
                DAC_Init(DAC_Channel_2, &DAC_InitStructure);

                /* Enable DAC Channel2 */
                DAC_Cmd(DAC_Channel_2, ENABLE);

                /* DMA2 channel3 configuration */
                DMA_DeInit(DMA2_Channel3);
                DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_ADDRESS;
                DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
                DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
                DMA_InitStructure.DMA_BufferSize = 32;
                DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
                DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
                DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
                DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
                DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
                DMA_InitStructure.DMA_Priority = DMA_Priority_High;
                DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
                DMA_Init(DMA2_Channel3, &DMA_InitStructure);

                /* Enable DMA2 Channel3 */
                DMA_Cmd(DMA2_Channel3, ENABLE);

                /* Enable DMA for DAC Channel2 */
                DAC_DMACmd(DAC_Channel_2, ENABLE);

                /* Escalator Wave generator ----------------------------------------*/

                /* DAC channel1 Configuration */
                DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
                DAC_Init(DAC_Channel_1, &DAC_InitStructure);

                /* DMA2 channel4 configuration */
                DMA_DeInit(DMA2_Channel4);
                DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS;
                DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit;
                DMA_InitStructure.DMA_BufferSize = 6;
                DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
                DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
                DMA_Init(DMA2_Channel4, &DMA_InitStructure);

                /* Enable DMA2 Channel4 */
                DMA_Cmd(DMA2_Channel4, ENABLE);

                /* Enable DAC1 Channel1: Once the DAC1 channel1 is enabled, PA.05 is
                   automatically connected to the DAC converter. */
                DAC_Cmd(DAC_Channel_1, ENABLE);

                /* Enable DMA for DAC Channel1 */
                DAC_DMACmd(DAC_Channel_1, ENABLE);
            }
            /* The triangle wave and the noise wave has been selected */
            else
            {
                /* Noise Wave generator --------------------------------------------*/

                /* DAC channel1 Configuration */
                DAC_DeInit();
                DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
                DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
                DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
                DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
                DAC_Init(DAC_Channel_1, &DAC_InitStructure);

                /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is
                   automatically connected to the DAC converter. */
                DAC_Cmd(DAC_Channel_1, ENABLE);

                /* Set DAC Channel1 DHR12L register */
                DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0);

                /* Enable DAC channel1 wave generator */
                DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE);

                /* Triangle Wave generator -----------------------------------------*/

                /* DAC channel2 Configuration */
                DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
                DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
                DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
                DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
                DAC_Init(DAC_Channel_2, &DAC_InitStructure);

                /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
                   automatically connected to the DAC converter. */
                DAC_Cmd(DAC_Channel_2, ENABLE);

                /* Set DAC channel2 DHR12RD register */
                DAC_SetChannel2Data(DAC_Align_12b_R, 0x100);
            }
            WaveChange = !WaveChange;
        }
    }
}
コード例 #11
0
ファイル: main.c プロジェクト: Lembed/STM32CubeF1-mirrors
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F103xG HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 72 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  
  /* Initialize LEDs on board */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED1);
  
  /* Configure Key push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
  
  /* Configure the ADC peripheral */
  ADC_Config();
  
  /* Run the ADC calibration */  
  if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }


#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
  /* Configure the DAC peripheral */
  DAC_Config();
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */
  

  /*## Enable peripherals ####################################################*/
  
#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
  /* Set DAC Channel data register: channel corresponding to ADC channel CHANNELa */
  /* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, RANGE_12BITS/2) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }
  
  /* Enable DAC Channel: channel corresponding to ADC channel CHANNELa */
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */

  /*## Start ADC conversions #################################################*/
  
  /* Start ADC conversion on regular group with transfer by DMA */
  if (HAL_ADC_Start_DMA(&AdcHandle,
                        (uint32_t *)aADCxConvertedValues,
                        ADCCONVERTEDVALUES_BUFFER_SIZE
                       ) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  
  
  /* Infinite loop */
  while (1)
  {

    /* Wait for event on push button to perform following actions */
    while ((ubUserButtonClickEvent) == RESET)
    {
    }
    /* Reset variable for next loop iteration */
    ubUserButtonClickEvent = RESET;

#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
    /* Set DAC voltage on channel corresponding to ADCx_CHANNELa              */
    /* in function of user button clicks count.                               */
    /* Set DAC output successively to:                                        */
    /*  - minimum of full range (0 <=> ground 0V)                             */
    /*  - 1/4 of full range (4095 <=> Vdda=3.3V): 1023 <=> 0.825V             */
    /*  - 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
    /*  - 3/4 of full range (4095 <=> Vdda=3.3V): 3071 <=> 2.475V             */
    /*  - maximum of full range (4095 <=> Vdda=3.3V)                          */
    if (HAL_DAC_SetValue(&DacHandle,
                         DACx_CHANNEL_TO_ADCx_CHANNELa,
                         DAC_ALIGN_12B_R,
                         (RANGE_12BITS * ubUserButtonClickCount / USERBUTTON_CLICK_COUNT_MAX)
                        ) != HAL_OK)
    {
      /* Start Error */
      Error_Handler();
    }
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */

    /* Wait for DAC settling time */
    HAL_Delay(1);
    
    /* Start ADC conversion */
    /* Since sequencer is enabled in discontinuous mode, this will perform    */
    /* the conversion of the next rank in sequencer.                          */
    /* Note: For this example, conversion is triggered by software start,     */
    /*       therefore "HAL_ADC_Start()" must be called for each conversion.  */
    /*       Since DMA transfer has been initiated previously by function     */
    /*       "HAL_ADC_Start_DMA()", this function will keep DMA transfer      */
    /*       active.                                                          */
    HAL_ADC_Start(&AdcHandle);
      
    /* Wait for conversion completion before conditional check hereafter */
    HAL_ADC_PollForConversion(&AdcHandle, 1);
    
    /* Turn-on/off LED1 in function of ADC sequencer status */
    /* - Turn-off if sequencer has not yet converted all ranks */    
    /* - Turn-on if sequencer has converted all ranks */
    if (ubSequenceCompleted == RESET)
    {
      BSP_LED_Off(LED1);
    }
    else
    {
      BSP_LED_On(LED1);
      
      /* Computation of ADC conversions raw data to physical values */
      /* Note: ADC results are transferred into array "aADCxConvertedValues"  */
      /*       in the order of their rank in ADC sequencer.                   */
      uhADCChannelToDAC_mVolt    = COMPUTATION_DIGITAL_12BITS_TO_VOLTAGE(aADCxConvertedValues[0]);
      uhVrefInt_mVolt            = COMPUTATION_DIGITAL_12BITS_TO_VOLTAGE(aADCxConvertedValues[2]);
      wTemperature_DegreeCelsius = COMPUTATION_TEMPERATURE_STD_PARAMS(aADCxConvertedValues[1]);

      /* Reset variable for next loop iteration */
      ubSequenceCompleted = RESET;
    }
  }
}
コード例 #12
0
/**
  * @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_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */ 

  /* Preconfiguration before using DAC----------------------------------------*/
  DAC_Config();
  
  /* TIM2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  
  /* Time base configuration */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Period = 0xFF;          
  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);
  
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
  
  /* Configures Button GPIO and EXTI Line */
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);

  /* Infinite loop */
  while (1)
  {
    /* If the wave form is changed */
    if (WaveChange == 1)
    {  
      /* Switch the selected waves forms according the Button status */
      if (SelectedWavesForm == 1)
      {
          /* The sine wave has been selected */
          /* Sine Wave generator ---------------------------------------------*/
          DAC_DeInit(); 
          
          /* DAC channel1 Configuration */
          DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
          DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
          
          /* DMA channel3 Configuration */
          DMA_DeInit(DMA1_Channel3); 
          DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS;
          DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
          DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
          DMA_InitStructure.DMA_BufferSize = 32;
          DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
          DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
          DMA_InitStructure.DMA_Priority = DMA_Priority_High;
          DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
          DMA_Init(DMA1_Channel3, &DMA_InitStructure);

          /* Enable DMA1 Channel3 */
          DMA_Cmd(DMA1_Channel3, ENABLE);

          /* DAC Channel1 Init */
          DAC_Init(DAC_Channel_1, &DAC_InitStructure);

          /* 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 DMA for DAC Channel1 */
          DAC_DMACmd(DAC_Channel_1, ENABLE);
         
      }
          /* The Escalator wave has been selected */
       else
       {
         
          /* Escalator Wave generator -----------------------------------------*/
          DAC_DeInit();
          
          /* DAC channel1 Configuration */
          DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
          DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
          
  
          /* DMA1 channel2 configuration */
          DMA_DeInit(DMA1_Channel3);

          DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS;
          DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit;
          DMA_InitStructure.DMA_BufferSize = 6;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
          DMA_Init(DMA1_Channel3, &DMA_InitStructure);
    
          /* Enable DMA1 Channel2 */
          DMA_Cmd(DMA1_Channel3, ENABLE);
    
          /* DAC channel1 Configuration */
          DAC_Init(DAC_Channel_1, &DAC_InitStructure);

          /* 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 DMA for DAC Channel1 */
          DAC_DMACmd(DAC_Channel_1, ENABLE);
      }
      WaveChange = !WaveChange;
    }
  }
}
コード例 #13
0
ファイル: main.c プロジェクト: glocklueng/USB_STM32F4
int main(void)
{
  uint16_t i;
  uint16_t fleft=440;
  uint16_t fright=261;
uint32_t f0;
  uint32_t phL1=0,phR1=0,phL2=0,phR2=0;
  SystemInit();

  
  /* Initialize LEDs and User_Button on STM32F4-Discovery --------------------*/
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
  
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);

  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
  InitDDSPorts();
  InitDDSSPI(SPI_BaudRatePrescaler_2);
  //PLLSendFreq(freq_khz/(50*2));
  TMR2_Config(fs*mult_factor/2, RCC_Clocks.PCLK1_Frequency);
  //DDSSendFreq(freq);
  ad9951_init();
  f0=__REV(TIM2->CCR4);
  //ad9951_sendfreq(freq);
  //PLLSendCtrlWord(0x0032F0);
  EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, 70, fs);
  //SetI2SMCLK(fs);
  //Sin_Gen (Audio,fs,fleft,fright,96);
  //Sin_Gen (&Audio2[0],fs,fleft,fright,96);
  //MPX_Gen (fs,mult_factor,Audio,MPX_buf,192);
  //EVAL_AUDIO_Play(Audio,384);
  DAC_Config();
 Audio_DAC_play(DAC_buf,mult_factor*bufsize);
 TMR4_Config(DDS_buf,mult_factor*bufsize);
 TMR3_Config();
 mpx_init(mult_factor,bufsize/2);
 TMR6_Config(1000000UL, RCC_Clocks.PCLK1_Frequency*2);

 USBD_Init(&USB_OTG_dev,
            USB_OTG_FS_CORE_ID,
            &USR_desc,
            &AUDIO_cb,
            &USR_cb);
  while(1)
  {

	    if (audio_buffer_fill==1)
	      			{
		  //Sin_Gen (&Audio[0],fs,fleft,fright,48);
	    	memcpy(Audio,xbuf,192); //moved to codec routines
	    	//if (!(UserButtonPressed&2))
	    	memcpy(Audio2,Audio,192);
		    audio_buffer_fill=0;
	      			};
	      	if (audio_buffer_fill==2)
	      		  	{
	     		//Sin_Gen (&Audio[96],fs,fleft,fright,48);
	      		memcpy((uint8_t*)Audio+192,xbuf,192);// moved to codec routines
	      		//if (!(UserButtonPressed&2))
	      		memcpy((uint8_t*)Audio2+192,(uint8_t*)Audio+192,192);
	      		audio_buffer_fill=0;
	      	    	};
	      	//while (audio_buffer_fill==0);

	/*if (audio_buffer_fill==1)
	 	  	      			{memcpy(Audio,xbuf,192);audio_buffer_fill=0;
	 	  	      			};
	 	   	if (audio_buffer_fill==2)
	 	  	      	    	{memcpy((uint8_t*)Audio+192,xbuf,192);audio_buffer_fill=0;
	 	  	      	    	};

*/
//while(mpx_buff_fill==0);
	      	//DDS is synchronous with DAC
	  if (mpx_buff_fill==1)
	  {
		 // if (UserButtonPressed&2)
		 // Sin_Gen (&Audio2[0],fs,fleft,fright,48);

		  MPX_Gen (fs,mult_factor,&Audio2[0],MPX_buf,bufsize/2);
	      DAC_normalise(&MPX_buf[0],&DAC_buf[0],mult_factor*bufsize/2);
	      FM_MPX(f0,&MPX_buf[0],mult_factor*bufsize/2, &DDS_buf[0]);

	      mpx_buff_fill=0;
	  };
	  if (mpx_buff_fill==2)
	  {
		//  if (UserButtonPressed&2)
		//  Sin_Gen (&Audio2[96],fs,fleft,fright,48);

		  MPX_Gen (fs,mult_factor,&Audio2[bufsize],&MPX_buf[mult_factor*bufsize/2],bufsize/2);
	  	  DAC_normalise(&MPX_buf[mult_factor*bufsize/2],&DAC_buf[mult_factor*bufsize/2],mult_factor*bufsize/2);
	      FM_MPX(f0,&MPX_buf[mult_factor*bufsize/2],mult_factor*bufsize/2, &DDS_buf[mult_factor*bufsize/2]);
	  	  mpx_buff_fill=0;
	  };
  }

}
コード例 #14
0
ファイル: main.c プロジェクト: flodur1/redbull
/**
 * @brief  Main program.
 * @param  None
 * @retval None
 */
int main(void)
{
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);

  Redbull_Init();

  char buff[128] = { 0 };

  LCD_DisplayStringLine(LINE(10), (uint8_t*) " Analog ..............................");
  Analog_Config();
  printRight(10, "ok");

  LCD_DisplayStringLine(LINE(11), (uint8_t*) " DAC .................................");
  DAC_Config();
  printRight(11, "ok");

  f_mount(0, &fatfs);

  LCD_DisplayStringLine(LINE(12), (uint8_t*) " TEMP ................................");
  OneWireInit();
  float c = Read_Temperature();
  sprintf(buff, "%.1f", c);
  printRight(12, buff);
  printf("%s\n", buff);

  RGBLED_GPIO_Config(0xff);

  RGBLED_Update(0xff,0xff,0xff);
  Delay(200);
  RGBLED_Update(0xff,0,0);
  Delay(200);
  RGBLED_Update(0,0xff,0);
  Delay(200);
  RGBLED_Update(0,0,0xff);
  Delay(200);
  RGBLED_Update(0,0,0);

  //static char path[1024] = "0:";
  //LCD_Clear(LCD_COLOR_BLUE);
  //LCD_DisplayStringLine(0, (uint8_t*) "Loading...");
  Delay(1000);

  //int i = 10;

  LCD_Clear(LCD_COLOR_BLACK);

  LCD_SetDisplayWindow(0, 0, 239, 319);
  LCD_WriteReg(3, 0x1000);
  LCD_WriteRAM_Prepare();


  TIM_Cmd(TIM7, ENABLE);

  DMARead();

  //float h = 0, s = 1, v = 1;

  while (1)
  {
    while (DMA_GetFlagStatus(DMA1_FLAG_TE1) == SET)
    {
      printf("DMA error\n");
      Delay(100);
    }

    //uint32_t ms = millis();

    uint16_t rem = DMA_GetCurrDataCounter(DMA1_Channel1);
    uint16_t cnt = 0xffff - rem;

    if (cnt >= 80 && cnt < 512)
    {
      continue;
    }

    uint32_t buff[160] = { 0 };

    uint16_t tillend = cnt > 160 ? 160 : cnt;
    uint16_t fromstart = 160 - tillend;

    uint16_t bcnt = cnt - 160;

    // before
    SRAM_ReadBuffer((uint16_t*) &buff[0], bcnt * 4, tillend * 2);
    // after
    if (fromstart)
    {
      SRAM_ReadBuffer((uint16_t*) &buff[tillend], 0, fromstart * 2);
    }

    uint8_t prevsv = 0;

    for (uint16_t i = 0; i < 320; i++)
    {
      uint16_t val = 0;
      if (i & 1)
      {
        val = buff[i >> 1] & 0x0fff;
      }
      else
      {
        val = (buff[i >> 1] >> 16) & 0x0fff;
      }

#define MAXV (17 * 238)

      val = val > MAXV ? MAXV : val;

      uint8_t sv = val / 17;

      sv = sv > 238 ? 238 : sv;

      sv++;

      if (!prevsv)
      {
        prevsv = sv;
      }

      uint16_t pbuf[240];

      uint8_t minv = sv > prevsv ? prevsv : sv;
      uint8_t maxv = sv < prevsv ? prevsv : sv;

      for (uint8_t y = 0; y < 240; y++)
      {
        if (y >= minv && y <= maxv)
        {
          pbuf[y] = LCD_COLOR_YELLOW;
        }
        else
        {
          pbuf[y] = LCD_COLOR_BLACK;
        }
      }

      prevsv = sv;

      DMA_InitTypeDef DMA_InitStructure;

      DMA_DeInit(DMA2_Channel5);
      DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) pbuf;
      DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &LCD->LCD_RAM; // FSMC
      DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
      DMA_InitStructure.DMA_BufferSize = 240;
      DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
      DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
      DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
      DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
      DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
      DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
      DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;

      DMA_Init(DMA2_Channel5, &DMA_InitStructure);

      DMA_Cmd(DMA2_Channel5, ENABLE);

      RTC_t rtc;
      RTC_GetTime(&rtc);

      //RGBLED_Update((rtc.min & 1) ? 0 : (rtc.hour * 10), ((rtc.sec >> 2) & 1) ? 0 : (rtc.min * 4), rtc.sec * 4);

      while (!DMA_GetFlagStatus(DMA2_FLAG_TC5))
        ;

      DMA_ClearFlag(DMA2_FLAG_TC5);

      while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == RESET)
        ;

    }
コード例 #15
0
ファイル: main.c プロジェクト: svn2github/fbedit
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void) {
  __IO uint16_t i;
  __IO uint16_t *ptr;
  __IO uint16_t tmp1;
  __IO uint16_t tmp2;
  uint32_t scpcnt;
  uint32_t scpwait;
  uint32_t scptpos;
  /* RCC Configuration */
  RCC_Config();
  /* GPIO Configuration */
  GPIO_Config();
  /* TIM Configuration */
  TIM_Config();
  /* NVIC Configuration */
  NVIC_Config();
  /* DAC Configuration */
  DAC_Config();
  /* SPI Configuration */
  SPI_Config();
  /* USART Configuration */
  USART_Config(115200);
  /* Calibrate LC Meter */
  LCM_Calibrate();

  // /* Update bluetooth baudrate */
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount);
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount);
  // USART3_puts("AT\0");
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount)
  // {
    // if ((USART3->SR & USART_FLAG_RXNE) != 0)
    // {
      // STM32_CMD.BTBuff[i] = USART3->DR;
      // i++;
    // }
  // }
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount);
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount);
  // USART3_puts("AT+BAUD8\0");
  // STM32_CMD.Cmd = STM32_CMD.TickCount;
  // while (STM32_CMD.Cmd == STM32_CMD.TickCount)
  // {
    // if ((USART3->SR & USART_FLAG_RXNE) != 0)
    // {
      // STM32_CMD.BTBuff[i] = USART3->DR;
      // i++;
    // }
  // }
  // while (1)
  // {
  // }

  while (1) {
    USART3_getdata((uint8_t *)&STM32_CMD.Cmd,4);
    switch (STM32_CMD.Cmd)
    {
      case CMD_LCMCAL:
        LCM_Calibrate();
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef));
        break;
      case CMD_LCMCAP:
        GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef));
        break;
      case CMD_LCMIND:
        GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7);
        GPIO_SetBits(GPIOD, GPIO_Pin_6);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef));
        break;
      case CMD_FRQCH1:
        GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7);
        GPIO_SetBits(GPIOD, GPIO_Pin_0);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        break;
      case CMD_FRQCH2:
        GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7);
        GPIO_SetBits(GPIOD, GPIO_Pin_1);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        break;
      case CMD_FRQCH3:
        GPIO_ResetBits(GPIOD, GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7);
        GPIO_SetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        break;
      case CMD_SCPSET:
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP.ADC_Prescaler,sizeof(STM32_SCPTypeDef));
        /* Scope magnify */
        i = (STM32_CMD.STM32_SCP.ScopeMag & 0x07) << 3;
        GPIO_SetBits(GPIOE,(i ^ 0x38));
        GPIO_ResetBits(GPIOE,i);
        /* Set V-Pos */
        DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeVPos);
        /* Set Trigger level */
        DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeTriggerLevel);
        if (STM32_CMD.STM32_SCP.ADC_TripleMode) {
          /* DMA Configuration */
          DMA_TripleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize);
          /* ADC Configuration */
          ADC_TripleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler, STM32_CMD.STM32_SCP.ADC_TwoSamplingDelay);
        } else {
          /* DMA Configuration */
          DMA_SingleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize);
          /* ADC Configuration */
          ADC_SingleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler,STM32_CMD.STM32_SCP.ADC_SampleTime);
        }
        if (STM32_CMD.STM32_SCP.ScopeTrigger == 2) {
          /* Rising edge */
          TIM5->CCER &= ~0x2;
        } else {
          /* Falling edge */
          TIM5->CCER |= 0x2;
        }
        if (STM32_CMD.STM32_SCP.ScopeTrigger) {
          /* Wait for trigger */
          scpcnt = TIM5->CNT;
          scpwait = STM32_CMD.TickCount + 2;
          while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount);
        }
        /* Start ADC1 Software Conversion */
        ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART;
        i = 0;
        while (i < 30000) {
          i++;
        }
        /* Since BlueTooth is slower than the lowest sampling rate there is no need to wait */
        SendCompressedBuffer((uint32_t *)SCOPE_DATAPTR, STM32_CMD.STM32_SCP.ADC_SampleSize / 4);
        /* Done */
        ADC->CCR=0;
        ADC1->CR2=0;
        ADC2->CR2=0;
        ADC3->CR2=0;
        break;
      case CMD_SCP2SET:
        STM_EVAL_LEDToggle(LED4);
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP2.SampleRateSet,sizeof(STM32_SCP2TypeDef));
        /* Scope magnify */
        i = ((uint32_t)STM32_CMD.STM32_SCP2.Mag & 0x07) << 3;
        GPIO_SetBits(GPIOE,(i ^ 0x38));
        GPIO_ResetBits(GPIOE,i);
        /* Set V-Pos */
        DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.VPos);
        /* Set Trigger level */
        DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.TriggerLevel);
        /* Get number of samples needed */
        SampleSize = GetScopeSampleSize();
        if (STM32_CMD.STM32_SCP2.Triple) {
          /* DMA Configuration */
          DMA_TripleConfig(SampleSize);
          /* ADC Configuration */
          ADC_TripleConfig((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 4,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0xF);
        } else {
          /* DMA Configuration */
          DMA_SingleConfig(SampleSize);
          /* ADC Configuration */
          ADC_SingleConfig(((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 3) & 0x3,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0x7);
        }
        /* Trigger configuration */
        if (STM32_CMD.STM32_SCP2.Trigger == 2) {
          /* Rising edge */
          TIM5->CCER &= ~0x2;
        } else {
          /* Falling edge */
          TIM5->CCER |= 0x2;
        }
        if (STM32_CMD.STM32_SCP2.Trigger) {
          /* Wait for trigger */
          scpcnt = TIM5->CNT;
          scpwait = STM32_CMD.TickCount + 2;
          while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount);
        }
        /* Start ADC1 Software Conversion */
        ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART;
        /* Reset wavedata */
        ScopeResetWave((uint32_t *)SCOPE_WAVEPTR,SCOPE_MAXWAVESIZE);
        ScopeResetWave((uint32_t *)SCOPE_COUNTPTR,SCOPE_MAXWAVESIZE);
        /* Wait until DMA transfer complete */
        while (DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0) == RESET);
        /* Done sampling */
        ADC->CCR=0;
        ADC1->CR2=0;
        ADC2->CR2=0;
        ADC3->CR2=0;
        ScopeSetWaveData((uint32_t *)SCOPE_WAVEPTR, (uint32_t *)SCOPE_COUNTPTR, (uint16_t *)SCOPE_DATAPTR);
        scptpos =  0;
        if (STM32_CMD.STM32_SCP2.Trigger) {
          scptpos = ScopeFindTrigger((uint16_t *)SCOPE_DATAPTR);
        }
        /* Send wave data */
        SendCompressedBuffer((uint32_t *)(SCOPE_DATAPTR + scptpos * 2), (uint32_t)STM32_CMD.STM32_SCP2.PixDiv * (uint32_t)STM32_CMD.STM32_SCP2.nDiv * 2 / 4);
        STM_EVAL_LEDToggle(LED4);
        break;
      case CMD_HSCSET:
        GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7);
        GPIO_SetBits(GPIOD, GPIO_Pin_0);
        USART3_getdata((uint8_t *)&STM32_CMD.STM32_HSC.HSCSet,sizeof(STM32_HSCTypeDef));
        TIM4->ARR = STM32_CMD.STM32_HSC.HSCSet;
        TIM4->CCR2 = (STM32_CMD.STM32_HSC.HSCSet+1) / 2;
        TIM4->PSC = STM32_CMD.STM32_HSC.HSCDiv;
        USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef));
        break;
      case CMD_DDSSET:
        USART3_getdata((uint8_t *)&STM32_CMD.STM32_DDS.DDS_Cmd,sizeof(STM32_DDSTypeDef));
        if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_PHASESET) {
          SPISendData(DDS_PHASESET);
          SPISendData32(STM32_CMD.STM32_DDS.DDS__PhaseAdd);
        }
        else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_WAVESET) {
          SPISendData(DDS_WAVESET);
          SPISendData(STM32_CMD.STM32_DDS.DDS_Wave);
          SPISendData(STM32_CMD.STM32_DDS.DDS_Amplitude);
          SPISendData(STM32_CMD.STM32_DDS.DDS_DCOffset);
        } else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_SWEEPSET) {
          SPISendData(DDS_SWEEPSET);
          SPISendData(STM32_CMD.STM32_DDS.SWEEP_Mode);
          SPISendData(STM32_CMD.STM32_DDS.SWEEP_Time);
          SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Step);
          SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Min);
          SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Max);
        }
        break;
      case CMD_LGASET:
        USART3_getdata((uint8_t *)&STM32_CMD.STM32_LGA.DataBlocks,sizeof(STM32_LGATypeDef));
        /* Set the Prescaler value */
        TIM8->PSC = STM32_CMD.STM32_LGA.LGASampleRateDiv;
        /* Set the Autoreload value */
        TIM8->ARR = STM32_CMD.STM32_LGA.LGASampleRate;
        TIM8->CNT =  STM32_CMD.STM32_LGA.LGASampleRate-1;
        DMA_LGAConfig();
        TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE);
        /* DMA2_Stream1 enable */
        DMA_Cmd(DMA2_Stream1, ENABLE);
        /* Enable timer */
        TIM8->CR1 |= TIM_CR1_CEN;
        while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_HTIF1) == RESET);
        /* Half done */
        USART3_putdata((uint8_t *)LGA_DATAPTR, STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2);
        while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_TCIF1) == RESET);
        /* Done */
        USART3_putdata((uint8_t *)(LGA_DATAPTR + STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2), STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2);
        TIM_Cmd(TIM8, DISABLE);
        DMA_DeInit(DMA2_Stream1);
        break;
      case CMD_WAVEUPLOAD:
        USART3_getdata((uint8_t *)WAVE_DATAPTR,4096);
        SPISendData(DDS_WAVEUPLOAD);
        ptr = (uint16_t *)WAVE_DATAPTR;
        i = 2048;
        while (i--) {
          SPISendData(*ptr);
          ptr++;
        }
        STM_EVAL_LEDToggle(LED4);
        break;
    }
コード例 #16
0
ファイル: main.c プロジェクト: taegwonkim/STM32Cube
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
   
  /* Configure the system clock to 72 MHz */
  SystemClock_Config();
  
  /*## Configuration of peripherals ##########################################*/
  /* Initialize LEDs on board */
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  
  /* Initialize the User Button in Interrupt mode */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
  
  /* Configure external lines 9 to 5 in interrupt mode */
  EXTILine9_5_Config();  

  /* Configuration of ADC peripheral */
  ADC_Config();
  
  /* Run the ADC calibration in differential mode */  
  if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_DIFFERENTIAL_ENDED) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
  
  /* Configuration of DAC peripheral */
  DAC_Config();
  

  /*## Enable peripherals ####################################################*/
  
  /* Set DAC Channel data register: channel corresponding to ADC channel ADCx_CHANNEL_DIFF_HIGH */
  /* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V */
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNEL_DIFF_HIGH, DAC_ALIGN_12B_R, RANGE_12BITS/2) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }

  /* Set DAC Channel data register: channel corresponding to ADC channel ADCx_CHANNEL_DIFF_LOW */
      /* Set DAC output to minimum of full range (0 <=> ground 0V) */
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNEL_DIFF_LOW, DAC_ALIGN_12B_R, 0) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }
  
  /* Enable DAC Channel: channel corresponding to ADC channel ADCx_CHANNEL_DIFF_HIGH */
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNEL_DIFF_HIGH) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  
  /* Enable DAC Channel: channel corresponding to ADC channel ADCx_CHANNEL_DIFF_LOW */
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNEL_DIFF_LOW) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  
  /*## Start ADC conversions #################################################*/
  
  /* Start ADC conversion on regular group with interruption */
  if (HAL_ADC_Start_IT(&AdcHandle) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
    
  
  /* Infinite loop */
  while (1)
  {
    /* Set DAC voltage on channel corresponding to ADC channel ADCx_CHANNEL_DIFF_LOW */
    /* in function of user button clicks count.                               */
    /* Set DAC output succesively to:                                         */
    /*  - minimum of full range (0 <=> ground 0V)                             */
    /*  - 1/4 of full range (4095 <=> Vdda=3.3V): 1023 <=> 0.825V             */
    /*  - 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
    /*  - 3/4 of full range (4095 <=> Vdda=3.3V): 3071 <=> 2.475V             */
    /*  - maximum of full range (4095 <=> Vdda=3.3V)                          */
    if (HAL_DAC_SetValue(&DacHandle,
	                 DACx_CHANNEL_TO_ADCx_CHANNEL_DIFF_LOW, DAC_ALIGN_12B_R,
	                 (RANGE_12BITS * uwUserButtonClickCount / USERBUTTON_CLICK_COUNT_MAX)
	                 ) != HAL_OK)
    {
      /* Start Error */
      Error_Handler();
    }
    
    /* ADC Differential conversion result calculation:                        */
    /*  - An offset of half of full range is added to keep the full dynamic   */
    /*    range of differential voltage.                                      */
    /*  - Channels voltage is divided by 2, resolution is lowered of 1 bit.   */
    /*  => Diff conversion result = mid-range + (channel_high-channel_low)/2  */

    /* Turn-on/off LED2 in function of ADC differential conversion result */
    /*  - Turn-on LED2 if differential voltage is positive */
    /*  - Turn-off LED2 if differential voltage is negative */
    if (uhADCxConvertedValue > RANGE_12BITS/2)
    {
      BSP_LED_On(LED2);
    }
    else
    {
      BSP_LED_Off(LED2);
    }
  }
}
コード例 #17
0
ファイル: COMP_LDR_Example.c プロジェクト: clarenceliu/Mplib
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void COMP_LDR_Example(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f30x.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */

  uint8_t ldrlevel = 11, ldrlevelp = 0, daclevel = 0;
  uint16_t tmp = 0;

  /* Initialize the TFT-LCD */
  STM32303C_LCD_Init();
  
  /* Clear the LCD */ 
  LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Displays Light dependent resistor (LDR) message on line 0 */
  LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);
  
  /* COMP Configuration */
  COMP_Config();
 
  /* DAC channel 1 Configuration */
  DAC_Config();
  
  
  /* Infinite loop */
  while (1)
  {
    for(daclevel = 0; daclevel < 11; daclevel++)
    {
      /* Set DAC Channel1 DHR register */
      DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(daclevel * 150));

      Delay((0xFFFF));
      /* Check on the Comp output level*/
      if (COMP_GetOutputLevel(COMP_Selection_COMP7) == COMP_OutputLevel_High)
      {
        ldrlevel--;
      }
    }

    switch(ldrlevel)
    {
    case 1:
      /* Displays MESSAGE on line 7 */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 0      ");
      break;
      
    case 2:
      /* Displays MESSAGE on line 7 */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 1      ");
      break;
      
    case 3:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 2      ");
      break;
      
    case 4:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 3      ");
      break;
      
    case 5:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 4      ");
      break;
      
    case 6:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 5      ");
      break;
      
    case 7:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 6      ");
      break;
      
    case 8:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 7      ");
      break;
      
    case 9:
      /* Displays MESSAGE on line 7 */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 8      ");
      break;
      
    case 10:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 9      ");
      break;
      
    case 11:
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 10     ");
      break;
      
    default :
      /* Displays MESSAGE on line 7  */
      LCD_DisplayStringLine(LINE(7), (uint8_t *)"       Level 0      ");
      ldrlevel = 1;
      break;
    }
    
    if(ldrlevelp != ldrlevel)
    {
      /* Set the LCD Text Color */
      LCD_SetTextColor(LCD_COLOR_WHITE);
      
      /* Displays a rectangle on the LCD */
      LCD_DrawRect(199, 311, 22, 302 );
      
      /* Set the LCD Back Color */
      LCD_SetBackColor(LCD_COLOR_BLACK);
      LCD_SetTextColor(LCD_COLOR_BLACK);
      LCD_DrawFullRect(200, 310, 300, 20);
      
      /* Set the LCD Text Color */
      LCD_SetTextColor(LCD_COLOR_YELLOW);
      
      /* Set the LCD Back Color */
      LCD_SetBackColor(LCD_COLOR_YELLOW);
      
      /* Displays a full rectangle */
      tmp = 30 * (ldrlevel-1);
      if (tmp ==0) tmp = 5;
      LCD_DrawFullRect(200, 310, tmp , 20);
    }
    
    ldrlevelp = ldrlevel;
    ldrlevel = 11;
    
    /* Set the LCD Back Color */
    LCD_SetBackColor(LCD_COLOR_BLACK);
  }

}
コード例 #18
0
int main( void )
{
	uart_config_t uart0;
	gpio_config_t key1;
	i2c_config_t i2c0;
	i2c_command_t command;
	spi_config_t spi0;
	spi_command_t spi_cmd;
	adc_config_t adc5;
	dac_config_t dac0;


	error_code_t error;
	uart0.baudrate = B115200;
	uart0.block_type = NON_BLOCKING;
	uart0.buffer = 0;
	uart0.irqhandler = 0;
	uart0.parity = UART_PARITY_NONE;
	uart0.uart_port = COM0;
	uart0.wordlen = UART_WORDLEN8;
	uart0.stopbit = STOP_BIT_1_BIT;


	key1.Direction = INPUT;
	key1.Initial_Value = LO;
	key1.Interrupt_Type = INTERRUPT_ENABLED_BOTH;
	key1.Pin = PIN11;
	key1.Port= PORT2;
	key1.Pin_Mode = PULLUP_PULLDOWN_DISABLED;
	key1.Pin_Mode_OD = PIN_MODE_OPEN_DRAIN_NORMAL;
	key1.Pin_Typedef = 0;
	key1.irqhandler = 0;

	i2c0.i2c_port = I2C0;
	i2c0.i2c_mode = MASTER;
	i2c0.datarate = STANDARD;
	i2c0.buffer = 0;
	i2c0.irqhandler = 0;

	spi0.bits = SPI_8_BITS;
	spi0.buffer = 0;
	spi0.clk_phase = SPI_PHASE_INPHASE;
	spi0.clk_polarity = SPI_CLK_RISING;
	spi0.freq = 5e6;
	spi0.irqhandler = 0;
	spi0.lsbf = MSB_FIRST;
	spi0.mode = SPI_MASTER;
	spi0.port = SPI0;
	spi0.dummyData = 0x00;

	adc5.channel = ADC_CHANNEL5;
	adc5.rate = 200e3;
	adc5.trigger_mode = BURST;
	adc5.irqhandler = 0;

	dac0.sampling_rate = 32e3;


	if ( (error = GPIO_Config(&key1)) != NO_ERROR ) {
		while (1);
	}

	signal_level_t trigger_mode;
	GPIO_GetLevel(&key1, &trigger_mode);
	if (trigger_mode == HI) {
		adc5.trigger_mode = BURST;
	} else {
		adc5.trigger_mode = MANUAL;
	}

	if ( (error = UART_Config(&uart0)) != NO_ERROR) {
		while (1);
	}
	SetDebug_Port(COM0);

	if ( (error = I2C_Config(&i2c0)) != NO_ERROR ) {
		while (1);
	}

	if ( (error = SPI_Config(&spi0)) != NO_ERROR ) {
		while (1);
	}

	if ( (error = ADC_Config(&adc5)) != NO_ERROR ) {
		while (1);
	}

	if ( (error = DAC_Config(&dac0)) != NO_ERROR ) {
		while (1);
	}
	uc_printf ("HardWare Initialized\n\r");

	/* READ ID USING READ-ID */
	//Write Register.
	spi_cmd.writeReg = 0x90;
	spi_cmd.writeRegValid = 1;
	uint16_t write_data[] = {0x00, 0x00, 0x00  };
	spi_cmd.writeBuffer = write_data;
	spi_cmd.writeDataSize = sizeof(write_data);

	//Read Register.
	uint16_t read_data[4];
	spi_cmd.readReg = 0x00;
	spi_cmd.readRegValid = 0;
	spi_cmd.readBuffer = read_data;
	spi_cmd.readDataSize = sizeof(read_data);

	spi_cmd.operation = SPI_WRITE | SPI_READ; //back to back.

	error = SPI_Write(SPI0, &spi_cmd);
	if (error != NO_ERROR) {
		while (1);
	}
	uc_printf ("SPI Flash Device ID(READ-ID) %d %d %d %d\n\r", read_data[0], read_data[1], read_data[2], read_data[3]);

	/* READ ID USING JEDEC READ */
	uint16_t read_data_jedec[4];
	spi_cmd.readReg = 0x9F;
	spi_cmd.readRegValid = 1;
	spi_cmd.readBuffer = read_data_jedec;
	spi_cmd.readDataSize = sizeof(read_data_jedec);
	error = SPI_Read(SPI0, &spi_cmd);
	if (error != NO_ERROR) {
		while (1);
	}
	uc_printf ("SPI Flash Device ID(Jedec) %d %d %d %d\n\r", read_data_jedec[0], read_data_jedec[1], read_data_jedec[2], read_data_jedec[3]);

	int size = 18;
	char fw_version[18] = "PowerAvrVersion5.5";
	char fw_version_read[18] = { 0 };
	command.address = 0x50;
	command.data = fw_version;
	command.operation = WRITE;
	command.reg = 0x00;
	command.size = 1; //11;

	int i, j, k;
	for (j=0; j<1; j++) {
		for (i=0; i<size;i++) {
			//UART_PutChars(COM0,"Writing", 9);
			command.reg = 0x00 + i;
			command.size = 1;
			command.data = &(fw_version[i]);
			error = I2C_Write(I2C0, &command);
			if (error != NO_ERROR) {
				break;
			}
			//This is to cause a delay after write. This should be replaced with the wait in the I2C_Write Operation.
			for (k=0; k<16; k++) {
				uc_printf(".");
				uc_printf("\b");
			}
		}
		if (error != NO_ERROR) {
			while (1);
		}
	}
	uc_printf("I2C Write Done\n\r");

	command.operation = READ;
	command.data = fw_version_read;
	command.size = 1 ;
	for (j=0; j<1; j++) {
		for (i=0; i<size;i++) {
				fw_version_read[i] = 'X';
		}
		for (i=0; i<size;i++) {
			command.reg = 0x00 + i;
			command.size = 1;
			command.data = &(fw_version_read[i]);
			error = I2C_Read(I2C0, &command);
			if (error != NO_ERROR) {
				break;
			}
		}
		if (error != NO_ERROR) {
			while (1);
		}
	}

	uc_printf("I2C Read Done\n\r");
	for (i=0; i<size; i++) {
		uc_printf("%c",fw_version_read[i]);
	}
	uc_printf("\n\r");
	pin_interrupt_type_t key1_status;
	float vol=0;
	int timeout = 0;
	while (1) {
		error = GPIO_GetIRQ(&key1, &key1_status);
		if (error != NO_ERROR) {
			while (1);
		}
		if (key1_status == INTERRUPT_ENABLED_FALLING) {
			uc_printf ("Falling Edge\n\r");
			error = GPIO_ClrIRQ(&key1);
			if (error != NO_ERROR) {
				while (1);
			}
			if ( (error = ADC_Read(&adc5)) != NO_ERROR ) {
				while (1);
			}
		} else if (key1_status == INTERRUPT_ENABLED_RISING) {
			uc_printf ("Rising Edge\n\r");
			error = GPIO_ClrIRQ(&key1);
			if (error != NO_ERROR) {
				while (1);
			}
		}
		//#if (DISPLAY_ADC == 1)
				//if (adc5.trigger_mode == BURST) {
				if (timeout == 20000) {
					ADC_Read(&adc5);
					timeout = 0;
				} else {
					timeout++;
				}
				//}
				if (adc5.done) {
					vol =0 ;
					int i=0;
					int result = adc5.result;
					for (i=0; i<4096; i+=256) {
						vol++;
						if (result >= i && result <= (i + 256) ) {

							break;
						}
					}

					/*
					for (i=0; i<adc5.result/33; i++) {
						uc_printf("*");
					}
					uc_printf("\r");
					*/
					//uc_printf ("%d\n\r", adc5.result);
					adc5.done = 0;
				}
		//#endif
		//#if (PLAY_PIANO == 1)
		if (vol == 0) {
			vol = 1;
		} else if (vol > 1) {
			//vol = vol ; //1 + (vol - 1)*0.2;
		}
		Play_Piano(&dac0, vol );
		//#endif
	}

	#if (UART_TEST == 1)
	char rx_data;
	char tx_data[] = "This is x nnnnn\n\r";
	while (1) {
		//if ( UART_GetChar(COM0, &rx_data) != FIFO_EMPTY ) {
			tx_data[8] = 'a';
			//UART_PutChar(COM0, rx_data);
			UART_PutChars(COM0, tx_data, 17 );
			int i;
			tx_data[8] = 'b';
			for (i=0; i<17; i++) {
				UART_PutChar(COM0, tx_data[i]);
			}
		//}
	}
	#endif
	return 0;
}
コード例 #19
0
ファイル: main.c プロジェクト: NjordCZ/STM32Cube_FW_F1
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F1xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 24 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  
  /* Initialize LEDs on board */
  BSP_LED_Init(LED4);
  BSP_LED_Init(LED3);
  
  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
  
  /* Configure the ADC peripheral */
  ADC_Config();
  
  /* Run the ADC calibration */  
  if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }

#if defined(ADC_TRIGGER_FROM_TIMER)
  /* Configure the TIM peripheral */
  TIM_Config();
#endif /* ADC_TRIGGER_FROM_TIMER */
#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
  /* Configure the DAC peripheral */
  DAC_Config();
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */

  /*## Enable peripherals ####################################################*/
#if defined(ADC_TRIGGER_FROM_TIMER)
  /* Timer enable */
  if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)
  {
    /* Counter Enable Error */
    Error_Handler();
  }
#endif /* ADC_TRIGGER_FROM_TIMER */

#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
  /* Set DAC Channel data register: channel corresponding to ADC channel CHANNELa */
  /* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, RANGE_12BITS/2) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }
  
  /* Enable DAC Channel: channel corresponding to ADC channel CHANNELa */
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */

  /*## Start ADC conversions #################################################*/
  
  /* Start ADC conversion on regular group with transfer by DMA */
  if (HAL_ADC_Start_DMA(&AdcHandle,
                        (uint32_t *)aADCxConvertedValues,
                        ADCCONVERTEDVALUES_BUFFER_SIZE
                       ) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  
  
  /* Infinite loop */
  while (1)
  {

    /* Wait for event on push button to perform following actions */
    while ((ubUserButtonClickEvent) == RESET)
    {
    }
    /* Reset variable for next loop iteration */
    ubUserButtonClickEvent = RESET;
    
    /* Start ADC conversion on injected group */
    if (HAL_ADCEx_InjectedStart_IT(&AdcHandle) != HAL_OK)
    {
      /* Start Conversation Error */
      Error_Handler();
    }
    

#if defined(WAVEFORM_VOLTAGE_GENERATION_FOR_TEST)
    /* Set DAC voltage on channel corresponding to ADCx_CHANNELa              */
    /* in function of user button clicks count.                               */
    /* Set DAC output successively to:                                        */
    /*  - minimum of full range (0 <=> ground 0V)                             */
    /*  - 1/4 of full range (4095 <=> Vdda=3.3V): 1023 <=> 0.825V             */
    /*  - 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
    /*  - 3/4 of full range (4095 <=> Vdda=3.3V): 3071 <=> 2.475V             */
    /*  - maximum of full range (4095 <=> Vdda=3.3V)                          */
    if (HAL_DAC_SetValue(&DacHandle,
                         DACx_CHANNEL_TO_ADCx_CHANNELa,
                         DAC_ALIGN_12B_R,
                         (RANGE_12BITS * ubUserButtonClickCount / USERBUTTON_CLICK_COUNT_MAX)
                        ) != HAL_OK)
    {
      /* Start Error */
      Error_Handler();
    }
#endif /* WAVEFORM_VOLTAGE_GENERATION_FOR_TEST */

    /* Wait for acquisition time of ADC samples on regular and injected       */
    /* groups:                                                                */
    /* wait time to let 1/2 buffer of regular group to be filled (in ms)      */
    HAL_Delay(16);
    
    /* Turn-on/off LED3 in function of ADC conversion result */
    /* - Turned-off if voltage measured by injected group is below voltage    */
    /*   measured by regular group (average of results table)                 */
    /* - Turned-off if voltage measured by injected group is above voltage    */
    /*   measured by regular group (average of results table)                 */
    
    /* Variables of conversions results are updated into ADC conversions      */
    /* interrupt callback.                                                    */
    if (uhADCxConvertedValue_Injected < *puhADCxConvertedValue_Regular_Avg)
    {
      BSP_LED_Off(LED3);
    }
    else
    {
      BSP_LED_On(LED3);
    }
  
    /* For information: ADC conversion results are stored into array          */
    /* "aADCxConvertedValues" (for debug: check into watch window)            */
    
  }
}
コード例 #20
0
ファイル: main.c プロジェクト: PaxInstruments/STM32CubeF3
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 64 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  /* Initialize LED on board */
  BSP_LED_Init(LED2);

  /* Configure User push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  /* Configure the ADC peripheral */
  ADC_Config();
  
  /* Run the ADC calibration in single-ended mode */  
  if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }

  /* Configure the TIM peripheral */
  TIM_Config();
  
  /* Configure the DAC peripheral */
  DAC_Config();
  

  /*## Enable peripherals ####################################################*/

  /* Timer counter enable */
  if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)
  {
    /* Counter Enable Error */
    Error_Handler();
  }
  
  /* Set DAC Channel data register: channel corresponding to ADC channel CHANNELa */
  /* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa, DAC_ALIGN_12B_R, RANGE_12BITS/2) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }
  
  /* Enable DAC Channel: channel corresponding to ADC channel CHANNELa */
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
    
  /*## Start ADC conversions #################################################*/
  
  /* Start ADC conversion on regular group with transfer by DMA */
  if (HAL_ADC_Start_DMA(&AdcHandle,
                        (uint32_t *)aADCxConvertedValues,
                        ADCCONVERTEDVALUES_BUFFER_SIZE
                       ) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  
  
  /* Infinite loop */
  while (1)
  {
    /* Turn-on/off LED2 in function of ADC conversion result */
    /*  - Turn-off if voltage is into AWD window */ 
    /*  - Turn-on if voltage is out of AWD window */

    /* Variable of analog watchdog status is set into analog watchdog         */
    /* interrupt callback                                                     */
    if (ubAnalogWatchdogStatus == RESET)
    {
      BSP_LED_Off(LED2);
    }
    else
    {
      BSP_LED_On(LED2);
      
      /* Reset analog watchdog status for next loop iteration */
      ubAnalogWatchdogStatus = RESET;
    }
  
    /* For information: ADC conversion results are stored into array          */
    /* "aADCxConvertedValues" (for debug: check into watch window)            */
  
  
    /* Wait for event on push button to perform following actions */
    while ((ubUserButtonClickEvent) == RESET)
    {
      __NOP();
    }
    /* Reset variable for next loop iteration */
    ubUserButtonClickEvent = RESET;

    /* Set DAC voltage on channel corresponding to ADCx_CHANNELa              */
    /* in function of user button clicks count.                               */
    /* Set DAC output successively to:                                        */
    /*  - minimum of full range (0 <=> ground 0V)                             */
    /*  - 1/4 of full range (4095 <=> Vdda=3.3V): 1023 <=> 0.825V             */
    /*  - 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V              */
    /*  - 3/4 of full range (4095 <=> Vdda=3.3V): 3071 <=> 2.475V             */
    /*  - maximum of full range (4095 <=> Vdda=3.3V)                          */
    if (HAL_DAC_SetValue(&DacHandle,
                         DACx_CHANNEL_TO_ADCx_CHANNELa,
                         DAC_ALIGN_12B_R,
                         (RANGE_12BITS * ubUserButtonClickCount / USERBUTTON_CLICK_COUNT_MAX)
                        ) != HAL_OK)
    {
      /* Start Error */
      Error_Handler();
    }
  }
}