|
PC機(jī)采用moudbus TCP與stm32進(jìn)行數(shù)據(jù)讀寫,stm32采用485與IO模塊進(jìn)行數(shù)據(jù)的交互
單片機(jī)源程序如下:
- /******************** (C) COPYRIGHT 2014 91mcu **************************
- * 文件名 :main.c
- * 描述 :W5500 以太網(wǎng)模塊測試程序:Modbus-TCP 功能實(shí)現(xiàn)。
- * 實(shí)驗(yàn)平臺:STM32F103VCT6+W5500
- * 庫版本 :ST3.5.0
- **********************************************************************************/
- #include <string.h>
- #include <stdio.h>
- #include "stm32f10x.h"
- #include "W5500.h"
- #include "device.h"
- #include "systick.h"
- #include "led.h"
- #include "usart.h"
- #include "mb.h"
- #include "time.h"
- #include "mbtcp.h"
- #include "data.h"
- #include "port.h"
- #include "sys.h"
- #define TimeOut 1001
- #define LED_4 PAout(11)
- #define Control_Out_ON GPIO_SetBits(GPIOC,GPIO_Pin_13);
- #define Control_Out_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- /* ----------------------- Holding register Defines保持寄存器 ------------------------------------------*/
- u16 MissCNT[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- u8 Module_NUM1 = 0;
- u8 Module_NUM2 = 0;
- u8 Module_NUM3 = 0;
- u8 Module_NUM4 = 0;
- u16 b[6]={1,2,3,4,5,6};
- u8 Start_Send = 1; //給下一個從站發(fā)送地址數(shù)據(jù)標(biāo)志位,1有效
- u8 Step_Send = 1;
- u8 x=0;
- //BOOL Write_Finsh=FALSE;
- // 讀取ADR 引腳電平
- #define Adr0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15);
- #define Adr1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3);
- #define Adr2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4);
- #define Adr3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5);
- #define Adr4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6);
- #define Adr5 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7);
- #define Adr6 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8);
- #define Adr7 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9);
-
- u16 cycle_1s=0;
- u16 Slave_Addr;
- void Clear_Rxbuf(void)
- {
- RS485_RX.buf[0]=0;
- RS485_RX.buf[1]=0;
- RS485_RX.buf[2]=0;
- RS485_RX.buf[3]=0;
- RS485_RX.len=0;
- }
- void Clear_Txbuf(void)
- {
- RS485_TX.buf[0]=0;
- RS485_TX.buf[1]=0;
- RS485_TX.buf[2]=0;
- RS485_TX.buf[3]=0;
- RS485_TX.len=0;
- }
- void FeedingWatchdog(void)
- {
- TIM3->CNT=1;
- TIM_Cmd(TIM3,ENABLE);
- }
- void StopWatchdog(void)
- {
- TIM3->CNT=1;
- TIM_Cmd(TIM3,DISABLE);
- }
- BOOL CheakWatchdog_us(u16 Time) //us
- {
- BOOL Time_Out;
- if((TIM3->CNT)>Time)
- {
- Time_Out = TRUE;
- TIM_Cmd(TIM3,DISABLE);
- }
- else
- {
- Time_Out = FALSE;
- }
- return Time_Out;
- }
- void CheakWatchdog(u16 Time)
- {
- if(CheakWatchdog_us(Time)) //true = time out
- {
- // Test_value2++;
- // if(Slave_Station[Module_CNT].Command==SlaveCMD_Run)
- // {
- // LED1_ON;Test_value++;//while(1);
- // //Test_value2 = TIM3->CNT;
- // }
- // LED2_OFF;
- if((Slave_Station[Module_CNT].Command!=Comm_1CFull)&&(Slave_Station[Module_CNT].Command!=Comm_2CFull))//注意狀態(tài)值申明不能重復(fù) //&&(Slave_Station[Module_CNT].Command!=SlaveCMD_Run)
- {
- Err_Infor[Module_CNT].Curr_Err ++;//= Err_Infor[Module_CNT].Curr_Err+1000;
- if(Err_Infor[Module_CNT].Curr_Err>255)
- {
- Err_Infor[Module_CNT].Curr_Err = 255;
- Err_Infor[Module_CNT].Err_Flag=1;
- }
- MissCNT[Module_CNT] = Err_Infor[Module_CNT].Curr_Err*1000;
- if((MissCNT[Module_CNT]>3000))//1S內(nèi)連續(xù)掉4個數(shù)據(jù)
- Err_Infor[Module_CNT].Err_Flag=1;
- }
-
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- else
- {
- if(Module_CNT>0)
- {
- if(MissCNT[Module_CNT-1]>0)
- MissCNT[Module_CNT-1]--;
- }
- else
- ;
- }
- if(Module_CNT>0)
- {
- if(MissCNT[Module_CNT-1]>500)
- {
- Show_Error(SlaveMissErr);
- }
- else
- {
- Show_Error(ClearSlaveMiss);
- }
- }
- }
- /* ----------------------- Holding register Defines保持寄存器 ------------------------------------------*/
- #define REG_HOLDING_START 0
- #define REG_HOLDING_NREGS 64
- /* ----------------------- Static variables 靜態(tài)變量 ---------------------------------*/
- static unsigned short usRegHoldingStart = REG_HOLDING_START;
- static unsigned short usRegHoldingBuf[REG_HOLDING_NREGS];
- /* ----------------------- 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}; //數(shù)量低于8個還需要驗(yàn)證一下,是否需要加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 }; //數(shù)量8的整數(shù)倍。
- void BSP_LED(void); //LED指示線圈操作
-
- /* W5500 configuration */
- void W5500_Configuration(int t)
- {
- 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 */ //169,254,131,99//這里得IP不是任意設(shè)的,要與本機(jī)所連得IP在同一網(wǎng)段
- array[0]=169;
- array[1]=254;
- array[2]=131;
- 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 */ //11,89,45,56,22,0xc4 //11,89,45,56,22,0xc4
- 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]=169;
- array[1]=254;
- array[2]=131;
- array[3]=t;
- Write_Bytes(SIPR, array, 4);
- }
- void Set_Address(void)
- {
- u16 i = 0;
- /*******************第一階段,設(shè)置IO從站地址********************/
- Control_Out_ON; //發(fā)送一個IO信號
- while(RS485_RX.len == 0)
- {
- Show_Error(SlaveFBErr);
- } //等待接受第一個數(shù)據(jù)
- Show_Error(ClearErr);
- Module_NUM = 0;
- for(i=0;i<MaxModuleNum;i++) //最大支持16個模塊
- {
- if(RS485_RX.len!=0)
- {
- if((RS485_RX.buf[0]&Type_Mask)==Type_Mask) //判斷反饋信息是否為設(shè)備類型
- {
- Module_NUM++; //從站數(shù)量
- Slave_Addr=(Module_NUM|0x0140); //給從站發(fā)送地址
- IO_SetAddr(Slave_Addr);
- Clear_Rxbuf();
- }
- else
- {
- ;
- }
- Delay_ms(10); //等待下一個地址反饋,超時自動退出
- }
- else
- ;
- }
- Control_Out_OFF; //IO信號拉低
- for(i=0;i<Module_NUM;i++)
- RS485_RX.buf[i]=0; //接收數(shù)據(jù)緩沖區(qū)清空
- RS485_RX.len = 0; //接收數(shù)據(jù)長度清零
- Creat_Slave_Station(Module_NUM); //創(chuàng)建存儲從站設(shè)備類型、地址、數(shù)據(jù)的數(shù)組
- Creat_Err_Infor(Module_NUM); //創(chuàng)建存儲從站錯誤信息的數(shù)組
- Step1_Finish = TRUE; //第一階段結(jié)束標(biāo)志位
- }
- void Get_Type(void)
- {
- u16 i = 0;
- u8 Enc_Pulse_NUM=0;
- for(i=0;i<Module_NUM;i++)//i為從機(jī)地址
- {
- IO_SetAddr(Slave_Station[i].Address);
- while(RS485_RX.len != 4)//等待數(shù)據(jù)到來,4個數(shù)據(jù)幀
- {
- Show_Error(SlaveTypeErr);
- }
- Show_Error(ClearErr);
- Delay_ms(1); //收到數(shù)據(jù),立刻判斷發(fā)送的,會出現(xiàn)問題,延時一下就好了
- if(RS485_RX.buf[1]==Slave_Station[i].Address)
- {
- if((RS485_RX.buf[0]&Type_Mask) == Type_Mask)
- {
- Slave_Station[i].Type=RS485_RX.buf[0];
- if(Slave_Station[i].Type==ENC_PULSE)
- {
- Enc_Pulse_NUM++;
- }
- }
- Clear_Txbuf(); //清零
- }
- }
- Creat_EncPosition_Buffer(Enc_Pulse_NUM);
- Step2_Finish = TRUE;
- }
- void Data_Exchange_Step(void)
- {
- if(Step_Send==1)
- {
- if(Module_CNT==Module_NUM) //一輪掃描完成
- {
- cycle_1s++;
- if(cycle_1s==500)
- {
- PAout(11)=!PAout(11); //485通訊燈 1s翻轉(zhuǎn)一次
- cycle_1s=0;
- }
- Step_Send = 1;
- Start_Send = 1;
- x=0;
- Module_CNT = 0;
- Delayfrq = 0;
- Clear_Rxbuf();
- Clear_Txbuf();
- }
- else
- {
- switch(Slave_Station[Module_CNT].Type)
- {
- case IO_IN8_PNP: //數(shù)字輸入PNP
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- IO_Input(Slave_Station[Module_CNT]);
- // usRegHoldingBuf[0]=RS485_RX.buf[1];
- // usRegHoldingBuf[1]=Module_NUM;
-
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- u16 crc_data=0;
- u8 In_data[2]={0,0};
- In_data[0]=(RS485_RX.buf[1]&0xff);
- In_data[1]=(RS485_RX.buf[1]>>8);
- crc_data=RS485_RX.buf[2] | RS485_RX.buf[3]<<8;
-
- if(crc_data==usMBCRC16(In_data,2)) //校驗(yàn)兩個字節(jié)
- {
- Slave_Station[Module_CNT].data1=RS485_RX.buf[1];
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
- x++;
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case IO_IN8_NPN: //數(shù)字輸入NPN
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- IO_Input(Slave_Station[Module_CNT]);
- //usRegHoldingBuf[0]=RS485_RX.buf[1];usRegHoldingBuf[1]=Module_NUM;
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- u16 crc_data=0;
- u8 In_data[2]={0,0};
- In_data[0]=(RS485_RX.buf[1]&0xff);
- In_data[1]=(RS485_RX.buf[1]>>8);
- crc_data=RS485_RX.buf[2] | RS485_RX.buf[3]<<8;
- if(crc_data==usMBCRC16(In_data, 2)) //校驗(yàn)兩個字節(jié)
- {
- Slave_Station[Module_CNT].data1=RS485_RX.buf[1];
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
- x++;
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case IO_OUT8_NPN: //數(shù)字輸出NPN
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
- x++;
- IO_Output(Slave_Station[Module_CNT]);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- Start_Send=1;
- Clear_Rxbuf();
- Clear_Txbuf();
- Module_CNT++;
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case IO_OUT8_PNP: //數(shù)字輸出PNP
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
- x++;
- IO_Output(Slave_Station[Module_CNT]);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- Start_Send=1;
- Clear_Rxbuf();
- Clear_Txbuf();
- Module_CNT++;
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case AD_IN4_VDS: //電壓型4路模擬通用輸入
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- IO_Input(Slave_Station[Module_CNT]);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
-
- u16 crc_data=0;
- u8 In_data[8];
- In_data[0]=(RS485_RX.buf[1]&0xff);
- In_data[1]=(RS485_RX.buf[2]&0xff);
- In_data[2]=(RS485_RX.buf[3]&0xff);
- In_data[3]=(RS485_RX.buf[4]&0xff);
- In_data[4]=(RS485_RX.buf[5]&0xff);
- In_data[5]=(RS485_RX.buf[6]&0xff);
- In_data[6]=(RS485_RX.buf[7]&0xff);
- In_data[7]=(RS485_RX.buf[8]&0xff);
- crc_data=RS485_RX.buf[9] | RS485_RX.buf[10]<<8;
- if(crc_data==usMBCRC16(In_data, 8)) //校驗(yàn)8個字節(jié)
- {
- Slave_Station[Module_CNT].data1=RS485_RX.buf[1]|RS485_RX.buf[2]<<8;
- if((Slave_Station[Module_CNT].data1>0)|(Slave_Station[Module_CNT].data1==0))
- {
- Slave_Station[Module_CNT].data1-=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
- x++;
- }
- else
- {
- Slave_Station[Module_CNT].data1+=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
- x++;
- }
- Slave_Station[Module_CNT].data2=RS485_RX.buf[3]|RS485_RX.buf[4]<<8;
- if((Slave_Station[Module_CNT].data2>0)|(Slave_Station[Module_CNT].data2==0))
- {
- Slave_Station[Module_CNT].data2-=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data2;
- x++;
- }
- else
- {
-
- Slave_Station[Module_CNT].data2+=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data2;
- x++;
- }
- Slave_Station[Module_CNT].data3=RS485_RX.buf[5]|RS485_RX.buf[6]<<8;
- if((Slave_Station[Module_CNT].data3>0)|(Slave_Station[Module_CNT].data3==0))
- {
- Slave_Station[Module_CNT].data3-=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data3;
- x++;
- }
- else
- {
- Slave_Station[Module_CNT].data3+=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data3;
- x++;
- }
- Slave_Station[Module_CNT].data4=RS485_RX.buf[7]|RS485_RX.buf[8]<<8;
- if((Slave_Station[Module_CNT].data4>0)|(Slave_Station[Module_CNT].data4==0))
- {
- Slave_Station[Module_CNT].data4-=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data4;
- x++;
- }
- else
- {
- Slave_Station[Module_CNT].data4+=32768;
- usRegHoldingBuf[x]=Slave_Station[Module_CNT].data4;
- x++;
- }
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case AD_IN2_BR: //壓力傳感器2路輸入
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- usRegHoldingBuf[Module_CNT*8+1]=Slave_Station[Module_CNT].Address;
- Slave_Station[Module_CNT].Command=usRegHoldingBuf[Module_CNT*8+3];
- IO_AInput(Slave_Station[Module_CNT]);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
-
- u16 crc_data=0;
- u8 In_data[8];
- In_data[0]=(RS485_RX.buf[2]&0xff);
- In_data[1]=(RS485_RX.buf[3]&0xff);
- In_data[2]=(RS485_RX.buf[4]&0xff);
- In_data[3]=(RS485_RX.buf[5]&0xff);
- crc_data=RS485_RX.buf[6] | RS485_RX.buf[7]<<8;
- if(crc_data==usMBCRC16(In_data,4)) //校驗(yàn)8個字節(jié)
- {
- Slave_Station[Module_CNT].State=RS485_RX.buf[1];
- usRegHoldingBuf[Module_CNT*8+3]=Slave_Station[Module_CNT].State;
- Slave_Station[Module_CNT].data1=(RS485_RX.buf[2]|RS485_RX.buf[3]<<8)-0x8000; //+32767
- Slave_Station[Module_CNT].data2=(RS485_RX.buf[4]|RS485_RX.buf[5]<<8)-0x8000;
- usRegHoldingBuf[Module_CNT*8+4]=Slave_Station[Module_CNT].data1;
- usRegHoldingBuf[Module_CNT*8+5]=Slave_Station[Module_CNT].data2;
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case DA_OUT4_VDS: //模擬輸出
- {
- if(Start_Send == 1)
- {
- Start_Send = 0;
- Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
- x++;
- Slave_Station[Module_CNT].data2=usRegHoldingBuf[x];
- x++;
- Slave_Station[Module_CNT].data3=usRegHoldingBuf[x];
- x++;
- Slave_Station[Module_CNT].data4=usRegHoldingBuf[x];
- x++;
- AO_Output(Slave_Station[Module_CNT]);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- Start_Send=1;
- Clear_Rxbuf();
- Clear_Txbuf();
- Module_CNT++;
- }
- else
- {
- ;
- }
- }
- CheakWatchdog(200);
- }
- break;
- case ENC_PULSE: //編碼器脈沖模塊
- {
- if(Start_Send == 1)
- {
- if(Slave_Station[Module_CNT].Command!=0)//等待SDO初始值SETPOS
- {
- Start_Send = 0;
- IO_EncInput(Slave_Station[Module_CNT],*Enc_Config);
- FeedingWatchdog();
- Clear_Txbuf();
- }
- else
- {
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- {
- if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
- {
- if(RS485_RX.len == 2)//配置完成反饋,空閑反饋
- {
- Slave_Station[Module_CNT].State = RS485_RX.buf[1];
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- else if(RS485_RX.len == 12)//正式運(yùn)行RUN完成反饋
- {
- u16 crc_data=0;
- u8 In_data[8];
- In_data[0]=(RS485_RX.buf[2]&0xff);
- In_data[1]=(RS485_RX.buf[3]&0xff);
- In_data[2]=(RS485_RX.buf[4]&0xff);
- In_data[3]=(RS485_RX.buf[5]&0xff);
- In_data[4]=(RS485_RX.buf[6]&0xff);
- In_data[5]=(RS485_RX.buf[7]&0xff);
- In_data[6]=(RS485_RX.buf[8]&0xff);
- In_data[7]=(RS485_RX.buf[9]&0xff);
- crc_data=RS485_RX.buf[10] | RS485_RX.buf[11]<<8;
- if(crc_data==usMBCRC16(In_data,8)) //校驗(yàn)8個字節(jié)
- {
- Slave_Station[Module_CNT].State=RS485_RX.buf[1];
- Slave_Station[Module_CNT].data1=(RS485_RX.buf[2]|(RS485_RX.buf[3]<<8)); //+32767
- Slave_Station[Module_CNT].data2=(RS485_RX.buf[4]|(RS485_RX.buf[5]<<8));
- Slave_Station[Module_CNT].data3=(RS485_RX.buf[6]|(RS485_RX.buf[7]<<8)); //+32767
- Slave_Station[Module_CNT].data4=(RS485_RX.buf[8]|(RS485_RX.buf[9]<<8));
- Module_CNT++;
- Start_Send=1;
- Clear_Rxbuf();
- }
- }
- else
- ;
- }
- else
- ;
- }
- CheakWatchdog(600);
- }
- break;
- default:
- {
- Module_CNT=0;
- Start_Send=1;
- Clear_Rxbuf();
- Clear_Txbuf();
- }
- break;
- }
- // if(CheakWatchdog_us(200)) //true = time out
- // {
- // if((Slave_Station[Module_CNT].Command!=Comm_1CFull)&&(Slave_Station[Module_CNT].Command!=Comm_2CFull))
- // {
- // Err_Infor[Module_CNT].Curr_Err ++;//= Err_Infor[Module_CNT].Curr_Err+1000;
- // if(Err_Infor[Module_CNT].Curr_Err>255)
- // {
- // Err_Infor[Module_CNT].Curr_Err = 255;
- // Err_Infor[Module_CNT].Err_Flag=1;
- // }
- // MissCNT[Module_CNT] = Err_Infor[Module_CNT].Curr_Err*1000;
- // if((MissCNT[Module_CNT]>3000))//1S內(nèi)連續(xù)掉4個數(shù)據(jù)
- // Err_Infor[Module_CNT].Err_Flag=1;
- // }
- // Module_CNT++;
- // Start_Send=1;
- // Clear_Rxbuf();
- // }
- // else
- // {
- // if(Module_CNT>0)
- // {
- // if(MissCNT[Module_CNT-1]>0)
- // MissCNT[Module_CNT-1]--;
- // }
- // else
- // ;
- // }
- // if(Module_CNT>0)
- // {
- // if(MissCNT[Module_CNT-1]>500)
- // {
- // Show_Error(SlaveMissErr);
- // }
- // else
- // {
- // Show_Error(ClearSlaveMiss);
- // }
- // }
- }
- }
- else
- {
- ;
- }
- }
- /*****************************************************************
- Main Program
- *****************************************************************/
- int main(void)
- {
- unsigned int i;
- unsigned int t=0,d=1;
- unsigned int a[8];
- /* Initialize STM32F103 */
- System_Initialization();//SPI 引腳是使能
- SysTick_Init();//定時使能
- /* Config W5500 */
- a[0]=!Adr0;a[1]=!Adr1;a[2]=!Adr2;a[3]=!Adr3;a[4]=!Adr4;a[5]=!Adr5;a[6]=!Adr6;a[7]=!Adr7;//確定從站地址
- for(i=0;i<8;i++)
- {
- t=(t+a[i]*d);
- d=d*2;
- }
- /************************第一階段********************************/
- /* Config W5500 */
- W5500_Configuration(t);
- Delay_ms(200);
- // /* Modbus-TCP Init */
- eMBTCPInit(MB_TCP_PORT_USE_DEFAULT); //502端口 SOCKET 0
- Delay_ms(200);
- // /* Enable Modbus-TCP Stack */
- eMBEnable();
- //printf("\r\nModbus-TCP Start!\r\n");
- /**********************第二階段********************************/
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
F1芯片+w5500+modbus 串口發(fā)送數(shù)據(jù)DMA1+串口1 數(shù)據(jù)傳輸無問題+4852.20.7z
(234.22 KB, 下載次數(shù): 262)
2019-2-28 18:50 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|