(完整word版)STM32的485最简单例程.pdf

上传人:Q****o 文档编号:55052001 上传时间:2022-10-29 格式:PDF 页数:7 大小:15.29KB
返回 下载 相关 举报
(完整word版)STM32的485最简单例程.pdf_第1页
第1页 / 共7页
(完整word版)STM32的485最简单例程.pdf_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《(完整word版)STM32的485最简单例程.pdf》由会员分享,可在线阅读,更多相关《(完整word版)STM32的485最简单例程.pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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

10、(USART3,USART_FLAG_RXNE);TIM3_Init(1000,7200);BLUE_ON();if(cnt=32)cnt=0;elsedatacnt=USART_ReceiveData(USART3);cnt+;if(USART_GetITStatus(USART3,USART_IT_TC)!=RESET)|(USART_GetITStatus(USART3,USART_IT_TXE)!=RESET)USART_ClearITPendingBit(USART3,USART_IT_TC);USART_ClearITPendingBit(USART3,USART_IT_TXE);

11、/USART_SendData(USART3,0XFFFF);void TIM3_IRQHandler(void)if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)TIM_ClearITPendingBit(TIM3,TIM_IT_Update);TIM3_DeInit();BLUE_OFF();/printf(rn%x,cnt);cnt=0;static void NVIC_TIM3Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;/*Set the Vector Table base ad

12、dress at 0 x08000000*/NVIC_SetVectorTable(NVIC_VectTab_FLASH,0 x0000);/*Enable the TIM3 gloabal Interrupt*/NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

13、NVIC_Init(&NVIC_InitStructure);定时器:void TIM3_Init(uint16_t tcon,uint16_t psc)TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/TIM3 clock enableRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);/reset timerTIM_DeInit(TIM3);/Time base configuration/value to reloadTIM_TimeBaseStructure.TIM_Period=(tcon-

14、1);/pre-scaler value against system clockTIM_TimeBaseStructure.TIM_Prescaler=(psc-1);/scaler value against system clockTIM_TimeBaseStructure.TIM_ClockDivision=0;/counting modeTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;/init timerTIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);/Clear TIM3

15、 update pending flagTIM_ClearITPendingBit(TIM3,TIM_IT_Update);/TIM IT enable of over-countingTIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);/config timers interruption functionNVIC_TIM3Configuration();/Enable TIM3 counterTIM_Cmd(TIM3,ENABLE);void TIM3_DeInit(void)/Disable TIM3 counterTIM_Cmd(TIM3,DISABLE);

16、/Reset timerTIM_DeInit(TIM3);/Disable TIM3 clockRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,DISABLE);/tim3_stat=0 x00;主函数:#ifdef _GNF_#ifdef _GNUC_/*With GCC/RAISONANCE,small printf(option LD Linker-Libraries-Small printfset to Yes)calls _io_putchar()*/#define PUTCHAR_PROTOTYPE int _io_putchar(int ch

17、)#else#define PUTCHAR_PROTOTYPE int fputc(int ch,FILE*f)#endif/*_GNUC_*/#endif/*_GNF_*/void Delay(uint32_t tick)while(tick-);/*Private function prototypes-*/extern unsigned char datasize;extern volatile unsigned char cnt;/*Function Name:main*Description:Main program*Input:None*Output:None*Return:Non

18、e*Attention:None*/int main(void)/*USART configuration*/USART3_Config();RED_ON();BLUE_ON();GREEN_ON();Delay(100000);BLUE_OFF();RED_OFF();GREEN_OFF();/*Infinite loop*/while(1)uint8_t i=0;RS_485_TX_ENABLE();printf(rn up up day!);for(i=0;i50;i+)GREEN_ON();Delay(500000);GREEN_OFF();Delay(500000);RS_485_RX_ENABLE();while(data0=0 x00)RED_ON();Delay(500000);RED_OFF();Delay(500000);memset(data,0,size);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁