current_v/Src/coulombmeter.c
2026-01-05 17:49:00 +08:00

248 lines
8.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
******************************************************************************
* @file coulombmeter.c
* @author Motor Control SDK Team, Yuwell Software XiangenWang
* @brief Voice Recognition Module Initialization Section,
including peripheral initialization and message node insertion, etc.
* @version 1.0
* @changelog version 1.0 ³õʼ°æ±¾ 2025.12.31
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2025 Yuwell Software Danyang.Jiangsu.China.
* All rights reserved.</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Yuwell Software nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for Yuwell Software.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY Yuwell Software AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL Yuwell Software OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* USER CODE END Header */
#include "coulombmeter.h"
// Ìṩ¸øÍⲿµÄ²ÉÑù½á¹ûÊý¾Ý
EleC_V_data elec_v_data;
// Ïò¶ÔÓ¦¼Ä´æÆ÷дÈëÊý¾Ý µÍλÔÚµÍλ ¸ßλÔÚ¸ßλ
// ¹²24bit£¬²»×ã´¦ÓÃ0²¹Æë£¬µÍλÏÈÐÐ
// ·¢ËÍÊý¾Ý´óСÏÞ¶¨Îª3¸ö×Ö½Ú
static void coulombmeter_write_reg(uint8_t addr, uint8_t* reg_data)
{
uint8_t check_sum = 0;
// 1. Ï·¢ÃüÁî×Ö½Ú BL0942_ADDR_W
COULOMBMETER_UART_TRANS_BYTE(BL0942_ADDR_W);
// 2. Ï·¢ÐèҪд¼Ä´æÆ÷µÄµØÖ·
COULOMBMETER_UART_TRANS_BYTE(addr);
// 3. ·¢ËÍÐèÒªÍù¼Ä´æÆ÷µØÖ·Ð´ÈëµÄÊý¾Ý
COULOMBMETER_UART_TRANS_BYTE(reg_data[0]);
COULOMBMETER_UART_TRANS_BYTE(reg_data[1]);
COULOMBMETER_UART_TRANS_BYTE(reg_data[2]);
// 4. ·¢ËÍУÑéºÍ
check_sum = (BL0942_ADDR_W + addr + reg_data[0] + reg_data[1] + reg_data[2]);
check_sum = ~(check_sum & 0xFF);
COULOMBMETER_UART_TRANS_BYTE(check_sum);
}
// ´ÓÌØ¶¨¼Ä´æÆ÷¶ÁÈ¡Êý¾Ý оƬ½«Í¨¹ý´®¿Ú½ÓÊÕÖжϷµ»Ø
// оƬÊÕµ½ÏûÏ¢ºó½«ÔÚ150usÄÚ·µ»ØÊý¾Ý
// 1sÖ´ÐÐÒ»´Î Êý¾ÝÔÚ½ÓÊÕÖжÏÖд¦Àí
static void coulombmeter_read_reg(uint8_t addr)
{
// 1. Ï·¢ÃüÁî×Ö½Ú BL0942_ADDR_R
COULOMBMETER_UART_TRANS_BYTE(BL0942_ADDR_R);
// 2. Ï·¢µØÖ·Êý¾Ý
COULOMBMETER_UART_TRANS_BYTE(addr);
}
//
uint8_t data[32];
void User_Coulombmeter_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
// ³õʼ»¯GPIO
GPIO_InitStruct.pin = COULOMBMETER_UART_RX_GPIO_PIN;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = FL_DISABLE;
GPIO_InitStruct.remapPin = FL_DISABLE;
GPIO_InitStruct.analogSwitch = FL_DISABLE;
FL_GPIO_Init(COULOMBMETER_UART_RX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.pin = COULOMBMETER_UART_TX_GPIO_PIN;
FL_GPIO_Init(COULOMBMETER_UART_TX_GPIO_PORT, &GPIO_InitStruct);
// ³õʼ»¯´®¿Ú²ÎÊý
FL_UART_InitTypeDef UART_InitStruct = {0};
UART_InitStruct.baudRate = 9600; //²¨ÌØÂÊ
UART_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B; //Êý¾ÝλÊý
UART_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B; //ֹͣλ
UART_InitStruct.parity = FL_UART_PARITY_NONE; //ÆæÅ¼Ð£Ñé
UART_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX; //½ÓÊÕ-·¢ËÍʹÄÜ
FL_UART_Init(COULOMBMETER_USED_UART_NUM, &UART_InitStruct);
NVIC_DisableIRQ(COULOMBMETER_IRQn);
NVIC_SetPriority(COULOMBMETER_IRQn,2); //ÖжÏÓÅÏȼ¶ÅäÖÃ
NVIC_EnableIRQ(COULOMBMETER_IRQn);
// ¿ªÆô½ÓÊÕÖжÏ
FL_UART_ClearFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM);
FL_UART_EnableIT_RXBuffFull(COULOMBMETER_USED_UART_NUM);
// ¿ªÆô·¢ËÍÍê±ÏÖжϣ¨ÓÉÓÚÐèÒª·¢Ë͵Ä×Ö½Ú½ÏÉÙ ´Ë´¦²»Ê¹Óã©
// FL_UART_ClearFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM);
// FL_UART_EnableIT_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM);
// Çå¿Õ½á¹û
memset(&elec_v_data, 0, sizeof(elec_v_data));
memset(data, 0, sizeof(data));
}
//
// ÍÆ¼ö1sË¢ÐÂÒ»´Î ×îÖÕÊý¾Ý½«´æ´¢ÔÚÈ«¾Ö±äÁ¿elec_v_dataÖÐ
void User_Coulombmeter_Data_refresh(void)
{
static uint8_t mod = 0;
if(mod == 0)
{
mod = 1;
coulombmeter_read_reg(Addr_V_RMS);
elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_VOLTAGE; // תÈëµÈ´ýµçѹÊý¾Ý״̬
elec_v_data.current_index = 0;
elec_v_data.voltage_raw = 0;
}else{
mod = 0;
coulombmeter_read_reg(Addr_I_RMS);
elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_CURRENT; // תÈëµÈ´ýµçѹÊý¾Ý״̬
elec_v_data.current_index = 0;
elec_v_data.current_raw = 0;
}
}
uint8_t i = 0;
void UART5_IRQHandler(void)
{
// ½ÓÊÕÖжϴ¦Àí
if(FL_UART_IsEnabledIT_RXBuffFull(COULOMBMETER_USED_UART_NUM) &&
FL_UART_IsActiveFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM))
{
if(elec_v_data.bus_com_stm == BUS_COM_STATE_WAITE_VOLTAGE)
{
//½ÓÊÕÖжϱêÖ¾¿Éͨ¹ý¶ÁÈ¡rxreg¼Ä´æÆ÷Çå³ý
uint8_t tmp_data = 0;
tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM);
elec_v_data.voltage_raw |= (tmp_data<<(elec_v_data.current_index*8));
elec_v_data.current_index++;
if(elec_v_data.current_index == 3)
{
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
elec_v_data.bus_rms_voltage_V =elec_v_data.voltage_raw / Voltage_K;
elec_v_data.voltage_raw = 0;
elec_v_data.current_index = 0;
}
}else if(elec_v_data.bus_com_stm == BUS_COM_STATE_WAITE_CURRENT)
{
uint8_t tmp_data = 0;
tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM);
elec_v_data.current_raw |= (tmp_data<<(elec_v_data.current_index*8));
elec_v_data.current_index++;
if(elec_v_data.current_index == 3)
{
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
elec_v_data.bus_rms_current_mA =elec_v_data.current_raw / Current_K;
// elec_v_data.current_raw = 0;
elec_v_data.current_index = 0;
}
}
}
// ·¢ËÍÖжϴ¦Àí
if(FL_UART_IsEnabledIT_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM) &&
FL_UART_IsActiveFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM))
{
FL_UART_ClearFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM); //Çå³ý·¢ËÍÖжϱêÖ¾
}
}
/************************ (C) COPYRIGHT YUWELL *****END OF FILE****/