void Init_array(void)//初始化数组 { unsigned int i,j; Init_data_buffer(); #if compilemodbus modbus_init(); #endif for(i=0;i<8;i++)//设备运行状态 { equipment_skate[i][0]=0; equipment_skate[i][1]=0; equipment_skate[i][2]=3; equipment_skate[i][3]=0; } for(i=0;i<12;i++)//波形初始化 for(j=0;j<64;j++) record_wave[i][j]=0; for(i=0;i<7;i++)//系统数据初始化 for(j=0;j<3;j++) WIRE_cost[i][j]=0; for(i=0;i<8;i++)//运行数据初始化 for(j=0;j<3;j++) LOAD_cost[i][j]=0; equiment_set[2]=P_E2PROM[13];//一共多少个主控地址 这个暂时保留 }
void init_devices(void) { // -------------------------------------------------------------- // DDR and core CPU init // -------------------------------------------------------------- // stop errant interrupts until set up cli(); MCUCR = 0; TIMSK = 0; //GICR = 0x00; #if HALF_DUPLEX HALF_DUPLEX_DDR |= _BV(HALF_DUPLEX_PIN); #endif LED1_DDR |= _BV(LED1); LED2_DDR |= _BV(LED2); // -------------------------------------------------------------- // initialize subsystems // -------------------------------------------------------------- lcd_init(); uart_init(); adc_init(); #if SERVANT_NPWM > 0 timer1_init(); // timers 0, 1 #endif timer0_init(); sei(); // re-enable interrupts modbus_init(); lcd_clear(); lcd_gotoxy( 0, 0 ); lcd_puts("Scan 1Wire:"); lcd_gotoxy( 0, 1 ); init_temperature(); menu_init(); // -------------------------------------------------------------- // all peripherals are now initialized, start 'em // -------------------------------------------------------------- adc_start(); #if SERVANT_NPWM > 0 timer1_start(); #endif }
/*lint -save -e970 Disable MISRA rule (6.3) checking. */ int main(void) /*lint -restore Enable MISRA rule (6.3) checking. */ { /* Write your local variable definition here */ uint8_t i ; //uint8_t send_buf[50] ; /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); /*** End of Processor Expert internal initialization. ***/ GPIO1_ClearFieldBits(GPIO1_Ptr, LAMP_CTR, 1); //turn off the lamp modbus_init(); Ram_Init(); read_from_flash(); task_init(); bmp180_init(); opa_init(); Aq_Init(); /* Write your code here */ /* For example: */ for(;;) { // sprintf((char*)send_buf,"#C%05u P%07u TP%04d TH%04d H%04u AQ%04u\n\r", modbus.Co2, modbus.PP, modbus.PT, modbus.HT, modbus.HH, modbus.Aq); // AS1_SendBlock(AS1_ptr, send_buf, strlen((char*)send_buf)) ; // Delay(6); if(heartbeat == TRUE) //tick = 1ms { heartbeat = FALSE; if(serial_receive_timeout_count > 0) { serial_receive_timeout_count--; if(serial_receive_timeout_count == 0) { revce_count = 0; } } if(serial_response_delay > 0) serial_response_delay--; if((dealwithTag == TRUE) && (serial_response_delay == 0)) { dealwithTag = FALSE; dealwithData(); } heartbeat_counter++; if(heartbeat_counter >= OS_TICK) // 1 * 10 = 10ms { heartbeat_counter = 0; task_manage(); } } Co2_Detect(); // // AS2_SendBlock(AS2_ptr, "start\n\r", sizeof("start\n\r")) ; // print_status = AD1_CreateSampleGroup(AdDataPTR, &ADC_CO2_Sample, 1) ; // Delay(300); // // sprintf((void*)str, "P = %u\n\r", (uint16_t)print_status); // AS1_SendBlock(AS1_ptr, str, sizeof(str)) ; // Delay(300); // if(enter_adc) // { // enter_adc = 0 ; // AS2_SendBlock(AS2_ptr, "enteradc\n\r", sizeof("enteradc\n\r")) ; // } // Delay(300); // switch(print_status) // { // case ERR_PARAM_SAMPLE_COUNT: // AS2_SendBlock(AS2_ptr, "Para\n\r", sizeof("Para\n\r")) ; // break; // case ERR_BUSY: // AS2_SendBlock(AS2_ptr, "BSY\n\r", sizeof("BSY\n\r")) ; // break; // case ERR_OK: // AS2_SendBlock(AS2_ptr, "OK\n\r", sizeof("OK\n\r")) ; // break ; // case ERR_PARAM_INDEX: // AS2_SendBlock(AS2_ptr, "index\n\r", sizeof("index\n\r")) ; // break ; // } // AD1_CreateSampleGroup(AdDataPTR, (LDD_ADC_TSample *)&ADC_PM25_Sample, 1U); // AD1_StartSingleMeasurement(AdDataPTR); // while(AD1_GetMeasurementCompleteStatus(AdDataPTR) != TRUE); // AD1_GetMeasuredValues(AdDataPTR, (LDD_TData *)&PM25_AD) ; // sprintf((void*)str, "P%u\n\r", (uint16_t)PM25_AD); // AS1_SendBlock(AS1_ptr, str, strlen((const char*)str)) ; // Delay(3000) ; } /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ #ifdef PEX_RTOS_START PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of RTOS startup code. ***/ /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ for(;;){} /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
int main(void) { /* ######################### Variables ######################### */ /* Variables for modbus */ modbus_t *ctx; int pollingInterval; uint16_t valueArray[2]; float floatValue; int no_register; uint8_t desiredBit; GenDataType_t dataType; MoBuRegType_t registerType; /* Variables for mosquitto */ struct mosquitto *mosq; char topic[BUFFER_SIZE]; char text[50]; /* Variables for time */ struct timespec last_read_time; struct timespec now; /* ######################### Initialize variables ######################### */ sprintf(topic, "/test"); /* Read values from configfile */ if(getStringFromFile_n(CONFIG_FILE_PATH, "mosquitto_topic", topic, BUFFER_SIZE) == -1) { log_entry(APP_NAME, "Error reading topic"); printf("Error reading topic..\n"); /* wait for new config */ wait_for_new_config(); return EXIT_FAILURE; } pollingInterval = getIntFromFile(CONFIG_FILE_PATH, "modbus_polling_Interval"); no_register = getIntFromFile(CONFIG_FILE_PATH, "modbus_register"); dataType = getIntFromFile(CONFIG_FILE_PATH, "modbus_data_type"); registerType = getIntFromFile(CONFIG_FILE_PATH, "modbus_register_type"); desiredBit = getIntFromFile(CONFIG_FILE_PATH, "modbus_desired_bit"); /* Check values of variables */ if(check_values(no_register, pollingInterval) == ERROR) { log_entry(APP_NAME, "Error: Invalid values"); /* wait for new config */ wait_for_new_config(); return EXIT_FAILURE; } /* ######################### Initialize communication ######################### */ /* Initialize Mosquitto */ printf(SPLIT_LINE); mosq = mosquitto_initialize(CONFIG_FILE_PATH); if(mosq == NULL) { log_entry(APP_NAME, "Error: Could not initialize mqtt connection"); /* wait for new config */ wait_for_new_config(); return EXIT_FAILURE; } /* Initialize Modbus */ printf(SPLIT_LINE); ctx = modbus_init(CONFIG_FILE_PATH); if(ctx == NULL) { log_entry(APP_NAME, "Error: Could not initialize modbus connection"); /* wait for new config */ wait_for_new_config(); return EXIT_FAILURE; } /* Initialize time */ clock_gettime(CLOCK_MONOTONIC, &last_read_time); /* ######################### Start application ######################### */ printf(SPLIT_LINE); log_entry(APP_NAME, "Start reading values.."); printf("\nStart reading values..\n\n"); /* Endless Loop */ while(1) { clock_gettime(CLOCK_MONOTONIC, &now); /* Check if polling time is over (multiply with 1000 to calculate seconds) */ if(diff(&last_read_time, &now) >= pollingInterval * 1000) { /* Read values from modbus */ if(read_modbus(ctx, 0, no_register, dataType, registerType, desiredBit, valueArray) == ERROR) { /* Set last read time */ clock_gettime(CLOCK_MONOTONIC, &last_read_time); continue; } switch(dataType) { case bitCoil: case sint16: case sint32: case uint16: case uint32: floatValue = modbus_get_float(valueArray); sprintf(text, "%.0f", floatValue); break; case float32: floatValue = modbus_get_float(valueArray); sprintf(text, "%.2f", floatValue); break; } /* Message for user */ printf("Value: %s\n", text); /* Publish value */ if(mosquitto_pub(mosq, topic, strlen(text), text) == SUCCESS) { log_entry(APP_NAME, "Published value successfully"); printf("Published value successfully.\n\n"); } else { log_entry(APP_NAME, "Error: Value could not be published."); printf("Error: Value could not be published.\n"); } /* Set last read time */ clock_gettime(CLOCK_MONOTONIC, &last_read_time); } /* taking care of processor and slow down the loop */ usleep(500); } /* We should never get here */ mosquitto_quit(mosq); modbus_quit(ctx); log_entry(APP_NAME, "Quit application"); return EXIT_SUCCESS; }
/*--------------------------------------------------------------------------------------------*/ static void udp_fill_data_rsp(struct udp_tx *udp_tx_info) { static char pdata_buf[20]; char data_buf[3]; #if sht11 float tc,hc; sht11_init(); unsigned int tempera = sht11_temp(); unsigned int humidity = sht11_humidity(); tc=sht11_TemperatureC(tempera); hc=sht11_Humidity(tempera,humidity); printf("sorce Tc:%d;Hc:%d\n",(int)tc,(int)hc); // tc=19.5; // hc=59.2; printf("Tc:%d;Hc:%d\n",(int)tc,(int)hc); pdata_buf[0]=0x02; sprintf(data_buf,"%d",(int)tc); memcpy(&pdata_buf[1],data_buf,3); pdata_buf[4]=0x03; sprintf(data_buf,"%d",(int)hc); memcpy(&pdata_buf[5],data_buf,3); // sprintf(temp_data_buf,"%d.%d",(int)hc,((int)(hc*10))%10); printf("temp_data:%s\n",pdata_buf); #endif #if tm_sensor int16_t temperature; temperature = sensor_temp_get(TEMP_UNIT_CELCIUS); pdata_buf[0]=0x02; sprintf(data_buf,"%d",temperature); memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif #if light_sensor int16_t illumination; modbus_init(); illumination= modbus_get(); printf("illumination_data:%d\n",illumination); illumination=55; data_buf[0]=0x00; data_buf[1]=0xff&(illumination>>8); data_buf[2]=0xff&illumination; pdata_buf[0]=0x04; memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif #if bh1750_sensor int16_t illumination; illumination=60; bh1750_init(); illumination= bh1750_light(); printf("illumination_data:%d\n",illumination); data_buf[0]=0x00; data_buf[1]=0xff&(illumination>>8); data_buf[2]=0xff&illumination; pdata_buf[0]=0x04; memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif uint8_t sum = 0; udp_tx_info->buf[0] = udp_header[0]; udp_tx_info->buf[1] = udp_header[1]; udp_tx_info->buf[2] =version[0]; udp_tx_info->buf[3] = DATA_INQUIRE_RSP; //0x10 udp_tx_info->buf[4]=user.userid[3]; udp_tx_info->buf[5]=user.userid[2]; udp_tx_info->buf[6]=user.userid[1]; udp_tx_info->buf[7]=user.userid[0]; udp_tx_info->buf[8] = K-8; memcpy(&udp_tx_info->buf[9],udp_id,K-8); memcpy(&udp_tx_info->buf[K+1],device_type,4); udp_tx_info->buf[K+5] = 0x00; udp_tx_info->buf[K+6] = 0x14; memcpy(&udp_tx_info->buf[K+7],pdata_buf,20); sum = calac_checksum(udp_tx_info); udp_tx_info->buf[K+27] = sum; udp_tx_info->buf[K+28] = 0x0D; udp_tx_info->buf[K+29] = 0x0A; udp_tx_info->len = K+30; }
//----------------------------------------------------------- void main (void) { init_hardware(); modbus_init(); blink_init(); data_holding_register[31]= VERSION; __enable_interrupt(); while(1) { inspect_reset(); delay_ms(1); if (INPUT1) { data_discret_inputs[0] &= ~(1<<0); if (togle[0] == 0){togle[0] = 1;data_input_register[0]++;} } else { data_discret_inputs[0] |= (1<<0); if (togle[0] == 1) togle[0] = 0; } if (INPUT2) { data_discret_inputs[0] &= ~(1<<1); if (togle[1] == 0){togle[1] = 1;data_input_register[1]++;} } else { data_discret_inputs[0] |= (1<<1); if (togle[1] == 1) togle[1] = 0; } if (INPUT3) { if (togle[2] == 0){togle[2] = 1;data_input_register[2]++;} data_discret_inputs[0] &= ~(1<<2); } else { data_discret_inputs[0] |= (1<<2); if (togle[2] == 1) togle[2] = 0; } if (INPUT4) { if (togle[3] == 0){togle[3] = 1;data_input_register[3]++;} data_discret_inputs[0] &= ~(1<<3); } else { data_discret_inputs[0] |= (1<<3); if (togle[3] == 1) togle[3] = 0; } if (INPUT5) { if (togle[4] == 0){togle[4] = 1;data_input_register[4]++;} data_discret_inputs[0] &= ~(1<<4); } else { data_discret_inputs[0] |= (1<<4); if (togle[4] == 1) togle[4] = 0; } if (INPUT6) { if (togle[5] == 0){togle[5] = 1;data_input_register[5]++;} data_discret_inputs[0] &= ~(1<<5); } else { if (togle[0] == 1) togle[5] = 0; data_discret_inputs[0] |= (1<<5); } if (INPUT7) { if (togle[6] == 0){togle[6] = 1;data_input_register[6]++;} data_discret_inputs[0] &= ~(1<<6); } else { if (togle[6] == 1) togle[6] = 0; data_discret_inputs[0] |= (1<<6); } if (INPUT8) { if (togle[7] == 0){togle[7] = 1;data_input_register[7]++;} data_discret_inputs[0] &= ~(1<<7); } else { data_discret_inputs[0] |= (1<<7); if (togle[7] == 1) togle[7] = 0; } if (INPUT9) { if (togle[8] == 0){togle[8] = 1;data_input_register[8]++;} data_discret_inputs[1] &= ~(1<<0); } else { data_discret_inputs[1] |= (1<<0); if (togle[8] == 1) togle[8] = 0; } if (INPUT10) { if (togle[9] == 0){togle[9] = 1;data_input_register[9]++;} data_discret_inputs[1] &= ~(1<<1); } else { data_discret_inputs[1] |= (1<<1); if (togle[9] == 1) togle[9] = 0; } if (INPUT11) { if (togle[10] == 0){togle[10] = 1;data_input_register[11]++;} data_discret_inputs[1] &= ~(1<<2); } else { if (togle[10] == 1) togle[10] = 0; data_discret_inputs[1] |= (1<<2); } if (INPUT12) { if (togle[11] == 0){togle[11] = 1;data_input_register[12]++;} data_discret_inputs[1] &= ~(1<<3); } else { if (togle[11] == 1) togle[11] = 0; data_discret_inputs[1] |= (1<<3); } if (INPUT13) { if (togle[12] == 0){togle[12] = 1;data_input_register[12]++;} data_discret_inputs[1] &= ~(1<<4); } else { if (togle[12] == 1) togle[12] = 0; data_discret_inputs[1] |= (1<<4); } if (INPUT14) { if (togle[13] == 0){togle[13] = 1;data_input_register[13]++;} data_discret_inputs[1] &= ~(1<<5); } else { if (togle[13] == 1) togle[13] = 0; data_discret_inputs[1] |= (1<<5); } if (INPUT15) { if (togle[14] == 0){togle[14] = 1;data_input_register[14]++;} data_discret_inputs[1] &= ~(1<<6); } else { if (togle[14] == 1) togle[14] = 0; data_discret_inputs[1] |= (1<<6); } if (INPUT16) { if (togle[15] == 0){togle[15] = 1;data_input_register[15]++;} data_discret_inputs[1] &= ~(1<<7); } else { if (togle[15] == 1) togle[15] = 0; data_discret_inputs[1] |= (1<<7); } } }