基于st mc workbench的stm32G431RBT的三电阻电流采样代码应该怎么写
时间: 2024-04-30 09:17:17 浏览: 161
STM32微控制器电压采样例程
为您提供一份基于ST MC Workbench的STM32G431RBT的三电阻电流采样代码:
#include "main.h"
#include "stm32g4xx_hal.h"
// 电流采样定义
#define CURRENT_GAIN 330.0 // 电流放大器增益
#define CURRENT_OFFSET 1.66 // 电流放大器偏移
#define CURRENT_SCALE 0.0008056640625 // 电流采样转换
// 采样ADC定义
ADC_HandleTypeDef hadc1; // ADC1句柄
DMA_HandleTypeDef hdma_adc1; // ADC1 DMA句柄
// DMA缓存区定义
uint16_t adc1_dma_buf[3]; // 放置DMA采样数据
// 电流采样变量
float current; // 采样电流
float voltage; // 采样电压
float power; // 采样功率
// ADC采样完成回调函数
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
uint16_t adc_value = adc1_dma_buf[0]; // 获取ADC值
// 计算采样电流,电压和功率
current = (((float)adc_value * CURRENT_SCALE) - CURRENT_OFFSET) * CURRENT_GAIN;
voltage = ((float)adc1_dma_buf[1] * 0.0008056640625) * 33;
power = current * voltage;
}
// ADC初始化函数
void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_CIRCULAR;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
// ADC DMA初始化函数
void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* DMA1_Channel1 Init */
hdma_adc1.Instance = DMA1_Channel1;
hdma_adc1.Init.Request = DMA_REQUEST_ADC1;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(&hadc1, DMA_Handle, hdma_adc1);
}
// 电流采集初始化函数
void Current_Init(void)
{
// ADC初始化
MX_ADC1_Init();
// DMA初始化
MX_DMA_Init();
// 启动ADC
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc1_dma_buf, 2);
// 等待DMA回调函数执行
while(!current);
}
// 电流采集处理函数
void Current_Process(void)
{
//do something
}
// ADC DMA中断函数
void DMA1_Channel1_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_adc1);
}
```
阅读全文