569 lines
18 KiB
C
569 lines
18 KiB
C
#include "HD_UART.h"
|
||
|
||
void USART2_IRQHandler(void) __attribute__((interrupt()));
|
||
void USART3_IRQHandler(void) __attribute__((interrupt()));
|
||
#ifdef uart4_enable
|
||
// void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||
void UART4_IRQHandler(void) __attribute__((interrupt()));
|
||
#endif
|
||
void UART5_IRQHandler(void) __attribute__((interrupt()));
|
||
void UART6_IRQHandler(void) __attribute__((interrupt()));
|
||
void UART7_IRQHandler(void) __attribute__((interrupt()));
|
||
void UART8_IRQHandler(void) __attribute__((interrupt()));
|
||
|
||
#define write Bit_SET
|
||
#define read Bit_RESET
|
||
|
||
/*
|
||
* @brief 初始化UART6对应的J1_485,默认为接收
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void J1_485_Init(uint32_t baud)
|
||
{
|
||
/* 初始化DE引脚 */
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = J1_DE_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||
GPIO_Init(J1_DE_PROT, &GPIO_InitStructure);
|
||
readJ1_485;
|
||
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* UART6 TX --> PC0 RX --> PC1 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART6, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC0为复用推挽输出
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC1为浮空输入
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(UART6, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART6_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(UART6, USART_IT_RXNE, ENABLE);
|
||
USART_ITConfig(UART6, USART_IT_IDLE, ENABLE);
|
||
|
||
USART_Cmd(UART6,ENABLE);
|
||
}
|
||
|
||
|
||
/*
|
||
* @brief 初始化UART7对应的J2_485,默认为接收,电源关闭状态
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void J2_485_Init(uint32_t baud)
|
||
{
|
||
/* 初始化DE引脚 */
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = J2_DE_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||
GPIO_Init(J2_DE_PROT, &GPIO_InitStructure);
|
||
readJ2_485;
|
||
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* UART7 TX --> PC2 RX --> PC3 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC2为复用推挽输出
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC3为浮空输入
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(UART7, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART7_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(UART7, USART_IT_RXNE, ENABLE);
|
||
USART_ITConfig(UART7, USART_IT_IDLE, ENABLE);
|
||
|
||
USART_Cmd(UART7,ENABLE);
|
||
}
|
||
|
||
/*
|
||
* @brief 初始化USART2对应的J3_485,默认为接收,电源关闭状态
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void J3_485_Init(uint32_t baud)
|
||
{
|
||
/* 初始化DE引脚 */
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = J3_DE_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||
GPIO_Init(J3_DE_PROT, &GPIO_InitStructure);
|
||
readJ3_485;
|
||
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* USART2 TX --> PA2 RX --> PA3 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PA2为复用推挽输出
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PA3为浮空输入
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(USART2, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为1
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为1
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
||
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
|
||
|
||
USART_Cmd(USART2,ENABLE);
|
||
}
|
||
|
||
/*
|
||
* @brief 初始化UART8对应的J4_485,默认为接收,电源关闭状态
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void J4_485_Init(uint32_t baud)
|
||
{
|
||
/* 初始化DE引脚 */
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = J4_DE_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||
GPIO_Init(J4_DE_PROT, &GPIO_InitStructure);
|
||
readJ4_485;
|
||
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* UART8 TX --> PC4 RX --> PC5 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC4为复用推挽输出
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC5为浮空输入
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(UART8, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(UART8, USART_IT_RXNE, ENABLE);
|
||
USART_ITConfig(UART8, USART_IT_IDLE, ENABLE);
|
||
|
||
USART_Cmd(UART8,ENABLE);
|
||
}
|
||
|
||
/*
|
||
* @brief 初始话USART3用于控制J5-0共6个485接口,设置控制的485为J0,电源全部关闭
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void J5_0_485_Init(uint32_t baud)
|
||
{
|
||
/* 初始化DE引脚 */
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = J5_0_DE_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||
GPIO_Init(J5_0_DE_PROT, &GPIO_InitStructure);
|
||
readJ5_0_485;
|
||
// writeJ5_0_485;
|
||
GPIO_InitStructure.GPIO_Pin = J5_0_A_PIN;
|
||
GPIO_Init(J5_0_DE_PROT, &GPIO_InitStructure);
|
||
GPIO_InitStructure.GPIO_Pin = J5_0_B_PIN;
|
||
GPIO_Init(J5_0_DE_PROT, &GPIO_InitStructure);
|
||
GPIO_InitStructure.GPIO_Pin = J5_0_C_PIN;
|
||
GPIO_Init(J5_0_DE_PROT, &GPIO_InitStructure);
|
||
USART_CONNET_J0();
|
||
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* USART3 TX --> PB10 RX --> PB11 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PB10为复用推挽输出
|
||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PB11为浮空输入
|
||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(USART3, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级为1
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级为1
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
|
||
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
|
||
// USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
|
||
|
||
USART_Cmd(USART3,ENABLE);
|
||
}
|
||
/*
|
||
* Function Name : USART_CONNET_J0
|
||
* Description : 控制串口3连接J0对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J0(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_RESET);
|
||
}
|
||
|
||
/*
|
||
* Function Name : USART_CONNET_J5
|
||
* Description : 控制串口3连接J5对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J5(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_SET);
|
||
}
|
||
|
||
/*
|
||
* Function Name : USART_CONNET_J6
|
||
* Description : 控制串口3连接J6对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J6(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_SET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_RESET);
|
||
}
|
||
|
||
/*
|
||
* Function Name : USART_CONNET_J7
|
||
* Description : 控制串口3连接J7对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J7(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_SET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_SET);
|
||
}
|
||
|
||
/*
|
||
* Function Name : USART_CONNET_J8
|
||
* Description : 控制串口3连接J8对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J8(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_SET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_RESET);
|
||
}
|
||
|
||
/*
|
||
* Function Name : USART_CONNET_J9
|
||
* Description : 控制串口3连接J9对应的485.
|
||
* Input : None
|
||
* Return : None
|
||
**/
|
||
void USART_CONNET_J9(void)
|
||
{
|
||
GPIO_WriteBit(J5_0_A_PROT, J5_0_A_PIN, Bit_SET);
|
||
GPIO_WriteBit(J5_0_B_PROT, J5_0_B_PIN, Bit_RESET);
|
||
GPIO_WriteBit(J5_0_C_PROT, J5_0_C_PIN, Bit_SET);
|
||
}
|
||
|
||
|
||
#ifdef uart4_enable
|
||
/*
|
||
* @brief 初始化UART4对应的LORA串口
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void LORA_UART4_Init(uint32_t baud)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* UART4 TX --> PC10 RX --> PC11 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC10为复用推挽输出
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC11为浮空输入
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(UART4, &USART_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
|
||
|
||
USART_Cmd(UART4,ENABLE);
|
||
}
|
||
#endif
|
||
|
||
/*
|
||
* @brief 初始化UART5对应的对上串口
|
||
* @param baud 波特率
|
||
* @retval
|
||
*
|
||
*/
|
||
void Upward_UART5_Init(uint32_t baud)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
USART_InitTypeDef USART_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
/* UART5 TX --> PC12 RX --> PD2 */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC12为复用推挽输出
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PD2为浮空输入
|
||
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||
|
||
USART_InitStructure.USART_BaudRate = baud;
|
||
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(UART5, &USART_InitStructure);
|
||
|
||
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
|
||
|
||
USART_Cmd(UART5,ENABLE);
|
||
}
|
||
|
||
|
||
|
||
void USART2_IRQHandler(void)
|
||
{
|
||
J3_Interrupt();
|
||
}
|
||
|
||
void USART3_IRQHandler(void)
|
||
{
|
||
J5_0_Interrupt();
|
||
}
|
||
|
||
#ifdef uart4_enable
|
||
void UART4_IRQHandler(void)
|
||
{
|
||
|
||
}
|
||
#endif
|
||
|
||
void UART5_IRQHandler(void)
|
||
{
|
||
Upward_Interrupt();
|
||
}
|
||
|
||
void UART6_IRQHandler(void)
|
||
{
|
||
J1_Interrupt();
|
||
}
|
||
|
||
void UART7_IRQHandler(void)
|
||
{
|
||
J2_Interrupt();
|
||
}
|
||
|
||
void UART8_IRQHandler(void)
|
||
{
|
||
J4_Interrupt();
|
||
}
|
||
|
||
|
||
/*
|
||
* @brief 发送一个字符
|
||
* @param pUSARTx 串口
|
||
data 字符
|
||
* @retval
|
||
*
|
||
*/
|
||
void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
|
||
{
|
||
while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
|
||
{
|
||
}
|
||
USART_SendData(pUSARTx, data);
|
||
}
|
||
|
||
/*
|
||
* @brief 发送字符串
|
||
* @param pUSARTx 串口
|
||
data 字符
|
||
len 发送字符串的长度
|
||
* @retval
|
||
*
|
||
*/
|
||
void USARTx_SendStr_Len(USART_TypeDef* pUSARTx, char *str, int len)
|
||
{
|
||
if (pUSARTx == J1_USART) {
|
||
writeJ1_485;
|
||
} else if (pUSARTx == J2_USART) {
|
||
writeJ2_485;
|
||
} else if (pUSARTx == J3_USART) {
|
||
writeJ3_485;
|
||
} else if (pUSARTx == J4_USART) {
|
||
writeJ4_485;
|
||
} else if (pUSARTx == J5_0_USART) {
|
||
writeJ5_0_485;
|
||
}
|
||
|
||
for (int i = 0; i < len; i++) {
|
||
USARTx_SendByte(pUSARTx, str[i]);
|
||
}
|
||
|
||
if (pUSARTx == J1_USART) {
|
||
readJ1_485;
|
||
} else if (pUSARTx == J2_USART) {
|
||
readJ2_485;
|
||
} else if (pUSARTx == J3_USART) {
|
||
readJ3_485;
|
||
} else if (pUSARTx == J4_USART) {
|
||
readJ4_485;
|
||
} else if (pUSARTx == J5_0_USART) {
|
||
readJ5_0_485;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|