Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Questions about C code for hardware delay

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi...

Bellow is the hardware delay program, can you help me to explain 3Q:

Code:
#include <reg52.h>

/*------------------------------------------------------------------------*/

void main(void)
{

	while(1)
	{
		unsigned char d;
	
		for(d=0; d<20; d++)
		{
			TMOD &= 0xF0; // 1) TMOD = TMOD & 0xF0 ??
			TMOD |= 0x01; // 2) TMOD = TMOD | 0x01 ??

			ET0 = 0; // 3)  ??

			TH0 = 0x00; 			
         TL0 = 0xFA;

			TF0 = 0; 
			TR0 = 1; 
					 
			while(TF0 == 0); 

			TR0 = 0; 				} 
	}
}

1Q) Is it the TMOD set the Timer0 (bit 0-3) is low and Timer1 (bit 4-7) is high, Why?? Why use AND operation TMOD = TMOD & 0xF0 ??

2Q) Is it the TMOD set the bit1=M1 is high, Why?? Why use OR operation TMOD = TMOD | 0xF0 ??

3Q) What the different ET is 0 or 1, why would't have any different?? Is it we must set the interupt there?

Thank You...
 

Re: C code (Delay)

TMOD &= 0xF0; // 1) TMOD = TMOD & 0xF0 ??

it is to clear register timer0(bit3-bit0), but for register timer1(bit7-bit4) have still old setting. remember AND operation, result = 0, if x AND with 0

TMOD |= 0x01; // 2) TMOD = TMOD | 0x01 ??

it is to set register timer0, but register timer1 have still old setting.
remember OR operation, result=1, if x AND 1
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

suromenggolo said:
TMOD &= 0xF0; // 1) TMOD = TMOD & 0xF0 ??

it is to clear register timer0(bit3-bit0), but for register timer1(bit7-bit4) have still old setting. remember AND operation, result = 0, if x AND with 0

TMOD |= 0x01; // 2) TMOD = TMOD | 0x01 ??

it is to set register timer0, but register timer1 have still old setting.
remember OR operation, result=1, if x AND 1

Hi..
1)
Why he want to clear register timer0 bit 3-0, and why remain the timer1 is H which mean that the [TMOD = 0xF0 (11110000)] = [TMOR-SFR(old) = 0xFF (11111111)] AND [TMOR-SFR = 0xF0 (11110000)], why don't we put directly TMOD = 0xF0??
2)
Why we set Timer1 is L and just set the Timer0 M0 is H?

If like that why we don't directly 2 in 1 just put TMOD = 0xF1;??

Thank for you answer....

Thank You..
 

Re: C code (Delay)

Yes, you can put directly for TMOD register, it is depend to timer using. look at datasheet about TMOD register.

But this operation method, may be can use another programming.
 

Re: C code (Delay)

Peace upon you
*************
Q1,2) 1st this function made just like that to let u make cope & paste in any other program without any problem, so in this code u wrote it you can immditly write

TMOD = 0x01; // used to make timer 0 work as 16 bit counter

to make copy and paste in any other program this technique made to save bit 4-7 as ( &0xF0) will clear just bits 0-3 then ( |0x01) to set only bit 0 so you sae value of bit 4-7 and set bit 0 and clear bits 1-3

Q3)here he deal only with timer 0 so he disable interrupt of timer 0
and it disable interrup here and use instead wait until timer over flow in command

while(TF0 == 0);

OK ???
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

If like that why we don't directly 2 in 1 just put TMOD = 0xF1;??

Just because that is not the purpose of those two statements:

TMOD &= 0xF0;
TMOD |= 0x01;

The results are different of what you expected you to be (TMOD = 0xF1).
The high nibble of TMOD shouldn't receive 1111 binary, otherwise the Timer 1 function is compromised.

TMOD = TMOD & 0xF0 means that you don't want to change the high nibble of TMOD.
Thus, if before get into delay loop the timer 1 would be configured to say 8 bit auto-reload, I bet you wouldn't want to change his behaviour when delay loop is finished.
That's why you performed the AND mask with 0xF0. To preseve the high nibble of TMOD.

Now, what you want to do it's set the timer 0 to work as 16 bit counter (That is, Mode selector bits, bit 0 of TMOD = 1 and
bit 1 of TMOD = 0).
Next you want Timer 0 to work as timer (input driven from internal system clock) and not counter (input driven from Tx input pin). Thus you must clear bit 2 of TMOD.
The last setting is to clear bit 3 of TMOD (GATE = 0) because you want to control software when Timer 0 is running (TR0 bit 4 of TCON register)

How would you perform all these settings in one shoot without even know the value of TMOD before getting into delay loop ?

- Set Bit 0 of TMOD
- Clear Bit 1 of TMOD
- Clear Bit 2 of TMOD
- Clear Bit 3 of TMOD
- Keep unchanged the bits 7, 6 and 5 of TMOD after leaving the delay loop
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

Hi ahm_hassaan,silvio


TMOD = 0x01; // used to make timer 0 work as 16 bit counter

to make copy and paste in any other program this technique made to save bit 4-7 as ( &0xF0) will clear just bits 0-3 then ( |0x01) to set only bit 0 so you sae value of bit 4-7 and set bit 0 and clear bits 1-3

Why we set the TMOD bit0(M0) = 1 and bit1(M1) = 0 because i find some internet information it set:

M0 M1 Mode Description
0 0 0 13-bit timer mode (8048 mode).
0 1 1 16-bit timer mode.
1 0 2 8-bit auto-reload mode

It should be bit0(M0) = 0 and bit0(M1) = 1 is it or i miss understanding the information...??

Q3)here he deal only with timer 0 so he disable interrupt of timer 0
and it disable interrup here and use instead wait until timer over flow in command

while(TF0 == 0);

Can you write a simple program if there are not use instead wait until timer over flow in command? Thanks....

Not OK.... :)

Thank You


Added after 1 hours 4 minutes:


TMOD = TMOD & 0xF0 means that you don't want to change the high nibble of TMOD.
Thus, if before get into delay loop the timer 1 would be configured to say 8 bit auto-reload, I bet you wouldn't want to change his behaviour when delay loop is finished.
That's why you performed the AND mask with 0xF0. To preseve the high nibble of TMOD.
I am not understand this paragraph...
Is it posible while program is start the TMOD=0x21 & 0F0, if like that what happen when finish this TMOD |= 0x01; statement??

- Keep unchanged the bits 7, 6 and 5 of TMOD after leaving the delay loop
If we leav the MSB like that wouldn't set any address, is it posible the program will indistinct occur?? Is it posible suddenly 2 timer (timer0 and timer1) is to be set??

Thank You
 

Re: C code (Delay)

Code:
M1  M0       Mode    Description 
0   0         0      13-bit timer mode (8048 mode). 
0   1         1      16-bit timer mode. 
1   0         2       8-bit auto-reload mode

It exists Mode 3, too.

For a better explanation and deep digging go to:
https://www.8052.it/download/programmers_guide.pdf

It's possible that when program start, elsewhere to set the timer 1 as baud rate generator for your serial routine. If you get into your delay loop and performing some changing on TMOD register which damage the high nibble of register, then you'll have big trouble with serial routine and you'll have to struggle enough to find where the cause is.
That's why as a good practice you have to mask other bits inside a register that don't counts for you.

And YES it's possible to set parameters for both timers in the same time.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

this program is good (as you see )
but Ithink for better result you must use Interrupt.
you most enable Timer Interrupt for time base Delay,


void main(void)
{

while(1)
{
unsigned char d;

for(d=0; d<20; d++)
{
TMOD &= 0xF0; // 1) TMOD = TMOD & 0xF0 ??
TMOD |= 0x01; // 2) TMOD = TMOD | 0x01 ??

ET0 = 0; // 3) ??

TH0 = 0x00;
TL0 = 0xFA;

TF0 = 0;
TR0 = 1;

while(TF0 == 0);

TR0 = 0; }
}
}
 

Re: C code (Delay)

peace upon u,
=========
here program make the same function of the one u worte but using idea of interrupt of timer 0

==========================================
#include <reg52.h>

/*------------------------------------------------------------------------*/
unsigned char d;

void main(void)
{

while(1)
{

TMOD &= 0xF0; // 1) TMOD = TMOD & 0xF0 ??
TMOD |= 0x01; // 2) TMOD = TMOD | 0x01 ??
EA=1;
ET0=1;
d=0;
TH0 = 0x00;
TL0 = 0xFA;
TF0 = 0;
TR0 = 1;

while(d<20);
TR0=0;
ET0=0;
}
}

void timer0 (void) interrupt 1 using 2
{
TR0 = 0;
TH0 = 0x00;
TL0 = 0xFA;
TF0 = 0;
d++;
TR0 = 1;

}
============================================

it's the main idea but i don't check its work as i'm buse now.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

Hi..

Code:
#include <reg52.h> 

/*------------------------------------------------------------------------*/ 
unsigned char d; 

void main(void) 
{ 

while(1) 
{ 

	TMOD &= 0xF0;  
	TMOD |= 0x01; 
	EA=1; 		// Enable-all is enable but each interrupt source is set or clear its enable bit.
				/* Q1)
					Is it the EA=1 then we can set ET0/ET1=1?? If EA=0 then ET0 must set to ET0/ET1=0 am i right?? 
				*/
	ET0=1; 		// Enable the Timer 0 overflow interrupt.
	d=0; 		// Q2) It should be put outside of main is it??
	TH0 = 0x00; 
	TL0 = 0xFA; 
	TF0 = 0; 
	TR0 = 1; 	// Start Timer 1 Running
				/* Q3)
					The timer will count after the 0xFA then give 1 interrupt 	
				*/

	while(d<20); // Q4) Why put the statement here?? and why don't you put while(++d<20)
	TR0=0; 
	ET0=0; 
	} 

void timer0 (void) interrupt 1 using 2 // Where should i put the timer0 in main function 
{ 
TR0 = 0; 
TH0 = 0x00; 
TL0 = 0xFA; 
TF0 = 0; 
d++; 
TR0 = 1; 
}

Hi..

The code is not completed izit?? The timer0 is missing function prototype and where the timer0 function should i put in main funtion??
 

Re: C code (Delay)

// Q4) Why put the statement here?? and why don't you put while(++d<20)

This is wait for delay. the 'd' will increment by Timer0 interrupt. so if d > 20 will exit from here.


may be this code is corrected:

#include <reg52.h>

/*------------------------------------------------------------------------*/
unsigned char d;

void main(void)
{

TMOD &= 0xF0;
TMOD |= 0x01;
EA=1; // Enable-all is enable but each interrupt source is set or clear its Enable bit.
ET0=1; // Enable the Timer 0 overflow interrupt.
TH0 = 0x00;
TL0 = 0xFA;
TF0 = 0;

//**********************
// Start of DELAY
d=0;
TR0 = 1; // Start Timer 1 Running
while(d<20);
TR0=0;
ET0=0;
//**********************
}

void timer0 (void) interrupt 1 using 2 // Where should i put the timer0 in main function
{
TR0 = 0;
TH0 = 0x00;
TL0 = 0xFA;
TF0 = 0;
d++;
TR0 = 1;
}
 

Re: C code (Delay)

Hi..

Sorry.. I not understand the what the function doing can you explain it??

void timer0 (void) interrupt 1 using 2 // Where should i put the timer0 in main function
{
TR0 = 0;
TH0 = 0x00;
TL0 = 0xFA;
TF0 = 0;
d++;
TR0 = 1;
}[/quote]

Thanks....
 

Re: C code (Delay)

SA
Sorry for being late,

Help said:
Hi..

Sorry.. I not understand the what the function doing can you explain it??

void timer0 (void) interrupt 1 using 2 // Where should i put the timer0 in main function

This function timer0 it didn't need any prototype or call it, as u defined it as "interrupt 1", Then compiler will put this code in address of interrupt of timer0 in ROM "0x000B" . So when timer overflow and gives hardware interrupt then microprocessor will push the program counter and jump to the address of interrupt 1 in this case the function of timer0 called by hardware interrupt and when finished this function will pop the program counter to began again from the same address it got interrupted. In our code this address is the address of
said:
while(d<20);

In the main function we set timer 0 to begin count and wait at condition of while(d<20 ), at the same time timer0 is count until overflow then will give interrupt so function of timer 0 will be called (so d will be incremented and timer will reset) and so on until d=20 then condition of
said:
while(d<20);
become not valid, then the program will be continued.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

Hi..

It is very hard for me to understand... :oops:..., because you one short explain all thing i cannot catch up..... can you explain from the program by point from

Code:
while(d<2);  // 2 easy to explain.,how can i check the d reach 2? 
TR0=0; 
ET0=0; 
} 
} 

void timer0 (void) interrupt 1 using 2 
{ 
TR0 = 0;             // Why need to set again??
TH0 = 0x00;          // Why need to set again??
TL0 = 0xFA;          // Why need to set again??
TF0 = 0;             // Why need to set again??
d++; 
TR0 = 1; 

}

when the Timer 0 is start run then.....
If explain from program i can easily to ask question...


Thank You...
 

Re: C code (Delay)

Help said:
while(d<2); // 2 easy to explain.,how can i check the d reach 2?
TR0=0;
ET0=0;
}
}

void timer0 (void) interrupt 1 using 2
{
TR0 = 0; // Why need to set again??
TH0 = 0x00; // Why need to set again??
TL0 = 0xFA; // Why need to set again??
TF0 = 0; // Why need to set again??
d++;
TR0 = 1;

}

before while statment u initiate timer0 then it run in background .
then the statement of while will just wait for timer overflow,then the function timer0 was called from hardware interrupt.
in timer0 function ==== to reload timer 0 we must stop runing by TR0=0; then after reload u make it run again by TR0=1;

TH0 = 0x00;
TL0 = 0xFA; // Why need to set again??

we set it again as it was overflow,so it's value now 0xff ,0xff

TF0 = 0; // Why need to set again??
this bit is set by hardware and must be cleared by software

d++; //now d =1

TR0 = 1; to make timer count again from the value of TH,TL until 0xFF

now function is over and will return to the main loop at statement of
while (d<2);
it'll wait here as d=1 < 2 until timer overflow again
function of timer0 will called and d will equal to 2,when return from this function now while (d<2);
not valed as d=2 !<2 ,then make
TR0=0;
ET0=0;
}
and finish the program\


i wish i could explain it well...

salam
 

Re: C code (Delay)

Hi...

Where can i see/check the d increment occur because i need to check/know the increment happen??

Thanks....

Added after 27 minutes:


function of timer0 will called and d will equal to 2,when return from this function now while (d<2);
not valed as d=2 !<2 ,then make
TR0=0;
ET0=0;
}
and finish the program\

I already understand but until there..., while the d = 2 "while(2<2)" the program still go through the time0 function and reload again, then... Then, after d++ again the d = 3 the program still loop time0 function again... after d++ again the d = 4 THEN make
TR0=0;
ET0=0;
}
and finish the program

I hope you understand, I already try the program already you can try n c......

Thanks....
 

Re: C code (Delay)

Hi
first what's the type of the cpompiler and debugger you use ?

just make break point at the begining of function of timer0 and then run the program, when it stopped at the break point just run step by step
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code (Delay)

hI...

Sorry... jst now program typed something wrong. I using uVision2.
How to use Breakpoint - what's mean for Toggle breakpoint on current line?

Why i change the TH,TL value the time delay don't have any different (alway at 14uSec), i use Performance Analyzer to debuge.

Thks..
 

Re: C code (Delay)

Help said:
hI...

1) just now program typed something wrong. I using uVision2.
2)How to use Breakpoint
3)Why i change the TH,TL value the time delay don't have any different (alway at 14uSec),
4)i use Performance Analyzer to debuge.

1) i tried program now it hadn't any problem (program which written by me in the begining of the forum)
2)to make breakpoint press right click for the line you want and choose insert breakpoint, we use breakppoint to make debugger stop at this line when comes to it.
3)TH,TL not changed it just reloaded as its value incremented each clock cycle until TH,TL overflow then the value in it is 0xff,0xff so u must reload it to begin to run again.

4) How do u use Performance Analyzer
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top