51单片机测量PWM波的占空比的C语言程序
急需用51单片机的定时/计数器测量PWM波占空比的c语言程序,哪一位学长(学姐也可以)能给我提供一个?谢谢...
急需用51单片机的定时/计数器测量PWM波占空比的c语言程序,哪一位学长(学姐也可以)能给我提供一个?谢谢
展开
推荐于2016-06-03 · 郑州西亚斯学院副教授
关注
展开全部
/****************************
pwm 从单片机的P3.2输入
/***************************/
#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16; //数据类型重定义
uint8 t;
bit get;
long pwm_all,pwm_h;
void init()
{
TMOD = 0x19; //t0 gate = 1 用于捕获正脉宽
TH1 = 0;
TL1 = 0;
TH0 = 0;
TL0 = 0;
TR0 = 1;
IT0 = 1;
IT1 = 1;
EX0 = 1;
EA = 1;
bit
}
void main()
{
uint16 pwm_w;
init();
while(1)
{
if(get)
{
pwm_w = (pwm_h*100)/pwm_all; //得出占空比
}
}
}
void ex0() interrupt 0
{
t++;
if(t == 1)
TR1 = 1;
if(t == 2)
{
pwm_all = TH1;
pwm_all = (pwm_all << 8) + TL1;
TH1 = TL1 = 0;
t == 0;
get = 1;
}
pwm_h = TH0;
pwm_h = (pwm_h << 8) + TL0;
TH0 = TL0 = 0;
}
pwm 从单片机的P3.2输入
/***************************/
#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16; //数据类型重定义
uint8 t;
bit get;
long pwm_all,pwm_h;
void init()
{
TMOD = 0x19; //t0 gate = 1 用于捕获正脉宽
TH1 = 0;
TL1 = 0;
TH0 = 0;
TL0 = 0;
TR0 = 1;
IT0 = 1;
IT1 = 1;
EX0 = 1;
EA = 1;
bit
}
void main()
{
uint16 pwm_w;
init();
while(1)
{
if(get)
{
pwm_w = (pwm_h*100)/pwm_all; //得出占空比
}
}
}
void ex0() interrupt 0
{
t++;
if(t == 1)
TR1 = 1;
if(t == 2)
{
pwm_all = TH1;
pwm_all = (pwm_all << 8) + TL1;
TH1 = TL1 = 0;
t == 0;
get = 1;
}
pwm_h = TH0;
pwm_h = (pwm_h << 8) + TL0;
TH0 = TL0 = 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询