单片机产生方波程序

怎样利用定时中断方式编程实现从p1.0输出一个周期为1s占空比为0.5〔系统时钟为12Mhz〕

50ms定时中断,计10次,即0.5s。每0.5s,P1.0求反一次,即为所要求的方波
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-22
#include "reg52.h"

sbit PWM_OUT_BIT = P1^0;

void Timer0_Init(void)
{
TMOD = 0x01; //工作在方式1,16位定时器

TH0 = 0xD8; //10us
TL0 = 0xF0;

ET0 = 1; //定时器0中断允许
}

void Timer0_Int_Routine(void) interrupt 1 using 1
{
static unsigned char Timer0_Int_Count = 0;

TH0 = 0xD8; //10us,数据重载
TL0 = 0xF0;

Timer0_Int_Count ++;

if(Timer0_Int_Count >= 50)
{
Timer0_Int_Count = 0;

PWM_OUT_BIT = ~PWM_OUT_BIT; //输入PWM波形
}
}

int main(void)
{
Timer0_Init();

TR0 = 1; //启动定时器0
EA = 1; //打开中断

while(1);
}本回答被提问者采纳