新手上路
- 积分
- 24
- 金钱
- 24
- 注册时间
- 2018-10-30
- 在线时间
- 5 小时
|

楼主 |
发表于 2019-5-12 13:53:44
|
显示全部楼层
本帖最后由 彩虹糖丶 于 2019-5-12 13:57 编辑
/*USART号、时钟、波特率*/
#define RS485_USART USART3
#define RS485_USART_BAUDRATE 62500
/*TX引脚*/
#define RS485_USART_TX_GPIO_PORT GPIOB
#define RS485_USART_TX_PIN GPIO_Pin_10
/*RX引脚*/
#define RS485_USART_RX_GPIO_PORT GPIOB
#define RS485_USART_RX_PIN GPIO_Pin_11
/*485收发控制引脚*/
#define RS485_RE_GPIO_PORT GPIOC
#define RS485_RE_PIN GPIO_Pin_8
/*中断相关*/
#define RS485_INT_IRQ USART3_IRQn
#define RS485_IRQHandler USART3_IRQHandler
/*控制收发引脚*/
//进入接收模式,必须要有延时等待485处理完数据
#define RS485_RX_EN() GPIO_ResetBits(RS485_RE_GPIO_PORT,RS485_RE_PIN);
//进入发送模式,必须要有延时等待485处理完数据
#define RS485_TX_EN() GPIO_SetBits(RS485_RE_GPIO_PORT,RS485_RE_PIN);
void bsp_rcc_init( void )
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
}
void bsp_nvic_init( void )
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RS485_INT_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =5;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RS485_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 将USART Tx的GPIO配置为推挽复用模式
GPIO_InitStructure.GPIO_Pin = RS485_USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RS485_USART_TX_GPIO_PORT, &GPIO_InitStructure);
// 将USART Rx的GPIO配置为浮空输入模式
GPIO_InitStructure.GPIO_Pin = RS485_USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(RS485_USART_RX_GPIO_PORT, &GPIO_InitStructure);
/* 485收发控制管脚 */
GPIO_InitStructure.GPIO_Pin = RS485_RE_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RS485_RE_GPIO_PORT, &GPIO_InitStructure);
/* USART 模式配置*/
USART_InitStructure.USART_BaudRate = RS485_USART_BAUDRATE;
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_Rx | USART_Mode_Tx;
USART_Init(RS485_USART, &USART_InitStructure);
USART_ITConfig(RS485_USART, USART_IT_RXNE, ENABLE);
/*使能USART*/
USART_Cmd(RS485_USART, ENABLE);
}
void RS485_IRQHandler(void)
{
u8 u8RsTemp;
if(USART_GetITStatus(RS485_USART, USART_IT_RXNE) != RESET) /*接收到数据触发*/
{
u8RsTemp = USART_ReceiveData(USART3); //读取接收到的数据
printf("%02x, ", u8RsTemp);
}
}
void RS485_SendCmd( u8 * scmd )
{
u8 i;
for(i= 0; i< scmd[0]; i++)
{
RS485_SendByte(scmd[i+1]);
vTaskDelay(1);
}
}
/**********************************************************************************************************
* 功能 : RS485发送一个字节的数据
* 参数 : 要发送的字节数
* 返回值 : 无
* 注释 : 使用单字节数据发送前要使能发送引脚,发送后要使能接收引脚。
**********************************************************************************************************/
void RS485_SendByte( u8 ch )
{
/* 发送一个字节数据到USART3 */
USART_SendData(RS485_USART,ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(RS485_USART, USART_FLAG_TC) == RESET);
}
vTaskDelay(1);
RS485_TX_EN(); //RS485 发送使能
vTaskDelay(1);/*加短暂延时,保证485配置发送ok*/
RS485_SendCmd(CatEngCmdEnter); //发送数据
printf("\n");
for(i = 0; i < 5; i++)
{
printf("%02x, ", CatEngCmdEnter[i+1]);
}
printf("\n");
vTaskDelay(1); /*加短暂延时,保证485发送数据完毕*/
RS485_RX_EN(); //RS485 接收使能
vTaskDelay(1);
vTaskDelay(2000);
|
|