找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于机智云的一主机多从机的程序结构设计

[复制链接]
跳转到指定楼层
楼主
ID:334435 发表于 2022-10-3 19:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
之前在学习做的设计里面使用了一主机多从机的结构设计。

  1. #include "main.h"
  2. uint8_t UIflag=0;
  3. uint8_t UIflag2=0;
  4. //对机智云设备进行初始化
  5. void GizWitsInit(void )  
  6. {
  7.   BASIC_TIM_Init();  //利用定时器给设备提供一个1MS的精确时钟
  8.   TIM_RCC_APB1PeriphClockCmd(TIM_RCC_APB1Periph_TIMX,ENABLE); //开定时器时钟
  9.   esp_usartx_init(); // wifi初始化 波特率必须为9600    //WIFI串口初始化 波特率9600
  10.   userInit();       //设备状态结构体初始化,用来跟ESP8266进行接收发送的通信
  11.   memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));  //初始化内存
  12.   gizwitsInit();    //缓冲区初始化        
  13. //        //初始化RTC时钟
  14. //        RTC_NVIC_Config();  // 配置RTC秒中断优先级
  15. //        RTC_CheckAndConfig(&systmtime);
  16. }
  17. /*
  18.   主板使用的模块功能进行初始化(不包括机智云要使用的设备)
  19. */
  20. void Hardware_Init(void )
  21. {
  22.   DHT11_Init();         //温湿度传感器初始化
  23.         Init_BH1750();        //光照传感器初始化
  24.         ADCx_Init();          //ADC采样初始化,进行烟雾的AD数值采样
  25.         LED_GPIO_Config();    //LED灯初始化
  26.         USART_Config();       //串口通信初始化
  27.         GOIO_PWM_MONE_Init();  //利用PWM控制舵机以及风扇转速
  28.   JLX12864G_086_GPIOInit();
  29.         initial_lcd();
  30.         clear_screen();
  31.         //TimeInit();
  32.   StepperMotor4_2();     //步进电机初始化  
  33. }


  34. /**
  35.   * @brief  主函数
  36.   * @param  无  
  37.   * @retval 无
  38.   */
  39. int main ( void )
  40. {
  41.         volatile int  key=0;
  42. //        char YearDate[100];
  43.         volatile int ty=0;
  44.         volatile int xz=0;   
  45.         UInum=1;
  46.         protocolTime_t *ptime=(protocolTime_t *)(uint8_t *)&gizwitsProtocol.TimeNTP;  //获取网络时间结构体
  47.         Hardware_Init();//对板卡硬件初始化
  48.         GizWitsInit();   //对云协议进行初始化
  49.   IWDG_Config(IWDG_Prescaler_256 ,10000);  
  50.         while ( 1 )
  51.         {        
  52.                 TIM_Cmd(TIM3, ENABLE);
  53.                 gizwitsnetwork();  //按键配网
  54.                 user_bh1750();     //开启BH1750工作,并获取数据
  55.                 userHandle();      //用户采集
  56.                 gizwitsHandle((dataPoint_t *)¤tDataPoint);//协议处理
  57. //          Manual_mode();
  58.                 Main_function();   
  59.                 ClockCalculate();
  60.                

  61. /***解析语音模块的串口数据***/               
  62.         if (usartdataflag==1)
  63.         {
  64.                 Uart5ReceiveEndDetection();
  65.                 usartdataflag=0;
  66.           DisposeUart5ReceiveData(); //数据解析
  67.         }
  68. /***解析台灯模块的串口数据***/                        
  69.         if(usart4dataflag==1)
  70.         {
  71.           Uart4ReceiveEndDetection();
  72.                 usart4dataflag=0;
  73.           DisposeUart4ReceiveData(); //数据解析
  74.         }
  75.                
  76.                 if (UInum==1) //自动模式
  77.                 {
  78.                   Automatic_mode();
  79.                 }

  80.                 if(timer>=100)  
  81.                 {
  82.                         timer=0;
  83.                   Displayinterface();
  84.                 }        

  85. /******************************RTC时钟*****************************/        
  86.          /* 每过1s 更新一次时间*/
  87.                 if (TimeDisplay == 1)
  88.                 {
  89.                         /* 当前时间 */
  90.                         Time_Display( RTC_GetCounter(),&systmtime);         
  91.      // Timedistributionfunction(&systmtime);                        
  92.                         TimeDisplay = 0;
  93.                         ty++;
  94.                   doortime++;
  95.                         worktime++;        
  96.                         if(ty==120)  //120秒,两分钟
  97.                         {
  98.                                 xz=1; //时间校准标志位
  99.                                 ty=0;
  100.                         }
  101.                 }        
  102. /*************从机时间校准***************/        
  103.                

  104. /******************运行保护**************************/                                
  105.     if (dooropen==1)  //门锁保护
  106.      {
  107.                         if (doortime==1)
  108.                                 {
  109.                                         dooropen=0;
  110.                                         GPIO_ResetBits(room_GPIO_PORT,room_GPIO_PIN);
  111.                                 }        
  112.                  }
  113.                  
  114.                 if ((closeflag1==1)&&(worktime>=1))  //窗帘保护
  115.                  {        
  116.                          closeflag1=0;
  117.                          closeflag=1;                                         
  118. //                         GPIO_SetBits(GPIOE,GPIO_Pin_4);
  119.                          TIM_Cmd(TIM2, DISABLE);
  120.                  }                 
  121.                 if((openflag1==1)&&(worktime>=1))
  122.                  {        
  123.                          openflag1=0;
  124.                          openflag=1;
  125. //                         GPIO_SetBits(GPIOE,GPIO_Pin_4);
  126.                          TIM_Cmd(TIM2, DISABLE);
  127.                  }        
  128. //                   sprintf (information3,"%d  %d  ",closeflag1,worktime);
  129. //             display_string_5x8_1(7,5,information3);
  130.                  
  131. /*******************************************************/
  132.                 if(wifi_link==1)
  133.                 {
  134.                         if (usartoneflag==0)  //网络链接成功下发一次时间同步     
  135.                         {
  136.                           usartoneflag=1;
  137.                                 Timedistributionfunction(); //下发给从机校准时间
  138.                         }
  139.                   if (alinghourflag2==1)
  140.                   {
  141.                         alinghourflag2=0;
  142.                   Timedistributionfunction(); //下发给从机校准时间
  143.                   }

  144.                         
  145.                         key=KEY_Scan(1);
  146.                         if( key == 16 )  //网络时间同步按键
  147.                         {
  148. //                         /*用接收到的时间设置RTC*/
  149. //                          Time_Adjust(&networktime);
  150. //                        //向备份寄存器写入标志
  151. //                          BKP_WriteBackupRegister(RTCC_BKP_DRX, RTc_BKP_DATA);
  152.                                 Timedistributionfunction(); //下发给从机校准时间
  153.                         }
  154. //                        else if( xz==1)  //网络时间校准
  155. //                        {
  156. //                         /*用接收到的时间设置RTC*/
  157. //                          Time_Adjust(&networktime);
  158. //                        //向备份寄存器写入标志
  159. //                          BKP_WriteBackupRegister(RTCC_BKP_DRX, RTc_BKP_DATA);
  160. //                                xz=0; //时间校准标志位清零
  161. //                                Timedistributionfunction(); //下发给从机校准时间
  162. //                        }                        
  163.    }
  164.                 IWDG_Feed();  //喂狗防止复位
  165. }
  166. }

  167. /************************************************
  168. 显示屏显示UI界面控制函数
  169. *************************************************/
  170. void Displayinterface(void)
  171. {
  172. if (UInum==1) //默认
  173. {
  174.         if(Manualflagbit2==0)
  175.         {        
  176.    if (UI1flag1==0)
  177.          {
  178.     UI1flag1=1;
  179.                 UI1flag2=0;
  180.                 UI2flag1=0;
  181.                 UI2flag2=0;
  182.                 clear_screen();
  183.           Manualflagbit=0;
  184.          }               
  185.    display_string_5x8_1(1,0,information1);
  186.    display_string_5x8_1(3,0,information3);
  187.    display_string_5x8_1(5,0,information4);        
  188.          display_string_5x8_1(7,0,information2);         
  189.    display_string_5x8_1(8,5,"M:A");        
  190.         }
  191.          else
  192.          {
  193.     if (UI1flag2==0)
  194.           {
  195.      UI1flag2=1;
  196.                  UI1flag1=0;
  197.                  UI2flag1=0;
  198.                  UI2flag2=0;
  199.                  clear_screen();
  200.            Manualflagbit=0;
  201.           }               
  202.                 display_string_5x8_1(1,0,information1);
  203.     display_string_5x8_1(3,0,UIBUFF1);
  204.     display_string_5x8_1(5,0,UIBUFF2);
  205.     display_string_5x8_1(7,0,UIBUFF3);         
  206. //  display_string_5x8_1(8,0,"                          ");        
  207.          }
  208. }

  209. if (UInum==2)  //手动档界面
  210. {
  211.          if(Manualflagbit==0)
  212.          {
  213.                 if (UI2flag1==0)  
  214.          {
  215.     UI1flag1=0;
  216.                 UI1flag2=0;
  217.                 UI2flag2=0;
  218.                 UI2flag1=1;
  219.                 clear_screen();
  220.           Manualflagbit2=0;
  221.          }               
  222.    display_string_5x8_1(1,0,information1);
  223.    display_string_5x8_1(3,0,information3);
  224.    display_string_5x8_1(5,0,information4);        
  225.          display_string_5x8_1(7,0,information2);         
  226.    display_string_5x8_1(8,5,"M:H");
  227.          }
  228.          else
  229.          {
  230.                  if (UI2flag2==0)
  231.           {
  232.      UI1flag1=0;
  233.                  UI1flag2=0;
  234.                  UI2flag2=1;
  235.                  UI2flag1=0;
  236.            clear_screen();
  237.                  Manualflagbit2=0; //清除自动设置
  238.          }
  239.          display_string_5x8_1(1,2,"Manual control mode");
  240.          sprintf (information6,"valueLED2:%d      ",currentDataPoint.valueLED2);
  241.          display_string_5x8_1(2,2,information6);
  242.          sprintf (information7,"valuefan:%d      ",currentDataPoint.valuefan);
  243.          display_string_5x8_1(3,2,information7);
  244.          sprintf (information7,"valueventilator:%d  ",currentDataPoint.valueventilator);
  245.          display_string_5x8_1(4,2,information7);
  246.          sprintf (information7,"valuewidow:%d      ",currentDataPoint.valuewidow);
  247.          display_string_5x8_1(5,2,information7);
  248.          sprintf (information7,"valuedoor:%d      ",currentDataPoint.valuedoor);
  249.          display_string_5x8_1(6,2,information7);
  250.          sprintf (information5,"valueLED1:%d     ",currentDataPoint.valueLED1);
  251.          display_string_5x8_1(7,2,information5);
  252.          }
  253. }
  254. if (UInum==3)  
  255. {
  256.   clear_screen();
  257.         UIflag=0;
  258.         UIflag2=0;
  259. }
  260. }

  261. /************************************************
  262. 对模块进行配网
  263. KEY1按下为:  WIFI复位
  264. *************************************************/
  265. void gizwitsnetwork(void )
  266. {
  267.           volatile int network_key=0;        
  268.                 network_key=KEY_Scan(1);   
  269.         
  270.                 if(network_key==1)//KEY1按键
  271.                 {
  272. //                        printf("WIFI进入AirLink连接模式\r\n");
  273.                         gizwitsSetMode(WIFI_AIRLINK_MODE);//Air-link模式接入
  274.                 }
  275. }


  276. /**
  277.   * @brief  用户获取光照强度数据接口
  278.   * @param  无
  279.   * @retval result_lx 光照强度
  280.   */
  281. void user_bh1750(void)
  282. {
  283.           float  light=0;  //光强度缓存区
  284.                 //获取光照
  285.     Start_BH1750();
  286.     delay_ms(180);
  287.     Read_BH1750();
  288.     light=Convert_BH1750();
  289.     //delay_ms(1000);
  290.          // printf("light:%f\n",light);
  291. }



  292. /*USART4数据帧超时检测*/
  293. void Uart4ReceiveEndDetection(void)                                                     //数据帧结束检测
  294. {                                                                                               //每一毫秒执行一次
  295.     if(Uart4ReceiveTimeout > 0)
  296.     {
  297.         Uart4ReceiveTimeout--;
  298.         if((Uart4ReceiveTimeout >= 0) && (usart4_rcvd_len > 4))                                    //判断通讯接收是否超时
  299.         {        
  300.             // Uart3ReceiveDataLength = 0;                                                   //将接收地址偏移寄存器清零
  301.             usart4_rcvd_flag = 1;
  302.         }
  303.     }  
  304. }

  305. void DisposeUart4ReceiveData(void)
  306. {
  307. //        uint8_t i;
  308.         uint8_t i=0,j=0,len;
  309.         char* pData1 = 0;
  310.         unsigned int usart4_data_hex[50];
  311.         if(usart4_rcvd_flag == 1)   
  312.          {
  313.                 //printf ("1\r\n");        
  314.                 /*对串口接收到的数据进行解析提前处理*/
  315.                 len=15;
  316.                 pData1 = strstr((const char*)usart4_rcvd_buf,"55AA01");
  317.                 for(i=0;i<=len;i++)
  318.                  {
  319.                         usart4_data_hex[i]=str2hex(pData1 + j, 2);                        //从条码开始接收解析
  320.                         j+=2;
  321.                  }                                 
  322.                         if(usart4_data_hex[2] == 0x01)  //地址码
  323.                         {                                       
  324.                
  325.                                 if(usart4_data_hex[3] == 0x01)  //功能码
  326.                                 {
  327.                                         switch(usart4_data_hex[4])          //        分类码
  328.                                         {                                         
  329.                                           case 0x02:  //台灯
  330.                                                 if(usart4_data_hex[6]==65535) break;
  331.                                                 else
  332.                                                 {
  333.                                                  currentDataPoint.valueLED2=usart4_data_hex[6];
  334.                                                  printf("台灯:%d\r\n",currentDataPoint.valueLED2);        
  335.                                                         break;        
  336.                                                 }
  337.                                                 default :break;
  338.                                         }
  339.                                 }
  340.                                 if(usart4_data_hex[3] == 0x02)  //功能码  查询时间
  341.                                         {
  342.                                           switch(usart4_data_hex[4])          //        分类码
  343.                                                 {
  344.                                                         case 0x01:Timedistributionfunction();break;
  345.                                                         default :break;
  346.                                                 }
  347.                                         }
  348.                         }
  349.                 memset(usart5_rcvd_buf, 0, sizeof(usart5_rcvd_buf));
  350.         usart5_rcvd_len = 0;                                                                   //将接收地址偏移寄存器清零               
  351.                 usart4_rcvd_flag = 0;
  352.         }
  353. }






  354. /*处理蓝牙音响和核心板直接的数据处理*/
  355. /*数据帧超时检测*/
  356. void Uart5ReceiveEndDetection(void)                                                     //数据帧结束检测
  357. {                                                                                               //每一毫秒执行一次
  358.     if(Uart5ReceiveTimeout > 0)
  359.     {
  360.         Uart5ReceiveTimeout--;
  361.         if((Uart5ReceiveTimeout >= 0) && (usart5_rcvd_len > 4))                                    //判断通讯接收是否超时
  362.         {        
  363.             // Uart3ReceiveDataLength = 0;                                                   //将接收地址偏移寄存器清零
  364.             usart5_rcvd_flag = 1;
  365.         }
  366.     }
  367.         //        printf ("U:%d,UL%d",Uart5ReceiveTimeout,usart5_rcvd_len);   
  368. }



  369. void DisposeUart5ReceiveData(void)
  370. {
  371. //        uint8_t i;
  372.         uint8_t i=0,j=0,len;
  373.         char* pData1 = 0;
  374.         unsigned int usart5_data_hex[50];
  375.         if(usart5_rcvd_flag == 1)   
  376.          {
  377.                 //printf ("1\r\n");        
  378.                 /*对串口接收到的数据进行解析提前处理*/
  379.                 len=15;
  380.                 pData1 = strstr((const char*)usart5_rcvd_buf,"55AA01");
  381.                 for(i=0;i<=len;i++)
  382.                  {
  383.                         usart5_data_hex[i]=str2hex(pData1 + j, 2);                        //从条码开始接收解析
  384.                         j+=2;
  385.                         //printf ("DATA:%d",usart5_data_hex[i]);
  386.                  }               
  387. //         if(usart4_data_hex[usart4_data_hex[5] + 6] == BCC_CheckSum(&usart4_rcvd_buf[2], (length + 4)))
  388. //                {               
  389.                  /*对接收的数据进行判断处理  55AA0101***DB */
  390.     //  printf ("2\r\n");                        
  391.                         if(usart5_data_hex[2] == 0x01)  //地址码
  392.                         {                                       
  393.        // printf ("3\r\n");                                
  394.                                 if(usart5_data_hex[3] == 0x01)  //功能码
  395.                                 {
  396.                                 //        printf ("4\r\n");
  397.                                         switch(usart5_data_hex[4])          //        分类码
  398.                                         {                                         
  399.                                                 case 0x01:  //灯光
  400.                                                                  currentDataPoint.valueLED1=usart5_data_hex[6];
  401.                                                      printf("灯光:%d\r\n",currentDataPoint.valueLED1);
  402.                                                    break;        
  403.                                           case 0x02:  //台灯
  404.                                                            currentDataPoint.valueLED2=usart5_data_hex[6];
  405.                                                      if (currentDataPoint.valueLED2==1)
  406.                                                                  {
  407.                                                       Usart_SendString(DEBUG_USART4,"55AA020102090100DB0D\r\n"); //开台灯
  408.                                                                  }
  409.                                                                  else
  410.                  {
  411.                                                                   Usart_SendString(DEBUG_USART4,"55AA020102090000DB0D\r\n");//关台灯
  412.                                                                  }
  413.                                                                         
  414.                                                      printf("台灯:%d\r\n",currentDataPoint.valueLED2);        
  415.                                                    break;        
  416.                                                 case 0x03:  //风扇
  417.                                                          currentDataPoint.valuefan=usart5_data_hex[6];
  418.                                                    printf("风扇:%d\r\n",currentDataPoint.valuefan);
  419.                                                    break;        
  420.                                                 case 0x05:  //窗帘
  421.                                                          currentDataPoint.valuewidow=usart5_data_hex[6];
  422.                                                     printf("窗帘:%d\r\n",currentDataPoint.valuewidow);

  423.                                                    break;        
  424.                                                 case 0x06:  //排烟机
  425.                                                          currentDataPoint.valueventilator=usart5_data_hex[6];
  426.                                                    printf("排烟机:%d\r\n", currentDataPoint.valueventilator);
  427.                                                    break;        
  428.                                                 default :break;
  429.                                         }
  430.                                 }

  431.                         }
  432.                 memset(usart5_rcvd_buf, 0, sizeof(usart5_rcvd_buf));
  433.         usart5_rcvd_len = 0;                                                                   //将接收地址偏移寄存器清零               
  434.                 usart5_rcvd_flag = 0;
  435.         }
  436. }


  437. unsigned int str2hex(char * start,char length)                                                               //字符串起始地址, length 字符长度
  438. {         
  439.         char i = 0;
  440.         unsigned char res_tem= 0x00;
  441.         unsigned int res = 0x0000;
  442.         res_tem = 0;
  443.         res = 0;

  444.     for(i = 0; i < length; i++)        
  445.         {
  446.                 if((*(start+i)>='0')&&(*(start+i)<='9'))
  447.                 {
  448.                         res_tem = *(start+i)-'0';
  449.                         res = res+res_tem*pow(16,length-i-1);                                
  450.                 }
  451.                 else if((*(start+i)>='A')&&(*(start+i)<='F'))                                                    //约定只用大写
  452.                 {
  453.                         res_tem = *(start+i)-'A'+10;
  454.                         res = res+res_tem*pow(16,length-i-1);
  455.                 }
  456.                 else
  457.                 {                                 
  458.                         res = 0xFFFF;                                                                          //错误数值就返回默认值
  459.                         break;                                 
  460.                 }
  461.         }
  462.         
  463.         return res & 0xFFFF;
  464. }

  465. /*将字符串s转换成相应的整数*/  
  466. // int atoi(char s[])  
  467. // {  
  468. //     int i;  
  469. //     int n = 0;  
  470. //     for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)  
  471. //     {  
  472. //         n = 10 * n + (s[i] - '0');  
  473. //     }  
  474. //     return n;  
  475. // }  

  476. unsigned int str2dec(char * start,char length)                                                               //字符转10进制, 字符串其实地址, length 字符长度
  477. {        
  478.         char i = 0;
  479.         unsigned char res_tem= 0x00;
  480.         unsigned int res = 0x0000;
  481.         res_tem = 0;
  482.         res = 0;

  483.         for(i = 0;i<length;i++)        
  484.         {
  485.                 if((*(start+i)>='0')&&(*(start+i)<='9'))
  486.                 {
  487.                         res_tem = *(start+i)-'0';
  488.                         res = res+res_tem*pow(10,length-i-1);
  489.                 }
  490.                 else
  491.                 {                                
  492.                         res = 0xFFFF;                                                                          //错误数值就返回默认值
  493.                         break;                                
  494.                 }
  495.         }
  496.         return res & 0xFFFF;
  497. }

  498. /* 将无符号十进制数拆分并转换为字符形式,返回字符串长度 */
  499. char uBinaryToAscii(unsigned int value, char *str)
  500. {
  501.     char i;
  502.     char num_str[10] = { 0 };
  503.     unsigned int quotient;
  504.     unsigned char len;

  505.     len = 0;

  506.     quotient = value / 10;
  507.     while (quotient != 0)
  508.     {
  509.         num_str[len] = value % 10 + '0';
  510.         len++;
  511.         value = quotient;
  512.         quotient = value / 10;
  513.     }

  514.     if (value != 0)
  515.     {
  516.         num_str[len] = value + '0';
  517.         len++;
  518.     }

  519.     for (i = 0; i < len; i++)
  520.     {
  521.         *str = num_str[len-i-1];
  522.         str++;
  523.     }

  524.     return len;
  525. }

  526. /* 将有符号十进制数拆分并转换为字符形式,返回字符串长度 */
  527. /* 调用了uBinaryToAscii函数 */
  528. char sBinaryToAscii(int value, char *str)
  529. {
  530.     char len;

  531.     if (value < 0)
  532.     {
  533.         *str = '-';
  534.         str++;
  535.         len = uBinaryToAscii(-value, str);
  536.         return len + 1;
  537.     }
  538.     else
  539.     {
  540.         len = uBinaryToAscii(value, str);
  541.         return len;
  542.     }
  543. }
  544. void Timedistributionfunction(void )  //用于接收网络时间
  545. {
  546.                  //时间
  547.                 if (networktime.tm_mon<10) networktime.tm_mon=networktime.tm_mon+60;
  548.           if (networktime.tm_mday<10) networktime.tm_mday=networktime.tm_mday+60;
  549.                 if (networktime.tm_hour<10) networktime.tm_hour=networktime.tm_hour+60;
  550.           if (networktime.tm_min<10) networktime.tm_min=networktime.tm_min+60;
  551.                  sBinaryToAscii(networktime.tm_year,timedata); //年
  552.                  sBinaryToAscii(networktime.tm_mon,timedata+4); //月
  553.                  sBinaryToAscii(networktime.tm_mday,timedata+6);  //日
  554.                  sBinaryToAscii(networktime.tm_hour,timedata+8); //时
  555.                  sBinaryToAscii(networktime.tm_min,timedata+10);  //分
  556.                  //sBinaryToAscii(networktime.tm_sec,timedata+7);  //秒
  557.                  sprintf(Timedatabuff,"%s%c%c%c%c%c%c%c%c%c%c%c%c%s\r\n",Timedata,timedata[0],timedata[1],timedata[2],timedata[3],timedata[4],timedata[5],timedata[6],\
  558.                                                      timedata[7],timedata[8],timedata[9],timedata[10],timedata[11],enddatabuff);     
  559.                 // printf("%s\r\n",Timedatabuff);        
  560.           Usart_SendString(DEBUG_USART4,Timedatabuff);
  561. }



  562. /*-------------------------------------------------------
  563. 内部通讯协议的CRC16计算函数。
  564. -------------------------------------------------------*/
  565. uint32_t  ecbm_databuff_rtu_crc=0xFFFF;                    //初始化CRC变量各位为1。
  566. void ecbm_databuff_rtu_crc16(uint8_t buf)
  567. {
  568.         uint8_t j;
  569.         ecbm_databuff_rtu_crc^=buf;                                   //当前数据异或CRC低字节,在C51里可以直接处理。
  570.         for(j=0;j<8;j++)
  571.                 {                                           //计算每个字节的每一位。
  572.                         if(ecbm_databuff_rtu_crc&0x01)
  573.                                 {                           //判断右移前最低位是否为1。
  574.                                         ecbm_databuff_rtu_crc=(ecbm_databuff_rtu_crc>>1)^0xA001;//如果为1则右移并异或表达式。
  575.                                 }
  576.                                 else
  577.                                 {
  578.                                  ecbm_databuff_rtu_crc>>=1;                            //否则直接右移一位。
  579.                                 }           
  580.          }
  581. }
复制代码


本人初学,仅供参考,存在错误和不足之处,请大家回帖多多指教,切勿照搬,代码下载:
代码.7z (554.77 KB, 下载次数: 10)

评分

参与人数 1黑币 +20 收起 理由
admin + 20 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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