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

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

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

  1. T2,ENABLE);
  2. USART_InitStructure.USART_BaudRate=9600;
  3. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  4. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  5. USART_InitStructure.USART_Parity=USART_Parity_No;
  6. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  7. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  8. USART_Init(USART2,&USART_InitStructure);
  9. USART_Cmd(USART2,ENABLE);
  10. NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
  11. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  12. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  13. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  14. NVIC_Init(&NVIC_InitStructure);
  15. }
  16. voidUART2_TX_DMA_Init(uint8_t*p_str,uint16_tcnt)
  17. {
  18. //DMA_InitTypeDefDMA_InitStructure;
  19. //DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&(USART2->DR);
  20. //DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)str;
  21. //DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
  22. //DMA_InitStructure.DMA_BufferSize=14;
  23. //DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
  24. //DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
  25. //DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
  26. //DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
  27. //DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
  28. //DMA_InitStructure.DMA_Priority=DMA_Priority_Low;
  29. //DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
  30. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  31. //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
  32. DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
  33. DMA1_Channel7->CMAR=(uint32_t)p_str;
  34. DMA1_Channel7->CNDTR=cnt;
  35. DMA1_Channel7->CCR=DMA_DIR_PeripheralDST|DMA_Priority_Low|
  36. DMA_Mode_Normal|DMA_PeripheralInc_Disable|
  37. DMA_MemoryInc_Enable|DMA_PeripheralDataSize_Byte|
  38. DMA_MemoryDataSize_Byte|DMA_M2M_Disable;
  39. }
  40. OS_EVENT*UART2_DMA_TX_Sem;
  41. uint8_tstr[]="HelloWorld!!!";
  42. voidTaskStart(void*pdata)
  43. {
  44. unsignedcharerr;
  45. SysTick_Config(SystemCoreClock/10);
  46. USART2_Init();
  47. USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
  48. UART2_TX_DMA_Init(str);
  49. UART2_DMA_TX_Sem=OSSemCreate(1);
  50. for(;;)
  51. {
  52. LED_Spark();
  53. OSSemPend(UART2_DMA_TX_Sem,0,&err);
  54. //DMA_Cmd(DMA1_Channel7,DISABLE);
  55. DMA1_Channel7->CCR&=(uint16_t)(~DMA_CCR1_EN);
  56. //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
  57. DMA1_Channel7->CNDTR=14;
  58. //DMA_Cmd(DMA1_Channel7,ENABLE);
  59. DMA1_Channel7->CCR|=DMA_CCR1_EN;
  60. //USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
  61. OSTimeDly(10);
  62. }
  63. }
  64. intmain(void)
  65. {
  66. SystemInit();
  67. LED_Init();
  68. OSInit();
  69. OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
  70. OSStart();
  71. for(;;)
  72. {
  73. }
  74. }
  75. voidDMA1_Channel7_IRQHandler(void)
  76. {
  77. OS_CPU_SRcpu_sr;
  78. OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
  79. OSIntNesting++;
  80. OS_EXIT_CRITICAL();
  81. OSSemPost(UART2_DMA_TX_Sem);
  82. //UART_PutChar(USART2,+);
  83. DMA1->IFCR=DMA1_FLAG_TC7;
  84. OSIntExit();
  85. }


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

网站地图

Top