current_v/8F_5AW_PowerMeter/8f_5aw_powerMeter.c
2026-01-08 08:45:31 +08:00

339 lines
12 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 8f_5aw_powerMeter.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 ³õʼ°æ±¾ 2026.1.6
******************************************************************************
* @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 "8f_5aw_powerMeter.h"
// Ìṩ¸øÍⲿµÄ²ÉÑù½á¹ûÊý¾Ý
EleC_V_Data elec_v_data;
#if POWER_METER_SET_FUNC_ENABLE // ʹÓÃĬÈϼĴæÆ÷ÅäÖà ²»Ð޸ļĴæÆ÷ÄÚÈÝ
/**
* @brief ÏòBL0942Ö¸¶¨¼Ä´æÆ÷дÈë24λÊý¾Ý
* @param addr: ¼Ä´æÆ÷µØÖ·(8λ)
* @param reg_data: ´ýдÈëµÄÊý¾Ý»º³åÇøÖ¸Õ룬Ðè´«Èë3¸ö×Ö½Ú£¬µÍλÔÚǰ¡¢¸ßλÔÚºó
* @retval ÎÞ
* @note 1. Êý¾Ý¸ñʽ£º¹²24bit£¬²»×ãλ²¹0£¬µÍλÏÈÐз¢ËÍ
* 2. ͨÐÅЭÒ飺UART·¢ËÍÖ¡¸ñʽΪ[дÃüÁî(BL0942_ADDR_W)+¼Ä´æÆ÷µØÖ·+3×Ö½ÚÊý¾Ý+УÑéºÍ]
* 3. УÑéºÍ¼ÆË㣺(дÃüÁî+µØÖ·+3×Ö½ÚÊý¾Ý)µÄµÍ8λȡ·´
* 4. Êý¾Ý·¢ËÍ´óСÏÞ¶¨Îª3¸ö×Ö½Ú£¬³¬³ö²¿·ÖÎÞЧ
*/
static void power_meter_write_reg(uint8_t addr, uint8_t* reg_data)
{
uint8_t check_sum = 0;
// 1. Ï·¢ÃüÁî×Ö½Ú BL0942_ADDR_W
POWER_METER_UART_TRANS_BYTE(BL0942_ADDR_W);
// 2. Ï·¢ÐèҪд¼Ä´æÆ÷µÄµØÖ·
POWER_METER_UART_TRANS_BYTE(addr);
// 3. ·¢ËÍÐèÒªÍù¼Ä´æÆ÷µØÖ·Ð´ÈëµÄÊý¾Ý
POWER_METER_UART_TRANS_BYTE(reg_data[0]);
POWER_METER_UART_TRANS_BYTE(reg_data[1]);
POWER_METER_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);
POWER_METER_UART_TRANS_BYTE(check_sum);
}
#endif
/**
* @brief ´ÓBL0942Ö¸¶¨¼Ä´æÆ÷¶ÁÈ¡Êý¾Ý
* @param addr: ¼Ä´æÆ÷µØÖ·(8λ)
* @retval ÎÞ
* @note 1. ͨÐÅЭÒ飺UART·¢ËÍÖ¡¸ñʽΪ[¶ÁÃüÁî(BL0942_ADDR_R)+¼Ä´æÆ÷µØÖ·]
* 2. оƬÊÕµ½¶ÁÃüÁîºó£¬»áÔÚ150usÄÚͨ¹ýUART·µ»Ø4×Ö½ÚÊý¾Ý(3×Ö½ÚÓÐЧÊý¾Ý+1×Ö½ÚУÑé)
* 3. ½¨Òé500msÖ´ÐÐÒ»´Î¸Ãº¯Êý£¬·µ»ØÊý¾ÝÔÚUART½ÓÊÕÖжÏÖд¦Àí(ϵͳ¼Ä´æÆ÷ÿ¸ô400msË¢ÐÂÒ»´Î)
*/
static void power_meter_read_reg(uint8_t addr)
{
// 1. Ï·¢ÃüÁî×Ö½Ú BL0942_ADDR_R
POWER_METER_UART_TRANS_BYTE(BL0942_ADDR_R);
// 2. Ï·¢µØÖ·Êý¾Ý
POWER_METER_UART_TRANS_BYTE(addr);
}
/**
* @brief ³õʼ»¯¿âÂØ¼Æ(UART5)Ïà¹ØÓ²¼þ
* @param ÎÞ
* @retval ÎÞ
* @note 1. ³õʼ»¯ÄÚÈÝ£ºUART TX/RXÒý½Å¡¢UARTͨÐŲÎÊý¡¢½ÓÊÕÖжÏ
* 2. UART²ÎÊý£º9600²¨ÌØÂÊ¡¢8λÊý¾Ýλ¡¢1λֹͣλ¡¢ÎÞУÑ顢ȫ˫¹¤
* 3. ÖжÏÅäÖãºUART5½ÓÊÕ»º³åÇøÂúÖжϣ¬ÓÅÏȼ¶ÉèΪ2
* 4. ³õʼ»¯ºóÇå¿ÕÈ«¾ÖÊý¾Ý½á¹¹Ìåelec_v_data
*/
void User_PowerMeter_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
// ³õʼ»¯GPIO
GPIO_InitStruct.pin = POWER_METER_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(POWER_METER_UART_RX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.pin = POWER_METER_UART_TX_GPIO_PIN;
FL_GPIO_Init(POWER_METER_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(POWER_METER_USED_UART_NUM, &UART_InitStruct);
NVIC_DisableIRQ(POWER_METER_IRQn);
NVIC_SetPriority(POWER_METER_IRQn,2); //ÖжÏÓÅÏȼ¶ÅäÖÃ
NVIC_EnableIRQ(POWER_METER_IRQn);
// ¿ªÆô½ÓÊÕÖжÏ
FL_UART_ClearFlag_RXBuffFull(POWER_METER_USED_UART_NUM);
FL_UART_EnableIT_RXBuffFull(POWER_METER_USED_UART_NUM);
// Çå¿Õ½á¹û
memset(&elec_v_data, 0, sizeof(elec_v_data));
}
/**
* @brief ˢпâÂØ¼ÆµÄµçѹ/µçÁ÷Êý¾Ý£¨ÂÖѯ¶ÁÈ¡£©
* @param ÎÞ
* @retval ÎÞ
* @note 1. ÍÆ¼ö1sµ÷ÓÃÒ»´Î¸Ãº¯Êý£¬½»Ìæ¶ÁÈ¡µçѹºÍµçÁ÷ÓÐЧֵ
* 2. Ê״ε÷ÓöÁÈ¡µçÁ÷¼Ä´æÆ÷(Addr_I_RMS)£¬µÚ¶þ´Î¶ÁÈ¡µçѹ¼Ä´æÆ÷(Addr_V_RMS)£¬Ñ­»·½»Ìæ
* 3. ´¥·¢¶ÁÈ¡ºó£¬»áÉèÖöÔÓ¦µÄµÈ´ý״̬£¬Êý¾Ý½ÓÊպͽâÎöÔÚUARTÖжÏÖÐÍê³É
* 4. ×îÖÕ½âÎöºóµÄµçѹ/µçÁ÷Öµ´æ´¢ÔÚÈ«¾Ö±äÁ¿elec_v_dataÖÐ
*/
void User_Power_refresh(void)
{
static Bus_Mod mod = MOD_VOLTAGE_METER;
if(mod == MOD_VOLTAGE_METER)
{
mod = MOD_CURRENT_METER;
power_meter_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 if(mod == MOD_CURRENT_METER){
mod = MOD_POWER_METER;
power_meter_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;
}else if(mod == MOD_POWER_METER){
mod = MOD_VOLTAGE_METER;
power_meter_read_reg(Addr_WATT);
elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_POWER; // תÈëµÈ´ý¹¦ÂÊÊý¾Ý״̬
elec_v_data.current_index = 0;
elec_v_data.power_raw = 0;
}
}
/**
* @brief UART5ÖжϷþÎñº¯Êý£¨µçÁ¿¼ÆÊý¾Ý½ÓÊÕ´¦Àí£©
* @param ÎÞ
* @retval ÎÞ
* @note 1. ½ö´¦Àí½ÓÊÕ»º³åÇøÂúÖжÏ(RXBuffFull)
* 2. ¸ù¾Ýµ±Ç°Í¨ÐÅ״̬(BUS_COM_STATE)·Ö±ð´¦Àíµçѹ/µçÁ÷/Óй¦¹¦ÂÊÊý¾Ý
* 3. Êý¾Ý½ÓÊÕ¸ñʽ£º3×Ö½ÚÓÐЧÊý¾Ý(µÍλÔÚǰ)+1×Ö½ÚУÑéºÍ
* 4. УÑéºÍÑé֤ͨ¹ýºó£¬×ª»»ÎªÊµ¼Êµçѹ(V)/µçÁ÷(mA)/Óй¦¹¦ÂÊÖµ²¢¸üÐÂÈ«¾Ö±äÁ¿
* 5. ½ÓÊÕÖжϱê־ͨ¹ý¶ÁÈ¡RXBuff¼Ä´æÆ÷×Ô¶¯Çå³ý£¬Îª·ÀÖ¹³öÏÖÔàÊý¾Ý»¹ÐèÊÖ¶¯Çå³ýRXBuffFull±êÖ¾
*/
void UART5_IRQHandler(void)
{
// ½ÓÊÕÖжϴ¦Àí
if(FL_UART_IsEnabledIT_RXBuffFull(POWER_METER_USED_UART_NUM) &&
FL_UART_IsActiveFlag_RXBuffFull(POWER_METER_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(POWER_METER_USED_UART_NUM);
if(elec_v_data.current_index < 3)
{
elec_v_data.voltage_raw |= (tmp_data<<(elec_v_data.current_index*8));
}
elec_v_data.voltage_array[elec_v_data.current_index++] = tmp_data;
if(elec_v_data.current_index == 4)
{
uint8_t checkSum = ~((elec_v_data.voltage_array[0] + elec_v_data.voltage_array[1] + elec_v_data.voltage_array[2] + BL0942_ADDR_R + Addr_V_RMS)&0xFF);
if(elec_v_data.voltage_array[3] == checkSum)
{
// УÑéÕýÈ·¶ÔÊý¾Ý½øÐи³Öµ
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(POWER_METER_USED_UART_NUM);
if(elec_v_data.current_index < 3)
{
elec_v_data.current_raw |= (tmp_data<<(elec_v_data.current_index*8));
}
elec_v_data.current_array[elec_v_data.current_index++] = tmp_data;
if(elec_v_data.current_index == 4)
{
uint8_t checkSum = ~((elec_v_data.current_array[0] + elec_v_data.current_array[1] + elec_v_data.current_array[2] + BL0942_ADDR_R + Addr_I_RMS)&0xFF);
if(elec_v_data.current_array[3] == checkSum)
{
// УÑéÕýÈ·¶ÔÊý¾Ý½øÐи³Öµ
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
elec_v_data.bus_rms_current_mA =elec_v_data.current_raw*100 / Current_K;
elec_v_data.current_raw = 0;
elec_v_data.current_index = 0;
}
}
}else if(elec_v_data.bus_com_stm == BUS_COM_STATE_WAITE_POWER)
{
uint8_t tmp_data = 0;
tmp_data = FL_UART_ReadRXBuff(POWER_METER_USED_UART_NUM);
if(elec_v_data.current_index < 3)
{
elec_v_data.power_raw |= (tmp_data<<(elec_v_data.current_index*8));
}
elec_v_data.power_array[elec_v_data.current_index++] = tmp_data;
if(elec_v_data.current_index == 4)
{
uint8_t checkSum = ~((elec_v_data.power_array[0] + elec_v_data.power_array[1] + elec_v_data.power_array[2] + BL0942_ADDR_R + Addr_WATT)&0xFF);
if(elec_v_data.power_array[3] == checkSum)
{
// УÑéÕýÈ·¶ÔÊý¾Ý½øÐи³Öµ
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
// µ±½ÓÊÕµ½µÄÊý¾ÝΪ¸ºÖµÊ±½«ÆäתΪÕýÖµ
if(elec_v_data.power_array[2] > 0x7F)
{
elec_v_data.power_raw = ~elec_v_data.power_raw + 1;
}
elec_v_data.bus_active_power_watt = elec_v_data.power_raw * 100 /Power_K;
elec_v_data.power_raw = 0;
elec_v_data.current_index = 0;
}
}
}
FL_UART_ClearFlag_RXBuffFull(POWER_METER_USED_UART_NUM);
}
}
/************************ (C) COPYRIGHT YUWELL *****END OF FILE****/