我想用51单片机控制一个机械手,用P2口输出5路PWM波
时间:10-02
整理:3721RD
点击:
编出的程序前三路有正常PWM波形,第四路方波形混乱,第五路没有波形
不知怎么回事‘
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr T2MOD=0xC9; //晶振11.0592MHz
sbit k1=P2^0; //5个PWM输出口
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit k5=P2^4;
sbit n=P1^0; //LED指示
uint t[10]={150,151,150,153,150,150,150,150,150,150}; //控制PWM的变量数组
uchar i,m,d,z,q,w,e;
void init() //程序初始化
{
TMOD=0x21;
T2MOD=0x00;
T2CON=0x00;
TH0=(65536-2304)/256;
TL0=(65536-2304)%256;
TH2=(65536-18432)/256;
TL2=(65536-18432)%256;
RCAP2H=(65536-18432)/256;
RCAP2L=(65536-18432)%256;
EA=1;
ET0=1;
ET2=1;
TR0=1;
TR2=1;
SCON=0X50;
}
void main()
{
init();
i=0;
m=0;
while(1); //等待中断发生
}
void T0_time() interrupt 1 //通过定时器0产生多路PWM,每次中断赋值下一次中断的时间,
{ //两个t[]一组,在20ms周期内分时启动各路PWM高电平,
TH0=(65536-9.216*t[i])/256; //各路的高电平只需1-2ms
TL0=(65536-9.216*t[i])%256;
i++;
switch(i)
{
case 1:
k1=1;
break;
case 2:
k1=0;
break;
case 3:
k2=1;
break;
case 4:
k2=0;
break;
case 5:
k3=1;
break;
case 6:
k3=0;
break;
case 7:
k4=1;
break;
case 8:
k4=0;
break;
case 9:
k5=1;
break;
case 10:
k5=0;
i=0;
ET0=0; //一个周期内第五次PWM结束后关T0
break;
}
}
void time2() interrupt 5 //用52单片机定时器2,16位自动重装模式控制20ms较准确基准周期时间
{
TF2=0;
i=0;
n=~n;
ET0=1; //开T0
}
不知怎么回事‘
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr T2MOD=0xC9; //晶振11.0592MHz
sbit k1=P2^0; //5个PWM输出口
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit k5=P2^4;
sbit n=P1^0; //LED指示
uint t[10]={150,151,150,153,150,150,150,150,150,150}; //控制PWM的变量数组
uchar i,m,d,z,q,w,e;
void init() //程序初始化
{
TMOD=0x21;
T2MOD=0x00;
T2CON=0x00;
TH0=(65536-2304)/256;
TL0=(65536-2304)%256;
TH2=(65536-18432)/256;
TL2=(65536-18432)%256;
RCAP2H=(65536-18432)/256;
RCAP2L=(65536-18432)%256;
EA=1;
ET0=1;
ET2=1;
TR0=1;
TR2=1;
SCON=0X50;
}
void main()
{
init();
i=0;
m=0;
while(1); //等待中断发生
}
void T0_time() interrupt 1 //通过定时器0产生多路PWM,每次中断赋值下一次中断的时间,
{ //两个t[]一组,在20ms周期内分时启动各路PWM高电平,
TH0=(65536-9.216*t[i])/256; //各路的高电平只需1-2ms
TL0=(65536-9.216*t[i])%256;
i++;
switch(i)
{
case 1:
k1=1;
break;
case 2:
k1=0;
break;
case 3:
k2=1;
break;
case 4:
k2=0;
break;
case 5:
k3=1;
break;
case 6:
k3=0;
break;
case 7:
k4=1;
break;
case 8:
k4=0;
break;
case 9:
k5=1;
break;
case 10:
k5=0;
i=0;
ET0=0; //一个周期内第五次PWM结束后关T0
break;
}
}
void time2() interrupt 5 //用52单片机定时器2,16位自动重装模式控制20ms较准确基准周期时间
{
TF2=0;
i=0;
n=~n;
ET0=1; //开T0
}