找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2037|回复: 0
打印 上一主题 下一主题
收起左侧

单片机MODBUD RTU从站C语言程序

[复制链接]
跳转到指定楼层
楼主
ID:268699 发表于 2017-12-29 14:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
单片机MODBUD RTU从站C语言程序

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @文件   
  4.   * @作者
  5.   * @版本
  6.   * @日期
  7.   * @概要
  8.   ******************************************************************************
  9.   * @注意事项
  10.   *
  11.   *
  12.   *
  13.   ******************************************************************************
  14.   */  

  15. /* 头文件                --------------------------------------------------------------*/
  16. #include <stdio.h>
  17. #include "stm32f10x.h"
  18. #include "mb.h"

  19. /* 私有数据类型 --------------------------------------------------------------*/
  20. /* 私有定义         --------------------------------------------------------------*/
  21. /* 私有宏定义         --------------------------------------------------------------*/
  22. #ifdef __GNUC__
  23. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  24.    set to 'Yes') calls __io_putchar() */
  25. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  26. #else
  27. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  28. #endif /* __GNUC__ */
  29. /* 私有变量         --------------------------------------------------------------*/
  30. /* 私有函数声明 --------------------------------------------------------------*/
  31. void LED_Config(void)
  32. {
  33.         GPIO_InitTypeDef GPIO_InitStructure;
  34.        
  35.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  36.         GPIO_InitStructure.GPIO_Pin          = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  37.         GPIO_InitStructure.GPIO_Mode         = GPIO_Mode_Out_PP;
  38.         GPIO_InitStructure.GPIO_Speed         = GPIO_Speed_50MHz;

  39.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  40. }


  41. /**
  42.   * @brief  Configure the nested vectored interrupt controller.
  43.   * @param  None
  44.   * @retval : None
  45.   */
  46.   #if 0
  47. void NVIC_Configuration(void)
  48. {
  49.   NVIC_InitTypeDef NVIC_InitStructure;

  50.   /* Enable the TIM2 gloabal Interrupt */
  51.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  52.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  53.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  54.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  55.   NVIC_Init(&NVIC_InitStructure);

  56.   /* Enable the TIM2 gloabal Interrupt */
  57.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  58.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  59.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  60.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  61.   NVIC_Init(&NVIC_InitStructure);
  62. }
  63. #endif


  64. #define USE_UART1_INT
  65. #define USE_TIM2_INT

  66. void NVIC_Configuration(void)
  67. {
  68.         NVIC_InitTypeDef NVIC_InitStructure;
  69.         /* Set the Vector Table base location at 0x08000000 */
  70.         NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
  71.         /* Configure the NVIC Preemption Priority Bits */  
  72.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  73. #if 1
  74. #ifdef USE_UART1_INT
  75.         /* Enable the USART1 Interrupt */
  76.         NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
  77.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  78.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  79.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  80.         NVIC_Init(&NVIC_InitStructure);
  81. #endif

  82. #ifdef USE_UART2_INT
  83.         /* Enable the USART2 Interrupt */
  84.         NVIC_InitStructure.NVIC_IRQChannel                   = USART2_IRQn;
  85.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  86.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  87.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  88.         NVIC_Init(&NVIC_InitStructure);
  89. #endif

  90. #ifdef USE_UART3_INT
  91.         /* Enable the USART3 Interrupt */
  92.         NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn;
  93.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  94.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  95.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  96.         NVIC_Init(&NVIC_InitStructure);
  97. #endif

  98. #ifdef USE_UART4_INT
  99.         /* Enable the USART4 Interrupt */
  100.         NVIC_InitStructure.NVIC_IRQChannel                   = UART4_IRQn;
  101.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  102.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  103.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  104.         NVIC_Init(&NVIC_InitStructure);
  105. #endif

  106. #ifdef USE_UART5_INT
  107.         /* Enable the USART5 Interrupt */
  108.         NVIC_InitStructure.NVIC_IRQChannel                   = UART5_IRQn;
  109.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  110.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  111.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  112.         NVIC_Init(&NVIC_InitStructure);
  113. #endif

  114. #ifdef USE_EXTI0_INT
  115.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI0_IRQn;
  116.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  117.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  118.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  119.         NVIC_Init(&NVIC_InitStructure);
  120. #endif

  121. #ifdef USE_EXTI1_INT
  122.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI1_IRQn;
  123.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  124.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  125.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  126.         NVIC_Init(&NVIC_InitStructure);
  127. #endif

  128. #ifdef USE_EXTI2_INT
  129.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI2_IRQn;
  130.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  131.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  132.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  133.         NVIC_Init(&NVIC_InitStructure);
  134. #endif

  135. #ifdef USE_EXTI3_INT
  136.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI3_IRQn;
  137.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  138.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  139.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  140.         NVIC_Init(&NVIC_InitStructure);
  141. #endif

  142. #ifdef USE_EXTI4_INT
  143.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI4_IRQn;
  144.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  145.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  146.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  147.         NVIC_Init(&NVIC_InitStructure);
  148. #endif

  149. #ifdef USE_EXTI9_5_INT
  150.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI9_5_IRQn;
  151.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  152.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  153.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  154.         NVIC_Init(&NVIC_InitStructure);
  155. #endif

  156. #ifdef USE_EXTI15_10_INT
  157.         NVIC_InitStructure.NVIC_IRQChannel                   = EXTI15_10_IRQn;
  158.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  159.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  160.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  161.         NVIC_Init(&NVIC_InitStructure);
  162. #endif

  163. #ifdef USE_TIM1_CC_INT
  164.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_CC_IRQn;
  165.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  166.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  167.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  168.         NVIC_Init(&NVIC_InitStructure);
  169. #endif  

  170. #ifdef USE_TIM2_INT
  171.         /* Enable the TIM2,4~7 Interrupt */
  172.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM2_IRQn;
  173.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  174.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
  175.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  176.         NVIC_Init(&NVIC_InitStructure);
  177. #endif

  178. #ifdef USE_TIM4_INT
  179.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM4_IRQn;
  180.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  181.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  182.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  183.         NVIC_Init(&NVIC_InitStructure);
  184. #endif

  185. #ifdef USE_TIM5_INT
  186.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM5_IRQn;
  187.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  188.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  189.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  190.         NVIC_Init(&NVIC_InitStructure);
  191. #endif

  192. #ifdef USE_TIM6_INT
  193.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM6_IRQn;
  194.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  195.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  196.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  197.         NVIC_Init(&NVIC_InitStructure);
  198. #endif

  199. #ifdef USE_TIM7_INT
  200.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM7_IRQn;
  201.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  202.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  203.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  204.         NVIC_Init(&NVIC_InitStructure);
  205. #endif   

  206. #ifdef USE_PWM_IN_INT
  207.         NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
  208.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  209.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  210.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  211.         NVIC_Init(&NVIC_InitStructure);
  212. #endif   

  213. #ifdef USE_RTC_INT
  214.         /* Enable the RTC Interrupt */
  215.         NVIC_InitStructure.NVIC_IRQChannel                   = RTC_IRQn;
  216.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  217.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  218.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  219.         NVIC_Init(&NVIC_InitStructure);
  220. #endif

  221. #ifdef USE_RTCALARM_INT
  222.         NVIC_InitStructure.NVIC_IRQChannel                   = RTCAlarm_IRQn;
  223.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  224.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 2;
  225.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  226.         NVIC_Init(&NVIC_InitStructure);
  227. #endif

  228. #endif



  229. #if 1 //#ifdef USE_CAN1_INT
  230.         //CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0消息挂号中断允许.                  
  231. #if 1 /* For compiling F105 TO F103 */
  232.         NVIC_InitStructure.NVIC_IRQChannel                   = USB_LP_CAN1_RX0_IRQn;//CAN1_RX0_IRQn;
  233. #endif
  234.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  235.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
  236.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  237.         NVIC_Init(&NVIC_InitStructure);
  238. #endif

  239. #if 0 //#ifdef USE_CAN2_INT
  240.         NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;
  241.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  242.         NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
  243.         NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  244.         NVIC_Init(&NVIC_InitStructure);
  245. #endif
  246. }

  247. #define Open10x_DAC_CLK                                                        RCC_APB2Periph_GPIOB
  248. #define Open10x_CLK_PORT                                                                         GPIOB
  249. #define Open10x_CLK_PIN                                                              GPIO_Pin_2

  250. #define Open10x_SDI_PORT                                                                        GPIOB
  251. #define Open10x_SDI_PIN                                                              GPIO_Pin_3

  252. #define Open10x_CLR_PORT                                                                        GPIOB
  253. #define Open10x_CLR_PIN                                                                                GPIO_Pin_0

  254. #define Open10x_LD_PORT                                                                                GPIOB
  255. #define Open10x_LD_PIN                                                              GPIO_Pin_1

  256. #define Open10x_CS_PORT                                                                                GPIOB
  257. #define Open10x_CS_PIN                                                              GPIO_Pin_4



  258. #define Open10x_LED_CLK                                                                        RCC_APB2Periph_GPIOC
  259. #define Open10x_LED0_PORT                                                                 GPIOC
  260. #define Open10x_LED0_PIN                                                          GPIO_Pin_14

  261. #define Open10x_LED1_PORT                                                                GPIOC
  262. #define Open10x_LED1_PIN                                                          GPIO_Pin_15

  263. void GPIO_Configuration(void)
  264. {
  265.         GPIO_InitTypeDef GPIO_InitStructure;

  266.         //启动时钟安全系统CSS
  267.         RCC_ClockSecuritySystemCmd(ENABLE);
  268.         /* Enable GPIO clock */
  269.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);

  270.         GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

  271.         GPIO_InitStructure.GPIO_Pin   = Open10x_CLK_PIN;
  272.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  273.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  274.         GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
  275.         //GPIO_ResetBits(GPIOB, GPIO_Pin_7);
  276.         GPIO_InitStructure.GPIO_Pin   = Open10x_SDI_PIN;
  277.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  278.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  279.         GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
  280.         GPIO_ResetBits(Open10x_CLK_PORT, Open10x_SDI_PIN);
  281.         GPIO_SetBits(Open10x_CLK_PORT, Open10x_SDI_PIN);
  282.        
  283.         GPIO_InitStructure.GPIO_Pin   = Open10x_CLR_PIN;
  284.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  285.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  286.         GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
  287.         GPIO_SetBits(Open10x_CLK_PORT, Open10x_CLR_PIN);
  288.        
  289.         GPIO_InitStructure.GPIO_Pin   = Open10x_LD_PIN;
  290.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  291.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  292.         GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
  293.         //GPIO_ResetBits(GPIOB, GPIO_Pin_7);
  294.        
  295.         GPIO_InitStructure.GPIO_Pin   = Open10x_CS_PIN;
  296.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  297.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  298.         GPIO_Init(Open10x_CS_PORT, &GPIO_InitStructure);
  299.        
  300.         GPIO_ResetBits(Open10x_CS_PORT, Open10x_CS_PIN);
  301.    
  302.         GPIO_InitStructure.GPIO_Pin   = Open10x_LED0_PIN;
  303.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
  304.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  305.         GPIO_Init(Open10x_LED0_PORT, &GPIO_InitStructure);
  306.         GPIO_SetBits(Open10x_LED0_PORT, Open10x_LED0_PIN);
  307.        
  308.         GPIO_InitStructure.GPIO_Pin   = Open10x_LED1_PIN;
  309.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
  310.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  311.         GPIO_Init(Open10x_LED0_PORT, &GPIO_InitStructure);
  312.         GPIO_SetBits(Open10x_LED0_PORT, Open10x_LED1_PIN);

  313. }

  314. /**
  315.   * @功能
  316.   * @参数
  317.   * @返回值
  318.   */
  319. int main(void)
  320. {
  321.         //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  322.         NVIC_Configuration();
  323.       GPIO_Configuration();
  324.         //LED_Config();
  325.           dac7611_Init();
  326.           dac7611Conv(0);
  327.         /*模式         从机地址 端口 波特率 校验位*/
  328.         eMBInit( MB_RTU, 0x64, 0, 9600, MB_PAR_NONE );
  329.        
  330.         /* Enable the Modbus Protocol Stack. */
  331.         eMBEnable(  );
  332.        
  333.         for( ;; )
  334.         {
  335.          ( void )eMBPoll(  );
  336.          /* Here we simply count the number of poll cycles. */
  337.          //usRegInputBuf[0]++;
  338.         }
  339. ……………………

  340. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
STM32_MODBUS.rar (647.51 KB, 下载次数: 27)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表