標(biāo)題: stm32的modbus協(xié)議測(cè)試源碼 [打印本頁(yè)]

作者: xsff    時(shí)間: 2018-4-30 11:31
標(biāo)題: stm32的modbus協(xié)議測(cè)試源碼
stm32 的modbus協(xié)議測(cè)試源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @文件   :MODBUS RTU通信協(xié)議  
  4.   * @作者   :藍(lán)照電子科技
  5.   * @版本   :LZKJ_V2.0
  6.   * @概要   : 通信口USART1(PA9\PA10)  9600,8,0,1
  7.   ******************************************************************************
  8.   */  

  9. /* 頭文件                --------------------------------------------------------------*/
  10. #include <stdio.h>
  11. #include "stm32f10x.h"
  12. #include "mb.h"

  13. /* 私有數(shù)據(jù)類型 --------------------------------------------------------------*/
  14. /* 私有定義         --------------------------------------------------------------*/
  15. /* 私有宏定義         --------------------------------------------------------------*/
  16. #ifdef __GNUC__
  17. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  18. #else
  19. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  20. #endif /* __GNUC__ */
  21. /* 私有變量         --------------------------------------------------------------*/
  22. /* 私有函數(shù)聲明 --------------------------------------------------------------*/
  23. void LED_Config(void)
  24. {
  25.         GPIO_InitTypeDef GPIO_InitStructure;
  26.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  27.         GPIO_InitStructure.GPIO_Pin          = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  28.         GPIO_InitStructure.GPIO_Mode         = GPIO_Mode_Out_PP;
  29.         GPIO_InitStructure.GPIO_Speed         = GPIO_Speed_50MHz;
  30.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  31. }


  32. /**
  33.   * @brief  Configure the nested vectored interrupt controller.
  34.   * @param  None
  35.   * @retval : None
  36.   */
  37. void NVIC_Configuration(void)
  38. {
  39.   NVIC_InitTypeDef NVIC_InitStructure;

  40.   /* Enable the TIM2 gloabal Interrupt */
  41.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  42.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  43.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  44.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  45.   NVIC_Init(&NVIC_InitStructure);

  46.   /* Enable the TIM2 gloabal Interrupt */
  47.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  48.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  49.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  50.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  51.   NVIC_Init(&NVIC_InitStructure);
  52. }

  53. /**
  54.   * @功能
  55.   * @參數(shù)
  56.   * @返回值
  57.   */
  58. int main(void)
  59. {
  60.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  61.         NVIC_Configuration();
  62.         LED_Config();
  63.        
  64.         /*模式         從機(jī)地址 端口 波特率 校驗(yàn)位*/
  65.         eMBInit( MB_RTU, 0x01, 0, 9600, MB_PAR_NONE );
  66.        
  67.         /* Enable the Modbus Protocol Stack. */
  68.         eMBEnable(  );
  69.        
  70.         for( ;; )
  71.         {
  72.          ( void )eMBPoll(  );
  73.          /* Here we simply count the number of poll cycles. */
  74.          //usRegInputBuf[0]++;
  75.         }
  76. }

  77. /**
  78.   * @brief  Retargets the C library printf function to the USART.
  79.   * @param  None
  80.   * @retval None
  81.   */
  82. PUTCHAR_PROTOTYPE
  83. {
  84.   /* Place your implementation of fputc here */

  85.   /* Loop until the end of transmission */
  86.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  87.   {}
  88.   /* e.g. write a character to the USART */
  89.   USART_SendData(USART1, (uint8_t) ch);       

  90.   return ch;
  91. }

  92. #ifdef  USE_FULL_ASSERT

  93. /**
  94.   * @brief  Reports the name of the source file and the source line number
  95.   *         where the assert_param error has occurred.
  96.   * @param  file: pointer to the source file name
  97.   * @param  line: assert_param error line source number
  98.   * @retval None
  99.   */
  100. void assert_failed(uint8_t* file, uint32_t line)
  101. {
  102.   /* User can add his own implementation to report the file name and line number,
  103.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  104.   /* Infinite loop */
  105.   while (1)
  106.   {
  107.   }
  108. }
  109. #endif
  110. /**
  111.   * @}
  112.   */


  113. /*******************************文件結(jié)尾**************************************/


  114. ……………………

  115. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
STM32-MODBUS標(biāo)準(zhǔn)通信協(xié)議測(cè)試_MY.rar (321.61 KB, 下載次數(shù): 225)



作者: lmwife    時(shí)間: 2018-9-26 09:48
請(qǐng)問(wèn),這個(gè)用的是什么芯片?
作者: hesan921    時(shí)間: 2018-10-14 19:54
I like it. Thanks
作者: 13456140382    時(shí)間: 2018-11-3 21:54
感謝分享
作者: rex236    時(shí)間: 2018-11-9 08:46
感謝分享有價(jià)值的東西,正需要,可惜沒有黑幣,誰(shuí)可以發(fā)下我的郵箱嗎?
作者: assouan    時(shí)間: 2018-11-28 14:00
學(xué)習(xí)下看看
作者: YZM168    時(shí)間: 2018-12-26 14:53
主站還是從站呢?
作者: hengihao    時(shí)間: 2019-1-6 18:56
一起學(xué)習(xí)單片機(jī)
作者: honglang2222    時(shí)間: 2019-2-20 09:50
謝謝!。。。。。!
作者: 計(jì)量王工    時(shí)間: 2019-2-23 14:56
謝謝樓主分享。有點(diǎn)不明白,自由從機(jī)程序就可以通訊了吧,那還要主機(jī)程序干嘛
作者: xo37    時(shí)間: 2019-2-24 07:04
lmwife 發(fā)表于 2018-9-26 09:48
請(qǐng)問(wèn),這個(gè)用的是什么芯片?

看引腳定義STM32F103系列均可
作者: koethen    時(shí)間: 2019-2-28 08:12
非常棒!正準(zhǔn)備學(xué)習(xí)!
作者: pm1981    時(shí)間: 2019-2-28 09:36
有點(diǎn)看不明白
作者: huyangzxh    時(shí)間: 2019-4-12 14:50

感謝分享
作者: irisice008    時(shí)間: 2019-5-17 22:24
感謝分享
作者: heabin    時(shí)間: 2019-5-20 21:31

感謝分享
作者: lx5831    時(shí)間: 2019-11-18 08:00
感謝分享




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1