完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
1.程序设计
USART3 初始化函数代码如下: /** * @name RS485_Configuration(uint32_t baudrate, uint16_t wordlength, * uint16_t stopbits, uint16_t Parity) * @brief Configure RS485(Usart2) clock, gpio and nvic: * RS485_TX USART2_TX PB10 * RS485_RX USART2_RX PB11 * RS485_RE PC4 * RS485_DE PC5 * @param baudrate 9600 115200 * wordlength USART_WordLength_8b/USART_WordLength_9b * stopbits USART_StopBits_1/USART_StopBits_2 * parity USART_Parity_No/USART_Parity_Even * @retval None */ void RS485_Configuration(uint32_t baudrate, uint16_t wordlength, uint16_t stopbits, uint16_t parity) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3,ENABLE); // Start Reset USART2 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3,DISABLE); // Stop Reset USART2 USART_InitStructure.USART_BaudRate = baudrate; USART_InitStructure.USART_WordLength = wordlength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 开启接收中断(字节中断) USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); // 开启空闲中断( 帧中断) USART_Cmd(USART3, ENABLE); RS485_TX_DISABLE; // Disable Tx Mode } USART3 中断函数代码如下: uint8_t USART3_RX_BUF[80]; // 缓存最大80个字节,第1个字节为接收帧字节数量 /** * @name USART3_IRQHandler * @brief This function handles USART3 Handler * @param None * @retval None */ void USART3_IRQHandler(void) { uint8_t res; uint8_t clear = clear; // 清除空闲中断标志位标志 static uint8_t Rx_Sta = 1; // 计数器 if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) // 接收到字节 { res = USART3->DR; // 读取接收到的字节,清除接收中断标志位 USART3_RX_BUF[Rx_Sta++] = res; // 缓存接收到的字节 } else if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) // 检测到空闲 { clear = USART3->SR; clear = USART3->DR; // 清除空闲中断标志位(请参照芯片参考手册) USART3_RX_BUF[0] = Rx_Sta - 1; // 一帧字节数计算 Rx_Sta = 1; } } USART3_RX_BUF为接收缓存区,定义为80个字节,第一个字节USART3_RX_BUF[0]为接收到的字节个数,后面为接收到的数据。
|
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1537 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1490 浏览 1 评论
910 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
654 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1538 浏览 2 评论
1843浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
593浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
496浏览 3评论
494浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
475浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-5 05:58 , Processed in 0.390755 second(s), Total 44, Slave 36 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号