123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include "myUart3.h"
- #include "stm32f10x.h"
- #include "stm32f10x_usart.h"
- #include "stm32f10x_gpio.h"
- #include "misc.h"
- #include <stdarg.h>
- #include <string.h>
- static uint16_t USART_RX_STA; //接收状态标记
- static UART_CALLBACK uartRxCallBack;
- static uint8_t USART3_RX_BUF[USART_REC_LEN];
- void USART3_IRQHandler(void) //串口1中断服务程序
- {
- uint8_t getValue;
- if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
- {
- getValue =USART_ReceiveData(USART3); //读取接收到的数据
- if (uartRxCallBack)
- {
- uartRxCallBack(USART3_RX_BUF, USART_RX_STA);
- memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
- USART_RX_STA=0;
- }
-
- }
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
- {
- getValue =USART_ReceiveData(USART3); //读取接收到的数据
- USART3_RX_BUF[USART_RX_STA]=getValue ;
- USART_RX_STA++;
- if(USART_RX_STA>(USART_REC_LEN-1))
- {
- USART_RX_STA=0;//接收数据错误,重新开始接收
- }
- }
- }
- void myUart3_init(uint32_t baudrate, UART_CALLBACK cb)
- {
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- //USART3_TX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
- //USART3_RX初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
- //Usart3 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_Init(USART3, &USART_InitStructure); //初始化串口
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//开启串口接受中断
- // USART_ITConfig(USART3, USART_IT_TC, ENABLE);// 使能串口发送完成中断
- uartRxCallBack = cb;
- USART_Cmd(USART3, ENABLE); //使能串口
- }
- static void myUart3_sendByte(uint8_t sendByte)
- {
- while(!USART_GetFlagStatus(USART3, USART_FLAG_TC))
- {
- ;
- }
- USART_SendData(USART3, sendByte);
- }
- static void myUart3_sendArray(uint8_t *buffer, uint16_t sendLen)
- {
- int i = 0;
- for (i = 0; i < sendLen; i++)
- {
- myUart3_sendByte(*buffer);
- buffer ++;
- }
-
- }
- void myUart3_printf(uint16_t sendLen, char *fmt, ...)
- {
- va_list va;
- char tempTab[256];
- int len;
- if (sendLen)
- {
- myUart3_sendArray((uint8_t *)fmt, sendLen);
- }
- else
- {
- va_start(va, fmt);
- memset(tempTab, 0, sizeof(tempTab));
- vsnprintf(tempTab, (int)sizeof(tempTab), fmt, va);
- len = strlen(tempTab);
- myUart3_sendArray((uint8_t *)tempTab, len);
- va_end(va);
- }
- }
|