This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C123GH6PM: When code run on Keil μVision, LED blink every 0.5 sec, but it run on CCS, LED blink every 1 sec

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

I am still new to CCS v7. 

I am migrating from Keil μVision. I was testing a code I wrote on Keil. The code is supposed to make TM4C on-board LED blink every 0.5 sec. But when I run this code using CCS, the TM4C blink every 1 sec. 

My optimization setting is --opt_levl: off, Speed: 5

I don't know if there is anything to do with the setting.

  • How are we to have any idea from that snippet of information? Really from that description I've no reason to expect any particular behaviour at all.

    Might I suggest showing the code?

    Robert
  • /* code to blink on-board LED every 0.5 sec */


    #include "PLL.h" #define GPIO_PORTF2 (*((volatile unsigned long *)0x40025010)) #define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) #define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420)) #define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) #define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528)) #define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C)) #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) #define SYSCTL_RCGC2_GPIOF 0x00000020 // port F Clock Gating Control // Delay100ms function from Lab 6 void Delay100ms(unsigned long time){ unsigned long i; while(time > 0){ i = 1333333; // this number means 100ms while(i > 0){ i = i - 1; } time = time - 1; // decrements every 100 ms } } int main(void){ volatile unsigned long delay; PLL_Init(); SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF; // activate port F delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize GPIO_PORTF_DIR_R |= 0x04; // make PF2 out (PF2 built-in LED) GPIO_PORTF_AFSEL_R &= ~0x04; // regular port function GPIO_PORTF_DEN_R |= 0x04; // enable digital I/O on PF2 // configure PF4 as GPIO GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFF0FFFF)+0x00000000; GPIO_PORTF_AMSEL_R = 0; // disable analog functionality on PF while(1){ GPIO_PORTF2 = 0x04; // turn on LED1 (blue) Delay100ms(5); // delay ~0.5 sec at 80 MHz GPIO_PORTF2 = 0x00; // turn off LED1 (blue) Delay100ms(5); // delay ~0.5 sec at 80 MHz } }

  • PLL_Init() is a void function to set the clock to 80MHz
  • I used this code on Keil, the LED ON 0.5 sec and OFF 0.5 sec;
    But when this code run on CCS, LED ON 1.0 sec and OFF 1.0 sec
  • Tom Liang said:
    // Delay100ms function from Lab 6
    
    void Delay100ms(unsigned long time){
    
      unsigned long i;
    
      while(time > 0){
    
        i = 1333333;  // this number means 100ms
    
        while(i > 0){
    
          i = i - 1;
    
        }
    
        time = time - 1; // decrements every 100 ms
    
      }
    
    }
    

    OK, this is compiler dependent. It potentially depends on the compiler, the compiler version,the optimization level, clock frequency and caching behaviour at the least. The compiler is quite free to completely eliminate this function at any priority level and a really good compiler would do so.

    The way to deal with this 'properly' is one of the following

    1. Kernal based delay function
    2. H/W Timer
    3. H/W timer with H/W output
    4. A fixed delay function written in asm
    5. A C fixed delay function with use of volatile.

    #1 is preferred for any sort of RTK/RTOS and even for some non-RTOS setups. #2 works reasonably well but doesn't scale to a large number of timers. #3 is best for really tight timing. #4 is really a sort of last resort and should be avoided.

    Really #4 and #5 require a lot of justification to use. They can be used for something quick and dirty (i.e. to prove whether you have a timing dependency) but otherwise you should almost certainly use one of the first three. The first three generally only depend on the clock. #1 is usually based on the same principles as #2. It's advantage is that the work is already done.

    Robert

  • Thanks Robert. Can you give any reference for #3? Can I find it on user guide?
  • Yes, in the timer section. Also in the TivaWare manual in the timer section, you'll want to review that in any case.

    Robert