⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 8051系列单片机的利用定时器进行脉冲宽度测量的c语言的简单的操作例程
💻 C
字号:
/*---------------------------------------------------------
---------------------------------------------------------*/
#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 at
11.0592 MHz.  Note that we use Timer 1
for the 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)));
  }
}

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -