新手上路
- 积分
- 29
- 金钱
- 29
- 注册时间
- 2019-3-1
- 在线时间
- 10 小时
|

楼主 |
发表于 2020-1-15 09:01:05
|
显示全部楼层
贴上代码,USART1 和USART2 DMA是正常的,USART3-4都不行,直接用串口发送是可以的,使用DMA就不能,查寄存器,感觉是少了CXS寄存器的配置,有没有大神指导一下的?或者给我个stm32f030xc的demo就行,不胜感激
void Usart3_DMA_config()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; //定义串口初始化结构体
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //使能GPIOA的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART的时钟
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);//配置PA0成第二功能引脚 u3TX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);//配置PA1成第二功能引脚 u3RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_10; //选中串口默认输出管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //定义输出最大速率
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//定义管脚9的模式
GPIO_Init(GPIOB, &GPIO_InitStructure); //调用函数,把结构体参数输入进行初始化
/*串口通讯参数设置*/
USART_InitStructure.USART_BaudRate = 250000;//250K
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_2; //停止位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(USART3, &USART_InitStructure);
//TXE发送中断,TC传输完成中断,RXNE接收中断,PE奇偶错误中断,可以是多个
USART_ITConfig(USART3,USART_IT_TC,ENABLE);
USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);
USART3->ICR |= 1<<4; //必须先清除IDLE中断,否则会一直进IDLE中断
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->TDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;
//设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//设置DMA的内存递增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据字长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//内存数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//设置DMA的传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//设置DMA的优先级别
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
//设置DMA的2个memory中的变量互相访问
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
//使能通道5
DMA_Cmd(DMA1_Channel4,ENABLE);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPriority=0x01;
NVIC_Init(&NVIC_InitStruct);
//采用DMA方式发送
USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);
//采用DMA方式接收
//USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_Cmd(USART3, ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);//失能串口发送DMA
}
void USART3_SentDMX()//串口发送数据
{
uint16_t delay = 150;
uint16_t i;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB , GPIO_Pin_10);
while(delay--);
delay = 20;
GPIO_SetBits(GPIOB, GPIO_Pin_10);
while(delay--);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_Cmd(USART3, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
// for(i=0;i<512;i++)
// {
// USART3->TDR = RxBuffer;
// while((USART3->ISR&0X80)==0);
// }
// while((USART3->ISR & 0x40) == 0);
// USART3->ISR =0;
}
void DMA1_Channel4_5_IRQHandler(void)//DMA1_Channel4_5_IRQn
{
if(DMA_GetITStatus(DMA1_IT_TC4)==SET) //串口 1 发送完数 据
{
DMA_ClearFlag(DMA1_IT_TC4);
DMA_Cmd(DMA1_Channel4, DISABLE); //不使能 DMA 对于 USART1 的发 送功能
}
}
|
|