View previous topic :: View next topic |
Author |
Message |
JulsPower
Joined: 18 Feb 2008 Posts: 11
|
Delay in uS |
Posted: Fri Feb 29, 2008 8:57 pm |
|
|
Hi
its me
again I know
I got some probly with delay_us
while(TRUE) //Main Loop
{
if(k==200)
{
output_toggle(PIN_A4);
k=0;
}
k++;
delay_us(500);
}
and
while(TRUE) //Main Loop
{
if(k==400)
{
output_toggle(PIN_A4);
k=0;
}
k++;
delay_us(250);
}
should be the same right?
first one the led if flashing at right speed
but second one too much fast what is happening there?
should I use a timer to do this? |
|
![](templates/subSilver/images/spacer.gif) |
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
|
Posted: Fri Feb 29, 2008 9:32 pm |
|
|
What type is "k"? |
|
![](templates/subSilver/images/spacer.gif) |
Franck26
Joined: 29 Dec 2007 Posts: 122 Location: Ireland
|
|
Posted: Sat Mar 01, 2008 4:40 am |
|
|
Hi JulsPower,
I don't now what is the problem, maybe the type of k like suggested by bwhiten, but in every case it will be better for you to use a timer.
Your micro is spending is time in the "delay_us" function. It won't be able to do anything else...
Franck. |
|
![](templates/subSilver/images/spacer.gif) |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Delay in uS |
Posted: Sat Mar 01, 2008 6:41 am |
|
|
JulsPower wrote: |
if(k==200)
and
if(k==400)
should be the same right?
|
In case you haven't figured out why bwhiten and Franck26 are so concerned with the type of 'k', it is that if k is an 8-bit int, it cannot ever be equal to 400. However, the compiler makes a stupid assumption. It assumes that all you want to do is compare k with the low byte of 400 (which is 144). Therefore (k==400) is the same as (k==144). That would explain the second loop being a lot faster.
Robert Scott
Real-Time Specialties |
|
![](templates/subSilver/images/spacer.gif) |
JulsPower
Joined: 18 Feb 2008 Posts: 11
|
|
Posted: Tue Mar 04, 2008 8:12 pm |
|
|
ooh you are all right
it was a int
but i already made it with a timer
and soon with int |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Mar 05, 2008 4:40 pm |
|
|
JulsPower wrote: | ooh you are all right
it was a int
but i already made it with a timer
and soon with int |
Is this a haiku? |
|
![](templates/subSilver/images/spacer.gif) |
|