Beruflich Dokumente
Kultur Dokumente
EIE322
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
RS232
9-pin
U1
K
1K
1K
10 K
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)));
}
}