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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "USART2.h"
  2. void USART2_Init(u32 bound)
  3. {
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. USART_InitTypeDef USART_InitStructure;
  6. #if USART2_EN
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);//GPIOAʱʹ
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //USART2ʱʹ
  9. //USART2_TX GPIOA2
  10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
  11. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  13. GPIO_Init(GPIOA, &GPIO_InitStructure);
  14. //USART2_RX GPIOA3
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
  16. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  17. GPIO_Init(GPIOA, &GPIO_InitStructure);
  18. //USART
  19. USART_InitStructure.USART_BaudRate = bound;//
  20. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8λ
  21. USART_InitStructure.USART_StopBits = USART_StopBits_1;//1ֹͣλ
  22. USART_InitStructure.USART_Parity = USART_Parity_No;//У
  23. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//Ӳݿ
  24. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //շģʽ
  25. USART_Init(USART2, &USART_InitStructure); //ʼ2
  26. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ʼڽж
  27. USART_Cmd(USART2, ENABLE);
  28. #endif
  29. }
  30. //-------------------------------------------------
  31. //char SendUartByte (u16 Sendbyte,USART_TypeDef* Uart)
  32. //{ // send a byte down a uart
  33. // while(USART_GetFlagStatus(Uart, USART_FLAG_TXE) == RESET) //wait for send out
  34. // {
  35. // }
  36. // USART_SendData(Uart,Sendbyte);
  37. // return 1;
  38. //}
  39. //char SendUartStr(const char *s, USART_TypeDef* Uart)
  40. //{ // send a string from rom
  41. // char c;
  42. // if (!s) return 1;
  43. // for (;*s;)
  44. // {
  45. // c = *s++;
  46. // if (c == '\n')
  47. // { // insert a CR (\r) before the LF (\n)
  48. // if (!SendUartByte('\r', Uart)) return 0; // Send byte
  49. // }
  50. // if (!SendUartByte(c, Uart)) return 0; // Send byte
  51. // }
  52. // return 1;
  53. //}
  54. //char SendUartData(char *s, u16 len, USART_TypeDef* Uart)
  55. //{ // Send data down a uart
  56. // char c;
  57. // if (!s) return 1;
  58. // for (; len; len--)
  59. // {
  60. // c = *s++;
  61. // if (!SendUartByte(c, Uart)) return 0; // Send byte
  62. // }
  63. // return 1;
  64. //}
  65. //void SendUSART1Data(u8 *d, u16 len)
  66. //{ // send data from ram
  67. // SendUartData(d, len,USART1);
  68. //}
  69. //void SendUSART2Data(u8 *d, u16 len)
  70. //{ // send data from ram
  71. // SendUartData(d, len,USART2);
  72. //}
  73. //void SendUSART2Str(char *d)
  74. //{
  75. // SendUartStr(d,USART2);
  76. //}
  77. #if EN_USART2_RX
  78. u8 USART2_RX_BUF[USART2_REC_LEN];
  79. u8 USART2_TX_CNT; //ͼ
  80. u8 USART2_RX_CNT; //ռ
  81. u16 USART2_REC_Status; //ɱ־λ
  82. #endif
  83. void USART2_IRQHandler(void)
  84. {
  85. u8 res;
  86. if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
  87. {
  88. USART_ClearITPendingBit(USART2,USART_IT_RXNE); //жϱ־
  89. res = USART_ReceiveData(USART2);
  90. USART2_RX_BUF[USART2_RX_CNT] = res;
  91. USART2_RX_CNT ++;
  92. }
  93. if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET) //
  94. {
  95. USART_ClearFlag(USART2,USART_FLAG_ORE); //SR
  96. USART_ReceiveData(USART2); //DR
  97. }
  98. }