完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
写在前面外部定义串口接收回调 当串口数据接收完成后,在该回调中处理串口数据即可 void Uart_recvCallBack(void *buf, int len) { } 外部初始化串口 myUart_initialize(115200, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, Uart_recvCallBack); 二、源码 myUart.h #ifndef __myUart_H #define __myUart_H #include "stm32f0xx.h" #include "stm32f0xx_usart.h" #include "stm32f0xx_gpio.h" #include "stm32f0xx_rcc.h" #include "stm32f0xx_misc.h" #define USART_REC_LEN 200 //定义最大接收字节数 typedef void (*myUart_cb)(void *buf, int len); void myUart_initialize( uint32_t USART_BaudRate, uint32_t USART_WordLength, uint32_t USART_StopBits, uint32_t USART_Parity, myUart_cb cb); void myUart_setBaudRate(uint32_t USART_BaudRate); void myUart_sendByte(uint8_t byte); void myUart_sendBytes(uint8_t *Buffer, uint32_t Length); uint8_t myUart_recvByte(void); int fputc(int ch, FILE *f); #endif /* __UART_H */ myUart.c #include "myUart.h" #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) uint8_t USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. uint16_t USART_RX_count = 0; //接收状态标记 myUart_cb myUart_callBack; /* Private functions ---------------------------------------------------------*/ USART_InitTypeDef USART_InitStructure; void myUart_initialize( uint32_t USART_BaudRate, uint32_t USART_WordLength, uint32_t USART_StopBits, uint32_t USART_Parity, myUart_cb cb) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE ); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1); /* * USART1_TX -> PA9 , USART1_RX -> PA10 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority=1 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 USART_InitStructure.USART_BaudRate = USART_BaudRate;//设置串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength;//设置数据位 USART_InitStructure.USART_StopBits = USART_StopBits;//设置停止位 USART_InitStructure.USART_Parity = USART_Parity;//设置效验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置工作模式 USART_Init(USART1, &USART_InitStructure); //配置入结构体 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接受超时中断 USART_Cmd(USART1, ENABLE);//使能串口1 myUart_callBack = cb; } void myUart_setBaudRate(uint32_t USART_BaudRate) { USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); USART_Cmd(USART1, DISABLE); USART_InitStructure.USART_BaudRate = USART_BaudRate;//设置串口波特率 USART_Init(USART1, &USART_InitStructure); //配置入结构体 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接收中断 USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接收超时中断 USART_Cmd(USART1, ENABLE);//使能串口1 } void myUart_sendByte(uint8_t byte) //发送1字节数据 { while(!((USART1->ISR)&(1<<7))); USART1->TDR=byte; } void myUart_sendBytes(uint8_t *Buffer, uint32_t Length) { while(Length != 0) { while(!((USART1->ISR)&(1<<7)));//等待发送完 USART1->TDR= *Buffer; Buffer++; Length--; } } uint8_t myUart_recvByte(void) { while(!(USART1->ISR & (1<<5)));//等待接收到数据 return(USART1->RDR); //读出数据 } PUTCHAR_PROTOTYPE { /* 将Printf内容发往串口 */ USART_SendData(USART1,(uint8_t) ch); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {} return (ch); } void USART1_IRQHandler(void) //串口1中断服务程序 { uint8_t Res; if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_IDLE);//USART_IT_IDLE需要手动清除 if (USART_RX_count) { if (myUart_callBack) { myUart_callBack(USART_RX_BUF, USART_RX_count); } } USART_RX_count = 0; } if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断 { USART_RX_BUF[USART_RX_count] = USART_ReceiveData(USART1); //读取接收到的数据,自动清除USART_IT_RXNE中断标志 USART_RX_count ++; } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1771 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1619 浏览 1 评论
1070 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
724 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1673 浏览 2 评论
1936浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
729浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
569浏览 3评论
594浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
552浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 23:29 , Processed in 0.612888 second(s), Total 44, Slave 37 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号