STM32F10x 学习笔记8(USART实现串口通讯 DMA 方式)
时间:11-20
来源:互联网
点击:
- te=9600;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- 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(USART2,&USART_InitStructure);
- USART_Cmd(USART2,ENABLE);
- }
- voidUART2_TX_DMA_Init(uint8_t*p_str,uint16_tcnt)
- {
- //DMA_InitTypeDefDMA_InitStructure;
- //DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&(USART2->DR);
- //DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)str;
- //DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
- //DMA_InitStructure.DMA_BufferSize=14;
- //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_Low;
- //DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
- DMA1_Channel7->CMAR=(uint32_t)p_str;
- DMA1_Channel7->CNDTR=cnt;
- DMA1_Channel7->CCR=DMA_DIR_PeripheralDST|DMA_Priority_Low|
- DMA_Mode_Normal|DMA_PeripheralInc_Disable|
- DMA_MemoryInc_Enable|DMA_PeripheralDataSize_Byte|
- DMA_MemoryDataSize_Byte|DMA_M2M_Disable;
- }
- uint8_tstr[]="HelloWorld!!!";
- voidTaskStart(void*pdata)
- {
- SysTick_Config(SystemCoreClock/10);
- USART2_Init();
- UART2_TX_DMA_Init(str,14);
- for(;;)
- {
- LED_Spark();
- //DMA_Cmd(DMA1_Channel7,DISABLE);
- DMA1_Channel7->CCR&=(uint16_t)(~DMA_CCR1_EN);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CNDTR=14;
- //DMA_Cmd(DMA1_Channel7,ENABLE);
- DMA1_Channel7->CCR|=DMA_CCR1_EN;
- //USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
- OSTimeDly(10);
- }
- }
- intmain(void)
- {
- SystemInit();
- LED_Init();
- OSInit();
- OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
- OSStart();
- for(;;)
- {
- }
- }
下面再说说如何在DMA传输完成之后产生中断。当传输一半的数据后,半传输标志(HTIF)被置1,当设置了允许半传输中断位(HTIE)时,将产生一个中断请求。在数据传输结束后,传输完成标志(TCIF)被置1,当设置了允许传输完成中断位(TCIE)时,将产生一个中断请求。
DMA的CCR寄存器中有1位TCIE(Transfercompleteinterruptenable)
该位由软件设置和清除。
0:禁止TC中断
1:允许TC中断
所以为了使用DMA中断,我们需要下面的代码:
- DMA1_Channel7->CCR|=DMA_IT_TC;//Transfercompleteinterruptenable
- NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
下面还是给个简单的示例程序,示例程序中还用到了uCOS的信号量,中断处理函数通过信号量通知Task完成了DMA传输:
- #include"stm32f10x.h"
- #include"uart.h"
- #include"led.h"
- #include"COMMRTOS.H"
- #include"ucos_ii.h"
- #defineTASK_STK_SIZE128
- OS_STKTaskStartStk[TASK_STK_SIZE];
- voidUSART2_Init(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- USART_InitTypeDefUSART_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConfigureUSARTRxasinputfloating*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USAR
STM32F10xUSART串口通讯DMA方 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)