我這里用的是CH32V307,寫一個(gè)modbus從機(jī),需要配合空閑中斷判斷數(shù)據(jù)幀有咩有結(jié)束,外部是通過458通信的,有幾點(diǎn)疑問請(qǐng)大家?guī)兔纯垂?/p>
1,發(fā)現(xiàn)無法進(jìn)入空閑中斷,只進(jìn)入到了接受中斷,
2,我看官方evk的代碼示例都沒有在中斷里面清除標(biāo)志,清除標(biāo)志用USART_ClearFlag還是USART_ClearITPendingBit?
3,usart和uart都支持dma嗎?
下面貼我的代碼大家?guī)兔纯础?/p>
初始化:
void rs485_init(void)
{
? ? GPIO_InitTypeDef? GPIO_InitStructure = {0};
? ? USART_InitTypeDef USART_InitStructure = {0};
? ? RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8, ENABLE);
? ? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
? ? GPIO_Init(GPIOB, &GPIO_InitStructure);
? ? RS485_READ_EN(true);
? ? /* UART8 TX-->C.4? RX-->C.5 */
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
? ? GPIO_Init(GPIOC, &GPIO_InitStructure);
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
? ? GPIO_Init(GPIOC, &GPIO_InitStructure);
? ? USART_InitStructure.USART_BaudRate = 9600;
? ? 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_InitTypeDef NVIC_InitStructure;
? ? NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;
? ? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
? ? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
? ? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
? ? NVIC_Init(&NVIC_InitStructure);
? ? USART_ITConfig(UART8, USART_IT_RXNE | USART_IT_IDLE, ENABLE);
? ? USART_Cmd(UART8, ENABLE);//先使能串口 再使能中斷
}
中斷:
void UART8_IRQHandler(void)
{
? ? uint8_t revice = 0;
? ? if (USART_GetITStatus(UART8, USART_IT_IDLE) == SET)
? ? {
? ? ? ? USART_ClearFlag(UART8, USART_IT_IDLE);
? ? ? ? revice = UART8->STATR;
? ? ? ? revice = UART8->DATAR;
? ? ? ? printf("rs485_msg.rx_len: %d\r\n", rs485_msg.rx_len);
? ? ? ? printf_hex(rs485_msg.rx_buffer, rs485_msg.rx_len);
? ? }
? ? if (USART_GetITStatus(UART8, USART_IT_RXNE) == SET)
? ? {
? ? ? ? USART_ClearITPendingBit(UART8, USART_IT_RXNE);
? ? ? ? revice =? USART_ReceiveData(UART8);
? ? ? ? rs485_msg.rx_buffer[rs485_msg.rx_len++] = revice;
? ? ? ? printf("%02x",revice);
? ? }
}