FreeModbus移植經驗分享
阿新 • • 發佈:2018-12-19
- /**
- * @brief 串列埠初始化
- * @param ucPORT 串列埠號
- * ulBaudRate 波特率
- * ucDataBits 資料位
- * eParity 校驗位
- * @retval None
- */
- BOOL
- xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
- {
- (void)ucPORT; //不修改串列埠
- (void)ucDataBits; //不修改資料位長度
- (void)eParity; //不修改校驗格式
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- //使能USART1,GPIOA
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
- RCC_APB2Periph_USART1, ENABLE);
- //GPIOA9 USART1_Tx
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推輓輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //GPIOA.10 USART1_Rx
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮動輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = ulBaudRate; //只修改波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- 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(USART1, &USART_InitStructure);
- //使能USART1
- USART_Cmd(USART1, ENABLE);
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- //設定USART1 中斷優先順序
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- //最後配置485傳送和接收模式
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
- //GPIOD.8
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- return TRUE;
- }