(完整word版)STM32的485最简单例程.pdf
《(完整word版)STM32的485最简单例程.pdf》由会员分享,可在线阅读,更多相关《(完整word版)STM32的485最简单例程.pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、485 最基本的半双工通信配置采用 STM32F103ZET6串口 3 连接 485 芯片通信口,485 芯片的 A,B 通过 485 转串口模块与电脑相连,完成在串口软件上输入输出功能。串口 3,配置函数:void USART3_Config(void)GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFI
2、O,ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);/USART3_TX-PB10,USART3_RX-PB11GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1
3、1;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);USART_InitStructure.USART_BaudRate=115200;/1200;USART_InitStructure.USART_WordLength=USART_WordLength_8b;USART_InitStructure.USART_StopBits=USART_StopBits_1;USART_
4、InitStructure.USART_Parity=USART_Parity_No;/USART_Parity_Even;USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;USART_Init(USART3,&USART_InitStructure);NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitStruct
5、ure.NVIC_IRQChannel=USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);#ifdef PDU_USART3_DMAUSART_ITConfig(USART3,USART_IT_IDLE,ENABLE);#else/Enable the USART R
6、eceive interrupt:this interrupt is generated when the/USART3 receive data register is not empty/USART_ClearITPendingBit(USART3,USART_IT_TC);USART_ClearFlag(USART3,USART_FLAG_TC);USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);USART_ITConfig(USART3,USART_IT_TC,ENABLE);/USART_ITConfig(USART3,USART_IT_TXE,
7、ENABLE);USART_ClearFlag(USART3,USART_FLAG_TC);#endif USART_Cmd(USART3,ENABLE);USART_ClearFlag(USART3,USART_FLAG_TC);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Sp
8、eed=GPIO_Speed_50MHz;GPIO_Init(GPIOE,&GPIO_InitStructure);RS_485_TX_ENABLE();PUTCHAR_PROTOTYPE/*Place your implementation of fputc here*/*e.g.write a character to the USART*/USART_SendData(USART3,(uint8_t)ch);/USART_SendData(USART1,(uint8_t)ch);/*Loop until the end of transmission*/while(USART_GetFl
9、agStatus(USART1,USART_FLAG_TC)=RESET)while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)=RESET)return ch;中断函数:void USART3_IRQHandler(void)if(USART_GetITStatus(USART3,USART_IT_IDLE)!=RESET)USART_ClearITPendingBit(USART3,USART_IT_IDLE);if(USART_GetFlagStatus(USART3,USART_FLAG_RXNE)!=RESET)USART_ClearFlag
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 完整 word STM32 485 简单 例程
限制150内