STM32+W5500網路通信
板子:STM32F103C8
模塊:W5500
移植系統:uc/OS-III
編譯工具:Keil&STM32CubeMX
燒錄工具:FlyMcu
文章目錄
- STM32+W5500網路通信
- 一、W5500&Demos
- 1. W5500以太網模塊
- 2. 廠家Demo測驗
- 二、STM32+W5500+modbus協議編程
- 1. 工程檔案下載地址
- 2. 原始碼演示
- 三、STM32+W5500的web服務
- 1. 專案下載:
- 2. 原始碼演示:
- 四、總結
- 五、參考文章
- 六、源代碼
一、W5500&Demos
1. W5500以太網模塊
Niren_W5500模塊是一款基于WIZnet W5500芯片的以太網模塊,是泥人電子繼 Niren_W5100模塊后設計的一塊性能更好、性價比更高的以太網模塊,模塊集成硬體化TCP/IP協議:內部32K位元組存盤器作TX/RX
快取:支持10/100Mbps的傳輸速率;支持8個獨立埠同時運行;同時模塊還支持3.3V或5V電源供電,5V供電時還可以輸出3.3V電源,方便用戶在不同的單片機系統中使用;模塊與單片機系統的通訊方式是簡單、方便的SPI通信,
| 排針標識 | 功能說明 | 接法 | 排針標識 | 功能說明 | |
|---|---|---|---|---|---|
| 3.3V | 3.3V電源輸入引腳 | – | 5V | 5V電源輸入引腳 | – |
| MISO | SPI主機輸入從機輸出引腳 | A6 | GND | 電源地引腳 | – |
| MOSI | SPI主機輸出從機輸入引腳 | A7 | RST | W5500硬體初始化引腳(低電平有效) | A15 |
| SCS | SPI SLAVE選擇引腳(低電平有效) | A4 | INT | W5500中斷引腳(低電平有效) | C4 |
| SCLK | SPI時鐘引腳 | A5 | NC | 懸空 | – |
2. 廠家Demo測驗
資料地址:
鏈接:https://pan.baidu.com/s/1fiWWfmWQT9CNh4EimU-Igw
提取碼:1234
具體例程的除錯程序請參考壓縮包中NiRen_W5500模塊用戶手冊(用Adobe Reader打開)
客戶端:

服務器端:

UDP:

二、STM32+W5500+modbus協議編程
1. 工程檔案下載地址
工程檔案:https://github.com/Wattson1128/Embedded-System/tree/main/week15-W5500/stm32_w5500_freemodbus_v1-master
2. 原始碼演示

核心部分:
main.c
int main(void)
{
unsigned char i;
/* Initialize STM32F103 */
System_Initialization();//系統配置
SysTick_Init();//啟動系統滴答定時器 SysTick
/* Config W5500 */
W5500_Configuration();//W5500配置
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);//設定“Socket n”為“TCP服務器模式”
}
else if(i==SOCK_ESTABLISHED) //建立TCP連接
{
eMBPoll();//啟動modbus偵聽
BSP_LED();//線圈控制LED燈
}
}
}
W5500的配置函式
/* 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);//寫入W5500普通暫存器一個位元組
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);//設定網關IP
/* 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);//設定MAC地址
/* 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);//設定W5500的IP地址
}
三、STM32+W5500的web服務
1. 專案下載:
https://github.com/Wattson1128/Embedded-System/tree/main/week15-W5500/HTTP_Server-Two_Page
2. 原始碼演示:

核心代碼:
main.c:
int main(void)
{
Systick_Init(72);//系統時鐘初始化
GPIO_Configuration(); //GPIO configuration
USART1_Init(); //串口初始化:115200@8-n-1
printf("W5500 EVB initialization over.\r\n");
Reset_W5500();
WIZ_SPI_Init();//W5500相關引腳配置
printf("W5500 initialized!\r\n");
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))
{
DefaultSet();//出廠值
}
else
{
get_config();//read config data from flash
}
printf("Firmware ver%d.%d\r\n",ConfigMsg.sw_ver[0],ConfigMsg.sw_ver[1]);
if(ConfigMsg.debug==0) ConfigMsg.debug=1;
set_network();//配置網路資訊
printf("Network is ready.\r\n");
while(1)
{
if(ConfigMsg.JTXD_Control == 0)
do_http();//開啟http服務
else
JTXD_do_http();
if(reboot_flag)
NVIC_SystemReset();//發起系統復位請求復位單片機
// reboot();
}
}
GPIO.C
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO , ENABLE);
// Port A output
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_1| GPIO_Pin_2 |GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOA, GPIO_Pin_0);
// GPIO_ResetBits(GPIOA, GPIO_Pin_1);
// GPIO_SetBits(GPIOA, GPIO_Pin_2); // led off
// GPIO_SetBits(GPIOA, GPIO_Pin_3); // led off
// Port B output;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
// Port C input
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//控制flash
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
}
W5500配置:
void WIZ_SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO , ENABLE);
// Port B output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* Configure SPIy pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
http請求:
void do_http(void)
{
uint8 ch=SOCK_HTTP;
uint16 len;
st_http_request *http_request;
memset(rx_buf,0x00,MAX_URI_SIZE);
http_request = (st_http_request*)rx_buf; // struct of http request
/* http service start */
switch(getSn_SR(ch))
{
case SOCK_INIT:
listen(ch);
break;
case SOCK_LISTEN:
break;
case SOCK_ESTABLISHED:
//case SOCK_CLOSE_WAIT:
if(getSn_IR(ch) & Sn_IR_CON)
{
setSn_IR(ch, Sn_IR_CON);
}
if ((len = getSn_RX_RSR(ch)) > 0)
{
len = recv(ch, (uint8*)http_request, len);
*(((uint8*)http_request)+len) = 0;
proc_http(ch, (uint8*)http_request); // request is processed
disconnect(ch);
}
break;
case SOCK_CLOSE_WAIT:
if ((len = getSn_RX_RSR(ch)) > 0)
{
//printf("close wait: %d\r\n",len);
len = recv(ch, (uint8*)http_request, len);
*(((uint8*)http_request)+len) = 0;
proc_http(ch, (uint8*)http_request); // request is processed
}
disconnect(ch);
break;
case SOCK_CLOSED:
socket(ch, Sn_MR_TCP, 80, 0x00); /* reinitialize the socket */
break;
default:
break;
}// end of switch
}
void JTXD_do_http(void)
{
uint8 ch=SOCK_HTTP;
uint16 len;
st_http_request *http_request;
memset(rx_buf,0x00,MAX_URI_SIZE);
http_request = (st_http_request*)rx_buf; // struct of http request
/* http service start */
switch(getSn_SR(ch))
{
case SOCK_INIT:
listen(ch);
break;
case SOCK_LISTEN:
break;
case SOCK_ESTABLISHED:
//case SOCK_CLOSE_WAIT:
if(getSn_IR(ch) & Sn_IR_CON)
{
setSn_IR(ch, Sn_IR_CON);
}
if ((len = getSn_RX_RSR(ch)) > 0)
{
len = recv(ch, (uint8*)http_request, len);
*(((uint8*)http_request)+len) = 0;
JTXD_proc_http(ch, (uint8*)http_request); // request is processed
disconnect(ch);
}
break;
case SOCK_CLOSE_WAIT:
if ((len = getSn_RX_RSR(ch)) > 0)
{
//printf("close wait: %d\r\n",len);
len = recv(ch, (uint8*)http_request, len);
*(((uint8*)http_request)+len) = 0;
JTXD_proc_http(ch, (uint8*)http_request); // request is processed
}
disconnect(ch);
break;
case SOCK_CLOSED:
socket(ch, Sn_MR_TCP, 80, 0x00); /* reinitialize the socket */
break;
default:
break;
}// end of switch
}
四、總結
w5500通過對PB7引腳的操作來實作預設值和flash之間的切換,
五、參考文章
天才精英2020: STM32+W5500+Freemodbus Modbus-TCP協議功能實作,工程檔案下載
天才精英2020: 基于STM32_DM9000_UIP_FreeModbus的Modbus-TCP功能實作工程下載
六、源代碼
https://github.com/Wattson1128/Embedded-System
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394057.html
標籤:其他
上一篇:串列理解和str.split()回傳串列中的串列而不是單個字串
下一篇:生成串列,允許重復并符合條件
