#include "usart7.h"
#include "usart.h"
#include "CRC16.h"
#include "delay.h"
#include "stm32h7xx_hal_uart_ex.h"
#include "timer123.h"
//485 9600
//PE0-RX PE1-TX
//FF010008000C15 上
//FF010010001627 下
//FF01000406000B 左
//FF010002060009 右
//FF 01 00 00 00 00 01 停
//气体模块 地址1 通道地址1,2,3,4 波特率9600
// 01 03 00 01 00 01 D5 CA 通道1 二氧化碳
// 01 03 00 02 00 01 25 CA 通道2 氯气
// 01 03 00 03 00 01 74 0A 通道3 氢气
// 01 03 00 04 00 01 C5 CB 通道4 硫化氢
//气体模块 地址2 通道地址2,3,4,5 波特率9600
// 02 03 00 02 00 01 25 F9 通道1 甲烷
// 02 03 00 03 00 01 74 39 通道2 氧气
// 02 03 00 04 00 01 C5 F8 通道3 一氧化碳
// 02 03 00 05 00 01 94 38 通道4 氨气
int qi_ti_in[10]={0x00};
uint16_t zhuanhuan;
uint8_t a=0;
uint8_t ucTemp8;
UART_HandleTypeDef UartHandle8;
uart8_def rschuanko_8;
uint16_t byte_tmp8[2];
extern uint8_t Rxflag8;
uint8_t usRxCount8;
uint8_t ucaRxBuf8[13];
uint8_t chuanko8jiesho[2];
//FF 01 00 20 00 00 21//变倍+
//FF 01 00 40 00 00 41//变倍-
uint8_t chuanko85[]={0xff,0x01,0x00,0x20,0x00,0x00,0x21}; //上
uint8_t chuanko86[]={0xff,0x01,0x00,0x40,0x00,0x00,0x41}; //下
uint8_t chuanko82[]={0xff,0x01,0x00,0x08,0x00,0x0c,0x15}; //上
uint8_t chuanko83[]={0xff,0x01,0x00,0x10,0x00,0x16,0x27}; //下
uint8_t chuanko84[]={0xff,0x01,0x00,0x04,0x06,0x00,0x0b}; //左
uint8_t chuanko8[]={0xff,0x01,0x00,0x02,0x06,0x00,0x09}; //右
uint8_t chuanko81[]={0xff,0x01,0x00,0x00,0x00,0x00,0x01}; //停止
/**
* @brief DEBUG_USART GPIO 配置,工作模式配置。115200 8-N-1
* @param 无
* @retval 无
*/
void DEBUG_USART_Config8(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
/* 使能串口1时钟 */
__USART8_CLK_ENABLE();
/* 配置Tx引脚为复用功能 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART8;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* 配置Rx引脚为复用功能 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Alternate = GPIO_AF8_UART8;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* 配置串DEBUG_USART 模式 */
UartHandle8.Instance = UART8;
UartHandle8.Init.BaudRate = 9600;
UartHandle8.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle8.Init.StopBits = UART_STOPBITS_1;
UartHandle8.Init.Parity = UART_PARITY_NONE;
UartHandle8.Init.Mode = UART_MODE_TX_RX;
UartHandle8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle8.Init.OverSampling = UART_OVERSAMPLING_16;
UartHandle8.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
UartHandle8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_Init(&UartHandle8);
HAL_UART_Receive_IT(&UartHandle8,&ucTemp8,1);
/*串口1中断初始化 */
// __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_IDLE);
HAL_NVIC_SetPriority(UART8_IRQn, 4, 1);
HAL_NVIC_EnableIRQ(UART8_IRQn);
/*配置串口接收中断 */
// __HAL_UART_ENABLE_IT(&UartHandle8,UART_IT_RXNE);
// __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_IDLE);
}
//void UART8_IRQHandler(void)
//{
//
//
// if(__HAL_UART_GET_IT( &UartHandle8, UART_IT_RXNE ) != RESET)
// {
//
// HAL_UART_Receive(&UartHandle8, (uint8_t *)&ucTemp8, 1, 1000);
// }
//
//
// HAL_UART_IRQHandler(&UartHandle8);
//}
void UART8_IRQHandler(void)
{
/* USER CODE BEGIN USART6_IRQn 0 */
/* USER CODE END USART6_IRQn 0 */
HAL_UART_IRQHandler(&UartHandle8);
/* USER CODE BEGIN USART6_IRQn 1 */
/* USER CODE END USART6_IRQn 1 */
}
void rschuanko_8_uart8_tx(void)
{
if(rschuanko_8.rx8_complete_flag==0)
{
if(rschuanko_8.rx8_num<7)
{
rschuanko_8.rx8_data[rschuanko_8.rx8_num] = ucTemp8;
rschuanko_8.rx8_num++;
//刷新延时
rschuanko_8.rx8_delay = 30; //超过50ms未收到数据及表示接收完毕
}
else
{
rschuanko_8.rx8_complete_flag=1;
}
}
HAL_UART_Receive_IT(&UartHandle8,&ucTemp8,1);
}
//逻辑处理函数
void rschuanko_8_uart8_rx_handle(void)
{
if(rschuanko_8.rx8_complete_flag)
{
if(rschuanko_8.rx8_num ==7)
{
byte_tmp8[0]=CRC16_Get((uint8_t *)rschuanko_8.rx8_data,5);
if((rschuanko_8.rx8_data[6]==(byte_tmp8[0]>>8))&&(rschuanko_8.rx8_data[5]==(byte_tmp8[0]&0x00ff)))
{
switch(buff[3])
{
case 1:
//二氧化碳
rschuanko_8.tx8_data[0]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[1]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[0]<<8;
qi_ti_in[0]=zhuanhuan|rschuanko_8.tx8_data[1];
break;
case 2:
//氯气
rschuanko_8.tx8_data[2]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[3]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[2]<<8;
qi_ti_in[1]=zhuanhuan|rschuanko_8.tx8_data[3];
break;
case 3:
//氢气
rschuanko_8.tx8_data[4]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[5]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[4]<<8;
qi_ti_in[2]=zhuanhuan|rschuanko_8.tx8_data[5];
break;
case 4:
//硫化氢
rschuanko_8.tx8_data[6]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[7]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[6]<<8;
qi_ti_in[3]=zhuanhuan|rschuanko_8.tx8_data[7];
break;
case 5:
//甲烷
rschuanko_8.tx8_data[8]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[9]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[8]<<8;
qi_ti_in[4]=zhuanhuan|rschuanko_8.tx8_data[9];
break;
case 6:
//氧气
rschuanko_8.tx8_data[10]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[11]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[10]<<8;
qi_ti_in[5]=zhuanhuan|rschuanko_8.tx8_data[11];
break;
case 7:
//一氧化碳
rschuanko_8.tx8_data[12]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[13]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[12]<<8;
qi_ti_in[6]=zhuanhuan|rschuanko_8.tx8_data[13];
break;
case 8:
//氨气
rschuanko_8.tx8_data[14]=rschuanko_8.rx8_data[3];
rschuanko_8.tx8_data[15]=rschuanko_8.rx8_data[4];
zhuanhuan=rschuanko_8.tx8_data[14]<<8;
qi_ti_in[7]=zhuanhuan|rschuanko_8.tx8_data[15];
break;
}
}
}
//重新开始接收
rschuanko_8.rx8_complete_flag=0;
rschuanko_8.rx8_num=0;
rschuanko_8.rx8_delay =0;
}
}
//定时器处理函数,定时器周期10ms
void rschuanko8_uart8_rx_timer_handle(void)
{
if(rschuanko_8.rx8_delay)
{
rschuanko_8.rx8_delay--;
if(rschuanko_8.rx8_delay==0)
{
rschuanko_8.rx8_complete_flag=1; //接收完毕
}
}
}串口配置按照这里的代码来,改完把完整的代码发给我