STM32 上移植 FreeModbus RTU

2020-02-29 10:44发布

解压 freemodbus v1.6 源码 看到如下文件目录结构  
708085e57a1496ee2b.png



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
19条回答
stm32jy
1楼-- · 2020-03-01 16:17
 精彩回答 2  元偷偷看……
stm32jy
2楼-- · 2020-03-01 19:36
发送一个字节函数 BOOL xMBPortSerialPutByte( CHAR ucByte )
  1. BOOL
  2. xMBPortSerialPutByte( CHAR ucByte )
  3. {
  4. /* Put a byte in the UARTs transmit buffer. This function is called
  5. * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
  6. * called. */
  7. USART_SendData(USART2, ucByte); //发送一个字节
  8. return TRUE;
  9. }
复制代码



stm32jy
3楼-- · 2020-03-01 22:38
接收一个字节函数 BOOL xMBPortSerialGetByte( CHAR * pucByte )
  1. BOOL
  2. xMBPortSerialGetByte( CHAR * pucByte )
  3. {
  4. /* Return the byte in the UARTs receive buffer. This function is called
  5. * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
  6. */
  7. *pucByte = USART_ReceiveData(USART2); //接收一个字节
  8. return TRUE;
  9. }
复制代码

stm32jy
4楼-- · 2020-03-02 03:57
串口中断服务函数 void USART2_IRQHandler(void)
  1. void USART2_IRQHandler(void)
  2. {
  3. if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //接收中断
  4. {
  5. prvvUARTRxISR();USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  6. }
  7. if (USART_GetITStatus(USART2, USART_IT_TC) == SET) //发送中断
  8. {
  9. prvvUARTTxReadyISR();
  10. USART_ClearITPendingBit(USART2, USART_IT_TC);
  11. }
  12. }
复制代码



stm32jy
5楼-- · 2020-03-02 08:00
打开 porttimer.c 文件, RTU 模式需要定时器支持,定时器初始化函数
  1. BOOL
  2. xMBPortTimersInit( USHORT usTim1Timerout50us )
  3. BOOL
  4. xMBPortTimersInit( USHORT usTim1Timerout50us )
  5. {
  6. // return FALSE;
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. uint16_t PrescalerValue = 0;
  10. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  11. //
  12. //HCLK 为 72MHz
  13. //时基频率 72 / (1 + Prescaler) = 20KHz
  14. //
  15. PrescalerValue = (uint16_t)((SystemCoreClock / 20000) - 1);
  16. //
  17. //初始化定时器参数
  18. //
  19. TIM_TimeBaseStructure.TIM_Period = (uint16_t)usTim1Timerout50us;
  20. TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  21. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  22. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  23. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  24. //
  25. //使能预装
  26. //
  27. TIM_ARRPreloadConfig(TIM2, ENABLE);
  28. //
  29. //初始化中断优先级//
  30. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  31. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  32. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  33. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  34. NVIC_Init(&NVIC_InitStructure);
  35. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  36. TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
  37. TIM_Cmd(TIM2, DISABLE);
  38. return TRUE;
  39. }
复制代码



stm32jy
6楼-- · 2020-03-02 09:24
 精彩回答 2  元偷偷看……

一周热门 更多>