微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F10x 学习笔记8(USART实现串口通讯 DMA 方式)

STM32F10x 学习笔记8(USART实现串口通讯 DMA 方式)

时间:11-20 来源:互联网 点击:

  1. te=9600;
  2. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  3. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  4. USART_InitStructure.USART_Parity=USART_Parity_No;
  5. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  6. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  7. USART_Init(USART2,&USART_InitStructure);
  8. USART_Cmd(USART2,ENABLE);
  9. }
  10. voidUART2_TX_DMA_Init(uint8_t*p_str,uint16_tcnt)
  11. {
  12. //DMA_InitTypeDefDMA_InitStructure;
  13. //DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&(USART2->DR);
  14. //DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)str;
  15. //DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
  16. //DMA_InitStructure.DMA_BufferSize=14;
  17. //DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
  18. //DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
  19. //DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
  20. //DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
  21. //DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
  22. //DMA_InitStructure.DMA_Priority=DMA_Priority_Low;
  23. //DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
  24. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  25. //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
  26. DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
  27. DMA1_Channel7->CMAR=(uint32_t)p_str;
  28. DMA1_Channel7->CNDTR=cnt;
  29. DMA1_Channel7->CCR=DMA_DIR_PeripheralDST|DMA_Priority_Low|
  30. DMA_Mode_Normal|DMA_PeripheralInc_Disable|
  31. DMA_MemoryInc_Enable|DMA_PeripheralDataSize_Byte|
  32. DMA_MemoryDataSize_Byte|DMA_M2M_Disable;
  33. }
  34. uint8_tstr[]="HelloWorld!!!";
  35. voidTaskStart(void*pdata)
  36. {
  37. SysTick_Config(SystemCoreClock/10);
  38. USART2_Init();
  39. UART2_TX_DMA_Init(str,14);
  40. for(;;)
  41. {
  42. LED_Spark();
  43. //DMA_Cmd(DMA1_Channel7,DISABLE);
  44. DMA1_Channel7->CCR&=(uint16_t)(~DMA_CCR1_EN);
  45. //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
  46. DMA1_Channel7->CNDTR=14;
  47. //DMA_Cmd(DMA1_Channel7,ENABLE);
  48. DMA1_Channel7->CCR|=DMA_CCR1_EN;
  49. //USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
  50. OSTimeDly(10);
  51. }
  52. }
  53. intmain(void)
  54. {
  55. SystemInit();
  56. LED_Init();
  57. OSInit();
  58. OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
  59. OSStart();
  60. for(;;)
  61. {
  62. }
  63. }

下面再说说如何在DMA传输完成之后产生中断。当传输一半的数据后,半传输标志(HTIF)被置1,当设置了允许半传输中断位(HTIE)时,将产生一个中断请求。在数据传输结束后,传输完成标志(TCIF)被置1,当设置了允许传输完成中断位(TCIE)时,将产生一个中断请求。

DMA的CCR寄存器中有1位TCIE(Transfercompleteinterruptenable)

该位由软件设置和清除。

0:禁止TC中断

1:允许TC中断

所以为了使用DMA中断,我们需要下面的代码:

  1. DMA1_Channel7->CCR|=DMA_IT_TC;//Transfercompleteinterruptenable

  1. NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQn;
  2. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;
  3. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  4. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  5. NVIC_Init(&NVIC_InitStructure);

下面还是给个简单的示例程序,示例程序中还用到了uCOS的信号量,中断处理函数通过信号量通知Task完成了DMA传输:

  1. #include"stm32f10x.h"
  2. #include"uart.h"
  3. #include"led.h"
  4. #include"COMMRTOS.H"
  5. #include"ucos_ii.h"
  6. #defineTASK_STK_SIZE128
  7. OS_STKTaskStartStk[TASK_STK_SIZE];
  8. voidUSART2_Init(void)
  9. {
  10. GPIO_InitTypeDefGPIO_InitStructure;
  11. USART_InitTypeDefUSART_InitStructure;
  12. NVIC_InitTypeDefNVIC_InitStructure;
  13. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
  14. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  15. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  16. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  17. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  18. GPIO_Init(GPIOD,&GPIO_InitStructure);
  19. /*ConfigureUSARTRxasinputfloating*/
  20. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  21. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
  22. GPIO_Init(GPIOD,&GPIO_InitStructure);
  23. GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
  24. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USAR

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top