51单片机测量PWM波的占空比的C语言程序

如题所述

第1个回答  2020-02-02
用定时器产生程序如下:
unsigned
int
a=0
,b=10;
void
main()
{
tmod=0x01;
th0=(65536-50000)/256;
tl0=(65536-50000)%256;
ea=1;
et0=1;
tr0=1;
}
void
pwm()
interrupt
1
{
th0=(65536-50000)/256;
tl0=(65536-50000)%256
;
a++;
p1^1=0;
if(a=20)
{
a=0;
}
if(a=b)
{
p1^1=1;
}
}
调节a
,b的值即可调节占空比,程序框架就是这样,具体自己改吧
第2个回答  2020-03-31
/****************************
pwm
从单片机的P3.2输入
/***************************/
#include
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;
}
相似回答
大家正在搜