Usart1.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. void Usart1_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  8. GPIO_InitTypeDef GPIO_InitStructure;
  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. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16. GPIO_Init(GPIOA, &GPIO_InitStructure);
  17. USART_InitTypeDef USART_InitStruture;
  18. USART_InitStruture.USART_BaudRate = 115200;
  19. USART_InitStruture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  20. USART_InitStruture.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  21. USART_InitStruture.USART_Parity = USART_Parity_No;
  22. USART_InitStruture.USART_StopBits = USART_StopBits_1;
  23. USART_InitStruture.USART_WordLength = USART_WordLength_8b;
  24. USART_Init(USART1, &USART_InitStruture);
  25. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  26. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  27. NVIC_InitTypeDef NVIC_InitStructure;
  28. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  29. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  30. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
  31. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  32. NVIC_Init(&NVIC_InitStructure);
  33. USART_Cmd(USART1, ENABLE);
  34. }
  35. // 串口打印日志
  36. //void USART1_SendByte(uint8_t Byte)
  37. //{
  38. // USART_SendData(USART1, Byte);
  39. // while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  40. //}
  41. //// 打印字节
  42. //void USART1_SendArray(uint8_t * arr, uint16_t len)
  43. //{
  44. // uint16_t i;
  45. // for(i = 0; i < len; i ++)
  46. // {
  47. // USART1_SendByte(arr[i]);
  48. // }
  49. //}
  50. //// 打印字符串
  51. //void USART1_SendString(char *String)
  52. //{
  53. // uint16_t i;
  54. // for(i = 0; String[i] != '\0'; i ++)
  55. // {
  56. // USART1_SendByte(String[i]);
  57. // }
  58. //}
  59. //// 打印的缓存
  60. //char USART1_String[512];
  61. //void USART1_Printf(char *format, ...)
  62. //{
  63. // va_list arg;
  64. // va_start(arg, format);
  65. // vsprintf(USART1_String, format, arg);
  66. // va_end(arg);
  67. // USART1_SendString(USART1_String);
  68. //}