Sie sind auf Seite 1von 4

Pulse Width Measurement Exercise

EIE322

Hardware connections to be made:


1. Circuit Modifications to HexAvoider:

8252/8253 (on-board)

RxD (P3.0)
TxD (P3.1)
/INT0 (P3.2)
/INT1 (P3.3)
T0 (P3.4)
T1 (P3.5)
/WR (P3.6)
/RD (P3.7)

L_Antenna
R_Antenna
LED_Ant

P2.2
P2.3
P2.4
P2.5
P2.6
P2.7

U1 ( 2 x 9-pin Piggyback
Protoboard connector)
GND

GND
VCC

GND

GND

GND

2. Construct Piggyback prototype board :


Ultrasonic sensor SRF04
----------------------5V
Echo Pulse output
Trigger Pulse input
Ground
RS232 connection to PC
---------------------Construct the same
Circuit found on
Keil kit's Activ8R.
See circuit in manual.

Expansion Connector on HexAvoider


-------------------------------pin 17
pin 11
(/INT0)
pin 16
(T1 or P3.5)
pin 9/10
Expansion Connector on HexAvoider
--------------------------------Serial Output TxD is on
pin 14
Serial Input RxD is on
pin 12

RS232
9-pin

U1
K
1K

1K
10 K

Prepare Sample Program

The attached example program is not originally written fot the


Keil compiler. Make the necessary fixes first.
This example program shows how to compute the width of a pulse
using timer0 in mode 1.
Port 3.2 (!INT0) is used as an external gate that enables timer
0 to count. An interrupt is used to accumulate timer overflow.
Modify the program such that the width of each pulse is
reported to the PC via the serial port.
Demonstrate the modified program
1. Modify the source program's syntax for the Keil Compiler.
2. Modify the program to repeatedly sent out a pulse on P3.5 .
3. Modify the program to measure the echosed pulse from P3.2 (!
INT0).
4. Modify the program to report the new measurement by a message
sent via the serial port to PC
(Terminal program Mon51 or Windows Hyperterminal).
5. For testing you dont want Hex avoider to start moving.
So, it is better to disconnect the servos.
6. Compile, download and run the program.
The distance from a object or wall should be continuous updated
and shown on the PC screen..

Reference: http://www.keil.com/download/docs/8051_timer0_pulse.zip.asp.

#include <reg52.h>
#include <stdio.h>
/*--------------------------------------------------------Timer 0 Overflow Interrupt
---------------------------------------------------------*/
unsigned int T0_ISR_count = 0;
void T0_ISR (void) interrupt 1
{
T0_ISR_count++;
TF0 = 0;
// Reset the interrupt request
}
/*--------------------------------------------------------MAIN C function
---------------------------------------------------------*/
void main (void)
{
/*Set serial port for 9600 baud. Timer 1 is used as baud rate generator.
--------------------------------------*/
SCON = 0x50;
TMOD |= 0x20;
TH1
= 0xFA;
TR1
= 1;
TI
= 1;
PCON |= 0x80;
printf ("\nPulse Width Example Program\n\n");
/*-------------------------------------Enable interrupts for timer 0.
--------------------------------------*/
ET0 = 1;
EA = 1;
/*-------------------------------------Set Timer0 for 16-bit interval timer mode.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x09;
while (1)
{
/*-------------------------------------Clear the timer overflow counter and the timer low and high registers.
Then, start the timer.
--------------------------------------*/
T0_ISR_count = 0;
TH0 = 0;
TL0 = 0;
TR0 = 1;
printf ("\nStart a pulse.\n");
/*-------------------------------------Wait for the pulse to start. Then, wait for the pulse to end.
--------------------------------------*/
while (!INT0);
while (INT0);
/*-------------------------------------Compute the width of the pulse -- one clock cycle is 1us
for a standard 8051 and display it.
--------------------------------------*/
printf ("The width pulse is: %ld uSec\n",
(unsigned long)((TH0 << 8) | TL0 | ((unsigned long)T0_ISR_count
<< 16)));
}
}

Das könnte Ihnen auch gefallen