更新:

1、RS485波特率提升至115200;
2、频率操作由宏定义更改为函数;
3、数据接收&发送接收时的数组操作优化;
This commit is contained in:
吴俊潮 2025-05-14 10:18:08 +08:00
parent 9ef099f074
commit 054354fbad
9 changed files with 16828 additions and 16793 deletions

View File

@ -166,8 +166,7 @@ void trans_hart2pc(void)
//接收到的数据是否符合HART数据报符合则写入485的tx准备发送至上位机
if ((scom1_hart.rx_buff[0] == 0xff) && (scom1_hart.rx_buff[1] == 0xff) && (scom1_hart.rx_buff[scom1_hart.rx_len - 1] == 0xAA))
{
//SIG -> PC
memcpy(scom2_rs485.tx_buff, scom1_hart.rx_buff, sizeof(scom1_hart.rx_buff));
memcpy(scom2_rs485.tx_buff, scom1_hart.rx_buff, scom1_hart.rx_len);
scom2_rs485.tx_len = scom1_hart.rx_len;
scom2_rs485.tx_flag = TRUE;
}
@ -176,15 +175,12 @@ void trans_hart2pc(void)
{
scom2_rs485.tx_flag = FALSE;
//wu_delay_us(500);
//将数据发送至上位机
//HAL_UART_Transmit(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len, 0xFFFF);
HAL_UART_Transmit_DMA(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len);
}
//清空缓存区,等待新的数据
memset(scom1_hart.rx_buff, 0, sizeof(scom1_hart.rx_buff));
memset(scom1_hart.rx_buff, 0, scom1_hart.rx_len);
scom1_hart.rx_len = 0;
}
}
@ -198,7 +194,7 @@ void trans_pc2hart(void)
if ((scom2_rs485.rx_buff[0] == 0xff) && (scom2_rs485.rx_buff[1] == 0xff) && (scom2_rs485.rx_buff[scom2_rs485.rx_len - 1] == 0xaa))
{
//接收到的数据是否符合HART数据报符合则写入HART的tx准备发送至HART设备
memcpy(scom1_hart.tx_buff, scom2_rs485.rx_buff, sizeof(scom2_rs485.rx_buff));
memcpy(scom1_hart.tx_buff, scom2_rs485.rx_buff, scom2_rs485.rx_len);
scom1_hart.tx_len = scom2_rs485.rx_len;
scom1_hart.tx_flag = TRUE;
}
@ -209,29 +205,29 @@ void trans_pc2hart(void)
scom1_hart.tx_flag = FALSE;
HART_RTS(RTS_ON);
wu_delay_us(15000);
wu_delay_us(10000);
//将tx中的数据发送至HART设备
//hart_send(&huart1, scom1_hart.tx_buff);
//HAL_UART_Transmit(&huart1, scom1_hart.tx_buff, scom1_hart.tx_len , 0xFFFF);
HAL_UART_Transmit_DMA(&huart1, scom1_hart.tx_buff, scom1_hart.tx_len);
}
//清空缓存区,等待新的数据
memset(scom2_rs485.rx_buff, 0, sizeof(scom2_rs485.rx_buff));
memset(scom2_rs485.rx_buff, 0, scom2_rs485.rx_len);
scom2_rs485.rx_len = 0;
}
}
void trans_ble2pc(void)
{
if( ble_init() == 0 ) return;
if (scom6_ble.rx_flag == TRUE)
{
scom6_ble.rx_flag = FALSE;
//将接收到的数据存入485的tx准备发送至上位机
memcpy(scom2_rs485.tx_buff, scom6_ble.rx_buff, sizeof(scom6_ble.rx_buff));
memcpy(scom2_rs485.tx_buff, scom6_ble.rx_buff, scom6_ble.rx_len);
scom2_rs485.tx_len = scom6_ble.rx_len;
scom2_rs485.tx_flag = TRUE;
@ -239,15 +235,12 @@ void trans_ble2pc(void)
{
scom2_rs485.tx_flag = FALSE;
//wu_delay_us(500);
//将数据发送至上位机
//HAL_UART_Transmit(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len, 0xFFFF);
HAL_UART_Transmit_DMA(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len);
}
//清空缓存区,等待新的数据
memset(scom6_ble.rx_buff, 0, sizeof(scom6_ble.rx_buff));
memset(scom6_ble.rx_buff, 0, scom6_ble.rx_len);
scom6_ble.rx_len = 0;
}
}
@ -259,7 +252,7 @@ void trans_pc2ble(void)
scom2_rs485.rx_flag = FALSE;
//将接收到的数据存入BLE的tx准备发送至蓝牙设备
memcpy(scom6_ble.tx_buff, scom2_rs485.rx_buff, sizeof(scom2_rs485.rx_buff));
memcpy(scom6_ble.tx_buff, scom2_rs485.rx_buff, scom2_rs485.rx_len);
scom6_ble.tx_len = scom2_rs485.rx_len;
scom6_ble.tx_flag = TRUE;
@ -269,13 +262,11 @@ void trans_pc2ble(void)
scom6_ble.tx_flag = FALSE;
//将tx中的数据发送至蓝牙设备
//ble_send(&huart6, scom6_ble.tx_buff);
//HAL_UART_Transmit(&huart6, scom6_ble.tx_buff, scom6_ble.tx_len, 0xFFFF);
HAL_UART_Transmit_DMA(&huart6, scom6_ble.tx_buff, scom6_ble.tx_len);
}
//清空缓存区,等待新的数据
memset(scom2_rs485.rx_buff, 0, sizeof(scom2_rs485.rx_buff));
memset(scom2_rs485.rx_buff, 0, scom2_rs485.rx_len);
scom2_rs485.rx_len = 0;
}
}
@ -292,15 +283,12 @@ void trans_modbus_pc2sig(void)
{
scom2_rs485.tx_flag = FALSE;
//wu_delay_us(500);
//将数据发送至上位机
//HAL_UART_Transmit(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len, 0xFFFF);
HAL_UART_Transmit_DMA(&huart2, scom2_rs485.tx_buff, scom2_rs485.tx_len);
}
//清空缓存区,等待新的数据
memset(scom2_rs485.rx_buff, 0, sizeof(scom2_rs485.rx_buff));
memset(scom2_rs485.rx_buff, 0, scom2_rs485.rx_len);
scom2_rs485.rx_len = 0;
}
}

View File

@ -150,15 +150,14 @@ void mux_signal_switch(st_mux_signal *mux_signal)
case CH2_OUT_CUR:
dac8552_operation(&mux_signal->data_sv, NULL);
fun_get_sig16132_ch(7, &mux_signal->data_pv);
// ads1220_operation(CH2_OUT_CUR, &mux_signal->data_pv);
break;
case CH3_OUT_FRE:
{
if(tabdata.hart_enable == 0)
{
freq_operation(mux_signal->data_sv, pulse, TIM_CHANNEL_2);
//freq_operation(mux_signal->data_sv, pulse, TIM_CHANNEL_2);
frequence_output(mux_signal->data_sv, pulse, TIM_CHANNEL_2);
fre_set = mux_signal->data_sv;
fun_get_freq(&freq_signal, &mux_signal->data_pv);
}
}
break;
@ -196,3 +195,42 @@ void mux_signal_switch(st_mux_signal *mux_signal)
break;
}
}
void frequence_output(uint32_t freq, uint8_t pulse, uint32_t chan)
{
HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3);
if (freq <= 0 || freq >= 500000 || pulse <= 0 || pulse >= 100)
{
__HAL_TIM_SET_PRESCALER(&htim1, 1500-1);
__HAL_TIM_SET_AUTORELOAD(&htim1, 55-1);
__HAL_TIM_SET_COMPARE(&htim1, chan, 28-1);
return;
}
int fre_duty = 0;
if(freq <= 650)
{
__HAL_TIM_SET_PRESCALER(&htim1, 1500-1);
fre_duty = ( (float32)FREQ_SYS / (float32)1500.0 / (float32)freq + (float32)0.5 );
}
else
{
__HAL_TIM_SET_PRESCALER(&htim1, 2-1);
fre_duty = ( (float32)FREQ_SYS / (float32)2.0 / (float32)freq + (float32)0.5 );
}
__HAL_TIM_SET_AUTORELOAD(&htim1, fre_duty -1);
__HAL_TIM_SET_COMPARE(&htim1, chan, fre_duty * pulse / 100 - 1);
if( chan == TIM_CHANNEL_2)
{
HAL_TIMEx_PWMN_Start(&htim1, chan);
}
else
{
HAL_TIM_PWM_Start(&htim1, chan);
}
}

View File

@ -55,5 +55,6 @@ extern float32 fre_set;
void pwr_cosume(BOOL rst, uint32_t ms);
void mux_signal_switch(st_mux_signal *mux_signal);
void frequence_output(uint32_t freq, uint8_t pulse, uint32_t chan);
#endif

View File

@ -40,15 +40,15 @@ extern "C" {
__HAL_TIM_SET_PRESCALER(&htim1, 1500-1); \
__HAL_TIM_SET_AUTORELOAD(&htim1, 55-1); \
__HAL_TIM_SET_COMPARE(&htim1, CHAN, 28-1); \
return; \
break; \
} \
int fre_duty = 0; \
if(FREQ <= 650) \
if( (0 < FREQ )&&(FREQ <= 650) ) \
{ \
__HAL_TIM_SET_PRESCALER(&htim1, 1500-1); \
fre_duty = ( (float32)FREQ_SYS / (float32)1500.0 / (float32)FREQ + (float32)0.5 ); \
} \
else \
else if(FREQ > 650) \
{ \
__HAL_TIM_SET_PRESCALER(&htim1, 2-1); \
fre_duty = ( (float32)FREQ_SYS / (float32)2.0 / (float32)FREQ + (float32)0.5 ); \

View File

@ -216,7 +216,8 @@ void start_task_hart(void const * argument)
{
if(fre_set != 460800)
{
freq_operation(460800, 50, TIM_CHANNEL_3);
//freq_operation(460800, 50, TIM_CHANNEL_3);
frequence_output(460800, 50, TIM_CHANNEL_3);
fre_set = 460800;
}
@ -228,9 +229,15 @@ void start_task_hart(void const * argument)
scom1_hart.tx_flag = TRUE;
hart_communicate(&scom1_hart);
}
// sig_trans = TRANS_HART;
// transparent_hart(&scom1_hart);
}
else
{
if(fre_set != 0)
{
//freq_operation(0, 50, TIM_CHANNEL_3);
frequence_output(0, 50, TIM_CHANNEL_3);
fre_set = 0;
}
}
osDelay(HART_TASK_PERIOD);
@ -265,8 +272,6 @@ void start_task_ble(void const * argument)
ble_send(&huart6, scom6_ble.tx_buff);
}
// sig_trans = TRANS_BLUETOOTH;
// transparent_bluetooth(&scom6_ble);
}
}
@ -288,18 +293,11 @@ void start_rs485(void const * argument)
/* Infinite loop */
for (;;)
{
// if(tabdata.modbus_enable == 1)
// {
// sig_trans = TRANS_MODBUS;
// }
if( !(tabdata.bluetooth_enable||tabdata.hart_enable||tabdata.modbus_enable) )
{
sig_trans = TRANS_NONE;
}
// transparent_485(&scom2_rs485);
osDelay(RS485_TASK_PERIOD);
}
/* USER CODE END start_rs485 */

View File

@ -442,19 +442,19 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
//HART重新使能接收
HART_RTS(RTS_OFF);
memset(scom1_hart.tx_buff, 0, sizeof(scom1_hart.tx_buff));
memset(scom1_hart.tx_buff, 0, scom1_hart.tx_len);
scom1_hart.tx_len = 0;
}
if(huart == &huart2)
{
memset(scom2_rs485.tx_buff, 0, sizeof(scom2_rs485.tx_buff));
memset(scom2_rs485.tx_buff, 0, scom2_rs485.tx_len);
scom2_rs485.tx_len = 0;
}
if(huart == &huart6)
{
memset(scom6_ble.tx_buff, 0, sizeof(scom6_ble.tx_buff));
memset(scom6_ble.tx_buff, 0, scom6_ble.tx_len);
scom6_ble.tx_len = 0;
}

View File

@ -94,7 +94,7 @@ void MX_USART2_UART_Init(void)
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
@ -644,6 +644,7 @@ void proc_huart_it(UART_HandleTypeDef *huart)
//DMA绕过CPU因此可以不用挂起显示任务
void proc_huart_it_dma(UART_HandleTypeDef *huart)
{
//不对发送中断进行处理
if(st_flag == 3)
{
st_flag = 0;
@ -728,6 +729,7 @@ void proc_huart_it_dma(UART_HandleTypeDef *huart)
if (!scom) return;
//进入此处说明DMA接收已经完成
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET)
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
@ -748,6 +750,7 @@ void proc_huart_it_dma(UART_HandleTypeDef *huart)
tick_middle = xTaskGetTickCountFromISR();
//等待下一次接收
HAL_UART_Receive_DMA(huart, scom->rx_buff, BUFFER_SIZE);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -699,7 +699,7 @@ USART1.IPParameters=VirtualMode,Parity,WordLength,BaudRate
USART1.Parity=PARITY_ODD
USART1.VirtualMode=VM_ASYNC
USART1.WordLength=WORDLENGTH_9B
USART2.BaudRate=9600
USART2.BaudRate=115200
USART2.IPParameters=VirtualMode,BaudRate
USART2.VirtualMode=VM_ASYNC
USART3.IPParameters=VirtualMode