打印

Definition of variables, an operation of variables

[复制链接]
361|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kaloulin|  楼主 | 2018-9-9 09:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Dear all,
I have an experience to develop C code for different microcontrollers and DSPs. However, this is first time to develop code for Arm Core microcontrollers. I am using STM32F334 and STM32F745 for different projects. Last couple of weeks, I have learnt to control peripherals and I hadn\'t calculated any mathematical expression.  I started to write mathematical calculations and I realized they took a lot of time.
For example:
uint16_t Middle, Low, High;
Middle = ((Low + High) * 0.5);
This code takes ~6.25us. Actually, it adds two unsigned integer and one floating point multiplication. It should be very fast because STM32F334 has FPU. I used this code for Texas Instrument TMS320F335. (Texas Complier understand 0.5 is floating and no need to declared again.) I searched on internet and I revised some sample codes and I realized that I should write (float)0.5 or 0.5f. Then I changed the code as like below:
Middle = ((Low + High) * 0.5f);
This new code takes 259ns. Which is ~24 times faster than previous code. (I have developed many codes for TMS320F28335 and they worked well. I thought it was in C and this is also in C languages and it should work.) One letter makes the execution time much faster. I also tried to understand if I need to declared integer number or casting floating point number into integer number but I observed no need to do it. For example:
Middle = (uint16_t) ((Low + High)  0.5f);   is exactly consumes same time with Middle = ((Low + High)  0.5f);
or Middle = 34; takes same time with Middle = (uint16_t)34;...
Another example:
define Offset 5.0fdefine Correction 1.002fvolatile float Voltage;
Voltage = ((ADC1->DR) - Offset)*Correction;
ADC1->DR is 32-bit unsigned integer and all others are floating numbers. I was not sure if I have to write the code as below:
Voltage = ((float)(ADC1->DR) - Offset)*Correction;   
I tried and I didn\'t see any difference. So, time is very crucial in my code. I have to write an optimum code as much as I can. That is why I don\'t use HAL function and I wrote my own functions.  I searched documents to learn how to define variables or how to handle different data type in one equation. I think it is about Arm Complier and I had a look to Arm complier user guide. However, I didn\'t find any enough explanations.
I would like to ask you if you have a document/sample codes and etc, could you please share me.
Last thing that: if Offset is greater than 31.0f, the execution time is more than if the offset is lower than 32.0f. What is the relation?
define Offset 32.0f   It is slower... 2^5 = 32?  It took few hundreds nanosecond more than if Offset is 31.0f.Thank you very much in advance.

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

439

主题

448

帖子

0

粉丝