代码
#include "sys.h" #include "485.h" #include "delay.h" #include "modbus.h" void RS485_Init(u32 bound){ //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO时钟 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2时钟 // GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);//调用重映射函数 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //LED0-->PA.8 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD,GPIO_Pin_3); // //USART1_TX GPIO // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //P // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 // GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO // // //USART1_RX GPIOA初始化 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 // GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO //USART2_TX PA.2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //USART2_RX PA.3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_Cmd(USART2, ENABLE); //使能串口1 RS485_TX_EN=0; //默认为接收模式 } /***********************************************/ u8 RS485_8bit_PutChar_send(u8 ch) { RS485_TX_EN=1; USART_SendData(USART2, (u8) ch); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) { } RS485_TX_EN=0; return ch; } void RS485_8bit_PutString_send(u8* buf , u16 len) { u8 i; // RS485_TX_EN=1; for(i=0;i<len;i++) { RS485_8bit_PutChar_send(*buf++); } // RS485_TX_EN=0; } u16 RS485_16bit_PutChar_send(u16 ch) { /* Write a character to the USART */ USART_SendData(USART2, (u8) ch); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) { } return ch; } void RS485_16bit_PutString_send(u16* buf , u16 len) { u8 i; for(i=0;i<len;i++) { RS485_16bit_PutChar_send(*buf++); } } void RS485_byte(u8 d) //485发送一个字节 { // RS485_RT_1; //?? USART_SendData(USART2, d); while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); USART_ClearFlag(USART2,USART_FLAG_TC ); // RS485_RT_0; //? } void USART2_IRQHandler(void) //串口1中断服务程序 { u8 st,sbuf; if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)//注意!不能使用if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)来判断 { sbuf=USART_ReceiveData(USART2); } st=USART_GetITStatus(USART2, USART_IT_RXNE); if(st==SET) // { sbuf=USART2->DR; if( modbus.reflag==1) //有数据包正在处理 { return ; } modbus.rcbuf[modbus.recount++]=sbuf; modbus.timout=0; if(modbus.recount==1) //收到主机发来的一帧数据的第一字节 { modbus.timrun=1; //启动定时 } USART_ClearITPendingBit(USART2,USART_IT_RXNE); } }485.c
标签:NVIC,通讯,USART,modbus,InitStructure,Mode,串口,GPIO,USART2 From: https://www.cnblogs.com/zhouyuqing1024/p/17903323.html