0

Optimize code

Hi, can you help me? I want to optimize execution time of my code: while (1) { // 1. Sending Measure Command buf[0] = AVG_COMMND; //Sensor register HAL_I2C_Master_Transmit(&hi2c1, DLHR_ADDR, buf, 1, 100); // write // 2. Status Read. Statusbuf[0] = AVG_COMMND; //Sensor register HAL_I2C_Master_Receive(&hi2c1, DLHR_ADDR, &Statusbuf[0], 1, 100); // Read //statusByte = Statusbuf[0]; //Read EOL status EOC_Status = 0; EOC_Status = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10); if (EOC_Status == 1){ buf[0] = AVG_COMMND; //Sensor register ret = HAL_I2C_Master_Receive(&hi2c1, DLHR_ADDR, &buf[0], 7, 100); // Read // If there are data if ( ret == HAL_OK ) { //Combine the bytes for pressure. Combine 4 8-bit into a one 32-bit (but sensor just give a 24-bit) // buf data in hex. statusByte = (int16_t)buf[0] << 8; buf[0] = 0; Press = ((int16_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3]); //Scale this output offsetP = 8388608; Press_c = (((float)Press - offsetP)/16777216)*1.25; //Combine the bytes for temp. Combine 4 8-bit into a one 32-bit (but sensor just give a 24-bit) Temp = ((int16_t)buf[0] << 24) | (buf[4] << 16) | (buf[5] << 8) | (buf[6]); //Scale this output Temp_c = ((float)Temp*125/16777216)-40; // Turn on green led HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); } else{ buf[0] = 0xAD; //Sensor register //On error Press_c = 1000; Temp_c = 1000; } // // serial transmission: // char buffer[50] = {}; // sprintf(buffer, "Press: %f Temp: %f \n", Press_c, Temp_c); // HAL_UART_Transmit(&huart2, (uint8_t *)buffer, sizeof(buffer), 10); } else { //No EOC // Turn on green led HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); } /* USER CODE END WHILE */ }

30th Jul 2021, 8:28 PM
Edwin Julian Gomez Porras
Edwin Julian Gomez Porras - avatar
1 Resposta
+ 1
You probably won't get very many answers from people on this site for microcontroller and prototyping board applications, sadly, as most of the user base is focused more on pc, mobile, and web applications. For the most part, you're just using library functions and a few shift statements, so I can't see anywhere you can really "optimize". At the very most, you could try using some inline assembly to replace some of your code, but I can't guarantee you'll get a performance boost. Rather than worrying about if you can optimize code for a low-end device (unless I'm misinterpreting this, which I may be), you should probably buy a higher spec board or controller to run the sensor. Skimping on hardware will only cause you trouble. For example, you shouldn't be trying to run this on an Arduino Uno and expect fantastic performance, because you won't (8 bit core running at 16Mhz isn't ideal). A Due or other 32 bit and > 16 Mhz core would probably work.
31st Jul 2021, 12:39 AM
BootInk
BootInk - avatar