完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
一直在用STM32F103做项目,记录一下,如果觉得有用的,也可以参考一下。
步骤
`/********************************************************************************************************* ** Function name: RS485_IO_Init ** Descriptions: usart GPIO 初始化 ** input parameters: void ** output parameters: void ** Returned value: void *********************************************************************************************************/ void RS485_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(USART_RCC | RCC_APB2Periph_AFIO,ENABLE); //串口2配置 //output GPIO_InitStructure.GPIO_Pin = USART2_OUTIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ; GPIO_Init(USART2_PORT,&GPIO_InitStructure); //Input GPIO_InitStructure.GPIO_Pin = USART2_INIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; GPIO_Init(USART2_PORT,&GPIO_InitStructure); //485RE GPIO_InitStructure.GPIO_Pin = USART2_REIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ; GPIO_Init(USART2_PORT,&GPIO_InitStructure); //串口3配置 GPIO_InitStructure.GPIO_Pin = USART3_OUTIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ; GPIO_Init(USART3_PORT,&GPIO_InitStructure); //Input GPIO_InitStructure.GPIO_Pin = USART3_INIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; GPIO_Init(USART3_PORT,&GPIO_InitStructure); //485RE GPIO_InitStructure.GPIO_Pin = USART3_REIO ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ; GPIO_Init(USART3_PORT,&GPIO_InitStructure); RS485_2RE(1);//串口2默认为发送 RS485_3RE(0);//串口3默认为接收 } /********************************************************************************************************* ** Function name: USART_485_Init ** Descriptions: usart 配置 ** input parameters: void ** output parameters: void ** Returned value: void *********************************************************************************************************/ void USART_485_Init(void) { USART_InitTypeDef USART_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB1Periph_USART2,ENABLE); NVIC_InitTypeDef NVIC_InitStructure; /*设置串口2中断的先占优先级为0,从占优先级为0*/ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*设置串口3中断的先占优先级为0,从占优先级为1*/ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* USART2、USART3设置波特率9600、数据位8bit、停止位1bit、无校验位、无硬件流、接收发送 、中断打开*/ USART_InitStruct.USART_BaudRate = 9600; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx ; USART_Init(USART2,&USART_InitStruct); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_InitStruct.USART_BaudRate = 9600; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx ; USART_Init(USART3,&USART_InitStruct); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); /*使能串口2、3*/ USART_Cmd(USART2,ENABLE); USART_Cmd(USART3,ENABLE); } rs485.h /**************485使能开关***************/ #define RS485_2RE(x) x?(GPIOA->ODR |= (1<<4),USART2->CR1 &=~ (3<<2),USART2->CR1 |= (2<<2)):(GPIOA->ODR &=~ (1<<4),USART2->CR1 &=~ (3<<2),USART2->CR1 |= (1<<2)) #define RS485_3RE(x) x?(GPIOB->ODR |= (1<<12),USART3->CR1 &=~ (3<<2),USART3->CR1 |= (2<<2)):(GPIOB->ODR &=~ (1<<12),USART3->CR1 &=~ (3<<2),USART3->CR1 |= (1<<2)) /*************485IO定义*****************/ #define USART_RCC RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA #define USART2_OUTIO GPIO_Pin_2 #define USART2_INIO GPIO_Pin_3 #define USART2_REIO GPIO_Pin_4 #define USART2_PORT GPIOA #define USART3_OUTIO GPIO_Pin_10 #define USART3_INIO GPIO_Pin_11 #define USART3_REIO GPIO_Pin_12 #define USART3_PORT GPIOB #ifndef YES #define YES 1 #endif #ifndef NO #define NO 0 #endif 中断处理函数: void USART2_IRQHandler(void) { Usart2_Send = NO;//发送标志 if(USART_GetITStatus(USART2,USART_IT_RXNE)) { DRData.usart2_DRdata = USART2->DR;//清中断 //接下来就是事件处理 } Usart2_Send = YES; } void USART3_IRQHandler(void) { Usart3_Send = NO;//发送标志 if(USART_GetITStatus(USART3,USART_IT_RXNE)) { DRData.usart3_DRdata = USART3->DR;//清中断 //接下来就是事件处理 } Usart3_Send = YES; } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1617 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1543 浏览 1 评论
977 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
683 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1595 浏览 2 评论
1863浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
645浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
516浏览 3评论
532浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
505浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 11:21 , Processed in 0.819490 second(s), Total 78, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号