myUart3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "myUart3.h"
  2. #include "stm32f10x.h"
  3. #include "stm32f10x_usart.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "misc.h"
  6. #include <stdarg.h>
  7. #include <string.h>
  8. static uint16_t USART_RX_STA; //接收状态标记
  9. static UART_CALLBACK uartRxCallBack;
  10. static uint8_t USART3_RX_BUF[USART_REC_LEN];
  11. void USART3_IRQHandler(void) //串口1中断服务程序
  12. {
  13. uint8_t getValue;
  14. if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
  15. {
  16. getValue =USART_ReceiveData(USART3); //读取接收到的数据
  17. if (uartRxCallBack)
  18. {
  19. uartRxCallBack(USART3_RX_BUF, USART_RX_STA);
  20. memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
  21. USART_RX_STA=0;
  22. }
  23. }
  24. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  25. {
  26. getValue =USART_ReceiveData(USART3); //读取接收到的数据
  27. USART3_RX_BUF[USART_RX_STA]=getValue ;
  28. USART_RX_STA++;
  29. if(USART_RX_STA>(USART_REC_LEN-1))
  30. {
  31. USART_RX_STA=0;//接收数据错误,重新开始接收
  32. }
  33. }
  34. }
  35. void myUart3_init(uint32_t baudrate, UART_CALLBACK cb)
  36. {
  37. //GPIO端口设置
  38. GPIO_InitTypeDef GPIO_InitStructure;
  39. USART_InitTypeDef USART_InitStructure;
  40. NVIC_InitTypeDef NVIC_InitStructure;
  41. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  42. //USART3_TX
  43. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //
  44. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  46. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  47. //USART3_RX初始化
  48. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  50. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  51. //Usart3 NVIC 配置
  52. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  53. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  54. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  55. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  56. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  57. //USART 初始化设置
  58. USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
  59. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  60. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  61. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  62. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  63. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  64. USART_Init(USART3, &USART_InitStructure); //初始化串口
  65. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
  66. USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//开启串口接受中断
  67. // USART_ITConfig(USART3, USART_IT_TC, ENABLE);// 使能串口发送完成中断
  68. uartRxCallBack = cb;
  69. USART_Cmd(USART3, ENABLE); //使能串口
  70. }
  71. static void myUart3_sendByte(uint8_t sendByte)
  72. {
  73. while(!USART_GetFlagStatus(USART3, USART_FLAG_TC))
  74. {
  75. ;
  76. }
  77. USART_SendData(USART3, sendByte);
  78. }
  79. static void myUart3_sendArray(uint8_t *buffer, uint16_t sendLen)
  80. {
  81. int i = 0;
  82. for (i = 0; i < sendLen; i++)
  83. {
  84. myUart3_sendByte(*buffer);
  85. buffer ++;
  86. }
  87. }
  88. void myUart3_printf(uint16_t sendLen, char *fmt, ...)
  89. {
  90. va_list va;
  91. char tempTab[256];
  92. int len;
  93. if (sendLen)
  94. {
  95. myUart3_sendArray((uint8_t *)fmt, sendLen);
  96. }
  97. else
  98. {
  99. va_start(va, fmt);
  100. memset(tempTab, 0, sizeof(tempTab));
  101. vsnprintf(tempTab, (int)sizeof(tempTab), fmt, va);
  102. len = strlen(tempTab);
  103. myUart3_sendArray((uint8_t *)tempTab, len);
  104. va_end(va);
  105. }
  106. }