Discussion in "8051 Discussion Forum" started by    neil03    Nov 13, 2009.
Fri Nov 13 2009, 02:13 am
#1
hi guys I want to generate a 1 second delay using timers. my problem is I dont know what time reload value will I place since the desired count exceeds maximum register count.
This is the following step that I follow on generating the delay.

1. 1 machine cycle = 12 crystal pulse

2. Im using a 12 megahertz crystal so; crystal time = 12 / crystal frequency
; crystal time = 1uS ,since im using a 12Mhz crystal.

3. desired count = time desired /1 uS
; desired count = 1second / 1us
;desired count =1,000,00

4. time reload value = maximum register count - desired count
= 65536 - 1000000
: the fourth step is my problem since my desired count exceeds the number of register count and will result to negative value... please help me how can I resolve this issue thanks.

Fri Nov 13 2009, 03:20 am
#2
A 16bit timer will give a max delay of 65,536 uS ,about 65.5mS
so you can't do it in one shot.

Create a general purpose 50mS delay and call it 20 times.


[ Edited Fri Nov 13 2009, 03:23 am ]
Fri Nov 13 2009, 05:06 am
#3
Have a look at a similar discussion here:

http://www.8051projects.net/forum-t28059-last.html

The example I gave can be used for a one second delay by calling

Delay( 1000 );

See if this makes sense to you...

Sat Nov 14 2009, 12:42 am
#4
sorry sir but it seems not clear to me..umm I generated a code of blinking led and generate a .05 second delay can you teach me how can i call it twice.. thank you sir
#include <REG2051.H>

#include <math.h>

#define led P1_2

delay()
{
  TL0 = 0xAF;
  TH0 = 0x3C;
  TR0 = 1;
}

setup_timer()
{
 TMOD = 0x05;
 EA  = 1;
 ET0 = 1;
 
}


void main()
{
 setup_timer();
while(1) {

 delay();
 P1_2 = 0;
 delay();
 P1_2 = 1;
 		  }
}
Sat Nov 14 2009, 01:37 am
#5
@ dave umm on your code the "ticker" can you explain me this part im sorry im new to this I cant understand how it will generate the delay...

void Delay( unsigned int NumMillisecs )
   {
   unsigned long DelayTickValue;

   DelayTickValue = Ticker + NumMillisecs;
   while( Ticker < DelayTickValue );
   }
   



thank you sir
Sat Nov 14 2009, 02:00 am
#6
Sure.

The "ticker" is a global variable that you can get the value of whenever you want.

Since there is an interrupt running, the value is being incremented every millisecond.

So, if you want to delay for 10 milliseconds, all you have to do is read the value of ticker, then add 10 to it, then wait until it equals this value...

That is what the Delay() function is doing. You pass it a value (in milliseconds) that you want to delay, and it will wait until the ticker value reaches the right value before returning...
Sat Nov 14 2009, 01:11 pm
#7
on the code that I wrote can you teach how can I call it twice please.. thank you sir
Sat Nov 14 2009, 02:47 pm
#8
void delay_us(unsigned int us)
{
int a=(us)/13; //BY TRIAL
while(a--);
}
Sat Nov 14 2009, 03:34 pm
#9


void delay_us(unsigned int us)
{
int a=(us)/13; //BY TRIAL
while(a--);
}



thank you sir for looking at my thread..umm Im sorry I cant really understand how the code will work can you explain to me please..thanks
Sat Nov 14 2009, 06:18 pm
#10
delay()
{
  TL0 = 0xAF;
  TH0 = 0x3C;
  TR0 = 1;
  while(TF0==0); //wait for timer to finish the delay
  TF0 = 0; //clear timer overflow flag
}


if the above you have calculated for 0.5 s then call the function twice for 1 sec.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

scaneraNom
Sat May 04 2024, 02:21 am
gtaletrzua
Fri May 03 2024, 10:55 am
Clydehet
Wed May 01 2024, 06:44 pm
Davidoried
Wed May 01 2024, 06:11 pm
KevinTab
Sun Apr 28 2024, 05:35 am
Tumergix
Sun Apr 28 2024, 12:59 am
StevenDrulk
Sat Apr 27 2024, 08:47 pm
StephenHauct
Sat Apr 27 2024, 09:38 am