请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32G491试用】(3)OPAMP

[复制链接]
wenyangzeng 提问时间:2021-3-28 21:37 / 未解决
【STM32G491试用】(3)OPAMP

    STM32G491内置4通道可编程增益放大器,本次使用OPAMP3测试性能。DAC1产生800mV正弦波,从PA4输出,连接到OPAMP3同相输入端PB0,用一条导线连接CN7的32脚和34脚。将示波器连接到OPAMP3_OUT输出端 PB1观察显示波形。        
PC13是按键输入口,配置为中断方式,用于改变OPAMP3的增益在2倍到4倍。


DAC.png
DAC1配置

DMA.png
DMA配置

GPIO.png
GPIO配置

OPAMP.png
OPAMP3配置

TIME2.png
TIM2配置

main.c
  1. #include "main.h"
  2. #include "dac.h"
  3. #include "dma.h"
  4. #include "usart.h"
  5. #include "opamp.h"
  6. #include "tim.h"
  7. #include "gpio.h"

  8. /* Private includes ----------------------------------------------------------*/
  9. /* USER CODE BEGIN Includes */
  10. __IO uint32_t UserButtonStatus = 0;

  11. const uint16_t Sine12bit[32] = { 511,  611,  707,  796,  873,  937,  984, 1013,
  12.                                 1023, 1013,  984,  937,  873,  796,  707,  611,
  13.                                  511,  412,  315,  227,  149,   86,   38,    9,
  14.                                    0,    9,   38,   86,  149,  227,  315,  412};
  15. /* USER CODE END Includes */

  16. /* Private typedef -----------------------------------------------------------*/
  17. /* USER CODE BEGIN PTD */

  18. /* USER CODE END PTD */

  19. /* Private define ------------------------------------------------------------*/
  20. /* USER CODE BEGIN PD */
  21. /* USER CODE END PD */

  22. /* Private macro -------------------------------------------------------------*/
  23. /* USER CODE BEGIN PM */

  24. /* USER CODE END PM */

  25. /* Private variables ---------------------------------------------------------*/

  26. /* USER CODE BEGIN PV */

  27. /* USER CODE END PV */

  28. /* Private function prototypes -----------------------------------------------*/
  29. void SystemClock_Config(void);
  30. /* USER CODE BEGIN PFP */

  31. /* USER CODE END PFP */

  32. /* Private user code ---------------------------------------------------------*/
  33. /* USER CODE BEGIN 0 */

  34. /* USER CODE END 0 */

  35. /**
  36.   * @brief  The application entry point.
  37.   * @retval int
  38.   */
  39. int main(void)
  40. {
  41.   /* USER CODE BEGIN 1 */

  42.   /* USER CODE END 1 */

  43.   /* MCU Configuration--------------------------------------------------------*/

  44.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  45.   HAL_Init();

  46.   /* USER CODE BEGIN Init */

  47.   /* USER CODE END Init */

  48.   /* Configure the system clock */
  49.   SystemClock_Config();

  50.   /* USER CODE BEGIN SysInit */

  51.   /* USER CODE END SysInit */

  52.   /* Initialize all configured peripherals */
  53.   MX_GPIO_Init();
  54.   MX_DMA_Init();
  55.   MX_DAC1_Init();
  56. //  MX_LPUART1_UART_Init();
  57.   MX_TIM2_Init();
  58.   MX_OPAMP3_Init();

  59.   /* USER CODE BEGIN 2 */
  60.         
  61.   if(HAL_OK != HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1,
  62.                        (uint32_t*)Sine12bit, 32, DAC_ALIGN_12B_R))
  63.   {
  64.     /* Start DMA Error */
  65.     Error_Handler();
  66.   }
  67.         
  68.          if(HAL_OK != HAL_TIM_Base_Start(&htim2))
  69.   {
  70.     Error_Handler();
  71.   }
  72.           if(HAL_OK != HAL_OPAMP_Start(&hopamp3))
  73.   {
  74.     Error_Handler();
  75.   }  
  76.         
  77.   /* USER CODE END 2 */

  78.   /* Infinite loop */
  79.   /* USER CODE BEGIN WHILE */
  80.   while (1)
  81.   {
  82.                
  83.                     if (UserButtonStatus == 1)
  84.     {
  85.       HAL_Delay(200);
  86.       UserButtonStatus = 0;
  87.       /* Change the gain */
  88.       if (hopamp3.Init.PgaGain == OPAMP_PGA_GAIN_2_OR_MINUS_1)
  89.       {
  90.         hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_4_OR_MINUS_3;
  91.       }
  92.       else
  93.       {
  94.         hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_2_OR_MINUS_1;
  95.       }
  96.       /* Update OPAMP config */
  97.       /* Gain is changed on the fly */
  98.       if(HAL_OK != HAL_OPAMP_Init(&hopamp3))
  99.       {
  100.         Error_Handler();
  101.       }
  102.     }

  103.                
  104.     /* USER CODE END WHILE */

  105.     /* USER CODE BEGIN 3 */
  106.   }
  107.   /* USER CODE END 3 */
  108. }

  109. /**
  110.   * @brief System Clock Configuration
  111.   * @retval None
  112.   */
  113. void SystemClock_Config(void)
  114. {
  115.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  116.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  117.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  118.   /** Configure the main internal regulator output voltage
  119.   */
  120. // HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  121.         HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  122.   /** Initializes the RCC Oscillators according to the specified parameters
  123.   * in the RCC_OscInitTypeDef structure.
  124.   */
  125.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  126.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  127.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  128.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  129.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  130.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
  131.   RCC_OscInitStruct.PLL.PLLN = 85;
  132.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  133.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  134.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  135.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  136.   {
  137.     Error_Handler();
  138.   }
  139.   /** Initializes the CPU, AHB and APB buses clocks
  140.   */
  141.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  142.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  143.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  144.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  145.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  146.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  147.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  148.   {
  149.     Error_Handler();
  150.   }
  151.   /** Initializes the peripherals clocks
  152.   */
  153.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
  154.   PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  155.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  156.   {
  157.     Error_Handler();
  158.   }
  159. }

  160. /* USER CODE BEGIN 4 */
  161. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  162. {

  163.   {
  164.     UserButtonStatus = 1;
  165.   }
  166. }
复制代码

opamp.c
  1. OPAMP_HandleTypeDef hopamp3;

  2. /* OPAMP3 init function */
  3. void MX_OPAMP3_Init(void)
  4. {

  5.   /* USER CODE BEGIN OPAMP3_Init 0 */

  6.   /* USER CODE END OPAMP3_Init 0 */

  7.   /* USER CODE BEGIN OPAMP3_Init 1 */

  8.   /* USER CODE END OPAMP3_Init 1 */
  9.   hopamp3.Instance = OPAMP3;
  10.   hopamp3.Init.PowerMode =  OPAMP_POWERMODE_NORMAL;
  11.   hopamp3.Init.Mode = OPAMP_PGA_MODE;
  12.   hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
  13.   hopamp3.Init.InternalOutput = DISABLE;
  14.   hopamp3.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE;
  15.   hopamp3.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO;
  16.   hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_2_OR_MINUS_1;
  17.   hopamp3.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
  18.   if (HAL_OPAMP_Init(&hopamp3) != HAL_OK)
  19.   {
  20.     Error_Handler();
  21.   }
  22.   /* USER CODE BEGIN OPAMP3_Init 2 */

  23.   /* USER CODE END OPAMP3_Init 2 */

  24. }

  25. void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* opampHandle)
  26. {

  27.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  28.   if(opampHandle->Instance==OPAMP3)
  29.   {
  30.   /* USER CODE BEGIN OPAMP3_MspInit 0 */

  31.   /* USER CODE END OPAMP3_MspInit 0 */

  32.     __HAL_RCC_GPIOA_CLK_ENABLE();
  33.     __HAL_RCC_GPIOB_CLK_ENABLE();
  34.     /**OPAMP3 GPIO Configuration
  35.     PA1     ------> OPAMP3_VINP
  36.     PB1     ------> OPAMP3_VOUT
  37.     */
  38.     GPIO_InitStruct.Pin = GPIO_PIN_1;
  39.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  40.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  41.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  42.     GPIO_InitStruct.Pin = GPIO_PIN_1;
  43.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  44.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  45.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  46.   /* USER CODE BEGIN OPAMP3_MspInit 1 */

  47.   /* USER CODE END OPAMP3_MspInit 1 */
  48.   }
  49. }
复制代码


运行结果

GANE2.jpg

增益2倍

GANE4.jpg

增益4倍


可见这是一款非常适合灵活应用的性价比较高的芯片。
STM32G491_OPAMP.rar (699.72 KB, 下载次数: 16)
收藏 评论1 发布时间:2021-3-28 21:37

举报

1个回答
xmshao 回答时间:2021-3-28 22:39:55
谢谢分享! 效果不错!

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版