timer

This is a discussion on timer within the C Programming forums, part of the General Programming Boards category; hi would anyone be able to tell me why the delay function does not work correctly? the programs made to ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    13

    timer

    hi would anyone be able to tell me why the delay function does not work correctly? the programs made to run on a c167 microprocessor. I'm trying to implement a timer using the T6 timer in GPT2 however it doesnt seem to delay/work if anyone can help i;d be really greatful

    thanks

    Code:
    void delay (unsigned short MS);
    void Timer_Six_Interrupt (void); 
    void InitialiseTimer (void);
        
    
    int TimerCount;
    
    void main(void)
    {
      
        DP2 = 0xFFFF;	// make the port pins as outputs
        ODP2 = 0xFFFF;	// output type open drain I.E pull down
    
        CC0IO = 0x0000; //THIS CONTROLS THE LEDS TURNING each led on
    	
    	InitialiseTimer();
    
    
                    delay(10000);
    	CC15IO = 0xFF;	//this tuns led 15 off
      	CC4IO = 0xFF; //this turns led five off
    	delay(5);
    	CC15IO = 0x0000;	//this tuns led 15 on
    	CC4IO = 0x0000; //this turns led five on
     }
    
    void InitialiseTimer (void)
    {
     T6CON = 0x0;
     T6 = ReloadValue;
     TimerCount = 0;  
     T6IE = 1;
     T6R = 1;                                                                                                                                                                                                                                                                                    
    }
    		 
    void delay (unsigned short MS)
    {
      static unsigned long StartTime, EndTime;
    
    do{
    StartTime = TimerCount;
    EndTime = StartTime+MS;
    TimerCount++;
    }while(TimerCount != EndTime);
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >hi would anyone be able to tell me why the delay function does not work correctly?

    Don't calculate the EndTime each time in the loop -- it will continually be updated and loop forever. The following is an example of what is happening.
    Code:
    #include <stdio.h>
    
    unsigned long TimerCount = 0;
    
    void delay (unsigned short MS)
    {
       static unsigned long StartTime, EndTime;
       do
       {
          StartTime = TimerCount;
          EndTime = StartTime+MS;
          TimerCount++;
          printf("StartTime = %lu, EndTime = %lu, TimerCount = %lu\n",
                 StartTime, EndTime, TimerCount);
       }while ( TimerCount != EndTime );
    
    }
    
    int main(void)
    {
       delay(10);
       return 0;
    }
    
    /* my output
    StartTime = 0, EndTime = 10, TimerCount = 1
    StartTime = 1, EndTime = 11, TimerCount = 2
    StartTime = 2, EndTime = 12, TimerCount = 3
    StartTime = 3, EndTime = 13, TimerCount = 4
    StartTime = 4, EndTime = 14, TimerCount = 5
    StartTime = 5, EndTime = 15, TimerCount = 6
    StartTime = 6, EndTime = 16, TimerCount = 7
    StartTime = 7, EndTime = 17, TimerCount = 8
    StartTime = 8, EndTime = 18, TimerCount = 9
    StartTime = 9, EndTime = 19, TimerCount = 10
    StartTime = 10, EndTime = 20, TimerCount = 11
    StartTime = 11, EndTime = 21, TimerCount = 12
    StartTime = 12, EndTime = 22, TimerCount = 13
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21