之前在學習做的設計里面使用了一主機多從機的結(jié)構(gòu)設計。
- #include "main.h"
- uint8_t UIflag=0;
- uint8_t UIflag2=0;
- //對機智云設備進行初始化
- void GizWitsInit(void )
- {
- BASIC_TIM_Init(); //利用定時器給設備提供一個1MS的精確時鐘
- TIM_RCC_APB1PeriphClockCmd(TIM_RCC_APB1Periph_TIMX,ENABLE); //開定時器時鐘
- esp_usartx_init(); // wifi初始化 波特率必須為9600 //WIFI串口初始化 波特率9600
- userInit(); //設備狀態(tài)結(jié)構(gòu)體初始化,用來跟ESP8266進行接收發(fā)送的通信
- memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t)); //初始化內(nèi)存
- gizwitsInit(); //緩沖區(qū)初始化
- // //初始化RTC時鐘
- // RTC_NVIC_Config(); // 配置RTC秒中斷優(yōu)先級
- // RTC_CheckAndConfig(&systmtime);
- }
- /*
- 主板使用的模塊功能進行初始化(不包括機智云要使用的設備)
- */
- void Hardware_Init(void )
- {
- DHT11_Init(); //溫濕度傳感器初始化
- Init_BH1750(); //光照傳感器初始化
- ADCx_Init(); //ADC采樣初始化,進行煙霧的AD數(shù)值采樣
- LED_GPIO_Config(); //LED燈初始化
- USART_Config(); //串口通信初始化
- GOIO_PWM_MONE_Init(); //利用PWM控制舵機以及風扇轉(zhuǎn)速
- JLX12864G_086_GPIOInit();
- initial_lcd();
- clear_screen();
- //TimeInit();
- StepperMotor4_2(); //步進電機初始化
- }
- /**
- * @brief 主函數(shù)
- * @param 無
- * @retval 無
- */
- int main ( void )
- {
- volatile int key=0;
- // char YearDate[100];
- volatile int ty=0;
- volatile int xz=0;
- UInum=1;
- protocolTime_t *ptime=(protocolTime_t *)(uint8_t *)&gizwitsProtocol.TimeNTP; //獲取網(wǎng)絡時間結(jié)構(gòu)體
- Hardware_Init();//對板卡硬件初始化
- GizWitsInit(); //對云協(xié)議進行初始化
- IWDG_Config(IWDG_Prescaler_256 ,10000);
- while ( 1 )
- {
- TIM_Cmd(TIM3, ENABLE);
- gizwitsnetwork(); //按鍵配網(wǎng)
- user_bh1750(); //開啟BH1750工作,并獲取數(shù)據(jù)
- userHandle(); //用戶采集
- gizwitsHandle((dataPoint_t *)¤tDataPoint);//協(xié)議處理
- // Manual_mode();
- Main_function();
- ClockCalculate();
-
- /***解析語音模塊的串口數(shù)據(jù)***/
- if (usartdataflag==1)
- {
- Uart5ReceiveEndDetection();
- usartdataflag=0;
- DisposeUart5ReceiveData(); //數(shù)據(jù)解析
- }
- /***解析臺燈模塊的串口數(shù)據(jù)***/
- if(usart4dataflag==1)
- {
- Uart4ReceiveEndDetection();
- usart4dataflag=0;
- DisposeUart4ReceiveData(); //數(shù)據(jù)解析
- }
-
- if (UInum==1) //自動模式
- {
- Automatic_mode();
- }
- if(timer>=100)
- {
- timer=0;
- Displayinterface();
- }
- /******************************RTC時鐘*****************************/
- /* 每過1s 更新一次時間*/
- if (TimeDisplay == 1)
- {
- /* 當前時間 */
- Time_Display( RTC_GetCounter(),&systmtime);
- // Timedistributionfunction(&systmtime);
- TimeDisplay = 0;
- ty++;
- doortime++;
- worktime++;
- if(ty==120) //120秒,兩分鐘
- {
- xz=1; //時間校準標志位
- ty=0;
- }
- }
- /*************從機時間校準***************/
-
- /******************運行保護**************************/
- if (dooropen==1) //門鎖保護
- {
- if (doortime==1)
- {
- dooropen=0;
- GPIO_ResetBits(room_GPIO_PORT,room_GPIO_PIN);
- }
- }
-
- if ((closeflag1==1)&&(worktime>=1)) //窗簾保護
- {
- closeflag1=0;
- closeflag=1;
- // GPIO_SetBits(GPIOE,GPIO_Pin_4);
- TIM_Cmd(TIM2, DISABLE);
- }
- if((openflag1==1)&&(worktime>=1))
- {
- openflag1=0;
- openflag=1;
- // GPIO_SetBits(GPIOE,GPIO_Pin_4);
- TIM_Cmd(TIM2, DISABLE);
- }
- // sprintf (information3,"%d %d ",closeflag1,worktime);
- // display_string_5x8_1(7,5,information3);
-
- /*******************************************************/
- if(wifi_link==1)
- {
- if (usartoneflag==0) //網(wǎng)絡鏈接成功下發(fā)一次時間同步
- {
- usartoneflag=1;
- Timedistributionfunction(); //下發(fā)給從機校準時間
- }
- if (alinghourflag2==1)
- {
- alinghourflag2=0;
- Timedistributionfunction(); //下發(fā)給從機校準時間
- }
-
- key=KEY_Scan(1);
- if( key == 16 ) //網(wǎng)絡時間同步按鍵
- {
- // /*用接收到的時間設置RTC*/
- // Time_Adjust(&networktime);
- // //向備份寄存器寫入標志
- // BKP_WriteBackupRegister(RTCC_BKP_DRX, RTc_BKP_DATA);
- Timedistributionfunction(); //下發(fā)給從機校準時間
- }
- // else if( xz==1) //網(wǎng)絡時間校準
- // {
- // /*用接收到的時間設置RTC*/
- // Time_Adjust(&networktime);
- // //向備份寄存器寫入標志
- // BKP_WriteBackupRegister(RTCC_BKP_DRX, RTc_BKP_DATA);
- // xz=0; //時間校準標志位清零
- // Timedistributionfunction(); //下發(fā)給從機校準時間
- // }
- }
- IWDG_Feed(); //喂狗防止復位
- }
- }
- /************************************************
- 顯示屏顯示UI界面控制函數(shù)
- *************************************************/
- void Displayinterface(void)
- {
- if (UInum==1) //默認
- {
- if(Manualflagbit2==0)
- {
- if (UI1flag1==0)
- {
- UI1flag1=1;
- UI1flag2=0;
- UI2flag1=0;
- UI2flag2=0;
- clear_screen();
- Manualflagbit=0;
- }
- display_string_5x8_1(1,0,information1);
- display_string_5x8_1(3,0,information3);
- display_string_5x8_1(5,0,information4);
- display_string_5x8_1(7,0,information2);
- display_string_5x8_1(8,5,"M:A");
- }
- else
- {
- if (UI1flag2==0)
- {
- UI1flag2=1;
- UI1flag1=0;
- UI2flag1=0;
- UI2flag2=0;
- clear_screen();
- Manualflagbit=0;
- }
- display_string_5x8_1(1,0,information1);
- display_string_5x8_1(3,0,UIBUFF1);
- display_string_5x8_1(5,0,UIBUFF2);
- display_string_5x8_1(7,0,UIBUFF3);
- // display_string_5x8_1(8,0," ");
- }
- }
- if (UInum==2) //手動檔界面
- {
- if(Manualflagbit==0)
- {
- if (UI2flag1==0)
- {
- UI1flag1=0;
- UI1flag2=0;
- UI2flag2=0;
- UI2flag1=1;
- clear_screen();
- Manualflagbit2=0;
- }
- display_string_5x8_1(1,0,information1);
- display_string_5x8_1(3,0,information3);
- display_string_5x8_1(5,0,information4);
- display_string_5x8_1(7,0,information2);
- display_string_5x8_1(8,5,"M:H");
- }
- else
- {
- if (UI2flag2==0)
- {
- UI1flag1=0;
- UI1flag2=0;
- UI2flag2=1;
- UI2flag1=0;
- clear_screen();
- Manualflagbit2=0; //清除自動設置
- }
- display_string_5x8_1(1,2,"Manual control mode");
- sprintf (information6,"valueLED2:%d ",currentDataPoint.valueLED2);
- display_string_5x8_1(2,2,information6);
- sprintf (information7,"valuefan:%d ",currentDataPoint.valuefan);
- display_string_5x8_1(3,2,information7);
- sprintf (information7,"valueventilator:%d ",currentDataPoint.valueventilator);
- display_string_5x8_1(4,2,information7);
- sprintf (information7,"valuewidow:%d ",currentDataPoint.valuewidow);
- display_string_5x8_1(5,2,information7);
- sprintf (information7,"valuedoor:%d ",currentDataPoint.valuedoor);
- display_string_5x8_1(6,2,information7);
- sprintf (information5,"valueLED1:%d ",currentDataPoint.valueLED1);
- display_string_5x8_1(7,2,information5);
- }
- }
- if (UInum==3)
- {
- clear_screen();
- UIflag=0;
- UIflag2=0;
- }
- }
- /************************************************
- 對模塊進行配網(wǎng)
- KEY1按下為: WIFI復位
- *************************************************/
- void gizwitsnetwork(void )
- {
- volatile int network_key=0;
- network_key=KEY_Scan(1);
-
- if(network_key==1)//KEY1按鍵
- {
- // printf("WIFI進入AirLink連接模式\r\n");
- gizwitsSetMode(WIFI_AIRLINK_MODE);//Air-link模式接入
- }
- }
- /**
- * @brief 用戶獲取光照強度數(shù)據(jù)接口
- * @param 無
- * @retval result_lx 光照強度
- */
- void user_bh1750(void)
- {
- float light=0; //光強度緩存區(qū)
- //獲取光照
- Start_BH1750();
- delay_ms(180);
- Read_BH1750();
- light=Convert_BH1750();
- //delay_ms(1000);
- // printf("light:%f\n",light);
- }
- /*USART4數(shù)據(jù)幀超時檢測*/
- void Uart4ReceiveEndDetection(void) //數(shù)據(jù)幀結(jié)束檢測
- { //每一毫秒執(zhí)行一次
- if(Uart4ReceiveTimeout > 0)
- {
- Uart4ReceiveTimeout--;
- if((Uart4ReceiveTimeout >= 0) && (usart4_rcvd_len > 4)) //判斷通訊接收是否超時
- {
- // Uart3ReceiveDataLength = 0; //將接收地址偏移寄存器清零
- usart4_rcvd_flag = 1;
- }
- }
- }
- void DisposeUart4ReceiveData(void)
- {
- // uint8_t i;
- uint8_t i=0,j=0,len;
- char* pData1 = 0;
- unsigned int usart4_data_hex[50];
- if(usart4_rcvd_flag == 1)
- {
- //printf ("1\r\n");
- /*對串口接收到的數(shù)據(jù)進行解析提前處理*/
- len=15;
- pData1 = strstr((const char*)usart4_rcvd_buf,"55AA01");
- for(i=0;i<=len;i++)
- {
- usart4_data_hex[i]=str2hex(pData1 + j, 2); //從條碼開始接收解析
- j+=2;
- }
- if(usart4_data_hex[2] == 0x01) //地址碼
- {
-
- if(usart4_data_hex[3] == 0x01) //功能碼
- {
- switch(usart4_data_hex[4]) // 分類碼
- {
- case 0x02: //臺燈
- if(usart4_data_hex[6]==65535) break;
- else
- {
- currentDataPoint.valueLED2=usart4_data_hex[6];
- printf("臺燈:%d\r\n",currentDataPoint.valueLED2);
- break;
- }
- default :break;
- }
- }
- if(usart4_data_hex[3] == 0x02) //功能碼 查詢時間
- {
- switch(usart4_data_hex[4]) // 分類碼
- {
- case 0x01:Timedistributionfunction();break;
- default :break;
- }
- }
- }
- memset(usart5_rcvd_buf, 0, sizeof(usart5_rcvd_buf));
- usart5_rcvd_len = 0; //將接收地址偏移寄存器清零
- usart4_rcvd_flag = 0;
- }
- }
- /*處理藍牙音響和核心板直接的數(shù)據(jù)處理*/
- /*數(shù)據(jù)幀超時檢測*/
- void Uart5ReceiveEndDetection(void) //數(shù)據(jù)幀結(jié)束檢測
- { //每一毫秒執(zhí)行一次
- if(Uart5ReceiveTimeout > 0)
- {
- Uart5ReceiveTimeout--;
- if((Uart5ReceiveTimeout >= 0) && (usart5_rcvd_len > 4)) //判斷通訊接收是否超時
- {
- // Uart3ReceiveDataLength = 0; //將接收地址偏移寄存器清零
- usart5_rcvd_flag = 1;
- }
- }
- // printf ("U:%d,UL%d",Uart5ReceiveTimeout,usart5_rcvd_len);
- }
- void DisposeUart5ReceiveData(void)
- {
- // uint8_t i;
- uint8_t i=0,j=0,len;
- char* pData1 = 0;
- unsigned int usart5_data_hex[50];
- if(usart5_rcvd_flag == 1)
- {
- //printf ("1\r\n");
- /*對串口接收到的數(shù)據(jù)進行解析提前處理*/
- len=15;
- pData1 = strstr((const char*)usart5_rcvd_buf,"55AA01");
- for(i=0;i<=len;i++)
- {
- usart5_data_hex[i]=str2hex(pData1 + j, 2); //從條碼開始接收解析
- j+=2;
- //printf ("DATA:%d",usart5_data_hex[i]);
- }
- // if(usart4_data_hex[usart4_data_hex[5] + 6] == BCC_CheckSum(&usart4_rcvd_buf[2], (length + 4)))
- // {
- /*對接收的數(shù)據(jù)進行判斷處理 55AA0101***DB */
- // printf ("2\r\n");
- if(usart5_data_hex[2] == 0x01) //地址碼
- {
- // printf ("3\r\n");
- if(usart5_data_hex[3] == 0x01) //功能碼
- {
- // printf ("4\r\n");
- switch(usart5_data_hex[4]) // 分類碼
- {
- case 0x01: //燈光
- currentDataPoint.valueLED1=usart5_data_hex[6];
- printf("燈光:%d\r\n",currentDataPoint.valueLED1);
- break;
- case 0x02: //臺燈
- currentDataPoint.valueLED2=usart5_data_hex[6];
- if (currentDataPoint.valueLED2==1)
- {
- Usart_SendString(DEBUG_USART4,"55AA020102090100DB0D\r\n"); //開臺燈
- }
- else
- {
- Usart_SendString(DEBUG_USART4,"55AA020102090000DB0D\r\n");//關臺燈
- }
-
- printf("臺燈:%d\r\n",currentDataPoint.valueLED2);
- break;
- case 0x03: //風扇
- currentDataPoint.valuefan=usart5_data_hex[6];
- printf("風扇:%d\r\n",currentDataPoint.valuefan);
- break;
- case 0x05: //窗簾
- currentDataPoint.valuewidow=usart5_data_hex[6];
- printf("窗簾:%d\r\n",currentDataPoint.valuewidow);
- break;
- case 0x06: //排煙機
- currentDataPoint.valueventilator=usart5_data_hex[6];
- printf("排煙機:%d\r\n", currentDataPoint.valueventilator);
- break;
- default :break;
- }
- }
- }
- memset(usart5_rcvd_buf, 0, sizeof(usart5_rcvd_buf));
- usart5_rcvd_len = 0; //將接收地址偏移寄存器清零
- usart5_rcvd_flag = 0;
- }
- }
- unsigned int str2hex(char * start,char length) //字符串起始地址, length 字符長度
- {
- char i = 0;
- unsigned char res_tem= 0x00;
- unsigned int res = 0x0000;
- res_tem = 0;
- res = 0;
- for(i = 0; i < length; i++)
- {
- if((*(start+i)>='0')&&(*(start+i)<='9'))
- {
- res_tem = *(start+i)-'0';
- res = res+res_tem*pow(16,length-i-1);
- }
- else if((*(start+i)>='A')&&(*(start+i)<='F')) //約定只用大寫
- {
- res_tem = *(start+i)-'A'+10;
- res = res+res_tem*pow(16,length-i-1);
- }
- else
- {
- res = 0xFFFF; //錯誤數(shù)值就返回默認值
- break;
- }
- }
-
- return res & 0xFFFF;
- }
- /*將字符串s轉(zhuǎn)換成相應的整數(shù)*/
- // int atoi(char s[])
- // {
- // int i;
- // int n = 0;
- // for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
- // {
- // n = 10 * n + (s[i] - '0');
- // }
- // return n;
- // }
- unsigned int str2dec(char * start,char length) //字符轉(zhuǎn)10進制, 字符串其實地址, length 字符長度
- {
- char i = 0;
- unsigned char res_tem= 0x00;
- unsigned int res = 0x0000;
- res_tem = 0;
- res = 0;
- for(i = 0;i<length;i++)
- {
- if((*(start+i)>='0')&&(*(start+i)<='9'))
- {
- res_tem = *(start+i)-'0';
- res = res+res_tem*pow(10,length-i-1);
- }
- else
- {
- res = 0xFFFF; //錯誤數(shù)值就返回默認值
- break;
- }
- }
- return res & 0xFFFF;
- }
- /* 將無符號十進制數(shù)拆分并轉(zhuǎn)換為字符形式,返回字符串長度 */
- char uBinaryToAscii(unsigned int value, char *str)
- {
- char i;
- char num_str[10] = { 0 };
- unsigned int quotient;
- unsigned char len;
- len = 0;
- quotient = value / 10;
- while (quotient != 0)
- {
- num_str[len] = value % 10 + '0';
- len++;
- value = quotient;
- quotient = value / 10;
- }
- if (value != 0)
- {
- num_str[len] = value + '0';
- len++;
- }
- for (i = 0; i < len; i++)
- {
- *str = num_str[len-i-1];
- str++;
- }
- return len;
- }
- /* 將有符號十進制數(shù)拆分并轉(zhuǎn)換為字符形式,返回字符串長度 */
- /* 調(diào)用了uBinaryToAscii函數(shù) */
- char sBinaryToAscii(int value, char *str)
- {
- char len;
- if (value < 0)
- {
- *str = '-';
- str++;
- len = uBinaryToAscii(-value, str);
- return len + 1;
- }
- else
- {
- len = uBinaryToAscii(value, str);
- return len;
- }
- }
- void Timedistributionfunction(void ) //用于接收網(wǎng)絡時間
- {
- //時間
- if (networktime.tm_mon<10) networktime.tm_mon=networktime.tm_mon+60;
- if (networktime.tm_mday<10) networktime.tm_mday=networktime.tm_mday+60;
- if (networktime.tm_hour<10) networktime.tm_hour=networktime.tm_hour+60;
- if (networktime.tm_min<10) networktime.tm_min=networktime.tm_min+60;
- sBinaryToAscii(networktime.tm_year,timedata); //年
- sBinaryToAscii(networktime.tm_mon,timedata+4); //月
- sBinaryToAscii(networktime.tm_mday,timedata+6); //日
- sBinaryToAscii(networktime.tm_hour,timedata+8); //時
- sBinaryToAscii(networktime.tm_min,timedata+10); //分
- //sBinaryToAscii(networktime.tm_sec,timedata+7); //秒
- 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],\
- timedata[7],timedata[8],timedata[9],timedata[10],timedata[11],enddatabuff);
- // printf("%s\r\n",Timedatabuff);
- Usart_SendString(DEBUG_USART4,Timedatabuff);
- }
- /*-------------------------------------------------------
- 內(nèi)部通訊協(xié)議的CRC16計算函數(shù)。
- -------------------------------------------------------*/
- uint32_t ecbm_databuff_rtu_crc=0xFFFF; //初始化CRC變量各位為1。
- void ecbm_databuff_rtu_crc16(uint8_t buf)
- {
- uint8_t j;
- ecbm_databuff_rtu_crc^=buf; //當前數(shù)據(jù)異或CRC低字節(jié),在C51里可以直接處理。
- for(j=0;j<8;j++)
- { //計算每個字節(jié)的每一位。
- if(ecbm_databuff_rtu_crc&0x01)
- { //判斷右移前最低位是否為1。
- ecbm_databuff_rtu_crc=(ecbm_databuff_rtu_crc>>1)^0xA001;//如果為1則右移并異或表達式。
- }
- else
- {
- ecbm_databuff_rtu_crc>>=1; //否則直接右移一位。
- }
- }
- }
復制代碼
本人初學,僅供參考,存在錯誤和不足之處,請大家回帖多多指教,切勿照搬,代碼下載:
代碼.7z
(554.77 KB, 下載次數(shù): 10)
2022-10-4 15:18 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|