标题:
STM32F4+W5500以太网通讯+freemodbus源码
[打印本页]
作者:
追求理想
时间:
2018-5-18 13:48
标题:
STM32F4+W5500以太网通讯+freemodbus源码
STM32F4+W5500以太网通讯+freemodbus
单片机源程序如下:
/******************** (C) COPYRIGHT 2014 91mcu **************************
* 文件名 :main.c
* 描述 :W5500 以太网模块测试程序:Modbus-TCP 功能实现。
* 实验平台:STM32F103VCT6+W5500
* 库版本 :ST3.5.0
*
* 作者 :zhangsz
**********************************************************************************/
#include <string.h>
#include <stdio.h>
#include "stm32f10x.h"
#include "W5500.h"
#include "device.h"
#include "systick.h"
#include "led.h"
#include "mb.h"
/* ----------------------- Holding register Defines ------------------------------------------*/
#define REG_HOLDING_START 1000
#define REG_HOLDING_NREGS 4
/* ----------------------- Static variables ---------------------------------*/
static unsigned short usRegHoldingStart = REG_HOLDING_START;
static unsigned short usRegHoldingBuf[REG_HOLDING_NREGS]={0xaa,0x55,0x5599,0x5588};
/* ----------------------- input register Defines ------------------------------------------*/
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
/* ----------------------- Static variables ---------------------------------*/
static unsigned short usRegInputStart = REG_INPUT_START;
static unsigned short usRegInputBuf[REG_INPUT_NREGS]={0,0,0x5599,0x5588};
/* ----------------------- coils register Defines ------------------------------------------*/
#define REG_COILS_START 1000
#define REG_COILS_SIZE 16
/* ----------------------- Static variables ---------------------------------*/
static unsigned char ucRegCoilsBuf[REG_COILS_SIZE / 8]={0xaa,0xfe}; //数量低于8个还需要验证一下,是否需要加1呢。
/* ----------------------- discrete register Defines ------------------------------------------*/
#define REG_DISC_START 1000
#define REG_DISC_SIZE 16
/* ----------------------- Static variables ---------------------------------*/
static unsigned char ucRegDiscBuf[REG_DISC_SIZE / 8] = { 0x98, 0x6e }; //数量8的整数倍。
void BSP_LED(void); //LED指示线圈操作
/* W5500 configuration */
void W5500_Configuration()
{
unsigned char array[6];
GPIO_SetBits(GPIO_W5500_RST_PORT, GPIO_W5500_RST_Pin);
Delay_ms(100); /*delay 100ms 使用systick 1ms时基的延时*/
while((Read_1_Byte(PHYCFGR)&LINK)==0); /* Waiting for Ethernet Link */
Write_1_Byte(MR, RST);
Delay_ms(20); /*delay 20ms */
/* Set Gateway IP as: 192.168.1.1 */
array[0]=192;
array[1]=168;
array[2]=1;
array[3]=1;
Write_Bytes(GAR, array, 4);
/* Set Subnet Mask as: 255.255.255.0 */
array[0]=255;
array[1]=255;
array[2]=255;
array[3]=0;
Write_Bytes(SUBR, array, 4);
/* Set MAC Address as: 0x48,0x53,0x00,0x57,0x55,0x00 */
array[0]=0x48;
array[1]=0x53;
array[2]=0x00;
array[3]=0x57;
array[4]=0x55;
array[5]=0x00;
Write_Bytes(SHAR, array, 6);
/* Set W5500 IP as: 192.168.1.128 */
array[0]=192;
array[1]=168;
array[2]=1;
array[3]=128;
Write_Bytes(SIPR, array, 4);
}
/*****************************************************************
Main Program
*****************************************************************/
int main(void)
{
unsigned char i;
/* Initialize STM32F103 */
System_Initialization();
SysTick_Init();
/* Config W5500 */
W5500_Configuration();
Delay_ms(200);
/* Modbus-TCP Init */
eMBTCPInit(MB_TCP_PORT_USE_DEFAULT);
Delay_ms(200);
/* Enable Modbus-TCP Stack */
eMBEnable();
printf("\r\nModbus-TCP Start!\r\n");
printf("IP:192.168.1.128\r\n");
while(1)
{
i=Read_SOCK_1_Byte(0,Sn_SR); //读W5500状态
if(i==0)
{
do
{
Delay_ms(100);
}while(Socket_Listen(0)==FALSE);
}
else if(i==SOCK_ESTABLISHED) //建立TCP连接
{
eMBPoll();
BSP_LED();
}
}
}
eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_HOLDING_START ) &&
( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegHoldingStart );
switch ( eMode )
{
/* Pass current register values to the protocol stack. */
case MB_REG_READ:
while( usNRegs > 0 )
{
*pucRegBuffer++ =
( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ =
( unsigned char )( usRegHoldingBuf[iRegIndex] &
0xFF );
iRegIndex++;
usNRegs--;
}
break;
/* Update current register values with new values from the
* protocol stack. */
case MB_REG_WRITE:
while( usNRegs > 0 )
{
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* @功能
* @参数
* @返回值
*/
eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_INPUT_START )
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegInputStart );
while( usNRegs > 0 )
{
*pucRegBuffer++ =
( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
*pucRegBuffer++ =
( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* @功能
* @参数
* @返回值
*/
eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
int iNCoils = ( int )usNCoils;
unsigned short usBitOffset;
/* Check if we have registers mapped at this block. */
if( ( usAddress >= REG_COILS_START ) &&
( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
{
usBitOffset = ( unsigned short )( usAddress - REG_COILS_START );
switch ( eMode )
{
/* Read current values and pass to protocol stack. */
case MB_REG_READ:
while( iNCoils > 0 )
{
*pucRegBuffer++ =
xMBUtilGetBits( ucRegCoilsBuf, usBitOffset,
( unsigned char )( iNCoils >
8 ? 8 :
iNCoils ) );
iNCoils -= 8;
usBitOffset += 8;
}
break;
/* Update current register values. */
case MB_REG_WRITE:
while( iNCoils > 0 )
{
xMBUtilSetBits( ucRegCoilsBuf, usBitOffset,
( unsigned char )( iNCoils > 8 ? 8 : iNCoils ),
*pucRegBuffer++ );
iNCoils -= 8;
usBitOffset += 8;
}
break;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
eMBErrorCode eStatus = MB_ENOERR;
short iNDiscrete = ( short )usNDiscrete;
unsigned short usBitOffset;
/* Check if we have registers mapped at this block. */
if( ( usAddress >= REG_DISC_START ) &&
( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_SIZE ) )
{
usBitOffset = ( unsigned short )( usAddress - REG_DISC_START );
while( iNDiscrete > 0 )
{
*pucRegBuffer++ =
xMBUtilGetBits( ucRegDiscBuf, usBitOffset,
( unsigned char )( iNDiscrete >
8 ? 8 : iNDiscrete ) );
iNDiscrete -= 8;
usBitOffset += 8;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* @}
*/
// 线圈操作用于LED控制
void BSP_LED(void)
{
uint8_t led_state = ucRegCoilsBuf[0];
if(led_state & 0x01)
LED1(ON);
else
LED1(OFF);
if(led_state & 0x02)
LED2(ON);
else
LED2(OFF);
if(led_state & 0x04)
LED3(ON);
else
LED3(OFF);
if(led_state & 0x08)
LED4(ON);
else
LED4(OFF);
}
/******************* (C) COPYRIGHT 2014 91mcu *****END OF FILE************/
复制代码
所有资料51hei提供下载:
fsmd-stm32_w5500_freemodbus_v1-master.zip
(386.72 KB, 下载次数: 263)
2018-5-18 13:48 上传
点击文件名下载附件
程序
下载积分: 黑币 -5
作者:
tianyu22
时间:
2018-6-26 22:08
正好需要这个。。
作者:
hesan921
时间:
2018-10-14 19:55
Thanks My Bro
作者:
headache
时间:
2019-1-23 14:36
楼主太没有节操了,拿着F1程序说是F4的
作者:
thmoasmary
时间:
2019-1-24 13:55
不错收下了
作者:
thmoasmary
时间:
2019-1-24 13:56
headache 发表于 2019-1-23 14:36
楼主太没有节操了,拿着F1程序说是F4的
有这事
作者:
pidaneng
时间:
2019-3-19 13:53
不错收下了
作者:
pidaneng
时间:
2019-3-19 13:54
不错收下了
作者:
ohmygod1234
时间:
2020-1-18 14:23
不错收下了 Thanks My Bro
作者:
tt163
时间:
2020-3-7 10:29
STM32F103VCT6+W5500的代码 不是STM32F4的代码
作者:
peeta
时间:
2020-3-20 16:55
谢谢分享!
作者:
cnc202
时间:
2020-3-20 20:44
good job
作者:
tt163
时间:
2020-6-3 08:47
正好需要这个
作者:
开心石头
时间:
2025-6-9 20:11
这个很不错啊!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1