本帖最后由 cwy0828 于 2018-11-19 13:31 编辑
USART1用DMA发送数据,1,总是会在最后多发一个0XFF?2,每次设置发送数据量时总要多设置2个(比如我要发送5个数据据,要设置成7才可以),不知道是什么原因,请大师们指点一下,谢谢
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA ,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1,ENABLE);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//KZ485 IO口设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//=========
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_2; //2个停止位
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);
//中断设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//用中断组0
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearITPendingBit(USART1,USART_IT_TXE); //清中断标志
USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //关发送中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开接收中断
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_ClearFlag(USART1,USART_FLAG_RXNE);
DMA_ClearITPendingBit(DMA1_IT_TC4);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //开启DMA发送
USART_Cmd(USART1, ENABLE); //使能
kz485=0;//先处于发送状态
}
void DMA_Usart1_Configuration(void) //DMA
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); //开启时钟
//设置UART1发送DMA==================================
DMA_DeInit(DMA1_Channel4); //复位所有寄存器
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART1->DR; // 源地址
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&TX_U1; //目标地址
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST; //内存作为数据传输的来源
DMA_InitStructure.DMA_BufferSize=20;//要发送的数据量
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte ; //数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte ; // 数据宽度为8位
DMA_InitStructure.DMA_Mode=DMA_Mode_Normal; // 工作在正常模式
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //拥有高优先级
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; // 没有设置为内存到内存传输
DMA_Init(DMA1_Channel4,&DMA_InitStructure); //
//中断设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//用中断组0
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); //开启传输完成中断
DMA_ClearITPendingBit(DMA1_IT_TC4);
// DMA_Cmd(DMA1_Channel4,ENABLE); //启动DMA1
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>