You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.2 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
  1. #include "uart0.h"
  2. void uart0_init(void) {
  3. /**
  4. * @brief 0115200
  5. * Fpclk
  6. *
  7. */
  8. GPIO_InitSettingType InitSet;
  9. InitSet.Dir = GPIO_Direction_Output;
  10. InitSet.DS = GPIO_DS_Output_Strong;
  11. InitSet.Func = GPIO_Reuse_Func2;
  12. InitSet.ODE = GPIO_ODE_Output_Disable;
  13. InitSet.PDE = GPIO_PDE_Input_Disable;
  14. InitSet.PUE = GPIO_PUE_Input_Disable;
  15. InitSet.Signal = GPIO_Pin_Signal_Digital;
  16. GPIO_Init(UART0_TXD0_PIN, &InitSet);
  17. InitSet.Dir = GPIO_Direction_Input;
  18. InitSet.DS = GPIO_DS_Output_Strong;
  19. InitSet.Func = GPIO_Reuse_Func2;
  20. InitSet.ODE = GPIO_ODE_Output_Disable;
  21. InitSet.PDE = GPIO_PDE_Input_Disable;
  22. InitSet.PUE = GPIO_PUE_Input_Disable;
  23. InitSet.Signal = GPIO_Pin_Signal_Digital;
  24. GPIO_Init(UART0_RXD0_PIN, &InitSet);
  25. UART_InitStruType UART_InitStruct;
  26. UART_InitStruct.UART_BaudRate = 115200; //波特率
  27. UART_InitStruct.UART_ClockSet = UART_Clock_1; //时钟选择不分频
  28. UART_InitStruct.UART_RxMode = UART_DataMode_8; // 8数据位无奇偶校验位
  29. //标准极性端口数据和传输数据相同
  30. UART_InitStruct.UART_RxPolar = UART_Polar_Normal;
  31. UART_InitStruct.UART_StopBits = UART_StopBits_1; //一个停止位
  32. UART_InitStruct.UART_TxMode = UART_DataMode_8; // 8数据位无奇偶校验位
  33. UART_InitStruct.UART_TxPolar = UART_Polar_Normal; //标准usart极性
  34. UART_Init(UART0, &UART_InitStruct);
  35. UART_ITConfig(UART0, UART_IT_RB, Enable); /* UART0接收中断使能 */
  36. /* UART0发送缓冲区空中断模式: 全空中断 */
  37. UART_TBIMConfig(UART0, UART_TBIM_Byte);
  38. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  39. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  40. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  41. NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_1, Enable); //使能串口0中断
  42. UART0_TxEnable();
  43. UART0_RxEnable();
  44. }
  45. void fun(void) {}
  46. void UART0_IRQHandler(void) {
  47. /**
  48. * @brief
  49. *
  50. */
  51. // int i;
  52. // for (i = 0; i < 100; i++) {
  53. // }
  54. // fun();
  55. // i++;
  56. // return;
  57. uint8_t uart0_data;
  58. uart0_data = UART_RecByte(UART0);
  59. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  60. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  61. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  62. // UART0->IF.Word = (uint32_t)0xffffffff;
  63. // UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  64. // UART_ClearITPendingBit(UART0, UART_FLAG_TC);
  65. // UART_ClearITPendingBit(UART0, UART_FLAG_TBWE);
  66. // UART_ClearITPendingBit(UART0, UART_FLAG_TBWO);
  67. // UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  68. // UART_ClearITPendingBit(UART0, UART_FLAG_ID);
  69. // UART_ClearITPendingBit(UART0, UART_FLAG_RO);
  70. // UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  71. // UART_ClearITPendingBit(UART0, UART_FLAG_PE);
  72. // UART_ClearITPendingBit(UART0, UART_FLAG_BDE);
  73. // UART_ClearITPendingBit(UART0, UART_FLAG_RBRE);
  74. // UART_ClearITPendingBit(UART0, UART_FLAG_RBRO);
  75. // if (UART_GetFlagStatus(UART0, UART_FLAG_TB) == SET) {
  76. // fun();
  77. // }
  78. // if (UART_GetFlagStatus(UART0, UART_FLAG_TC) == SET) {
  79. // fun();
  80. // }
  81. // if (UART_GetFlagStatus(UART0, UART_FLAG_TBWE) == SET) {
  82. // fun();
  83. // }
  84. // if (UART_GetFlagStatus(UART0, UART_FLAG_TBWO) == SET) {
  85. // fun();
  86. // }
  87. // if (UART_GetFlagStatus(UART0, UART_FLAG_RB) == SET) {
  88. // fun();
  89. // }
  90. // if (UART_GetFlagStatus(UART0, UART_FLAG_ID) == SET) {
  91. // fun();
  92. // }
  93. // if (UART_GetFlagStatus(UART0, UART_FLAG_RO) == SET) {
  94. // fun();
  95. // }
  96. // if (UART_GetFlagStatus(UART0, UART_FLAG_FE) == SET) {
  97. // fun();
  98. // }
  99. // if (UART_GetFlagStatus(UART0, UART_FLAG_PE) == SET) {
  100. // fun();
  101. // }
  102. // if (UART_GetFlagStatus(UART0, UART_FLAG_BDE) == SET) {
  103. // fun();
  104. // }
  105. // if (UART_GetFlagStatus(UART0, UART_FLAG_RBRE) == SET) {
  106. // fun();
  107. // }
  108. // if (UART_GetFlagStatus(UART0, UART_FLAG_RBRO) == SET) {
  109. // fun();
  110. // }
  111. // UART_SendByte(UART0, uart0_data);
  112. }
  113. void Uart0SendBuff(uint8_t *buff) {
  114. while (*buff != '\0') {
  115. // UART_SendByte(UART0, *buff);
  116. while (UART_GetFlagStatus(UART0, UART_FLAG_TC) == RESET)
  117. ;
  118. UART_ClearITPendingBit(UART0, UART_FLAG_TC);
  119. buff++;
  120. }
  121. }