myUart3.c 4.0 KB

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