51单片机输出周期20ms,占空比10%,pwm波控制舵机,这个程序错在哪?
#include<reg52.h>sbitPWMOUT=P0^1;unsignedcharHighRH=0;unsignedcharHighRL=0;unsignedch...
#include <reg52.h>
sbit PWMOUT = P0^1;
unsigned char HighRH = 0;
unsigned char HighRL = 0;
unsigned char LowRH = 0;
unsigned char LowRL = 0;
void ConfigPWM(unsigned int fr, unsigned char dc);
void ClosePWM();
void main()
{
unsigned int i;
EA = 1; //开总中断
while (1)
{
ConfigPWM(50, 10); //频率100Hz,占空比10%
}
}
void ConfigPWM(unsigned int fr, unsigned char dc)
{
unsigned long tmp;
unsigned int high, low;
tmp = 11059200/12/fr; //需要tmp个机器周期
high = (tmp*dc)/100;
low = tmp - high;
high = 65536 - high + 12;
low = 65536 - low + 12;
HighRH = (unsigned char)(high >>8);
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low >> 8);
LowRL = (unsigned char)low;
//TMOD &= 0xF0;
TMOD = 0x01;
TH0 = HighRH;
TL0 = HighRL;
ET0 = 1;
TR0 = 1;
PWMOUT = 1;
}
void InterruptTimer0() interrupt 1
{
if (PWMOUT == 1) //当前输出为高电平时,装载低电平值并输出低电平
{
TH0 = LowRH;
TL0 = LowRL;
PWMOUT = 0;
}
else //当前输出为低电平时,装载高电平值并输出高电平
{
TH0 = HighRH;
TL0 = HighRL;
PWMOUT = 1;
}
} 展开
sbit PWMOUT = P0^1;
unsigned char HighRH = 0;
unsigned char HighRL = 0;
unsigned char LowRH = 0;
unsigned char LowRL = 0;
void ConfigPWM(unsigned int fr, unsigned char dc);
void ClosePWM();
void main()
{
unsigned int i;
EA = 1; //开总中断
while (1)
{
ConfigPWM(50, 10); //频率100Hz,占空比10%
}
}
void ConfigPWM(unsigned int fr, unsigned char dc)
{
unsigned long tmp;
unsigned int high, low;
tmp = 11059200/12/fr; //需要tmp个机器周期
high = (tmp*dc)/100;
low = tmp - high;
high = 65536 - high + 12;
low = 65536 - low + 12;
HighRH = (unsigned char)(high >>8);
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low >> 8);
LowRL = (unsigned char)low;
//TMOD &= 0xF0;
TMOD = 0x01;
TH0 = HighRH;
TL0 = HighRL;
ET0 = 1;
TR0 = 1;
PWMOUT = 1;
}
void InterruptTimer0() interrupt 1
{
if (PWMOUT == 1) //当前输出为高电平时,装载低电平值并输出低电平
{
TH0 = LowRH;
TL0 = LowRL;
PWMOUT = 0;
}
else //当前输出为低电平时,装载高电平值并输出高电平
{
TH0 = HighRH;
TL0 = HighRL;
PWMOUT = 1;
}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
3个回答
展开全部
//TMOD &= 0xF0;
TMOD = 0x01;
TMOD |=0x01;
是或等于
以下你只申明没有定义
void ClosePWM();
unsigned int i;
TMOD = 0x01;
TMOD |=0x01;
是或等于
以下你只申明没有定义
void ClosePWM();
unsigned int i;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是不是没有输出脉冲?而且还进不了定时器中断?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以发给我看看
原理图有吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询