找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F4+W5500以太网通讯+freemodbus源码

  [复制链接]
跳转到指定楼层
楼主
ID:33370 发表于 2018-5-18 13:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32F4+W5500以太网通讯+freemodbus

单片机源程序如下:
  1. /******************** (C) COPYRIGHT 2014 91mcu **************************
  2. * 文件名  :main.c
  3. * 描述    :W5500 以太网模块测试程序:Modbus-TCP 功能实现。      
  4. * 实验平台:STM32F103VCT6+W5500
  5. * 库版本  :ST3.5.0
  6. *
  7. * 作者    :zhangsz
  8. **********************************************************************************/       
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include "stm32f10x.h"

  12. #include "W5500.h"
  13. #include "device.h"
  14. #include "systick.h"
  15. #include "led.h"
  16. #include "mb.h"


  17. /* ----------------------- Holding register Defines ------------------------------------------*/

  18. #define REG_HOLDING_START 1000
  19. #define REG_HOLDING_NREGS 4

  20. /* ----------------------- Static variables ---------------------------------*/
  21. static unsigned short usRegHoldingStart = REG_HOLDING_START;
  22. static unsigned short usRegHoldingBuf[REG_HOLDING_NREGS]={0xaa,0x55,0x5599,0x5588};

  23. /* ----------------------- input register Defines ------------------------------------------*/
  24. #define REG_INPUT_START 1000
  25. #define REG_INPUT_NREGS 4

  26. /* ----------------------- Static variables ---------------------------------*/
  27. static unsigned short usRegInputStart = REG_INPUT_START;
  28. static unsigned short usRegInputBuf[REG_INPUT_NREGS]={0,0,0x5599,0x5588};

  29. /* ----------------------- coils register Defines ------------------------------------------*/
  30. #define REG_COILS_START     1000
  31. #define REG_COILS_SIZE      16

  32. /* ----------------------- Static variables ---------------------------------*/
  33. static unsigned char ucRegCoilsBuf[REG_COILS_SIZE / 8]={0xaa,0xfe};          //数量低于8个还需要验证一下,是否需要加1呢。

  34. /* ----------------------- discrete register Defines ------------------------------------------*/
  35. #define REG_DISC_START     1000
  36. #define REG_DISC_SIZE      16

  37. /* ----------------------- Static variables ---------------------------------*/
  38. static unsigned char ucRegDiscBuf[REG_DISC_SIZE / 8] = { 0x98, 0x6e };           //数量8的整数倍。


  39. void BSP_LED(void);          //LED指示线圈操作

  40. /* W5500 configuration */
  41. void W5500_Configuration()
  42. {
  43.         unsigned char array[6];

  44.         GPIO_SetBits(GPIO_W5500_RST_PORT, GPIO_W5500_RST_Pin);
  45.         Delay_ms(100);    /*delay 100ms 使用systick 1ms时基的延时*/
  46.         while((Read_1_Byte(PHYCFGR)&LINK)==0);                 /* Waiting for Ethernet Link */

  47.         Write_1_Byte(MR, RST);
  48.         Delay_ms(20);                /*delay 20ms */

  49.         /* Set Gateway IP as: 192.168.1.1 */
  50.         array[0]=192;
  51.         array[1]=168;
  52.         array[2]=1;
  53.         array[3]=1;
  54.         Write_Bytes(GAR, array, 4);

  55.         /* Set Subnet Mask as: 255.255.255.0 */
  56.         array[0]=255;
  57.         array[1]=255;
  58.         array[2]=255;
  59.         array[3]=0;
  60.         Write_Bytes(SUBR, array, 4);

  61.         /* Set MAC Address as: 0x48,0x53,0x00,0x57,0x55,0x00 */
  62.         array[0]=0x48;
  63.         array[1]=0x53;
  64.         array[2]=0x00;
  65.         array[3]=0x57;
  66.         array[4]=0x55;
  67.         array[5]=0x00;
  68.         Write_Bytes(SHAR, array, 6);

  69.         /* Set W5500 IP as: 192.168.1.128 */
  70.         array[0]=192;
  71.         array[1]=168;
  72.         array[2]=1;
  73.         array[3]=128;
  74.         Write_Bytes(SIPR, array, 4);
  75. }




  76. /*****************************************************************
  77.                            Main Program
  78. *****************************************************************/
  79. int main(void)
  80. {
  81.         unsigned char i;

  82.         /* Initialize STM32F103 */
  83.         System_Initialization();
  84.         SysTick_Init();

  85.         /* Config W5500 */
  86.         W5500_Configuration();
  87.         Delay_ms(200);

  88.         /* Modbus-TCP Init */
  89.     eMBTCPInit(MB_TCP_PORT_USE_DEFAULT);
  90.         Delay_ms(200);
  91.        
  92.         /* Enable Modbus-TCP Stack */   
  93.     eMBEnable();       
  94.    

  95.     printf("\r\nModbus-TCP Start!\r\n");
  96.     printf("IP:192.168.1.128\r\n");


  97.         while(1)
  98.         {
  99.                
  100.                 i=Read_SOCK_1_Byte(0,Sn_SR);  //读W5500状态
  101.                 if(i==0)          
  102.                 {
  103.                         do
  104.                         {
  105.                                 Delay_ms(100);
  106.                        
  107.                         }while(Socket_Listen(0)==FALSE);
  108.                 }
  109.                 else if(i==SOCK_ESTABLISHED)                 //建立TCP连接
  110.                 {
  111.                 eMBPoll();
  112.                 BSP_LED();
  113.                 }
  114.                
  115.                
  116.         }
  117. }



  118. eMBErrorCode
  119. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
  120.                  eMBRegisterMode eMode )
  121. {
  122.     eMBErrorCode    eStatus = MB_ENOERR;
  123.     int             iRegIndex;

  124.     if( ( usAddress >= REG_HOLDING_START ) &&
  125.         ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
  126.     {
  127.         iRegIndex = ( int )( usAddress - usRegHoldingStart );
  128.         switch ( eMode )
  129.         {
  130.                 /* Pass current register values to the protocol stack. */
  131.             case MB_REG_READ:
  132.                 while( usNRegs > 0 )
  133.                 {
  134.                     *pucRegBuffer++ =
  135.                         ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
  136.                     *pucRegBuffer++ =
  137.                         ( unsigned char )( usRegHoldingBuf[iRegIndex] &
  138.                                            0xFF );
  139.                     iRegIndex++;
  140.                     usNRegs--;
  141.                 }
  142.                 break;

  143.                 /* Update current register values with new values from the
  144.                  * protocol stack. */
  145.             case MB_REG_WRITE:
  146.                 while( usNRegs > 0 )
  147.                 {
  148.                     usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
  149.                     usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
  150.                     iRegIndex++;
  151.                     usNRegs--;
  152.                 }
  153.         }
  154.     }
  155.     else
  156.     {
  157.         eStatus = MB_ENOREG;
  158.     }
  159.     return eStatus;
  160. }

  161. /**
  162.   * @功能
  163.   * @参数
  164.   * @返回值
  165.   */
  166. eMBErrorCode
  167. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  168. {
  169.     eMBErrorCode    eStatus = MB_ENOERR;
  170.     int             iRegIndex;

  171.     if( ( usAddress >= REG_INPUT_START )
  172.         && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  173.     {
  174.         iRegIndex = ( int )( usAddress - usRegInputStart );
  175.         while( usNRegs > 0 )
  176.         {
  177.             *pucRegBuffer++ =
  178.                 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  179.             *pucRegBuffer++ =
  180.                 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  181.             iRegIndex++;
  182.             usNRegs--;
  183.         }
  184.     }
  185.     else
  186.     {
  187.         eStatus = MB_ENOREG;
  188.     }

  189.     return eStatus;
  190. }


  191. /**
  192.   * @功能
  193.   * @参数
  194.   * @返回值
  195.   */


  196. eMBErrorCode
  197. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
  198.                eMBRegisterMode eMode )
  199. {
  200.     eMBErrorCode    eStatus = MB_ENOERR;
  201.     int             iNCoils = ( int )usNCoils;
  202.     unsigned short  usBitOffset;

  203.     /* Check if we have registers mapped at this block. */
  204.     if( ( usAddress >= REG_COILS_START ) &&
  205.         ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
  206.     {
  207.         usBitOffset = ( unsigned short )( usAddress - REG_COILS_START );
  208.         switch ( eMode )
  209.         {
  210.                 /* Read current values and pass to protocol stack. */
  211.             case MB_REG_READ:
  212.                 while( iNCoils > 0 )
  213.                 {
  214.                     *pucRegBuffer++ =
  215.                         xMBUtilGetBits( ucRegCoilsBuf, usBitOffset,
  216.                                         ( unsigned char )( iNCoils >
  217.                                                            8 ? 8 :
  218.                                                            iNCoils ) );
  219.                     iNCoils -= 8;
  220.                     usBitOffset += 8;
  221.                 }
  222.                 break;

  223.                 /* Update current register values. */
  224.             case MB_REG_WRITE:
  225.                 while( iNCoils > 0 )
  226.                 {
  227.                     xMBUtilSetBits( ucRegCoilsBuf, usBitOffset,
  228.                                     ( unsigned char )( iNCoils > 8 ? 8 : iNCoils ),
  229.                                     *pucRegBuffer++ );
  230.                     iNCoils -= 8;
  231.                     usBitOffset += 8;
  232.                 }
  233.                 break;
  234.         }

  235.     }
  236.     else
  237.     {
  238.         eStatus = MB_ENOREG;
  239.     }
  240.     return eStatus;
  241. }

  242. eMBErrorCode
  243. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  244. {
  245.     eMBErrorCode    eStatus = MB_ENOERR;
  246.     short           iNDiscrete = ( short )usNDiscrete;
  247.     unsigned short  usBitOffset;

  248.     /* Check if we have registers mapped at this block. */
  249.     if( ( usAddress >= REG_DISC_START ) &&
  250.         ( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_SIZE ) )
  251.     {
  252.         usBitOffset = ( unsigned short )( usAddress - REG_DISC_START );
  253.         while( iNDiscrete > 0 )
  254.         {
  255.             *pucRegBuffer++ =
  256.                 xMBUtilGetBits( ucRegDiscBuf, usBitOffset,
  257.                                 ( unsigned char )( iNDiscrete >
  258.                                                    8 ? 8 : iNDiscrete ) );
  259.             iNDiscrete -= 8;
  260.             usBitOffset += 8;
  261.         }
  262.     }
  263.     else
  264.     {
  265.         eStatus = MB_ENOREG;
  266.     }
  267.     return eStatus;
  268. }

  269. /**
  270.   * @}
  271.   */


  272. // 线圈操作用于LED控制
  273. void BSP_LED(void)
  274. {
  275.     uint8_t led_state = ucRegCoilsBuf[0];
  276.    
  277.     if(led_state & 0x01)
  278.                 LED1(ON);
  279.         else
  280.                 LED1(OFF);

  281.         if(led_state & 0x02)
  282.                 LED2(ON);
  283.         else
  284.                 LED2(OFF);

  285.         if(led_state & 0x04)
  286.                 LED3(ON);
  287.         else
  288.                 LED3(OFF);

  289.         if(led_state & 0x08)
  290.                 LED4(ON);
  291.         else
  292.                 LED4(OFF);
  293.    
  294. }



  295. /******************* (C) COPYRIGHT 2014 91mcu *****END OF FILE************/
复制代码

所有资料51hei提供下载:
fsmd-stm32_w5500_freemodbus_v1-master.zip (386.72 KB, 下载次数: 263)


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

使用道具 举报

沙发
ID:359762 发表于 2018-6-26 22:08 | 只看该作者
正好需要这个。。
回复

使用道具 举报

板凳
ID:409584 发表于 2018-10-14 19:55 | 只看该作者
Thanks My Bro
回复

使用道具 举报

地板
ID:310600 发表于 2019-1-23 14:36 | 只看该作者
楼主太没有节操了,拿着F1程序说是F4的
回复

使用道具 举报

5#
ID:344498 发表于 2019-1-24 13:55 | 只看该作者
不错收下了
回复

使用道具 举报

6#
ID:344498 发表于 2019-1-24 13:56 | 只看该作者
headache 发表于 2019-1-23 14:36
楼主太没有节操了,拿着F1程序说是F4的

有这事
回复

使用道具 举报

7#
ID:166520 发表于 2019-3-19 13:53 | 只看该作者
不错收下了
回复

使用道具 举报

8#
ID:166520 发表于 2019-3-19 13:54 | 只看该作者
不错收下了
回复

使用道具 举报

9#
ID:687139 发表于 2020-1-18 14:23 | 只看该作者
不错收下了 Thanks My Bro
回复

使用道具 举报

10#
ID:700809 发表于 2020-3-7 10:29 | 只看该作者
STM32F103VCT6+W5500的代码  不是STM32F4的代码
回复

使用道具 举报

11#
ID:628703 发表于 2020-3-20 16:55 | 只看该作者
谢谢分享!
回复

使用道具 举报

12#
ID:442911 发表于 2020-3-20 20:44 | 只看该作者
good job
回复

使用道具 举报

13#
ID:700809 发表于 2020-6-3 08:47 | 只看该作者
正好需要这个
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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