【STM32G491试用】(3)OPAMP
STM32G491内置4通道可编程增益放大器,本次使用OPAMP3测试性能。DAC1产生800mV正弦波,从PA4输出,连接到OPAMP3同相输入端PB0,用一条导线连接CN7的32脚和34脚。将示波器连接到OPAMP3_OUT输出端 PB1观察显示波形。
PC13是按键输入口,配置为中断方式,用于改变OPAMP3的增益在2倍到4倍。
DAC1配置
DMA配置
GPIO配置
OPAMP3配置
TIM2配置
main.c
- #include "main.h"
- #include "dac.h"
- #include "dma.h"
- #include "usart.h"
- #include "opamp.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- __IO uint32_t UserButtonStatus = 0;
- const uint16_t Sine12bit[32] = { 511, 611, 707, 796, 873, 937, 984, 1013,
- 1023, 1013, 984, 937, 873, 796, 707, 611,
- 511, 412, 315, 227, 149, 86, 38, 9,
- 0, 9, 38, 86, 149, 227, 315, 412};
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_DAC1_Init();
- // MX_LPUART1_UART_Init();
- MX_TIM2_Init();
- MX_OPAMP3_Init();
- /* USER CODE BEGIN 2 */
-
- if(HAL_OK != HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1,
- (uint32_t*)Sine12bit, 32, DAC_ALIGN_12B_R))
- {
- /* Start DMA Error */
- Error_Handler();
- }
-
- if(HAL_OK != HAL_TIM_Base_Start(&htim2))
- {
- Error_Handler();
- }
- if(HAL_OK != HAL_OPAMP_Start(&hopamp3))
- {
- Error_Handler();
- }
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
-
- if (UserButtonStatus == 1)
- {
- HAL_Delay(200);
- UserButtonStatus = 0;
- /* Change the gain */
- if (hopamp3.Init.PgaGain == OPAMP_PGA_GAIN_2_OR_MINUS_1)
- {
- hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_4_OR_MINUS_3;
- }
- else
- {
- hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_2_OR_MINUS_1;
- }
- /* Update OPAMP config */
- /* Gain is changed on the fly */
- if(HAL_OK != HAL_OPAMP_Init(&hopamp3))
- {
- Error_Handler();
- }
- }
-
-
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Configure the main internal regulator output voltage
- */
- // HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
- RCC_OscInitStruct.PLL.PLLN = 85;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the peripherals clocks
- */
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
- PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
-
- {
- UserButtonStatus = 1;
- }
- }
复制代码
opamp.c
- OPAMP_HandleTypeDef hopamp3;
- /* OPAMP3 init function */
- void MX_OPAMP3_Init(void)
- {
- /* USER CODE BEGIN OPAMP3_Init 0 */
- /* USER CODE END OPAMP3_Init 0 */
- /* USER CODE BEGIN OPAMP3_Init 1 */
- /* USER CODE END OPAMP3_Init 1 */
- hopamp3.Instance = OPAMP3;
- hopamp3.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
- hopamp3.Init.Mode = OPAMP_PGA_MODE;
- hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
- hopamp3.Init.InternalOutput = DISABLE;
- hopamp3.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE;
- hopamp3.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO;
- hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_2_OR_MINUS_1;
- hopamp3.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
- if (HAL_OPAMP_Init(&hopamp3) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN OPAMP3_Init 2 */
- /* USER CODE END OPAMP3_Init 2 */
- }
- void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* opampHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(opampHandle->Instance==OPAMP3)
- {
- /* USER CODE BEGIN OPAMP3_MspInit 0 */
- /* USER CODE END OPAMP3_MspInit 0 */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**OPAMP3 GPIO Configuration
- PA1 ------> OPAMP3_VINP
- PB1 ------> OPAMP3_VOUT
- */
- GPIO_InitStruct.Pin = GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* USER CODE BEGIN OPAMP3_MspInit 1 */
- /* USER CODE END OPAMP3_MspInit 1 */
- }
- }
复制代码
运行结果
增益2倍
增益4倍
可见这是一款非常适合灵活应用的性价比较高的芯片。
STM32G491_OPAMP.rar
(699.72 KB, 下载次数: 17)
|