硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2001|回复: 0
收起左侧

[运放/比较器] STM32H7运放跟随器模式实现

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
发表于 2018-9-19 02:33:10 | 显示全部楼层 |阅读模式
测试的例子:OPAMP_Follower

运放工作在跟随器模式,正向输入端接的是DAC输出的1.65V电平,然后将运放的输出接到比较器的反向输入端(对应PC4引脚),而比较器的正向输入端PB2引脚可接入比较电压,输出端是PA8引脚。

888.png


备份测试代码:

  1. /* Private typedef -----------------------------------------------------------*/
  2. /* Private define ------------------------------------------------------------*/
  3. /* Private macro -------------------------------------------------------------*/
  4. /* Private variables ---------------------------------------------------------*/
  5. COMP_HandleTypeDef   CompHandle;
  6. OPAMP_HandleTypeDef  OpampHandle;
  7. DAC_HandleTypeDef    DacHandle;

  8. __IO uint32_t UserButtonStatus = 0;  /* set to 1 after Wkup/Tamper push-button interrupt  */
  9. static DAC_ChannelConfTypeDef sConfig;
  10. /* Private function prototypes -----------------------------------------------*/
  11. void COMP_Config(void);
  12. void DAC_Config(void);
  13. void OPAMP_Config_Power(uint32_t powermode);
  14. static void CPU_CACHE_Enable(void);
  15. static void SystemClock_Config(void);
  16. void Error_Handler(void);

  17. /* Private functions ---------------------------------------------------------*/

  18. /**
  19.   * @brief  Main program.
  20.   * @param  None
  21.   * @retval None
  22.   */
  23. int main(void)
  24. {
  25.   uint32_t examplestate = 0;
  26.    
  27.   /* Enable the CPU Cache */
  28.   CPU_CACHE_Enable();

  29.   /* STM32H7xx HAL library initialization:
  30.        - Systick timer is configured by default as source of time base, but user
  31.          can eventually implement his proper time base source (a general purpose
  32.          timer for example or other time source), keeping in mind that Time base
  33.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  34.          handled in milliseconds basis.
  35.        - Set NVIC Group Priority to 4
  36.        - Low Level Initialization
  37.      */
  38.   HAL_Init();
  39.   
  40.   /* Configure the system clock  */
  41.   SystemClock_Config();

  42.   /* Configure LEDs */
  43.   BSP_LED_Init(LED1);
  44.   BSP_LED_Init(LED2);
  45.   BSP_LED_Init(LED3);

  46.   /* Initialize the Wkup/Tamper push-button.
  47.      It is used for changing the gain */
  48.   BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  49.   /* Configure the DAC to set input voltage of OPAMP */
  50.   DAC_Config();
  51.        
  52.   /* Configure the COMP connected to the output of OPAMP */
  53.   COMP_Config();

  54.   /* Configure the OPAMP1 in Follower mode */

  55.   while (1)
  56.   {
  57.     /* Test if user an action occured on the Wkup/Tamper push-button */
  58.     if (UserButtonStatus == 1)
  59.     {
  60.       UserButtonStatus = 0;
  61.       /* Change the mode */
  62.       if (examplestate == 0)
  63.       {
  64.         /* Turn LED1 on */
  65.         BSP_LED_On(LED1);
  66.         /* Turn LED2 off */
  67.         BSP_LED_Off(LED2);
  68.         /* normal mode */
  69.         OPAMP_Config_Power(OPAMP_POWERMODE_NORMAL);
  70.         examplestate= 1;
  71.       }
  72.       else
  73.       {
  74.         /* Turn LED2 on */
  75.         BSP_LED_On(LED2);
  76.         /* Turn LED1 off */
  77.         BSP_LED_Off(LED1);
  78.         /* High speed mode */
  79.         OPAMP_Config_Power(OPAMP_POWERMODE_HIGHSPEED);
  80.         examplestate= 0;
  81.       }
  82.     }
  83.   }
  84. }

  85. /**
  86.   * @brief  System Clock Configuration
  87.   *         The system Clock is configured as follow :
  88.   *            System Clock source            = PLL (HSE BYPASS)
  89.   *            SYSCLK(Hz)                     = 400000000 (CPU Clock)
  90.   *            HCLK(Hz)                       = 200000000 (AXI and AHBs Clock)
  91.   *            AHB Prescaler                  = 2
  92.   *            D1 APB3 Prescaler              = 2 (APB3 Clock  100MHz)
  93.   *            D2 APB1 Prescaler              = 2 (APB1 Clock  100MHz)
  94.   *            D2 APB2 Prescaler              = 2 (APB2 Clock  100MHz)
  95.   *            D3 APB4 Prescaler              = 2 (APB4 Clock  100MHz)
  96.   *            HSE Frequency(Hz)              = 8000000
  97.   *            PLL_M                          = 4
  98.   *            PLL_N                          = 400
  99.   *            PLL_P                          = 2
  100.   *            PLL_Q                          = 4
  101.   *            PLL_R                          = 2
  102.   *            VDD(V)                         = 3.3
  103.   *            Flash Latency(WS)              = 4
  104.   * @param  None
  105.   * @retval None
  106.   */
  107. static void SystemClock_Config(void)
  108. {
  109.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  110.   RCC_OscInitTypeDef RCC_OscInitStruct;
  111.   HAL_StatusTypeDef ret = HAL_OK;
  112.   
  113.   /*!< Supply configuration update enable */
  114.   MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);

  115.   /* The voltage scaling allows optimizing the power consumption when the device is
  116.      clocked below the maximum system frequency, to update the voltage scaling value
  117.      regarding system frequency refer to product datasheet.  */
  118.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  119.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  120.   
  121.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  123.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  124.   RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  125.   RCC_OscInitStruct.CSIState = RCC_CSI_OFF;
  126.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  128.   RCC_OscInitStruct.PLL.PLLM = 4;
  129.   RCC_OscInitStruct.PLL.PLLN = 400;
  130.   RCC_OscInitStruct.PLL.PLLP = 2;
  131.   RCC_OscInitStruct.PLL.PLLR = 2;
  132.   RCC_OscInitStruct.PLL.PLLQ = 4;

  133.   RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  134.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  135.   ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  136.   if(ret != HAL_OK)
  137.   {
  138.     Error_Handler();
  139.   }
  140.   
  141. /* Select PLL as system clock source and configure  bus clocks dividers */
  142.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \
  143.                                  RCC_CLOCKTYPE_PCLK2  | RCC_CLOCKTYPE_D3PCLK1);

  144.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  146.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  147.   RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;  
  148.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  149.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  150.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  151.   ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
  152.   if(ret != HAL_OK)
  153.   {
  154.     Error_Handler();
  155.   }
  156. }


  157. /**
  158.   * @brief  This function is executed in case of error occurrence.
  159.   * @param  None
  160.   * @retval None
  161.   */
  162. void Error_Handler(void)
  163. {
  164.   /* Turn LED3 on */
  165.   BSP_LED_On(LED3);

  166.   while (1)
  167.   {
  168.   }
  169. }

  170. void COMP_Config(void)
  171. {
  172.        
  173.   /* Configure the COMP peripheral instance */
  174.   CompHandle.Instance = COMP1;
  175.        
  176.   /*##-1- Deinitialize the COMP peripheral ######################################*/
  177.   if (HAL_COMP_DeInit(&CompHandle) != HAL_OK)
  178.   {
  179.     /* Initialization Error */
  180.     Error_Handler();
  181.   }
  182.         /*##-2- COMP channel Configuration ##########################################*/
  183.         CompHandle.Init.InvertingInput    = COMP_INPUT_MINUS_IO2;
  184.   CompHandle.Init.NonInvertingInput = COMP_INPUT_PLUS_IO2;  
  185.   CompHandle.Init.OutputPol         = COMP_OUTPUTPOL_NONINVERTED;
  186.   CompHandle.Init.Hysteresis        = COMP_HYSTERESIS_LOW;
  187.   CompHandle.Init.Mode              = COMP_POWERMODE_HIGHSPEED;
  188.   CompHandle.Init.WindowMode        = COMP_WINDOWMODE_DISABLE;
  189.   CompHandle.Init.BlankingSrce      = COMP_BLANKINGSRC_NONE;
  190.   CompHandle.Init.TriggerMode       = COMP_TRIGGERMODE_NONE;
  191.        
  192.   /*##-3- Initialize the COMP peripheral ######################################*/
  193.   if (HAL_COMP_Init(&CompHandle) != HAL_OK)
  194.   {
  195.     /* Initialization Error */
  196.     Error_Handler();
  197.   }
  198.        
  199.   /*##-4- Start the COMP peripheral ######################################*/
  200.   if (HAL_COMP_Start(&CompHandle) != HAL_OK)
  201.   {
  202.     /* Initialization Error */
  203.     Error_Handler();
  204.   }        
  205. }

  206. void DAC_Config(void)
  207. {
  208.   /* Configure the DAC peripheral instance */
  209.   DacHandle.Instance = DAC1;

  210.   /*##-1- Initialize the DAC peripheral ######################################*/
  211.   if (HAL_DAC_Init(&DacHandle) != HAL_OK)
  212.   {
  213.     /* Initialization Error */
  214.     Error_Handler();
  215.   }

  216.   /*##-2- DAC channel Configuration ##########################################*/
  217.   /* Select normal power mode */
  218.   sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  219.   /* Select trigger none */
  220.   sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  221.   /* Select DAC channel output buffer disabled */
  222.   sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
  223.   /* Connect DAC output to on chip peripheral */
  224.   sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  225.   /* Select factory trimming */
  226.   sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  227.        
  228.   if(HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  229.   {
  230.     /* Channel configuration Error */
  231.     Error_Handler();
  232.   }
  233.   /* Set DAC output voltage */
  234.         HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_8B_R,0x7F);
  235.        
  236.         /*##-3- Enable DAC Channel 1 ##########################################*/
  237.   if(HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1) != HAL_OK)
  238.   {
  239.    
  240.     Error_Handler();
  241.   }
  242. }
  243. /**            
  244.   * @brief  OPAMP Configuration
  245.   * @param  None
  246.   * @retval None
  247.   */
  248. void OPAMP_Config_Power(uint32_t powermode)
  249. {
  250.   /* Set OPAMP instance */
  251.   OpampHandle.Instance = OPAMP1;

  252.   /*##-1- Deinit  OPAMP    ##################################################*/
  253.   HAL_OPAMP_DeInit(&OpampHandle);

  254.   /*##-2- Configure OPAMP    ##################################################*/
  255.   
  256.   /* Select power mode */  
  257.   if (powermode == OPAMP_POWERMODE_NORMAL)
  258.   {
  259.     OpampHandle.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
  260.   }
  261.   else
  262.   {
  263.     OpampHandle.Init.PowerMode = OPAMP_POWERMODE_HIGHSPEED;
  264.   }

  265.   /* Select FOLLOWER Mode */
  266.   OpampHandle.Init.Mode = OPAMP_FOLLOWER_MODE;  

  267.   /* Select VPx as input for OPAMP */
  268.   OpampHandle.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_DAC_CH;
  269.    
  270.   /* Select the factory trimming */
  271.   OpampHandle.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;

  272.   /* Init */
  273.   if(HAL_OK != HAL_OPAMP_Init(&OpampHandle))
  274.   {
  275.     Error_Handler();
  276.   }

  277.   /*##-3- Start OPAMP    #####################################################*/
  278.   /* Enable OPAMP */
  279.   if(HAL_OK != HAL_OPAMP_Start(&OpampHandle))
  280.   {
  281.     Error_Handler();
  282.   }  
  283. }

  284. /**
  285.   * @brief  EXTI line detection callbacks.
  286.   * @param  GPIO_Pin: Specifies the pins connected EXTI line
  287.   * @retval None
  288.   */
  289. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  290. {
  291.   if (GPIO_Pin == USER_BUTTON_PIN)
  292.   {
  293.     UserButtonStatus = 1;
  294.   }
  295. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-5 02:24 , Processed in 0.152885 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表