-
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);
}
-
>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
*/