当前位置:网站首页>Lpc1768 optimization comparison of delay time and different levels

Lpc1768 optimization comparison of delay time and different levels

2022-04-23 19:29:00 ToneChip

Because I'm writing IIC When , A software is used to simulate IIC To drive the encryption chip , So you need to write a delay Function to control timing , But when I do a good job without code optimization code Become the highest level ( The fastest , Code optimization ) After the optimization of the , Find out IIC The verification failed , It is initially suspected that the timing of the software is not right , So using IO The time comparison of different delay functions is tested with the logic analyzer .

//=2022.04.21  In order to test delayms The specific time of function delay , Use IO Flip to measure 
void TestDelay(void)
{
    LEDInitOnCS0804();
//    __enable_irq();

    while(1)
    {
        LED_ON;
        atca_delay_us2(1);
        LED_OFF;
        atca_delay_us2(1);
    }
}


/************************************************
    Optimization grade    none    low      Med     High   (ms)
delayms(1);     0.29                     0.27
delayms(4);     0.59                     0.57
delayms(10);    1.19                     1.17
delayms(1000);  100.1            100.1   100.1
atca_delay_us2(1); 2.75us                1.5us
atca_delay_us3(1);  5us                  4.75us
************************************************/
The typesetting is not quite right , Only the highest level and none

Each function is defined as follows

//#pragma optimize=none
void delayms(int ms)
{
    //2019.01.01 Fill the pit here 
    // When high-level optimization is selected , In the circulation without any action, use volatile To define variables 
    volatile int i;
    volatile int j;

//    int i;
//    int j;

//    for( i = 0; i<16666; i++)  // about  1ms
//        for( j = 0; j<ms; j++);
    for( i = 0; i < 1000; i++)   //=1000  OK
        for( j = 0; j < ms; j++);

//    asm( " nop " );
//    asm( " nop " );
//    asm( " nop " );


}
void atca_delay_us2(uint32_t nus)
{
    int i;
    int j;

    for( i = 0; i < 17; i++) // about  1us = 16.6666
        for( j = 0; j < nus; j++);

}


void atca_delay_us3(uint32_t nus)
{
    volatile int i;
    volatile int j;

    for( i = 0; i < 17; i++) // about  1us = 16.6666
        for( j = 0; j < nus; j++);

}

Through comparison, we found that atca_delay_us2 and atca_delay_us3 The only difference is that the variable uses volatile

summary :  Use volatile Modified variables have no effect on code optimization in engineering

Finally, with IAR6.3 Code optimization opens the selection interface

版权声明
本文为[ToneChip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231923488342.html