51单片机内部定时器使用(产生周期和占空比可变的脉冲信号)

/*------------------------------
Function: generate pulse signal
Author: Zhang Kaizhou
Date: 2019-5-15 22:50:43
------------------------------*/
#include <reg51.h>
#define uchar unsigned char

sbit output = P1^0;
uchar period = 250;
uchar high = 50;
uchar tcount = 0;

void timer0() interrupt 1 using 1
{
	TH0 = (65536 - 10000) / 256;
	TL0 = (65536 - 10000) % 256;
	tcount++;
	if(tcount == high)
	{
		output = 0;
	}else if(tcount == period){
		tcount = 0;
		output = 1;
	}
}

void main()
{
	TMOD = 0x01; // set Timer1, working at mode 1(16 bit timer)
	
	TH0 = (65536 - 10000) / 256; // set initial value
	TL0 = (65536 - 10000) % 256;
	
	EA = 1; // open global interrupt
	ET0 = 1; // open Timer0 interrupt
	TR0 = 1; // start Timer0
	while(1);
}
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页