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

STM32F10x 学习笔记7(USART实现串口通讯 3)

时间:11-20 来源:互联网 点击:
在上一篇学习笔记《STM32F10x 学习笔记6(USART实现串口通讯 2)》给出了个利用环形缓冲区的串口驱动。最近研究了uCOS-II在STM32上的移植。下面再给个利用uCOS-II的信号量的串口驱动。整个驱动的基本框架和上一篇没什么区别,所以不多介绍。直接贴代码:

整个驱动包含四个文件:

uart.h

uart.c

COMMRTOS.H

COMMRTOS.c

其中前两个文件是对串口基本功能的封装

uart.h 的代码如下:

  1. #ifndef_UART_H_
  2. #define_UART_H_
  3. voidUSART1_Init(void);
  4. voidUSART2_Init(void);
  5. voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData);
  6. voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str);
  7. uint8_tUART_GetChar(USART_TypeDef*USARTx);
  8. #endif


uart.c 的代码

  1. #include"stm32f10x.h"
  2. voidUSART1_Init(void)
  3. {
  4. GPIO_InitTypeDefGPIO_InitStructure;
  5. USART_InitTypeDefUSART_InitStructure;
  6. NVIC_InitTypeDefNVIC_InitStructure;
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
  8. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  9. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  10. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
  11. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA,&GPIO_InitStructure);
  13. /*ConfigureUSARTRxasinputfloating*/
  14. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  15. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
  16. GPIO_Init(GPIOA,&GPIO_InitStructure);
  17. USART_InitStructure.USART_BaudRate=9600;
  18. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  19. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  20. USART_InitStructure.USART_Parity=USART_Parity_No;
  21. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  22. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  23. USART_Init(USART1,&USART_InitStructure);
  24. USART_Cmd(USART1,ENABLE);
  25. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
  26. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  27. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  28. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  29. NVIC_Init(&NVIC_InitStructure);
  30. }
  31. voidUSART2_Init(void)
  32. {
  33. GPIO_InitTypeDefGPIO_InitStructure;
  34. USART_InitTypeDefUSART_InitStructure;
  35. NVIC_InitTypeDefNVIC_InitStructure;
  36. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
  37. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  38. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  39. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  40. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  41. GPIO_Init(GPIOD,&GPIO_InitStructure);
  42. /*ConfigureUSARTRxasinputfloating*/
  43. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  44. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
  45. GPIO_Init(GPIOD,&GPIO_InitStructure);
  46. GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
  47. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  48. USART_InitStructure.USART_BaudRate=9600;
  49. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  50. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  51. USART_InitStructure.USART_Parity=USART_Parity_No;
  52. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  53. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  54. USART_Init(USART2,&USART_InitStructure);
  55. USART_Cmd(USART2,ENABLE);
  56. NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
  57. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  58. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  59. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  60. NVIC_Init(&NVIC_InitStructure);
  61. }
  62. voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData)
  63. {
  64. //while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET){};
  65. while((USARTx->SR&USART_FLAG_TXE)==0x00){};
  66. //USART_SendData(USARTx,Data);
  67. USARTx->DR=Data;
  68. }
  69. voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str)
  70. {
  71. while(0!=*str)
  72. {
  73. UART_PutChar(USARTx,*str);
  74. str++;
  75. }
  76. }
  77. uint8_tUART_GetChar(USART_TypeDef*USARTx)
  78. {
  79. //while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==RESET){};
  80. while((USARTx->SR&USART_FLAG_RXNE)==0x00){};
  81. //returnUSART_ReceiveData(US

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

网站地图

Top