[mw_shl_code=c,true]u8 Sbuf[100]="888888888888888888888888888888";
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//DMA优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel= DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 1;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*Configure one bit for preemption priority -------------------------------- */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/*UART1 -------------------------------------------------------------------- */
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);
}
void uart1Init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
NVIC_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE); //使能USART1,GPIOA时钟
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
// USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
// USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
// USART_ClockInitStructure.USART_LastBit= USART_LastBit_Disable;
// USART_ClockInit(USART1,&USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure); //初始化串口
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART1, ENABLE); //使能串口
}
void usart1SendGeneralStr(void)
{
DMA_InitTypeDef DMA_InitStructure;
// memcpy(Sbuf, cmd, strlen((char *)cmd));
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA channel6configuration */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)Sbuf;
DMA_InitStructure.DMA_DIR =DMA_DIR_PeripheralDST; //外设作为目的地址 //DMA_DIR_PeripheralSRC; //外设作为DMA的源端
DMA_InitStructure.DMA_BufferSize =100; //BufferSize; //传输大小
DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable; //外设递增模式禁止 DMA_PeripheralInc_Enable; //外设地址增加
DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc_Enable; //内存地址自增 DMA_MemoryInc_Enable DMA_MemoryInc_Disable
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //传输方式:字节 DMA_PeripheralDataSize_Word; //字(32位)
DMA_InitStructure.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte; //内存存储方式:字节 DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode =DMA_Mode_Normal; //DMA_Mode_Normal 正常模式,只传送一次; DMA_Mode_Circular:循环模式,不停的传送;
DMA_InitStructure.DMA_Priority =DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M =DMA_M2M_Disable; //DMA_M2M_Enable ;DMA_M2M_Disable
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC, ENABLE); //传输完成则进入DMA1_Channel4中断;
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //采用DMA方式发送
DMA_Cmd(DMA1_Channel4,ENABLE);
}
//主函数中的循环
while(1)
{
usart1SendGeneralStr();
}[/mw_shl_code]
原子哥,为什么我这样只能发送一次,不会在main函数中循环不停地发送呢?找了半天还是没找出原因,谢谢。 |