久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7267|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品免费一区二区三区四区 | 午夜不卡一区二区 | xxxcom在线观看| 99一区二区 | 秋霞a级毛片在线看 | 久久9精品 | 青草青草久热精品视频在线观看 | 亚洲一区二区三区 | 夜夜骑首页 | 国产在线91| 中文字幕精品一区二区三区精品 | 久久这里只有精品首页 | 国产亚洲精品久久久优势 | 在线成人av | 黄色在线免费看 | 久久一区二区三区四区 | 国产黄色在线观看 | 91av国产在线视频 | 免费黄色a级毛片 | 欧美国产视频 | 国产精品一区在线播放 | 精品网 | 精品久久视频 | 午夜欧美日韩 | 黄色大片观看 | 国产a一区二区 | 中文字幕国产一区 | 伊人免费在线 | 国产黄色电影 | 国产精品日韩欧美 | 日韩精品在线观看网站 | a免费在线 | 激情五月婷婷综合 | 日韩在线91 | 久久综合成人精品亚洲另类欧美 | 国产精品久久久久久久久久三级 | 一级黄色毛片免费 | 国产九九九九 | 亚洲欧洲日本国产 | 操操日 | 国产一区二区精华 |