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.

What will happen if you don‘t clear timer flag in 8051?

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
340
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,948
what will happen if i don't clear timer flag in 8051? I mean will the timer continue to run even if timer flag is 1. I m talking about mode 2(autoreload).

Thanks in advance
 

8051 timer doubt

"Once you have configured a timer in mode 2 you do not have to worry about checking to see if the timer has overflowed nor do you have to worry about resetting the value

The microcontroller hardware will do it all for you."

**broken link removed**
 

Re: 8051 timer doubt

After all it's auto-reload mode, so it takes care by itself ..

IanP
:D
 

8051 timer doubt

what will happen if i don't clear timer flag in 8051?

you should read the datasheet: your question is answered multiple times in the datasheet.

Added after 2 minutes:

"Once you have configured a timer in mode 2 you do not have to worry about checking to see if the timer has overflowed nor do you have to worry about resetting the value

that statement is not true. the flag, which is what the original poster was asking for, is only clear if the mcu vectors to the isr.

so you could conceivably have a case where the flag is never cleared if you someone doesn't have an isr for the timer - which is quite common on some mcus where you can manually clear the flag.

the reloading of the tmr counter is a separate issue.
 
Re: 8051 timer doubt

there is no need to clear TF flag. after overflow, TH will be reloaded with the value of TL, TF resets and timer starts counting till you stops timer.
 

8051 timer doubt

there is no need to clear TF flag. after overflow, TH will be reloaded with the value of TL, TF resets and timer starts counting till you stops timer.

what you said is only true if an interrupt is used. Otherwise, TFx is never cleared by hardware and you have to clear it manually.
 

8051 timer doubt

yeah, but it still does the Auto-reloading stuff, and the timer never stops...
 

Re: 8051 timer doubt

millwood said:
that statement is not true.
Nothing wrong with the statement, you can find the complete article form **broken link removed**.

Only thing is OP directed to do some basic literature about timers counters because of this

millwood said:
there is no need to clear TF flag. after overflow, TH will be reloaded with the value of TL, TF resets and timer starts counting till you stops timer.

what you said is only true if an interrupt is used. Otherwise, TFx is never cleared by hardware and you have to clear it manually.

Normally most of the users post their doubts without doing basic literature and also try to cross posting.

However discussion is going well
 

8051 timer doubt

eah, but it still does the Auto-reloading stuff, and the timer never stops...

that's correct. the only time TFx is cleared by hardware is if you vector to a (tmr) interrupt.

Nothing wrong with the statement,

it is wrong because it is not consistent with how the device works. you can easily test it on your own.

whether there is a webpage suggesting it is irrelevant.

Added after 2 hours 44 minutes:

to show that TFx isnot clear when the timer overflows in mode 2 (8-bit timer + auto reload), here is a simple example.

Code:
#include <REGX51.H>
#include "gpio.h"

#define LED_PORT		P2
#define LED				(1<<0)

void t0_init(void) {					//initiate tmr0 - no interrupt enabled
	TMOD = (TMOD & 0xf0) | 0x02;		//tmr0: mode 2, not gated, internal clock
	TH0=256-0xff;							//autorelated to tho's content
	TR0=1;								//turn on tmr0
}

void mcu_init(void) {					//initiate the mcu
}

int main(void) {
	mcu_init();							//initiate the mcu
	t0_init();							//initiate tmr0

	while (1) {
		while (!TF0) continue;			//wait for tmr0 to overflow
		TF0=0;							//reset trf0
		IO_FLP(LED_PORT, LED);			//flip led
		P3=TF0;							//output the value of TF0 on P3.0
	}
}

it essentially wait for the timer to overflow, reset the flag, and then flip LED (on p2.0).

as the auto reload is TH0=1, the code counts 255 cycles before it overflows. on a 12-state 8051 with 12Mhz xtal, it should overflows every 255us, and LED should be producing a square wave of 1/255us/2=1961hz.

try it on your 8051 and see for yourself if it is true.

Added after 1 minutes:

Notice that in the above program, we had to manually reset TF0 (since we are not using an interrupt here).

if you were to comment out that line - ie not resetting TF0 after tmr0 overflows, you will get much faster flipping on LED.

alternatively, you can observe P3.0's level as it represents the value of TF0.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top