基于STM32F103的ACS712电流传感器使用教程

首先介绍一下我使用的板子和外设,首先是正点原子的ministm32,mcu为stm32f103rct6,使用的电流传感器为acs712霍尔电流传感器。

然后想要测量电流数据你还得需要一个电流互感器,我因为需要测量的是大电流所以购买的是转化比比较低的版本。

然后说说电流互感器的使用方法,一开始我直接将整根电线穿进去,发现没有电流的变化,后来经过老师指导发现你需要将插座导线的保护壳拆开,单独测量火线的值

如图所示,我购买的是10/5A,穿心3匝(如果1匝则直接将火线穿过即可),所以需要多绕两圈(棕色的是火线),然后将电流互感器上的导线接到霍尔电流传感器上(接哪一头影响不大,可所以接)。

然后讲讲霍尔电流测量电流的方法,理论上就是通过测量电流值,输出电压值,然后我们可以根据adc读取到的电压值结合原理图来反推电流值。如图所示(不同量程的图不同,根据自己购买的型号进行换算),举个例子,如果adc读取到3v电压,那么测量到的电流值大约为5A:

 说完原理我们再来通过stm32cubemx来生成相关代码,这里我直接转载西瓜大大的文章,根据他的教程来即可生成代码

STM32HAL ADC+TIM+DMA采集交流信号 基于cubemx_四臂西瓜的博客-CSDN博客_基于stm32的交流信号采集程序

在tim3的设置教程中根据自己需要的采样率进行修改,比如我需要的是10Khz,则将其修改为7200-1

这里只贴上需要修改的代码:分别在main.c的51和107行

uint16_t adc_buff[1000];//存放ADC采集的数据
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_buff, 1000); //让ADC1去采集1000个数,存放到adc_buff数组里
	while (!AdcConvEnd)	;	//等待转换完毕
	for (uint16_t i = 0; i < 1000; i++)
	{
		  //printf("\r\n电流数据为:\r\n");		  
			printf("\r%.5f %.3f\r\n",i*0.00001,adc_buff[i] * 3.3 / 4095*5 - 2.480*5); //数据打印,查看结果
	}

则打印出来的0电流(即排插没有接其他电器)时数据如下,左边是时间,单位为s,右边为电流数据,单位为A,记得将波特率设置为115200,否则会乱码。

 然后通过Python打印

import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt('computer.txt')

plt.plot(data[:,0],data[:,1])
plt.xlabel("Time(s)")
plt.ylabel("Current(A)")
plt.show()

图片如下

该图为笔记本电脑充电时的数据。

测试完毕。

  • 5
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
下面是基于stm32f103c8t6单片机ACS712直流电流测量传感器的示例代码: ```C #include "stm32f10x.h" #define ADC1_DR_Address ((uint32_t)0x4001244C) // 定义ADC1的值 __IO uint16_t ADC1_ConvertedValue; // 初始化ADC1 void ADC1_Init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; // 使能ADC1和DMA1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // 配置GPIOA.1为模拟输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置DMA1通道1 DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC1_ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); // 使能DMA1通道1 DMA_Cmd(DMA1_Channel1, ENABLE); // 配置ADC1 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); // 配置ADC1通道1的采样时间和转换序列 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5); // 使能ADC1 DMA ADC_DMACmd(ADC1, ENABLE); // 使能ADC1 ADC_Cmd(ADC1, ENABLE); // 开始ADC1的转换 ADC_SoftwareStartConvCmd(ADC1, ENABLE); } // 初始化串口1 void USART1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 使能USART1和GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // 配置USART1的TX引脚为复用推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置USART1的RX引脚为浮空输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置USART1 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStructure); // 使能USART1 USART_Cmd(USART1, ENABLE); } // 发送字符串到串口1 void USART1_SendString(char* str) { while (*str != '\0') { USART_SendData(USART1, (uint16_t)(*str++)); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } } int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); ADC1_Init(); USART1_Init(); while (1) { // 将ADC1_ConvertedValue转换为电压值 float voltage = ADC1_ConvertedValue * 3.3 / 4096; // 将电压值转换为电流值 float current = (voltage - 1.65) / 0.066; // 将电流值转换为字符串 char str[20]; sprintf(str, "Current: %.2fA\r\n", current); // 发送字符串到串口1 USART1_SendString(str); // 等待一段时间 for (int i = 0; i < 1000000; i++); // 点亮LED GPIO_SetBits(GPIOC, GPIO_Pin_13); // 等待一段时间 for (int i = 0; i < 1000000; i++); // 熄灭LED GPIO_ResetBits(GPIOC, GPIO_Pin_13); } } ``` 在示例代码中,我们使用了ADC1和DMA1来读取ACS712的模拟输出。ADC1的采样时间设置为239.5个时钟周期,这样可以保证最高的精度。ADC1每次采样后,会将结果写入ADC1_DR寄存器中,然后DMA1会将这个结果读取到ADC1_ConvertedValue变量中。我们可以将ADC1_ConvertedValue转换为电压值,然后再将电压值转换为电流值。最后,我们将电流值发送到串口1,并将LED交替点亮和熄灭。 需要注意的是,ACS712会在0A附近输出1.65V的直流电压,因此需要将读取到的电压值减去1.65V,然后再除以0.066V/A,才能得到真正的电流值。 如果你使用的是其他型号的单片机或者其他型号的直流电流测量传感器,那么示例代码需要做一些修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值