我是用的STM32F103,485串口中断接收控制指令,然后解析,再通过485DMA返回数据,现在出现的问题是:485DMA返回的数据在字符串的首位和末位都会加0x00,比如应该返回:0x01,0x41,0xC0,0x10。结果返回的数据为:0x00,0x01,0x41,0xC0,0x10,0x00。不知道哪地方出问题了,如果用232DMA返回的话就不会出现这个问题,贴出代码,请原子哥和各位大神看看咋解决。PS:还有个奇怪的问题,程序里把串口的配置都关掉,用串口助手PC给F103发指令,还是会返回一个字节0x00
串口配置:
view sourceprint?
01 USART_InitStructure.USART_BaudRate = 115200;
02 USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; //打开串口的收发功能
03 /* 配置USART2,用于接收云控命令 */
04 USART_Init(USART3, &USART_InitStructure);
05 /* 打开串口2接收中断 */
06 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
07 /* 使能USART2的DMA的发送请求 */
08 USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
09 /* 使能串口2 */
10 USART_Cmd(USART3, ENABLE);
DMA配置:
view sourceprint?
01 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(USART3->DR));
02 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)UART2.SendBuf;
03 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
04 DMA_InitStructure.DMA_BufferSize = 0x00;
05 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
06 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
07 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
08 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
09 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
10 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
11 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
12 /* 配置DMA1_2 */
13 DMA_Init(DMA1_Channel2, &DMA_InitStructure);
14 DMA_ClearFlag(DMA1_IT_TC2);
15 /* 使能DMA1_2发送完成中断 */
16 /* 使能DMA1_2,用于发送摄像机命令 */
17 DMA_Cmd(DMA1_Channel2, ENABLE);
18 DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
串口接收中断:
view sourceprint?
1 void USART3_IRQHandler(void)
2 {
3
4 if(SET == USART_GetITStatus(USART3, USART_IT_RXNE))
5 {
6 Command_Receive();
7 }
8 USART_ClearITPendingBit(USART3,USART_IT_RXNE);
9 }
DMA发送:
view sourceprint?
1 GPIO_SetBits(GPIOA, GPIO_Pin_8);
2 delay_us(100);
3 DMA_Cmd(DMA1_Channel2, DISABLE);
4 DMA1_Channel2->CMAR = (uint32_t)UART2.SendBuf;
5 DMA1_Channel2->CNDTR=UART2.SendDataLen;
6 DMA_Cmd(DMA1_Channel2, ENABLE);
串口发送完成中断:
view sourceprint?01 void DMA1_Channel2_IRQHandler(void)
02 {
03 if(DMA_GetFlagStatus(DMA1_FLAG_TC2) != RESET )
04 {
05 DMA_ClearFlag(DMA1_FLAG_TC2);
06
07 USART_ITConfig(USART3,USART_IT_TC,ENABLE);
08
09 }
10 DMA_ClearITPendingBit(DMA1_IT_TE2);
11 DMA_Cmd(DMA1_Channel2, DISABLE);
12
13
14 while(RESET == USART_GetITStatus(USART3, USART_IT_TC))
15 {
16 }
17 delay_us(100);
18 USART_ClearITPendingBit(USART3,USART_IT_TC);
19 USART_ITConfig(USART3,USART_IT_TC,DISABLE);
20 GPIO_ResetBits(GPIOA, GPIO_Pin_8);
21
22 }
|