原因是程序初始的时候DMA1_FLAG_TC4标志是清零的
原因是程序初始的时候DMA1_FLAG_TC4标志是清零的,得DMA发送数据完成后才会有置1,我在发送数据的时候开始就判断是否是置1从而判断上次发送完毕,结果刚开机DMA1_FLAG_TC4是 0,一直死循环。现在在串口初始化的时候配置DMA做一次传输,就将DMA1_FLAG_TC4 位置1了,以后的发送即可在先前去判断这个位。但是这样做开机会发送一个字节的数据,属于笨办法,有没有更好的方式? /******************************************************************************** *函数原型: void RS232_Init(u32 baud) *参数说明: baud: 波特率 *返回值: 无 *说明: USART 通讯口初始化程序 设置帧格式: 8 个数据位,无奇偶效验位,1个停止位 8N1 ********************************************************************************/ void RS232_Init(u32 baud) //USART 通讯口初始化程序 { USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; USART_InitStructure.USART_BaudRate = baud; //波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //通讯格式 8N1,无硬件流控 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(USART1, &USART_InitStructure); /* Enable the USART Transmoit interrupt: this interrupt is generated when the USART1 transmit data register is empty */ /* Enable the USART Receive interrupt: this interrupt is generated when the USART1 receive data register is not empty */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); RS232_BufInit(); //初始化发送与接收缓冲区,并构缓冲区环型链表 ///////////////////////////////////首先开启一次DMA传送使DMA1_FLAG_TC4 置位///////////////////// DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RS232_TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel4, ENABLE); USART_Cmd(USART1, ENABLE); } /******************************************************************************** *函数原型: u8 RS232_SetSum (struct DATA_BUF *set_pk) *参数说明: *set_pk : 指向待计算的数据帧 *返回值: num_sum : 8位效验和 *说明: 计算数据帧效验和 ********************************************************************************/ u8 RS232_SetSum (struct DATA_BUF *set_pk) //计算数据帧效验和 { u8 num_sum = 0; u8 i = 0; num_sum = UART_START0 +UART_START1; num_sum += set_pk->IP_addr; num_sum += set_pk->length & GET_LEN; num_sum += set_pk->command; for(i=0; i<set_pk->length; i++) num_sum += set_pk->byte; return num_sum; } /******************************************************************************** *函数原型: void RS232_SendData(uint8 comm,uint8 Len) *参数说明: comm : 命令字 * Len : 帧数据长度 *返回值: 无 *说明: 将待发送的数据发送出去 ********************************************************************************/ void RS232_SendData(u8 comm,u8 Len) //将待发送的数据发送出去 { DMA_InitTypeDef DMA_InitStructure; u8 i = 0; struct DATA_BUF *send_pk = &TXD_BUF[0]; //初始化定义 while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET); //等待上一个数据帧发送完毕 send_pk->IP_addr = IP_ADDR; //设置发送从机的IP地址 send_pk->command = comm; //设置反回发送命令, send_pk->length = Len & GET_LEN; //设置帧数据长度 send_pk->state &= ~GET_ERROR; //设置错误标志位,无 RS232_TxBuffer[0] = UART_START0; RS232_TxBuffer[1] = UART_START1; RS232_TxBuffer[2] = IP_ADDR; //设置发送从机的IP地址 RS232_TxBuffer[3] = comm; //设置反回发送命令, RS232_TxBuffer[4] = Len & GET_LEN; //设置帧数据长度 for(i=0;i<Len;i++) { RS232_TxBuffer[i+5] = send_pk->byte; } RS232_TxBuffer[Len+5] = RS232_SetSum(send_pk); //计算校验和 DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RS232_TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = Len+6; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_ClearFlag(DMA1_FLAG_TC4); DMA_Cmd(DMA1_Channel4, ENABLE); }