Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

89C52 Timer

Status
Not open for further replies.
Hi, i wanna calculate the time elapsed for something ..
Code:
void DELAY_50ms(void)
{
// Configure Timer 0 as a 16-bit timer
TMOD &= 0xF0; // Clear all T0 bits (T1 left unchanged)
TMOD |= 0x01; // Set required T0 bits (T1 left unchanged)
ET0 = 0; // No interupts
// Values for 50 ms delay
TH0 =  0xC5; // Timer 0 initial value (High Byte)
TL0 = 0x68; // Timer 0 initial value (Low Byte)
TF0 = 0; // Clear overflow flag
TR0 = 1; // Start timer 0
while (TF0 == 0); // Loop until Timer 0 overflows (TF0 == 1)
TR0 = 0; // Stop Time
}
this code for doing a 50 ms delay , how do i calculate the elapsed time ?, i mean i wanna use it like a stop watch ..
 
It's complete unclear to me what you wanna time and how long that time is but...

Making a 50ms delay loop won't work.

If you wanna time several seconds then:
1) Declare T0 as a 10msec auto reload timer. That means, every 10ms the µC will jump to the ISR of T0.
2) In the ISR of T0 write some code that increments a 16 bit counter when your timing input is high (or low). That way you can count 655.36 seconds.

If you wanna time less than seconds then:
1) Use previous if the resolution of 10ms is enough for you.
2) Or, make a clock pulse of x(k)Hz (depending on the accuracy and time to mesure you want) feed that pulse train into an interrupt pin of the µC (P3.x)
3) Enable "edge sensitive interrupt" for that pin.
4) Write some code that increments a 16 bit counter on each edge of you clock pulse when your timing input is high (or low).

Clear to you?
No, give some more information on what you exactly want to do and we can help you further...
 
thanks for the reply
i wanna send a pulse from ultrasonic sensor ,then make a timer , when the echo returned , i'll calculate the passed time, i just wanna calculate that time go it ?
 
Aha, you want to measure the TOF (Time Of Flight) of an US signal :)
Any idea how long the interval between send and echo will be?
That will determine the way to go...
How accurate do you want the read-out? 1; 10; 100ms or ...???
 
Before you can design something you need specs!
Without specs it will fail because you don't know what you want.

What will be the maximum timed value?
Do I need an 8 or 16bit counter to store the timed value?
How accurate do I need the result?
Before designing anything, answer questions like that so that you know what you need to do.

Good luck
 
You really need to use interrupt in this case. When you send the ultrasonic pulse, turn on the timer and wait for echo-interrupt to arrive. In the interrupt handler, turn-off the timer. The remaining timer value will be directly proportional to the pulse-delay.
For 89C52, the timer increments every 12 clock cycles. Now its easy for you to calculate exact time between send and received pulses.
 
kinjalgp, yea that's excatly what i wanna do , but i really dont know how to implement , could you provide some snippets ^^ ?
 
kinjalgp said:
You really need to use interrupt in this case. When you send the ultrasonic pulse, turn on the timer and wait for echo-interrupt to arrive. In the interrupt handler, turn-off the timer. The remaining timer value will be directly proportional to the pulse-delay.
For 89C52, the timer increments every 12 clock cycles. Now its easy for you to calculate exact time between send and received pulses.
Basically that's my second proposal from some days ago except that you use an internal timer to count instead of external pulses form an oscillator.

kinjalgp, you need:
1) An internal timer to count time.
2) An edge sensitive interrupt driven input on your µC to check for the echo pulse
3) An output pin to enable the US pulse

How to do it:
A) In the main loop
If previous US pulse was finished ("blnPulseAnalysed" high) then
1) Set output pin to send an new US pulse
2) Start internal timer
3) Enable interrupt on P3.2 so that the pin can detect the echo pulse
4) Reset bit "blnPulseAnalysed"
Endif
Do all other things that needs to be done, but for the US part you're finished for now in your main loop.


B) In the ISR of P3.2
1) Stop internal timer
2) Read internal timer and convert it to TOF
3) Disable interrupt on P3.2
4) Set bit "blnPulseAnalysed" so that your main code knows that the pulse was analysed and he can send a new one, display the result, ...
RETI


Try to convert this "flowchart" to code and let us know what you made of it...
One thing is for sure, you need to remove the line
Code:
while (TF0 == 0); // Loop until Timer 0 overflows (TF0 == 1)
in your previous code.
 
ahmedragia21 said:
kinjalgp, yea that's excatly what i wanna do , but i really dont know how to implement , could you provide some snippets ^^ ?
Check this sample code I wrote. You need to do calculations to get distance.
Code:
#include <AT89C52.h>
#include <stdio.h>

sbit PULSE_ENABLE = P1^0;

bit timeout;

void ext_int0(void) interrupt 0 using 0
{
	TR0 = 0;	// Stop Timer-0
	EX0 = 0;	// Disbale EXT-INT0 interrupt
}

void timer0_int(void) interrupt 1 using 0
{
	timeout = 1;
}


void main(void)
{
	unsigned int count;

	TMOD = 0x01;	// Timer-0 as 16-bit Timer
	ET0 = 1;	// Enable Timer-0 interrupt

	IT0 = 1;	// EXT-INT-0 is Edge Triggered Interrupt
	EX0 = 1;	// Enable External Interrupt-0
	EA = 1;		// Global Interrupt Enable

	while(1)
	{
		TR0 = 0;		// Stop Timer-0 if it was running
		TH0 = 0;		// Clear count
		TL0 = 0;		// Clear count
		timeout = 0;		// Clear bit
		EX0 = 1;		/ /Enable EXT-INT-0 Interrupt
		PULSE_ENABLE = 1;	// Enable Transmitter
		TR0 = 1;		// Start Timer-0
		while(TR0 == 1 && timeout == 0);	// Wait till timer stops or timeout occurs
		PULSE_ENABLE = 0;
		EX0 = 0;
		if(!timeout)
		{
			// Get Timer-0 count
			count = ((unsigned int)TH0 << 8) | (unsigned int)TL0;
			// Do calculations on 'count' here to get distance
			printf("%d\r\n", count);
		}
		else
			printf("Pulse Lost!\r\n");
			
	};
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top