找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F1+w5500+modbus串口发送数据DMA1+串口1源程序

  [复制链接]
跳转到指定楼层
楼主
ID:403520 发表于 2019-2-28 17:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PC机采用moudbus TCP与stm32进行数据读写,stm32采用485与IO模块进行数据的交互

单片机源程序如下:
  1. /******************** (C) COPYRIGHT 2014 91mcu **************************
  2. * 文件名  :main.c
  3. * 描述    :W5500 以太网模块测试程序:Modbus-TCP 功能实现。      
  4. * 实验平台:STM32F103VCT6+W5500
  5. * 库版本  :ST3.5.0
  6. **********************************************************************************/        
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include "stm32f10x.h"
  10. #include "W5500.h"
  11. #include "device.h"
  12. #include "systick.h"
  13. #include "led.h"
  14. #include "usart.h"         
  15. #include "mb.h"
  16. #include "time.h"
  17. #include "mbtcp.h"
  18. #include "data.h"
  19. #include "port.h"
  20. #include "sys.h"
  21. #define TimeOut 1001  
  22. #define LED_4                PAout(11)
  23. #define Control_Out_ON       GPIO_SetBits(GPIOC,GPIO_Pin_13);   
  24. #define Control_Out_OFF      GPIO_ResetBits(GPIOC,GPIO_Pin_13);
  25. /* ----------------------- Holding register Defines保持寄存器 ------------------------------------------*/
  26. 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};
  27. u8 Module_NUM1 = 0;
  28. u8 Module_NUM2 = 0;
  29. u8 Module_NUM3 = 0;
  30. u8 Module_NUM4 = 0;
  31. u16 b[6]={1,2,3,4,5,6};
  32. u8  Start_Send  = 1;   //给下一个从站发送地址数据标志位,1有效
  33. u8  Step_Send  = 1;               
  34. u8 x=0;

  35. //BOOL Write_Finsh=FALSE;        
  36. // 读取ADR 引脚电平
  37. #define Adr0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15);
  38. #define Adr1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3);
  39. #define Adr2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4);
  40. #define Adr3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5);
  41. #define Adr4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6);
  42. #define Adr5 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7);
  43. #define Adr6 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8);
  44. #define Adr7 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9);
  45.         
  46. u16  cycle_1s=0;
  47. u16  Slave_Addr;
  48. void Clear_Rxbuf(void)
  49. {
  50.         RS485_RX.buf[0]=0;
  51.         RS485_RX.buf[1]=0;
  52.         RS485_RX.buf[2]=0;        
  53.         RS485_RX.buf[3]=0;        
  54.         RS485_RX.len=0;                                
  55. }
  56. void Clear_Txbuf(void)
  57. {
  58.         RS485_TX.buf[0]=0;
  59.         RS485_TX.buf[1]=0;
  60.         RS485_TX.buf[2]=0;
  61.         RS485_TX.buf[3]=0;
  62.         RS485_TX.len=0;                                
  63. }
  64. void FeedingWatchdog(void)
  65. {
  66.                 TIM3->CNT=1;
  67.                 TIM_Cmd(TIM3,ENABLE);
  68. }
  69. void StopWatchdog(void)
  70. {
  71.                 TIM3->CNT=1;
  72.          TIM_Cmd(TIM3,DISABLE);
  73. }
  74. BOOL CheakWatchdog_us(u16 Time) //us
  75. {
  76.         BOOL Time_Out;
  77.         if((TIM3->CNT)>Time)
  78.         {
  79.          Time_Out = TRUE;
  80.          TIM_Cmd(TIM3,DISABLE);
  81.         }
  82.         else
  83.         {
  84.                 Time_Out = FALSE;
  85.         }
  86.         return Time_Out;
  87. }
  88. void CheakWatchdog(u16 Time)
  89. {
  90.         if(CheakWatchdog_us(Time))        //true = time out
  91.         {
  92. //                Test_value2++;
  93. //                if(Slave_Station[Module_CNT].Command==SlaveCMD_Run)
  94. //                {
  95. //                        LED1_ON;Test_value++;//while(1);
  96. //                        //Test_value2 = TIM3->CNT;
  97. //                }
  98. //                LED2_OFF;
  99.                 if((Slave_Station[Module_CNT].Command!=Comm_1CFull)&&(Slave_Station[Module_CNT].Command!=Comm_2CFull))//注意状态值申明不能重复 //&&(Slave_Station[Module_CNT].Command!=SlaveCMD_Run)
  100.                 {
  101.                         Err_Infor[Module_CNT].Curr_Err ++;//= Err_Infor[Module_CNT].Curr_Err+1000;
  102.                         if(Err_Infor[Module_CNT].Curr_Err>255)
  103.                         {
  104.                                 Err_Infor[Module_CNT].Curr_Err = 255;
  105.                                 Err_Infor[Module_CNT].Err_Flag=1;
  106.                         }               
  107.                         MissCNT[Module_CNT] = Err_Infor[Module_CNT].Curr_Err*1000;
  108.                         if((MissCNT[Module_CNT]>3000))//1S内连续掉4个数据
  109.                                 Err_Infor[Module_CNT].Err_Flag=1;
  110.                 }
  111.                
  112.                 Module_CNT++;
  113.                 Start_Send=1;               
  114.                 Clear_Rxbuf();
  115.         }        
  116.         else
  117.         {
  118.                 if(Module_CNT>0)
  119.                 {
  120.                         if(MissCNT[Module_CNT-1]>0)
  121.                                 MissCNT[Module_CNT-1]--;
  122.                 }
  123.                 else
  124.                         ;
  125.         }        
  126.         if(Module_CNT>0)
  127.         {
  128.                 if(MissCNT[Module_CNT-1]>500)
  129.                 {
  130.                         Show_Error(SlaveMissErr);                                
  131.                 }
  132.                 else
  133.                 {                                
  134.                         Show_Error(ClearSlaveMiss);
  135.                 }
  136.         }                        
  137. }

  138. /* ----------------------- Holding register Defines保持寄存器 ------------------------------------------*/
  139. #define REG_HOLDING_START 0
  140. #define REG_HOLDING_NREGS 64

  141. /* ----------------------- Static variables 静态变量 ---------------------------------*/
  142. static unsigned short usRegHoldingStart = REG_HOLDING_START;
  143. static unsigned short usRegHoldingBuf[REG_HOLDING_NREGS];

  144. /* ----------------------- input register Defines ------------------------------------------*/
  145. #define REG_INPUT_START 1000
  146. #define REG_INPUT_NREGS 4

  147. /* ----------------------- Static variables ---------------------------------*/
  148. static unsigned short usRegInputStart = REG_INPUT_START;
  149. static unsigned short usRegInputBuf[REG_INPUT_NREGS]={0,0,0x5599,0x5588};

  150. /* ----------------------- coils register Defines ------------------------------------------*/
  151. #define REG_COILS_START     1000
  152. #define REG_COILS_SIZE      16

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

  155. /* ----------------------- discrete register Defines 离散寄存器------------------------------------------*/
  156. #define REG_DISC_START     1000
  157. #define REG_DISC_SIZE      16

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


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

  161. /* W5500 configuration */
  162. void W5500_Configuration(int t)
  163. {
  164.         unsigned char array[6];
  165.         GPIO_SetBits(GPIO_W5500_RST_PORT, GPIO_W5500_RST_Pin);//低电平有效
  166.         Delay_ms(100);    /*delay 100ms 使用systick 1ms时基的延时*/
  167.         while((Read_1_Byte(PHYCFGR)&LINK)==0);                 /* Waiting for Ethernet Link */ //看第八位

  168.         Write_1_Byte(MR, RST);
  169.         Delay_ms(20);                /*delay 20ms */

  170.         /* Set Gateway IP as: 192.168.1.1 */ //169,254,131,99//这里得IP不是任意设的,要与本机所连得IP在同一网段
  171.         array[0]=169;
  172.         array[1]=254;
  173.         array[2]=131;
  174.         array[3]=1;
  175.         Write_Bytes(GAR, array, 4);

  176.         /* Set Subnet Mask as: 255.255.255.0 */
  177.         array[0]=255;
  178.         array[1]=255;
  179.         array[2]=255;
  180.         array[3]=0;
  181.         Write_Bytes(SUBR, array, 4);

  182.         /* Set MAC Address as: 0x48,0x53,0x00,0x57,0x55,0x00 */ //11,89,45,56,22,0xc4  //11,89,45,56,22,0xc4
  183.         array[0]=0x48;
  184.         array[1]=0x53;
  185.         array[2]=0x00;
  186.         array[3]=0x57;
  187.         array[4]=0x55;
  188.         array[5]=0x00;
  189.         Write_Bytes(SHAR, array, 6);

  190.         /* Set W5500 IP as: 192.168.1.128 */
  191.         array[0]=169;
  192.         array[1]=254;
  193.         array[2]=131;
  194.         array[3]=t;
  195.         Write_Bytes(SIPR, array, 4);
  196. }


  197. void Set_Address(void)
  198. {
  199.                 u16 i = 0;
  200.           /*******************第一阶段,设置IO从站地址********************/
  201.                 Control_Out_ON;         //发送一个IO信号
  202.                 while(RS485_RX.len == 0)
  203.                 {
  204.      Show_Error(SlaveFBErr);                        
  205.                 }                                                 //等待接受第一个数据
  206.      Show_Error(ClearErr);        
  207.                 Module_NUM = 0;
  208.                 for(i=0;i<MaxModuleNum;i++)                       //最大支持16个模块
  209.                 {
  210.                         if(RS485_RX.len!=0)
  211.                         {
  212.                                  if((RS485_RX.buf[0]&Type_Mask)==Type_Mask)   //判断反馈信息是否为设备类型
  213.                                  {               
  214.                                           Module_NUM++;                             //从站数量
  215.                                                 Slave_Addr=(Module_NUM|0x0140);           //给从站发送地址
  216.                                                 IO_SetAddr(Slave_Addr);
  217.             Clear_Rxbuf();                                                
  218.                                  }
  219.                                  else
  220.                                  {
  221.                                                 ;
  222.                                  }
  223.                                           Delay_ms(10);                             //等待下一个地址反馈,超时自动退出
  224.                         }
  225.                         else
  226.                                    ;                                               
  227.                 }
  228.           Control_Out_OFF;                                  //IO信号拉低                     
  229.                 for(i=0;i<Module_NUM;i++)
  230.                 RS485_RX.buf[i]=0;                                //接收数据缓冲区清空
  231.                 RS485_RX.len = 0;                                 //接收数据长度清零
  232.                 Creat_Slave_Station(Module_NUM);                  //创建存储从站设备类型、地址、数据的数组
  233.                 Creat_Err_Infor(Module_NUM);                      //创建存储从站错误信息的数组
  234.                 Step1_Finish = TRUE;                              //第一阶段结束标志位                 
  235. }

  236. void Get_Type(void)
  237. {
  238.                 u16 i = 0;        
  239.          u8         Enc_Pulse_NUM=0;
  240.                 for(i=0;i<Module_NUM;i++)//i为从机地址
  241.                 {        
  242.                         IO_SetAddr(Slave_Station[i].Address);        
  243.                         while(RS485_RX.len != 4)//等待数据到来,4个数据帧
  244.                         {
  245.         Show_Error(SlaveTypeErr);                                
  246.                         }
  247.       Show_Error(ClearErr);
  248.                   Delay_ms(1);  //收到数据,立刻判断发送的,会出现问题,延时一下就好了
  249.                         if(RS485_RX.buf[1]==Slave_Station[i].Address)
  250.                         {
  251.                                 if((RS485_RX.buf[0]&Type_Mask) == Type_Mask)
  252.                                 {
  253.                                  Slave_Station[i].Type=RS485_RX.buf[0];                        
  254.            if(Slave_Station[i].Type==ENC_PULSE)
  255.                                  {
  256.            Enc_Pulse_NUM++;                                
  257.                                  }                                                                                 
  258.                                 }
  259.         Clear_Txbuf();          //清零                                       
  260.                         }
  261.                 }                        
  262.     Creat_EncPosition_Buffer(Enc_Pulse_NUM);               
  263.                 Step2_Finish = TRUE;        
  264. }

  265. void Data_Exchange_Step(void)
  266. {        
  267.         if(Step_Send==1)
  268.         {
  269.                 if(Module_CNT==Module_NUM) //一轮扫描完成        
  270.                 {                 
  271.                         cycle_1s++;
  272.                         if(cycle_1s==500)
  273.                         {
  274.                   PAout(11)=!PAout(11);       //485通讯灯  1s翻转一次
  275.                                 cycle_1s=0;
  276.                         }        
  277.                         Step_Send  = 1;
  278.                         Start_Send  = 1;
  279.       x=0;
  280.                         Module_CNT = 0;
  281.                         Delayfrq = 0;
  282.                         Clear_Rxbuf();
  283.                         Clear_Txbuf();
  284.                 }
  285.                 else
  286.                 {        
  287.                         switch(Slave_Station[Module_CNT].Type)
  288.                         {
  289.                         case IO_IN8_PNP:                 //数字输入PNP
  290.                         {
  291.                                 if(Start_Send == 1)
  292.                                 {
  293.                                         Start_Send = 0;
  294.                                         IO_Input(Slave_Station[Module_CNT]);
  295. //                                        usRegHoldingBuf[0]=RS485_RX.buf[1];
  296. //                                        usRegHoldingBuf[1]=Module_NUM;
  297.                                        
  298.                                         FeedingWatchdog();
  299.                                         Clear_Txbuf();
  300.                                 }
  301.                                 else
  302.                                 {
  303.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  304.                                          {
  305.                    u16 crc_data=0;                       
  306.                                                  u8  In_data[2]={0,0};                                    
  307.                                                  In_data[0]=(RS485_RX.buf[1]&0xff);
  308.                                                  In_data[1]=(RS485_RX.buf[1]>>8);
  309.                                                  crc_data=RS485_RX.buf[2] | RS485_RX.buf[3]<<8;   
  310.                                                 
  311.                                                  if(crc_data==usMBCRC16(In_data,2))   //校验两个字节            
  312.                                                  {
  313.                                                          Slave_Station[Module_CNT].data1=RS485_RX.buf[1];        
  314.                                                          usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
  315.                                                          x++;
  316.                                                          Module_CNT++;
  317.                                                          Start_Send=1;
  318.                                                          Clear_Rxbuf();                                                
  319.                                                  }                                                                        
  320.                                          }
  321.                                         else
  322.                                          {
  323.                                                  ;
  324.                                          }                                                         
  325.                                 }
  326.                                 CheakWatchdog(200);        
  327.                         }
  328.                         break;     
  329.                         case IO_IN8_NPN:                 //数字输入NPN
  330.                         {        
  331.                                 if(Start_Send == 1)
  332.                                 {
  333.                                         Start_Send = 0;
  334.                                         IO_Input(Slave_Station[Module_CNT]);
  335.                                                 //usRegHoldingBuf[0]=RS485_RX.buf[1];usRegHoldingBuf[1]=Module_NUM;
  336.                                         FeedingWatchdog();
  337.                                         Clear_Txbuf();
  338.                                 }
  339.                                 else
  340.                                 {
  341.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  342.                                          {
  343.                    u16 crc_data=0;                       
  344.                                                  u8  In_data[2]={0,0};                                    
  345.                                                  In_data[0]=(RS485_RX.buf[1]&0xff);
  346.                                                  In_data[1]=(RS485_RX.buf[1]>>8);
  347.                                                  crc_data=RS485_RX.buf[2] | RS485_RX.buf[3]<<8;   
  348.                                                  if(crc_data==usMBCRC16(In_data, 2))   //校验两个字节            
  349.                                                  {
  350.                                                          Slave_Station[Module_CNT].data1=RS485_RX.buf[1];        
  351.                                                          usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
  352.                                                          x++;
  353.                                                          Module_CNT++;
  354.                                                          Start_Send=1;
  355.                                                          Clear_Rxbuf();                                                
  356.                                                  }                                                                        
  357.                                          }
  358.                                         else
  359.                                          {
  360.                                                  ;
  361.                                          }                                                         
  362.                                 }
  363.                                 CheakWatchdog(200);        
  364.                         }
  365.                         break;     
  366.                         case IO_OUT8_NPN:                 //数字输出NPN
  367.                         {
  368.                                 if(Start_Send == 1)
  369.                                 {        
  370.                                         Start_Send = 0;
  371.                                         Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
  372.                                         x++;
  373.                                         IO_Output(Slave_Station[Module_CNT]);
  374.                                         FeedingWatchdog();
  375.                                         Clear_Txbuf();                                
  376.                                 }                                 
  377.                                 else
  378.                                 {
  379.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  380.                                         {
  381.                                                 Start_Send=1;
  382.                                                 Clear_Rxbuf();        
  383.                                                 Clear_Txbuf();
  384.                                                 Module_CNT++;                                                
  385.                                         }
  386.                                         else
  387.                                         {
  388.                                                 ;
  389.                                         }                                                                                
  390.                                 }               
  391.                                 CheakWatchdog(200);                                
  392.                         }        
  393.                         break;   
  394.                         case IO_OUT8_PNP:                 //数字输出PNP  
  395.                         {                        
  396.                                 if(Start_Send == 1)
  397.                                 {
  398.                                         Start_Send = 0;
  399.                                         Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
  400.                                         x++;
  401.                                         IO_Output(Slave_Station[Module_CNT]);
  402.                                         FeedingWatchdog();
  403.                                         Clear_Txbuf();                                                
  404.                                 }                                 
  405.                                 else
  406.                                 {
  407.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  408.                                         {
  409.                                                 Start_Send=1;
  410.                                                 Clear_Rxbuf();        
  411.                                                 Clear_Txbuf();
  412.                                                 Module_CNT++;                                                
  413.                                         }
  414.                                         else
  415.                                         {
  416.                                                 ;
  417.                                         }                                                                                
  418.                                 }        
  419.                                 CheakWatchdog(200);        
  420.                         }
  421.                         break;
  422.       case AD_IN4_VDS:                  //电压型4路模拟通用输入
  423.                         {        
  424.                                 if(Start_Send == 1)
  425.                                 {
  426.                                         Start_Send = 0;
  427.                                         IO_Input(Slave_Station[Module_CNT]);
  428.                                         FeedingWatchdog();
  429.                                         Clear_Txbuf();
  430.                                 }
  431.                                 else
  432.                                 {
  433.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  434.                                          {
  435.                                           
  436.                    u16 crc_data=0;                       
  437.                                                  u8  In_data[8];                                    
  438.                                                  In_data[0]=(RS485_RX.buf[1]&0xff);
  439.                                                  In_data[1]=(RS485_RX.buf[2]&0xff);
  440.                                                  In_data[2]=(RS485_RX.buf[3]&0xff);
  441.                                                  In_data[3]=(RS485_RX.buf[4]&0xff);        
  442.                                                  In_data[4]=(RS485_RX.buf[5]&0xff);
  443.                                                  In_data[5]=(RS485_RX.buf[6]&0xff);
  444.                                                  In_data[6]=(RS485_RX.buf[7]&0xff);
  445.                                                  In_data[7]=(RS485_RX.buf[8]&0xff);        
  446.                                                  crc_data=RS485_RX.buf[9] | RS485_RX.buf[10]<<8;   
  447.                                                  if(crc_data==usMBCRC16(In_data, 8))   //校验8个字节            
  448.                                                  {
  449.                Slave_Station[Module_CNT].data1=RS485_RX.buf[1]|RS485_RX.buf[2]<<8;                                                         
  450.                if((Slave_Station[Module_CNT].data1>0)|(Slave_Station[Module_CNT].data1==0))               
  451.                                                          {
  452.                                                                  Slave_Station[Module_CNT].data1-=32768;
  453.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
  454.                                                                  x++;
  455.                                                          }
  456.                                                          else
  457.                                                          {
  458.                                                                 Slave_Station[Module_CNT].data1+=32768;                        
  459.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data1;
  460.                                                                  x++;
  461.                                                          }
  462.                                                          Slave_Station[Module_CNT].data2=RS485_RX.buf[3]|RS485_RX.buf[4]<<8;
  463.                if((Slave_Station[Module_CNT].data2>0)|(Slave_Station[Module_CNT].data2==0))
  464.                                                          {
  465.                                                                  Slave_Station[Module_CNT].data2-=32768;
  466.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data2;
  467.                                                                  x++;
  468.                                                          }
  469.                                                          else
  470.                                                          {
  471.                                                                  
  472.                                                                 Slave_Station[Module_CNT].data2+=32768;
  473.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data2;
  474.                                                                  x++;
  475.                                                          }
  476.                                                         Slave_Station[Module_CNT].data3=RS485_RX.buf[5]|RS485_RX.buf[6]<<8;
  477.                if((Slave_Station[Module_CNT].data3>0)|(Slave_Station[Module_CNT].data3==0))
  478.                                                          {
  479.                                                                 Slave_Station[Module_CNT].data3-=32768;
  480.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data3;
  481.                                                                  x++;
  482.                                                          }
  483.                                                          else
  484.                                                          {
  485.                                                                 Slave_Station[Module_CNT].data3+=32768;
  486.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data3;
  487.                                                                  x++;
  488.                                                          }
  489.                                                          Slave_Station[Module_CNT].data4=RS485_RX.buf[7]|RS485_RX.buf[8]<<8;
  490.                if((Slave_Station[Module_CNT].data4>0)|(Slave_Station[Module_CNT].data4==0))
  491.                                                          {
  492.                                                                  Slave_Station[Module_CNT].data4-=32768;
  493.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data4;
  494.                                                                  x++;
  495.                                                          }
  496.                                                          else
  497.                                                          {
  498.                                                                 Slave_Station[Module_CNT].data4+=32768;
  499.                                                                  usRegHoldingBuf[x]=Slave_Station[Module_CNT].data4;
  500.                                                                  x++;
  501.                                                          }
  502.                                                          Module_CNT++;
  503.                                                          Start_Send=1;
  504.                                                          Clear_Rxbuf();                                                
  505.                                                  }                                                                        
  506.                                          }
  507.                                         else
  508.                                          {
  509.                                                  ;
  510.                                          }                                                         
  511.                                 }
  512.                                 CheakWatchdog(200);        
  513.                         }
  514.                         break;         
  515.       case AD_IN2_BR:               //压力传感器2路输入
  516.                         {        
  517.                                 if(Start_Send == 1)
  518.                                 {
  519.                                         Start_Send = 0;
  520.                                         usRegHoldingBuf[Module_CNT*8+1]=Slave_Station[Module_CNT].Address;
  521.                                   Slave_Station[Module_CNT].Command=usRegHoldingBuf[Module_CNT*8+3];
  522.                                         IO_AInput(Slave_Station[Module_CNT]);
  523.                                         FeedingWatchdog();
  524.                                         Clear_Txbuf();
  525.                                 }                                
  526.                                 else
  527.                                 {
  528.                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  529.                                          {
  530.                                           
  531.                    u16 crc_data=0;                       
  532.                                                  u8  In_data[8];                                                
  533.                                                  In_data[0]=(RS485_RX.buf[2]&0xff);
  534.                                                  In_data[1]=(RS485_RX.buf[3]&0xff);
  535.                                                  In_data[2]=(RS485_RX.buf[4]&0xff);
  536.                                                  In_data[3]=(RS485_RX.buf[5]&0xff);        
  537.                                                  crc_data=RS485_RX.buf[6] | RS485_RX.buf[7]<<8;                                          
  538.                                                  if(crc_data==usMBCRC16(In_data,4))   //校验8个字节            
  539.                                                  {
  540.                Slave_Station[Module_CNT].State=RS485_RX.buf[1];
  541.                                                          usRegHoldingBuf[Module_CNT*8+3]=Slave_Station[Module_CNT].State;
  542.                Slave_Station[Module_CNT].data1=(RS485_RX.buf[2]|RS485_RX.buf[3]<<8)-0x8000;                        //+32767                                                                                 
  543.                                                          Slave_Station[Module_CNT].data2=(RS485_RX.buf[4]|RS485_RX.buf[5]<<8)-0x8000;               
  544.                                                          usRegHoldingBuf[Module_CNT*8+4]=Slave_Station[Module_CNT].data1;        
  545.                                                          usRegHoldingBuf[Module_CNT*8+5]=Slave_Station[Module_CNT].data2;                                                         
  546.                                                          Module_CNT++;
  547.                                                          Start_Send=1;
  548.                                                          Clear_Rxbuf();                                                
  549.                                                  }                                                                        
  550.                                          }
  551.                                         else
  552.                                          {
  553.                                                  ;
  554.                                          }                                                         
  555.                                 }
  556.                                 CheakWatchdog(200);        
  557.                         }
  558.                         break;
  559.        case DA_OUT4_VDS:                 //模拟输出
  560.                                 {
  561.                                         if(Start_Send == 1)
  562.                                         {
  563.                                                 Start_Send = 0;
  564.                                                 Slave_Station[Module_CNT].data1=usRegHoldingBuf[x];
  565.                                         x++;
  566.                                                 Slave_Station[Module_CNT].data2=usRegHoldingBuf[x];
  567.                                         x++;
  568.                                                 Slave_Station[Module_CNT].data3=usRegHoldingBuf[x];
  569.                                         x++;
  570.                                                 Slave_Station[Module_CNT].data4=usRegHoldingBuf[x];
  571.                                         x++;
  572.                                                 AO_Output(Slave_Station[Module_CNT]);
  573.                                                 FeedingWatchdog();
  574.                                                 Clear_Txbuf();                                
  575.                                         }                                 
  576.                                         else
  577.                                         {
  578.                                                 if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  579.                                                 {
  580.                                                         Start_Send=1;
  581.                                                         Clear_Rxbuf();
  582.                                                         Clear_Txbuf();
  583.                                                         Module_CNT++;                                                
  584.                                                 }
  585.                                                 else
  586.                                                 {
  587.                                                         ;
  588.                                                 }                                                                                
  589.                                         }
  590.                                         CheakWatchdog(200);                                                
  591.                                 }        
  592.                                 break;         
  593.            case ENC_PULSE:                  //编码器脉冲模块
  594.                                 {
  595.                                                 if(Start_Send == 1)
  596.                                                 {
  597.                                                         if(Slave_Station[Module_CNT].Command!=0)//等待SDO初始值SETPOS                                
  598.                                                         {                                                
  599.                                                                  Start_Send = 0;                                                               
  600.                                                                  IO_EncInput(Slave_Station[Module_CNT],*Enc_Config);
  601.                                                                  FeedingWatchdog();
  602.                                                                  Clear_Txbuf();
  603.                                                         }
  604.                                                         else
  605.                                                         {
  606.                                                                  Module_CNT++;
  607.                                                                  Start_Send=1;
  608.                                                                  Clear_Rxbuf();
  609.                                                         }                                                        
  610.                                                 }                                                               
  611.                                                 else
  612.                                                 {
  613.                                                         if(RS485_RX.buf[0]==Slave_Station[Module_CNT].Address)
  614.                                                          {                                                                        
  615.                                                                  if(RS485_RX.len == 2)//配置完成反馈,空闲反馈
  616.                                                                  {               
  617.                                                                          Slave_Station[Module_CNT].State = RS485_RX.buf[1];
  618.                                                                          Module_CNT++;
  619.                                                                          Start_Send=1;
  620.                                                                          Clear_Rxbuf();
  621.                                                                  }
  622.                                                                  else if(RS485_RX.len == 12)//正式运行RUN完成反馈
  623.                                                                  {
  624.                                                                          u16 crc_data=0;                       
  625.                                                                          u8  In_data[8];                                       
  626.                                                                          In_data[0]=(RS485_RX.buf[2]&0xff);
  627.                                                                          In_data[1]=(RS485_RX.buf[3]&0xff);
  628.                                                                          In_data[2]=(RS485_RX.buf[4]&0xff);
  629.                                                                          In_data[3]=(RS485_RX.buf[5]&0xff);        
  630.                                                                          In_data[4]=(RS485_RX.buf[6]&0xff);
  631.                                                                          In_data[5]=(RS485_RX.buf[7]&0xff);
  632.                                                                          In_data[6]=(RS485_RX.buf[8]&0xff);
  633.                                                                          In_data[7]=(RS485_RX.buf[9]&0xff);        
  634.                                                                          crc_data=RS485_RX.buf[10] | RS485_RX.buf[11]<<8;                                          
  635.                                                                          if(crc_data==usMBCRC16(In_data,8))   //校验8个字节            
  636.                                                                          {                                                
  637.                                                                                  Slave_Station[Module_CNT].State=RS485_RX.buf[1];
  638.                                                                                  Slave_Station[Module_CNT].data1=(RS485_RX.buf[2]|(RS485_RX.buf[3]<<8));                        //+32767                                                                                 
  639.                                                                                  Slave_Station[Module_CNT].data2=(RS485_RX.buf[4]|(RS485_RX.buf[5]<<8));               
  640.                                                                                  Slave_Station[Module_CNT].data3=(RS485_RX.buf[6]|(RS485_RX.buf[7]<<8));                        //+32767                                                                                 
  641.                                                                                  Slave_Station[Module_CNT].data4=(RS485_RX.buf[8]|(RS485_RX.buf[9]<<8));                                                                                                
  642.                                                                                  Module_CNT++;
  643.                                                                                  Start_Send=1;
  644.                                                                                  Clear_Rxbuf();                                                
  645.                                                                          }                                                         
  646.                                                            }
  647.                                                                  else
  648.                                                                         ;
  649.                                                          }
  650.                                                                 else
  651.                                                                          ;                                                               
  652.                                                 }
  653.                                                 CheakWatchdog(600);        
  654.                                 }
  655.                                 break;                                       
  656.                         default:
  657.                         {
  658.                                 Module_CNT=0;
  659.                                 Start_Send=1;
  660.                                 Clear_Rxbuf();        
  661.                                 Clear_Txbuf();                                                         
  662.                         }
  663.                         break;
  664.                         }
  665. //                        if(CheakWatchdog_us(200))        //true = time out
  666. //                        {
  667. //                                if((Slave_Station[Module_CNT].Command!=Comm_1CFull)&&(Slave_Station[Module_CNT].Command!=Comm_2CFull))                          
  668. //                                {
  669. //                                        Err_Infor[Module_CNT].Curr_Err ++;//= Err_Infor[Module_CNT].Curr_Err+1000;
  670. //                                        if(Err_Infor[Module_CNT].Curr_Err>255)
  671. //                                        {
  672. //                                                Err_Infor[Module_CNT].Curr_Err = 255;
  673. //                                                Err_Infor[Module_CNT].Err_Flag=1;
  674. //                                        }               
  675. //                                        MissCNT[Module_CNT] = Err_Infor[Module_CNT].Curr_Err*1000;
  676. //                                        if((MissCNT[Module_CNT]>3000))//1S内连续掉4个数据
  677. //                                                Err_Infor[Module_CNT].Err_Flag=1;
  678. //                         }
  679. //                                Module_CNT++;
  680. //                                Start_Send=1;               
  681. //                                Clear_Rxbuf();
  682. //                        }        
  683. //                        else
  684. //                        {
  685. //                                if(Module_CNT>0)
  686. //                                {
  687. //                                        if(MissCNT[Module_CNT-1]>0)
  688. //                                                MissCNT[Module_CNT-1]--;
  689. //                                }
  690. //                                else
  691. //                                        ;
  692. //                        }        
  693. //                        if(Module_CNT>0)
  694. //                        {
  695. //                                if(MissCNT[Module_CNT-1]>500)
  696. //                                {
  697. //                                        Show_Error(SlaveMissErr);                                
  698. //                                }
  699. //                                else
  700. //                                {                                
  701. //                                        Show_Error(ClearSlaveMiss);
  702. //                                }
  703. //                        }                        
  704.                 }
  705.         }
  706.         else
  707.         {
  708.                 ;
  709.         }
  710. }
  711. /*****************************************************************
  712.                            Main Program
  713. *****************************************************************/
  714. int main(void)
  715. {
  716.         unsigned int i;
  717.         unsigned int t=0,d=1;
  718.   unsigned int a[8];
  719.         /* Initialize STM32F103 */
  720.         System_Initialization();//SPI 引脚是使能
  721.                 SysTick_Init();//定时使能
  722.         /* Config W5500 */
  723.         a[0]=!Adr0;a[1]=!Adr1;a[2]=!Adr2;a[3]=!Adr3;a[4]=!Adr4;a[5]=!Adr5;a[6]=!Adr6;a[7]=!Adr7;//确定从站地址
  724.       for(i=0;i<8;i++)
  725.                 {
  726.                         t=(t+a[i]*d);
  727.                         d=d*2;
  728.                 }
  729.         /************************第一阶段********************************/               
  730.         /* Config W5500 */
  731.           W5500_Configuration(t);
  732.            Delay_ms(200);
  733. //        /* Modbus-TCP Init */
  734.    eMBTCPInit(MB_TCP_PORT_USE_DEFAULT);  //502端口  SOCKET 0
  735.     Delay_ms(200);
  736. //        /* Enable Modbus-TCP Stack */   
  737.      eMBEnable();        
  738.    //printf("\r\nModbus-TCP Start!\r\n");
  739. /**********************第二阶段********************************/               
  740. ……………………

  741. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
F1芯片+w5500+modbus 串口发送数据DMA1+串口1 数据传输无问题+4852.20.7z (234.22 KB, 下载次数: 245)


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

使用道具 举报

沙发
ID:349355 发表于 2019-3-1 09:17 | 只看该作者
这个厉害。方便了开发。
回复

使用道具 举报

板凳
ID:142699 发表于 2019-10-29 17:47 | 只看该作者
485地址这块的作用是什么啊?
回复

使用道具 举报

地板
ID:86901 发表于 2019-11-1 14:21 | 只看该作者
谢谢分享!正在学习!
回复

使用道具 举报

5#
ID:390248 发表于 2020-2-27 10:20 | 只看该作者
这个很不错,值得学习
回复

使用道具 举报

6#
ID:27254 发表于 2020-2-27 17:09 | 只看该作者
楼主辛苦,下载学习,此程序有人下载试验过么?
回复

使用道具 举报

7#
ID:712272 发表于 2023-7-27 19:38 | 只看该作者
老程序员前来学习
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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