Jump to content

how to add in a delay?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
shifu

shifu

    Newbie

  • Members
  • PipPip
  • 23 posts
HELLO!
may i know how to put in a delay?

my code is this:


HR1 = 92160/pulseperiod;

ADDR1 = 1;


if(HR1 < 40)

{

}

else

{

TXBUF0 = ADDR1;

                <---------------------------------// i want to put a delay of 0.1sec here

TXBUF0 = HR1;

}



#2
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
The simple way is to just call Sleep() find information in it here

The more complicated way is to do it manually by getting the system time then creating a loop, checking the old time against the current time during each loop

#3
martian

martian

    Newbie

  • Members
  • Pip
  • 9 posts
is Sleep() only available in c++?

#4
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
I do not program in c, but as far as I am aware Sleep() should work for either c or c++ by including the header windows.h.

If not you can fall back to dos.h and use the sleep() function (note the small 's' in this function) the main difference between Sleep() and sleep() is Sleep() takes it's argument in milliseconds and sleep() takes it in full seconds.

#5
shifu

shifu

    Newbie

  • Members
  • PipPip
  • 23 posts
ya use sleep(); but for the header do we need like conio.h or stdio.h >???? cos when i directly put sleep() it promt me an warning.

#6
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
Either include Winbase.h or Windows.h to use Sleep()

#7
Bartimäus

Bartimäus

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I think the header for sleep is the time header
unistd.h

[SIGPIC][/SIGPIC]

#8
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
When i needed a delay I did it like this:
#include <time.h>

int tateSleep(int seconds, long nanoSeconds){
	struct timespec time;
	time.tv_sec=seconds;
	time.tv_nsec=nanoSeconds;
	return sleepTillOver(&time);
}

int sleepTillOver(struct timespec* time){
	struct timespec remaining;
	if(nanosleep(time, &remaining) < 0){
		printf("Nanosleep failed.\n");
		return 0;
	}
	if(remaining.tv_nsec>0 || remaining.tv_sec>0){
		printf("Needs more sleep.\n");
		return sleepTillOver(&remaining);
	}else{
		return 1; // Done sleeping
	}
}
Is this a good way to do this?
twas brillig

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

shifu said:

HELLO!
may i know how to put in a delay?

my code is this:


HR1 = 92160/pulseperiod;

ADDR1 = 1;


if(HR1 < 40)

{

}

else

{

TXBUF0 = ADDR1;

                <---------------------------------// i want to put a delay of 0.1sec here

TXBUF0 = HR1;

}


When you are working in an embedded environment, it is best to mention this straight away. Otherwise you will get a thread bogged down with PC solutions that are not available on your platform.

There should be examples of using timers that came with your development platform. Look into these examples. I've found that many show how to use both polling (which I would presume you to want in an inline case like this) or interrupts.